UNPKG

test-ic-wallet-middleware-icrc

Version:
208 lines (207 loc) 7.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LedgerWrapper = void 0; const ledger_icrc_1 = require("@dfinity/ledger-icrc"); const principal_1 = require("@dfinity/principal"); const icrcLedgerError_1 = require("../../../errors/icrcLedgerError"); class LedgerWrapper { icrcLedgerCanister; constructor(icrcLedgerCanister) { this.icrcLedgerCanister = icrcLedgerCanister; } static create(agent, ledgerAddress) { const icrcLedgerCanister = LedgerWrapper.getIcrcLedgerCanister(agent, ledgerAddress); return new LedgerWrapper(icrcLedgerCanister); } async getBalance(subAccountId, principal) { try { const balance = await this.icrcLedgerCanister.balance({ owner: principal, subaccount: subAccountId.toUint8Array(), certified: false, }); return balance; } catch (e) { throw new icrcLedgerError_1.IcrcLegerError("get.balance.generic", e.message); } } async transfer(info) { try { const blockIndex = await this.icrcLedgerCanister.transfer({ to: { owner: info.toAccountPrincipal, subaccount: info.toSubAccountId ? [info.toSubAccountId.toUint8Array()] : [], }, amount: info.amount, from_subaccount: info.fromSubAccountId.toUint8Array(), }); return blockIndex; } catch (e) { let message = e.message; const searchMessage = "Reject text: "; let indexOfMessage = message.indexOf("Reject text: "); if (indexOfMessage >= 0) { message = message.substring(indexOfMessage + searchMessage.length).trim(); throw new icrcLedgerError_1.IcrcLegerError("transfer.rejected", message); } throw new icrcLedgerError_1.IcrcLegerError("transfer.generic", e.message); } } async transferFrom(info) { try { const blockIndex = await this.icrcLedgerCanister.transferFrom({ from: { owner: info.fromAccountPrincipal, subaccount: [info.fromSubAccountId.toUint8Array()] }, to: { owner: info.toAccountPrincipal, subaccount: [info.toSubAccountId.toUint8Array()] }, amount: info.amount }); return blockIndex; } catch (e) { let message = e.message; const searchMessage = "Reject text: "; let indexOfMessage = message.indexOf("Reject text: "); if (indexOfMessage >= 0) { message = message.substring(indexOfMessage + searchMessage.length).trim(); throw new icrcLedgerError_1.IcrcLegerError("transfer.rejected", message); } throw new icrcLedgerError_1.IcrcLegerError("transfer.generic", e.message); } } async getIcrcMetadataInfo() { try { const metadata = await this.icrcLedgerCanister.metadata({ certified: false, }); return LedgerWrapper.parseMetadataInfo(metadata); } catch (e) { let message = e.message; const searchMessage = "Body: "; let indexOfMessage = message.indexOf(searchMessage); if (indexOfMessage >= 0) { message = message.substring(indexOfMessage + searchMessage.length).trim(); throw new icrcLedgerError_1.IcrcLegerError("get.metadata.bad.request", message); } throw new icrcLedgerError_1.IcrcLegerError("get.metadata.generic", e.message); } } async getTransactionFee() { try { const transactionFee = await this.icrcLedgerCanister.transactionFee({ certified: false, }); return transactionFee; } catch (e) { throw new icrcLedgerError_1.IcrcLegerError("get.transaction.fee.generic", e.message); } } async getAllowance(ownerPrincipal, spenderPrincipal, subaccount, spenderSubaccount) { try { const param = { spender: { owner: spenderPrincipal, subaccount: spenderSubaccount ? [spenderSubaccount.toUint8Array()] : [] }, account: { owner: ownerPrincipal, subaccount: [subaccount.toUint8Array()] } }; const allowance = await this.icrcLedgerCanister.allowance(param); return { allowance: allowance.allowance, expiration: this.convertDate(allowance.expires_at) }; } catch (e) { throw new icrcLedgerError_1.IcrcLegerError("approve.allowance.generic", e.message); } } convertDate(expiresAt) { return expiresAt.length > 0 ? expiresAt[0] : undefined; } static async approveAllowance(info, agent) { const ledgerWrapper = LedgerWrapper.create(agent, info.ledgerAddress); await ledgerWrapper.approveAllowance(info); } async approveAllowance(info) { try { const params = { amount: info.amount, spender: { owner: principal_1.Principal.fromText(info.spenderPrincipal), subaccount: [info.spenderSubId.toUint8Array()] }, from_subaccount: info.subAccountId.toUint8Array(), expires_at: info.expiration ? BigInt(info.expiration) : undefined }; const result = await this.icrcLedgerCanister.approve(params); return result; } catch (e) { throw new icrcLedgerError_1.IcrcLegerError("approve.allowance", e.message); } } static parseMetadataInfo(metadata) { let symbol = "symbol"; let name = "symbol"; let decimals = 0; let logo = ""; let fee = BigInt(0); metadata.map((dt) => { if (dt[0] === "icrc1:symbol") { const auxSymbol = dt[1]; symbol = auxSymbol.Text; } if (dt[0] === "icrc1:name") { const auxName = dt[1]; name = auxName.Text; } if (dt[0] === "icrc1:decimals") { const auxDec = dt[1]; decimals = Number(auxDec.Nat); } if (dt[0] === "icrc1:logo") { const auxName = dt[1]; logo = auxName.Text; } if (dt[0] === "icrc1:fee") { const feeList = dt[1]; let feeRaw = feeList.Nat[0]; if (!feeRaw) { const feeNumber = dt[1]; feeRaw = feeNumber.Nat; } fee = BigInt(feeRaw); } }); return { symbol, name, decimals, logo, fee: fee }; } static getIcrcLedgerCanister(agent, ledgerAddress) { try { const result = ledger_icrc_1.IcrcLedgerCanister.create({ agent: agent, canisterId: ledgerAddress, }); return result; } catch (e) { if (e.message && e.message.toString().indexOf("Canister ID is required") > -1) { throw e; } throw new icrcLedgerError_1.IcrcLegerError("getIcrcLedgerCanister", e.message); } } } exports.LedgerWrapper = LedgerWrapper;