UNPKG

ic0

Version:

An easy-to-use JavaScript API for the Internet Computer.

70 lines 3.1 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.agentCanister = exports.AgentCanister = void 0; const agent_1 = require("@dfinity/agent"); const cross_fetch_1 = __importDefault(require("cross-fetch")); const didJsCache = new Map(); class AgentCanister { constructor(agent, id) { this.agent = agent; this.id = id; } _fetchActor() { return __awaiter(this, void 0, void 0, function* () { if (this._actor) { return this._actor; } let js = didJsCache.get(this.id); if (!js) { const source = yield (0, agent_1.fetchCandid)(this.id, this.agent); // TODO: use local Candid UI canister const didJsAgent = agent_1.HttpAgent.createSync({ host: 'https://icp-api.io', fetch: cross_fetch_1.default, }); // mainnet const didJsCanisterId = 'a4gq6-oaaaa-aaaab-qaa4q-cai'; const didJsInterface = ({ IDL }) => IDL.Service({ did_to_js: IDL.Func([IDL.Text], [IDL.Opt(IDL.Text)], ['query']), }); const didJs = agent_1.Actor.createActor(didJsInterface, { canisterId: didJsCanisterId, agent: didJsAgent, }); js = (yield didJs.did_to_js(source))[0]; didJsCache.set(this.id, js); } const candid = yield eval(`import("data:text/javascript;charset=utf-8,${encodeURIComponent(js)}")`); const actor = agent_1.Actor.createActor(candid.idlFactory, { agent: this.agent, canisterId: this.id, }); this._actor = actor; return actor; }); } call(method, ...args) { return __awaiter(this, void 0, void 0, function* () { const actor = yield this._fetchActor(); const result = yield actor[method](...args); return result; }); } } exports.AgentCanister = AgentCanister; function agentCanister(agent, id) { return new AgentCanister(agent, id); } exports.agentCanister = agentCanister; //# sourceMappingURL=agentCanister.js.map