@ledgerhq/coin-hedera
Version:
Ledger Hedera Coin integration
81 lines • 3.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBalance = getBalance;
const state_1 = require("@ledgerhq/cryptoassets/state");
const errors_1 = require("@ledgerhq/errors");
const live_promise_1 = require("@ledgerhq/live-promise");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const config_1 = __importDefault(require("../config"));
const errors_2 = require("../errors");
const api_1 = require("../network/api");
const utils_1 = require("../network/utils");
async function getBalance(currency, address) {
try {
const coinConfig = config_1.default.getCoinConfig(currency.id);
// Fetch only the specific staked node (or nothing at all for non-staking
// accounts) instead of paginating the full /network/nodes list. The
// validator lookup is chained on the account promise so it still runs
// concurrently with the token fetches.
const mirrorAccountPromise = api_1.apiClient.getAccount(address);
const validatorPromise = mirrorAccountPromise.then(account => typeof account.staked_node_id === "number" && account.staked_node_id >= 0
? api_1.apiClient.getNode(account.staked_node_id)
: null);
const [mirrorAccount, mirrorTokens, erc20TokenBalances, validator] = await Promise.all([
mirrorAccountPromise,
api_1.apiClient.getAccountTokens(address),
coinConfig.useHgraphForErc20 ? (0, utils_1.getERC20BalancesForAccountV2)(address) : Promise.resolve([]),
validatorPromise,
]);
const mixedTokens = [...mirrorTokens, ...erc20TokenBalances];
const nativeBalance = {
asset: { type: "native" },
value: BigInt(mirrorAccount.balance.balance),
...(validator && {
stake: {
uid: address,
address,
asset: { type: "native" },
state: "active",
amount: BigInt(mirrorAccount.balance.balance) + BigInt(mirrorAccount.pending_reward),
amountDeposited: BigInt(mirrorAccount.balance.balance),
amountRewarded: BigInt(mirrorAccount.pending_reward),
delegate: validator.node_account_id,
details: {
overstaked: BigInt(validator.stake) >= BigInt(validator.max_stake),
},
},
}),
};
const tokenBalances = await (0, live_promise_1.promiseAllBatched)(3, mixedTokens, async (item) => {
const tokenAddress = "token_id" in item ? item.token_id : item.token.contractAddress;
const calToken = await (0, state_1.getCryptoAssetsStore)().findTokenByAddressInCurrency(tokenAddress, currency.id);
if (!calToken || !calToken.units.length) {
return null;
}
const roundedValue = new bignumber_js_1.default(item.balance).toFixed(0);
return {
value: BigInt(roundedValue),
asset: {
type: calToken.tokenType,
assetReference: calToken.contractAddress,
assetOwner: address,
name: calToken.name,
unit: calToken.units[0],
},
};
});
const balances = [nativeBalance, ...tokenBalances].filter((balance) => balance !== null);
return balances;
}
catch (err) {
const isNonExistentAccount = err instanceof errors_2.HederaAddAccountError || (err instanceof errors_1.LedgerAPI4xx && err.status === 404);
if (isNonExistentAccount) {
return [];
}
throw err;
}
}
//# sourceMappingURL=getBalance.js.map