@ledgerhq/coin-icon
Version:
Ledger Icon Coin integration
67 lines • 2.74 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAccountShape = void 0;
const accountId_1 = require("@ledgerhq/coin-framework/account/accountId");
const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers");
const api_1 = require("./api");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const node_1 = require("./api/node");
const logic_1 = require("./logic");
const getAccountShape = async (info) => {
const { address, initialAccount, currency, derivationMode } = info;
const accountId = (0, accountId_1.encodeAccountId)({
type: "js",
version: "2",
currencyId: currency.id,
xpubOrAddress: address,
derivationMode,
});
try {
const oldOperations = initialAccount?.operations || [];
const blockHeight = await (0, api_1.getCurrentBlockHeight)(currency);
const iconAccount = await (0, api_1.getAccount)(info.address, currency);
// Merge new operations with the previously synced ones
const newOperations = await (0, api_1.getOperations)(accountId, address, 0, // skip === 0 is the lastest transaction
currency, iconAccount?.contract_updated_block - oldOperations.length);
const operations = (0, jsHelpers_1.mergeOps)(oldOperations, newOperations);
const delegationData = await (0, node_1.getDelegation)(address, currency);
const iconResources = {
nonce: 0,
totalDelegated: delegationData.totalDelegated,
votingPower: delegationData.votingPower,
};
const balance = (0, logic_1.convertICXtoLoop)(iconAccount?.balance || 0);
const spendableBalance = balance
.minus(iconResources.totalDelegated)
.minus(iconResources.votingPower);
return {
id: accountId,
balance,
spendableBalance,
operationsCount: operations.length,
blockHeight,
iconResources,
operations,
};
}
catch {
return {
id: accountId,
balance: new bignumber_js_1.default(0),
spendableBalance: new bignumber_js_1.default(0),
operationsCount: 0,
iconResources: {
nonce: 0,
totalDelegated: new bignumber_js_1.default(0),
votingPower: new bignumber_js_1.default(0),
},
blockHeight: 0,
operations: [],
};
}
};
exports.getAccountShape = getAccountShape;
//# sourceMappingURL=synchronization.js.map