@funded-labs/dab-js
Version:
JS adapter for DAB
77 lines (76 loc) • 3.62 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 c3_did_1 = __importDefault(require("../../idls/c3.did"));
const default_1 = __importDefault(require("./default"));
const standards_1 = require("../../constants/standards");
class CCC extends default_1.default {
constructor(canisterId, agent, blsVerify) {
super(canisterId, agent);
this.standard = standards_1.NFT.c3;
this.serializeTokenData = (tokenData, prinId) => {
return {
index: BigInt(tokenData.id),
canister: this.canisterId,
url: `https://${prinId.toText()}.raw.icp0.io/token/${tokenData.id}`,
name: `${tokenData.id}`,
metadata: tokenData,
standard: this.standard,
};
};
this.actor = agent_1.Actor.createActor(c3_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.getAllNFT(principal);
const tokensData = yield Promise.all(tokensIndexes.map((item) => __awaiter(this, void 0, void 0, function* () {
const tokenIndex = item[0];
const principal = item[1];
const userTokensResult = yield this.actor.getTokenById(tokenIndex);
if ('err' in userTokensResult)
throw new Error(Object.keys(userTokensResult.err)[0]);
return { detail: userTokensResult.ok, principal };
})));
return tokensData.map((token) => this.serializeTokenData(token.detail, token.principal));
});
}
transfer(to, tokenIndex) {
return __awaiter(this, void 0, void 0, function* () {
const from = yield this.agent.getPrincipal();
const transferResult = yield this.actor.transferFrom(from, to, BigInt(tokenIndex));
if ('err' in transferResult)
throw new Error(Object.keys(transferResult.err)[0]);
});
}
details(tokenIndex) {
return __awaiter(this, void 0, void 0, function* () {
const tokenData = yield this.actor.getTokenById(BigInt(tokenIndex));
if ('err' in tokenData)
throw new Error(Object.keys(tokenData.err)[0]);
const prinId = yield this.actor.getNftStoreCIDByIndex(BigInt(tokenIndex));
if (!prinId)
throw new Error('Error tokenIndex');
return this.serializeTokenData(tokenData.ok, prinId);
});
}
}
exports.default = CCC;