UNPKG

@funded-labs/dab-js

Version:
94 lines (93 loc) 3.85 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 }); const methods_1 = require("./methods"); const principal_1 = require("@dfinity/principal"); const getMetadata = (actor) => __awaiter(void 0, void 0, void 0, function* () { var _a; const metadataResult = yield actor._icrc1_metadata(); const symbol = yield actor._icrc1_symbol(); const decimals = yield actor._icrc1_decimals(); const name = yield actor._icrc1_name(); const fee = yield actor._icrc1_fee(); const totalSupply = yield actor._icrc1_total_supply(); const mintingAccount = yield actor._icrc1_minting_account(); const logo = (_a = metadataResult.find(([name]) => name === 'icrc1:logo')) === null || _a === void 0 ? void 0 : _a[1]; const owner = !mintingAccount[0] ? principal_1.Principal.anonymous() : mintingAccount[0].owner; return { fungible: { symbol, decimals, name, fee, totalSupply, logo: (logo === null || logo === void 0 ? void 0 : logo['Text']) || '', owner, }, }; }); const getBalance = (actor, user) => __awaiter(void 0, void 0, void 0, function* () { try { const balance = yield actor._icrc1_balance_of({ owner: user, subaccount: [], }); return { value: balance.toString(), decimals: 8 }; } catch (e) { return { value: 'Error', decimals: 8, error: `Error while fetching your balance: ${e.message}`, }; } }); 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, memo: [], subaccount: [], }; const response = yield actor._icrc1_transfer({ to: { owner: principal_1.Principal.fromText(to), subaccount: (opts === null || opts === void 0 ? void 0 : opts.to_subaccount) || defaultArgs.subaccount, }, fee: [(opts === null || opts === void 0 ? void 0 : opts.fee) ? BigInt(opts === null || opts === void 0 ? void 0 : opts.fee) : BigInt(defaultArgs.fee)], memo: [opts.memo ? [Number(opts.memo)] : defaultArgs.memo], from_subaccount: [], created_at_time: [], amount, }); if (response['Err']) { return Promise.reject(response['Err']); } return { height: response['Ok'].toString() }; }); 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 = { getMetadata, getBalance, send, burnXTC, getDecimals, approve, };