@funded-labs/dab-js
Version:
JS adapter for DAB
96 lines (95 loc) • 4.21 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 icrc_7_did_1 = require("../../idls/icrc_7.did");
const default_1 = __importDefault(require("./default"));
const standards_1 = require("../../constants/standards");
const agent_1 = require("@dfinity/agent");
const PAGE_SIZE = 1;
class ICRC7 extends default_1.default {
constructor(canisterId, agent, blsVerify) {
super(canisterId, agent);
this.standard = standards_1.NFT.icrc7;
this.actor = agent_1.Actor.createActor(icrc_7_did_1.idlFactory, {
agent,
canisterId,
blsVerify,
});
}
findMetada(key, metadata) {
var _a, _b;
return (_b = (_a = metadata.find((meta) => meta[0] === key)) === null || _a === void 0 ? void 0 : _a[1]) === null || _b === void 0 ? void 0 : _b.Text;
}
mapToken(id, metadata, owner) {
return {
index: id,
canister: this.canisterId,
name: this.findMetada('Name', metadata),
url: this.findMetada('logo', metadata),
standard: 'ICRC7',
owner,
metadata,
};
}
getUserTokens(principal) {
return __awaiter(this, void 0, void 0, function* () {
let ids = [];
let shouldLoad = true;
while (shouldLoad) {
const newIds = yield this.actor.icrc7_tokens_of({ owner: principal, subaccount: [] }, ids.length === 0 ? [] : [ids[ids.length - 1]], [BigInt(PAGE_SIZE)]);
const filtered = newIds.filter((id) => !ids.includes(id));
ids = [...ids, ...filtered];
shouldLoad = filtered.length >= PAGE_SIZE;
}
const metadata = yield this.actor.icrc7_token_metadata(ids);
const response = metadata[0];
return response.map((metadata, index) => this.mapToken(ids[index], metadata, principal.toString()));
});
}
transfer(principal, tokenIndex) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.actor.icrc7_transfer([
{
to: {
owner: principal,
subaccount: [],
},
token_id: BigInt(tokenIndex),
memo: [],
from_subaccount: [],
created_at_time: [],
},
])[0];
if ('Err' in result[0]) {
throw new Error(Object.keys(result[0].Err)[0]);
}
});
}
getMetadata() {
return __awaiter(this, void 0, void 0, function* () {
throw new Error('Method not implemented.');
});
}
details(tokenIndex) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const details = yield this.actor.icrc7_token_metadata([BigInt(tokenIndex)]);
const owner = yield this.actor.icrc7_owner_of([BigInt(tokenIndex)]);
if (details.length === 0)
return Promise.reject('No metadata available');
return this.mapToken(BigInt(tokenIndex), details[0][0], ((_b = (_a = owner[0][0]) === null || _a === void 0 ? void 0 : _a.owner) === null || _b === void 0 ? void 0 : _b.toString()) || '');
});
}
}
exports.default = ICRC7;