UNPKG

@funded-labs/dab-js

Version:
63 lines (62 loc) 3.78 kB
"use strict"; 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 }); exports.getTokens = exports.TokenRegistry = exports.getTokenActor = exports.TOKEN_STANDARDS = void 0; const agent_1 = require("@dfinity/agent"); const token_registry_did_1 = __importDefault(require("../idls/dab_registries/token_registry.did")); const standard_registry_1 = __importDefault(require("./standard_registry")); const actorFactory_1 = require("../utils/actorFactory"); const registry_1 = require("../utils/registry"); const constants_1 = require("../constants"); const token_standards_1 = require("../standard_wrappers/token_standards"); const standards_1 = require("../constants/standards"); const CANISTER_ID = 'b7hhy-tyaaa-aaaah-abbja-cai'; const DEFAULT_AGENT = agent_1.HttpAgent.createSync({ fetch, host: constants_1.IC_HOST }); exports.TOKEN_STANDARDS = Object.values(standards_1.TOKEN); const getTokenActor = ({ canisterId, agent, standard, blsVerify, }) => { if (!exports.TOKEN_STANDARDS.includes(standard)) { console.error(`Standard ${standard} is not implemented`); throw new Error(`standard is not supported: ${standard}`); } return (0, token_standards_1.createTokenActor)(canisterId, agent, standard, blsVerify); }; exports.getTokenActor = getTokenActor; class TokenRegistry extends standard_registry_1.default { constructor(agent) { super(CANISTER_ID, agent); this.getAll = () => __awaiter(this, void 0, void 0, function* () { const tokenCanistersMetadata = yield this.actor.get_all(); return tokenCanistersMetadata.map(registry_1.formatMetadata); }); this.actor = (0, actorFactory_1.generateActor)({ agent: agent || DEFAULT_AGENT, canisterId: CANISTER_ID, IDL: token_registry_did_1.default, }); } } exports.TokenRegistry = TokenRegistry; const getTokens = ({ agent = DEFAULT_AGENT } = {}) => __awaiter(void 0, void 0, void 0, function* () { const tokenRegistry = new TokenRegistry(agent); const tokenCanisters = yield tokenRegistry.getAll(); return tokenCanisters.map((token) => (Object.assign(Object.assign({}, token), { logo: token.thumbnail, name: token.name, description: token.description, website: token.frontend.length ? token.frontend[0] : '', principal_id: token.principal_id, standard: token.details.standard, total_supply: [token.details.total_supply], symbol: token.details.symbol }))); }); exports.getTokens = getTokens; exports.default = { getTokenActor: exports.getTokenActor, getTokens: exports.getTokens, addToken: ({ agent, tokenInfo }) => __awaiter(void 0, void 0, void 0, function* () { return new TokenRegistry(agent).add(tokenInfo); }), // editToken: async ({ agent, tokenInfo }) => new TokenRegistry(agent).edit(tokenInfo), removeToken: ({ agent, canisterId }) => __awaiter(void 0, void 0, void 0, function* () { return new TokenRegistry(agent).remove(canisterId); }), };