UNPKG

js-isp

Version:

158 lines (157 loc) 5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ISP = void 0; const agent_1 = require("@dfinity/agent"); const isp_1 = require("./did/isp/isp"); const xtc_1 = require("./did/xtc/xtc"); const wicp_1 = require("./did/wicp/wicp"); const ledger_1 = require("./did/ledger/ledger"); const common_1 = require("../utils/common"); const ISPCanisterId = "p2pki-xyaaa-aaaan-qatua-cai"; const XTCCanisterId = "aanaa-xaaaa-aaaah-aaeiq-cai"; const WICPCanisterId = "utozz-siaaa-aaaam-qaaxq-cai"; const LedgerCanisterId = "ryjl3-tyaaa-aaaaa-aaaba-cai"; const WICPAccount = "cc659fe529756bae6f72db9937c6c60cf7ad57eb4ac5f930a75748927aab469a"; class ISP { constructor(agent) { this.canisterId = ISPCanisterId; this.agent = agent; this.ISPActor = agent_1.Actor.createActor(isp_1.idlFactory, { agent, canisterId: this.canisterId }); } async getXTCActor() { return agent_1.Actor.createActor(xtc_1.idlFactory, { agent: this.agent, canisterId: XTCCanisterId }); } async getWICPActor() { return agent_1.Actor.createActor(wicp_1.idlFactory, { agent: this.agent, canisterId: WICPCanisterId }); } async getLedgerActor() { return agent_1.Actor.createActor(ledger_1.idlFactory, { agent: this.agent, canisterId: LedgerCanisterId }); } async get_user_icsps() { try { return (await this.ISPActor.getUserICSPs()); } catch (e) { throw e; } } async get_sub_account() { try { const userSubAccount = (await this.ISPActor.getSubAccount()); return (0, common_1.ArrayToHexString)(userSubAccount); } catch (e) { throw e; } } async get_icp_balance() { try { return (await this.ISPActor.getUserSubAccountICPBalance()); } catch (e) { throw e; } } async transfer_out_icp(accountAdress, amount) { try { const u8Account = (0, common_1.getUint8ArrayFromHex)(accountAdress); return (await this.ISPActor.transferOutUserSubAccountICP(Array.from(u8Account), amount.e8s)); } catch (e) { throw e; } } /* * @argument * icspName:the name of icsp * * icp_to_create_amount:e8s icp amount to create canister * * xtc_to_topup_amount:e8s icp amount burn xtc to topup canister */ async create_icsp_topup_by_xtc(createArgs) { try { const icspCai = await this.CreateIcsp(createArgs.icspName, createArgs.icp_to_create_amount); if (icspCai.ok) { const burnRes = await this.top_up_icsp_with_xtc(icspCai.ok, createArgs.xtc_to_topup_amount.e8s); if (burnRes.Ok) { return { icspCanisterId: icspCai, burnTransactionId: burnRes }; } else throw new Error(Object.keys(burnRes.Err)[0]); } else throw new Error(Object.keys(icspCai.err)[0]); } catch (e) { throw e; } } async create_icsp_topup_by_icp(createArgs) { try { const icspCai = await this.CreateIcsp(createArgs.icspName, createArgs.icp_to_create_amount); if (icspCai.ok) { const burnRes = await this.top_up_icsp({ icsp_canisterId: icspCai.ok, icp_amount: createArgs.icp_to_topup_amount.e8s }); if (Object.keys(burnRes)[0] === "ok") return icspCai; else throw new Error(Object.keys(burnRes.err)[0]); } else throw new Error(Object.keys(icspCai.err)[0]); } catch (e) { throw e; } } async CreateIcsp(icspName, icp_to_create_amount) { try { return (await this.ISPActor.createICSP(icspName, icp_to_create_amount.e8s)); } catch (e) { throw e; } } async top_up_icsp_with_xtc(canisterId, amount) { try { const Actor = await this.getXTCActor(); const burnArgs = { canister_id: canisterId, amount: amount }; return await Actor.burn(burnArgs); } catch (e) { throw e; } } /* * @argument * icsp_canisterId:the canisterPrincipal of icsp * * icp_amount:e8s icp amount */ async top_up_icsp(topUpArg) { try { return (await this.ISPActor.topUpICSP(topUpArg)); } catch (e) { throw e; } } async getVersion() { try { return (await this.ISPActor.getVersion()); } catch (e) { throw e; } } } exports.ISP = ISP;