@funded-labs/dab-js
Version:
JS adapter for DAB
87 lines (86 loc) • 3.5 kB
JavaScript
;
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 methods_1 = require("./methods");
const account_1 = require("../../utils/account");
const validations_1 = require("../../utils/validations");
const DECIMALS = 8;
const NET_ID = {
blockchain: 'Internet Computer',
network: '00000000000000020101',
};
const ROSETTA_URL = 'https://rosetta-api.internetcomputer.org';
const getMetadata = (_actor) => __awaiter(void 0, void 0, void 0, function* () {
return {
fungible: {
symbol: 'ICP',
decimals: DECIMALS,
name: 'ICP',
fee: 10000,
},
};
});
const send = (actor, { to, amount, opts }) => __awaiter(void 0, void 0, void 0, function* () {
const defaultArgs = {
fee: BigInt(10000),
memo: BigInt(0),
};
const response = yield actor._send_dfx({
to: (0, validations_1.isValidPrincipal)(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* () {
const accountId = (0, account_1.getAccountId)(user);
const decimals = yield getDecimals(actor);
const response = yield fetch(`${ROSETTA_URL}/account/balance`, {
method: 'POST',
body: JSON.stringify({
network_identifier: NET_ID,
account_identifier: {
address: accountId,
},
}),
headers: {
'Content-Type': 'application/json',
Accept: '*/*',
},
});
if (!response.ok) {
return { value: 'Error', decimals, error: response.statusText };
}
// @ts-ignore
const { balances } = yield response.json();
const [{ value, currency }] = balances;
return { value, decimals: currency.decimals };
});
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,
};