UNPKG

@funded-labs/dab-js

Version:
97 lines (96 loc) 4.62 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable @typescript-eslint/camelcase */ const principal_1 = require("@dfinity/principal"); const agent_1 = require("@dfinity/agent"); const methods_1 = require("./methods"); const account_1 = require("../../utils/account"); const validations_1 = require("../../utils/validations"); const registries_1 = require("../../registries"); const getMetadata = (_actor) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c; const agent = agent_1.Actor.agentOf(_actor); try { const tokenRegistry = new registries_1.TokenRegistry(agent); const token = yield tokenRegistry.get(agent_1.Actor.canisterIdOf(_actor).toString()); const { fee = 0.002, decimals = 8 } = (token === null || token === void 0 ? void 0 : token.details) || {}; const numberFee = Number((_a = fee === null || fee === void 0 ? void 0 : fee.toString) === null || _a === void 0 ? void 0 : _a.call(fee)); const numberDecimals = Number((_b = decimals === null || decimals === void 0 ? void 0 : decimals.toString) === null || _b === void 0 ? void 0 : _b.call(decimals)); const parsedFee = numberFee * Math.pow(10, numberDecimals); return { fungible: { symbol: ((_c = token === null || token === void 0 ? void 0 : token.details) === null || _c === void 0 ? void 0 : _c.symbol) || 'ICP', name: (token === null || token === void 0 ? void 0 : token.name) || 'ICP', decimals: numberDecimals, fee: parsedFee, }, }; } catch (e) { console.error('Error while fetching token metadata, falling back to default values', e); // Fallback to default ICP values when dab is unavailable return { fungible: { symbol: 'ICP', name: 'ICP', decimals: 8, fee: 10000, }, }; } }); const send = (actor, { to, amount, opts }) => __awaiter(void 0, void 0, void 0, function* () { const metadata = yield getMetadata(actor); const { fee = 0.002, decimals = BigInt(8) } = (metadata === null || metadata === void 0 ? void 0 : metadata.fungible) || {}; const defaultArgs = { fee: BigInt(fee * Math.pow(10, parseInt(decimals.toString(), 10))), memo: BigInt(0), }; const response = yield actor._send_dfx({ to: (0, validations_1.validatePrincipalId)(to) ? (0, account_1.getAccountId)(principal_1.Principal.fromText(to)) : to, fee: { e8s: (opts === null || opts === void 0 ? void 0 : opts.fee) || defaultArgs.fee }, amount: { e8s: amount }, memo: (opts === null || opts === void 0 ? void 0 : opts.memo) ? BigInt(opts.memo) : defaultArgs.memo, from_subaccount: [], created_at_time: [], }); return { height: yield response.toString() }; }); const getBalance = (actor, user) => __awaiter(void 0, void 0, void 0, function* () { try { const account = (0, account_1.getAccountId)(user); const balance = yield actor._account_balance_dfx({ account }); return { value: balance.e8s.toString(), decimals: 8 }; } catch (e) { return { value: 'Error', decimals: 8, error: 'Error while fetching your balance', }; } }); const burnXTC = (_actor, _params) => __awaiter(void 0, void 0, void 0, function* () { throw new Error('BURN NOT SUPPORTED'); }); const approve = (_actor, _params) => __awaiter(void 0, void 0, void 0, function* () { throw new Error('APPROVE NOT SUPPORTED'); }); const getDecimals = (actor) => __awaiter(void 0, void 0, void 0, function* () { return (0, methods_1.getDecimalsFromMetadata)(yield getMetadata(actor)); }); exports.default = { send, getMetadata, getBalance, burnXTC, getDecimals, approve, };