@funded-labs/plug-controller
Version:
Internet Computer Plug wallet's controller
72 lines (71 loc) • 3.23 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getICPBalance = exports.getICPTransactions = exports.MILI_PER_SECOND = void 0;
/* eslint-disable camelcase */
/* eslint-disable @typescript-eslint/camelcase */
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const tokens_1 = require("../../../constants/tokens");
const errors_1 = require("../../../errors");
const constants_1 = require("../constants");
const ipcTransactionFormatter_1 = require("../../formatter/transaction/ipcTransactionFormatter");
exports.MILI_PER_SECOND = 1000000;
;
const getICPTransactions = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
const response = yield (0, cross_fetch_1.default)(`${constants_1.ROSETTA_URL}/search/transactions`, {
method: 'POST',
body: JSON.stringify({
network_identifier: constants_1.NET_ID,
account_identifier: {
address: accountId,
},
}),
headers: {
'Content-Type': 'application/json',
Accept: '*/*',
},
});
if (!response.ok)
throw Error(`${errors_1.ERRORS.GET_TRANSACTIONS_FAILS} ${response.statusText}`);
const { transactions, total_count } = yield response.json();
const transactionsInfo = transactions.map(({ transaction }) => (0, ipcTransactionFormatter_1.formatIcpTransaccion)(accountId, transaction));
return {
total: total_count,
transactions: transactionsInfo,
};
});
exports.getICPTransactions = getICPTransactions;
const getICPBalance = (accountId) => __awaiter(void 0, void 0, void 0, function* () {
const response = yield (0, cross_fetch_1.default)(`${constants_1.ROSETTA_URL}/account/balance`, {
method: 'POST',
body: JSON.stringify({
network_identifier: constants_1.NET_ID,
account_identifier: {
address: accountId,
},
}),
headers: {
'Content-Type': 'application/json',
Accept: '*/*',
},
});
if (!response.ok) {
return { value: 'Error', decimals: tokens_1.TOKENS.ICP.decimals, error: response.statusText };
}
const { balances } = yield response.json();
const [{ value, currency }] = balances;
return { value, decimals: currency.decimals };
});
exports.getICPBalance = getICPBalance;
exports.default = {};