@funded-labs/dab-js
Version:
JS adapter for DAB
68 lines (67 loc) • 3.23 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const agent_1 = require("@dfinity/agent");
const icpunks_did_1 = __importDefault(require("../../idls/icpunks.did"));
const default_1 = __importDefault(require("./default"));
const canisters_1 = require("../../constants/canisters");
const standards_1 = require("../../constants/standards");
const getICPBunnyCanisterId = (index) => canisters_1.NFT_CANISTERS.ICP_BUNNY_STORAGE[index % 10];
const imageUrl = (canisterId, index, tokenDataUrl) => ({
[canisters_1.NFT_CANISTERS.ICP_BUNNY_MAIN]: `https://${getICPBunnyCanisterId(index)}.raw.icp0.io/Token/${index}`,
}[canisterId] || `https://${canisterId}.raw.icp0.io${tokenDataUrl}`);
class ICPUNKS extends default_1.default {
constructor(canisterId, agent, blsVerify) {
super(canisterId, agent);
this.standard = standards_1.NFT.icpunks;
this.serializeTokenData = (tokenData) => ({
index: BigInt(tokenData.id),
canister: this.canisterId,
url: imageUrl(this.canisterId, Number.parseInt(tokenData.id.toString(), 10), tokenData.url),
name: tokenData.name,
metadata: tokenData,
standard: this.standard,
});
this.actor = agent_1.Actor.createActor(icpunks_did_1.default, {
agent,
canisterId,
blsVerify,
});
}
getMetadata() {
throw new Error('Method not implemented.');
}
getUserTokens(principal) {
return __awaiter(this, void 0, void 0, function* () {
const tokensIndexes = yield this.actor.user_tokens(principal);
const tokensData = yield Promise.all(tokensIndexes.map((tokenIndex) => this.actor.data_of(tokenIndex)));
return tokensData.map((token) => this.serializeTokenData(token));
});
}
transfer(to, tokenIndex) {
return __awaiter(this, void 0, void 0, function* () {
const success = yield this.actor.transfer_to(to, BigInt(tokenIndex));
if (!success) {
throw new Error('Error transfering token');
}
});
}
details(tokenIndex) {
return __awaiter(this, void 0, void 0, function* () {
const tokenData = yield this.actor.data_of(BigInt(tokenIndex));
return this.serializeTokenData(tokenData);
});
}
}
exports.default = ICPUNKS;