UNPKG

test-ic-wallet-middleware-icrc

Version:
156 lines (155 loc) 6.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Icrc84ActorWrapper = void 0; const agent_1 = require("@dfinity/agent"); const principal_1 = require("@dfinity/principal"); const icrc84_did_1 = require("../../../candid/icrc84/icrc84.did"); const icrc84Error_1 = require("../../../errors/icrc84Error"); const types_1 = require("../../../types"); class Icrc84ActorWrapper { actor; constructor(actor) { this.actor = actor; } static create(agent, servicePrincipal) { const actor = Icrc84ActorWrapper.getIcrc84Actor(agent, servicePrincipal); return new Icrc84ActorWrapper(actor); } async getSupportedAssets() { try { const result = await this.actor.icrc84_supported_tokens(); return result; } catch (e) { throw new icrc84Error_1.Icrc84Error("getSupportedAssets", e.message); } } async getAssetInfo(ledgerAddress) { try { const result = await this.actor.icrc84_token_info(principal_1.Principal.fromText(ledgerAddress)); return result; } catch (e) { throw new icrc84Error_1.Icrc84Error("getAssetInfo", e.message); } } async getAllCredits() { try { const credits = await this.actor.icrc84_all_credits(); const result = credits.map((cr) => { return { ledgerAddress: cr[0].toString(), credit: cr[1] }; }); return result; } catch (e) { throw new icrc84Error_1.Icrc84Error("getAllCredits", e.message); } } async getCredit(ledgerAddress) { try { const result = await this.actor.icrc84_credit(principal_1.Principal.fromText(ledgerAddress)); return result; } catch (e) { throw new icrc84Error_1.Icrc84Error("getCredit", e.message); } } async trackedDeposit(ledgerAddress) { try { const response = await this.actor.icrc84_trackedDeposit(principal_1.Principal.fromText(ledgerAddress)); const result = response.Ok; if (result || result === BigInt(0)) { return result; } if (response.Err?.NotAvailable) { throw new icrc84Error_1.Icrc84Error("tracked.deposit.error.not.available", response.Err.NotAvailable.message); } throw new icrc84Error_1.Icrc84Error("tracked.deposit", "tracked deposit actor error"); } catch (e) { throw new icrc84Error_1.Icrc84Error("trackedDeposit", e.message); } } async notify(ledgerAddress) { try { const response = await this.actor.icrc84_notify({ token: principal_1.Principal.fromText(ledgerAddress) }); if (response.Ok) { return { Credit: response.Ok.credit, CreditInc: response.Ok.credit_inc, DepositInc: response.Ok.deposit_inc }; } if (response.Err?.NotAvailable) { throw new icrc84Error_1.Icrc84Error("notify.error.not.available", response.Err.NotAvailable.message); } if (response.Err?.CallLedgerError) { throw new icrc84Error_1.Icrc84Error("notify.error.call.ledger.error", response.Err.CallLedgerError.message); } throw new icrc84Error_1.Icrc84Error("notify", "notify actor error"); } catch (e) { throw new icrc84Error_1.Icrc84Error("notify", e.message); } } async withdraw(owner, ledgerAddress, toSubAccount, amount, expectedFee) { try { const response = await this.actor.icrc84_withdraw({ token: principal_1.Principal.fromText(ledgerAddress), to: { owner: principal_1.Principal.fromText(owner), subaccount: toSubAccount, }, amount: amount, expected_fee: expectedFee }); if (response.Ok) { return { txId: response.Ok.txid, amount: response.Ok.amount }; } if (response.Err?.AmountBelowMinimum) { throw new icrc84Error_1.Icrc84Error("withdraw.amount.below.minimum", "Amount Below Minimum"); } if (response.Err?.InsufficientCredit) { throw new icrc84Error_1.Icrc84Error("withdraw.insufficient.credit", "Insufficient Credit"); } if (response.Err?.CallLedgerError) { throw new icrc84Error_1.Icrc84Error("withdraw.call.ledger.error", response.Err.CallLedgerError.message); } if (response.Err?.BadFee) { throw new icrc84Error_1.Icrc84Error("withdraw.bad.fee", `Bad Expected Fee: ${response.Err.BadFee.expected_fee}`); } throw new icrc84Error_1.Icrc84Error("withdraw", "withdraw actor error"); } catch (e) { throw new icrc84Error_1.Icrc84Error("withdraw custom message", e.message); } } async principalToSubaccount(principal) { try { const subAccount = await this.actor.principalToSubaccount(principal); if (subAccount.length > 0) { return types_1.SubAccountId.parseFromUint8Array(subAccount[0]); } return undefined; } catch (e) { throw new icrc84Error_1.Icrc84Error("principalToSubaccount", e.message); } } static getIcrc84Actor(agent, servicePrincipal) { const ownersActor = agent_1.Actor.createActor(icrc84_did_1.idlFactory, { agent: agent, canisterId: servicePrincipal, }); return ownersActor; } } exports.Icrc84ActorWrapper = Icrc84ActorWrapper;