UNPKG

@funded-labs/dab-js

Version:
77 lines (76 loc) 3.94 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.getAll = exports.getMultipleCanisterInfo = exports.getCanisterInfo = exports.CanisterRegistry = void 0; const agent_1 = require("@dfinity/agent"); const canister_registry_did_1 = __importDefault(require("../idls/dab_registries/canister_registry.did")); const constants_1 = require("../constants"); const standard_registry_1 = __importDefault(require("./standard_registry")); const actorFactory_1 = require("../utils/actorFactory"); const registry_1 = require("../utils/registry"); const principal_1 = require("@dfinity/principal"); const CANISTER_ID = 'curr3-vaaaa-aaaah-abbdq-cai'; const DEFAULT_AGENT = agent_1.HttpAgent.createSync({ fetch, host: constants_1.IC_HOST }); const formatBackwardsCompatible = (metadata) => { if (!metadata) { return metadata; } const { thumbnail, name, description, frontend, details } = metadata; return { url: (frontend === null || frontend === void 0 ? void 0 : frontend[0]) || '', name, description, version: Number(details.version), logo_url: thumbnail, }; }; class CanisterRegistry extends standard_registry_1.default { constructor(agent) { super(CANISTER_ID, agent); this.getAll = () => __awaiter(this, void 0, void 0, function* () { const canistersMetadata = yield this.actor.get_all(); return canistersMetadata.map(registry_1.formatMetadata); }); this.actor = (0, actorFactory_1.generateActor)({ agent: agent || DEFAULT_AGENT, canisterId: CANISTER_ID, IDL: canister_registry_did_1.default, }); } } exports.CanisterRegistry = CanisterRegistry; const getCanisterInfo = ({ canisterId, agent = DEFAULT_AGENT, }) => __awaiter(void 0, void 0, void 0, function* () { const canisterRegistry = new CanisterRegistry(agent); const canister = yield canisterRegistry.get(principal_1.Principal.from(canisterId).toString()); const formattedCanister = formatBackwardsCompatible(canister); return (formattedCanister && Object.assign(Object.assign({}, formattedCanister), { canisterId: canisterId.toString() })); }); exports.getCanisterInfo = getCanisterInfo; const getMultipleCanisterInfo = ({ canisterIds, agent = DEFAULT_AGENT, }) => __awaiter(void 0, void 0, void 0, function* () { const canistersMetadata = yield Promise.all(canisterIds.map((canisterId) => (0, exports.getCanisterInfo)({ canisterId, agent }))); if (canistersMetadata.length === 0) return []; return canistersMetadata.filter((canister) => !!canister); }); exports.getMultipleCanisterInfo = getMultipleCanisterInfo; const getAll = (agent) => __awaiter(void 0, void 0, void 0, function* () { const allCanisters = yield new CanisterRegistry(agent).getAll(); return allCanisters.map(formatBackwardsCompatible); }); exports.getAll = getAll; exports.default = { getCanisterInfo: exports.getCanisterInfo, getMultipleCanisterInfo: exports.getMultipleCanisterInfo, getAll: (agent) => new CanisterRegistry(agent).getAll, };