UNPKG

@ledgerhq/coin-aptos

Version:
40 lines 2.23 kB
import BigNumber from "bignumber.js"; import { AptosAPI } from "../network"; import { GetCurrentDelegatorBalancesData } from "./graphql/queries"; import { isTestnet } from "../logic/isTestnet"; import { APTOS_EXPLORER_ACCOUNT_URL } from "../constants"; import { formatUnlockTime } from "../logic/staking"; import { makeLRUCache, minutes } from "@ledgerhq/live-network/cache"; export async function getValidators(currencyId) { const api = new AptosAPI(currencyId); const queryResponse = await cachedValidators(api, currencyId); const stakingData = queryResponse.data.current_delegator_balances; const list = await Promise.all(stakingData.map(async (pool) => { const aptosName = pool.staking_pool_metadata.operator_aptos_name; const naming = Array.isArray(aptosName) && aptosName.length > 0 && aptosName[0].domain_with_suffix ? aptosName[0].domain_with_suffix : pool.current_pool_balance.staking_pool_address; const url = `${APTOS_EXPLORER_ACCOUNT_URL}/${pool.current_pool_balance.staking_pool_address}?network=${isTestnet(currencyId) ? "testnet" : "mainnet"}`; const unblockdata = await api.getNextUnlockTime(pool.current_pool_balance.staking_pool_address); const nextUnlockTime = unblockdata ? formatUnlockTime(unblockdata) : undefined; //`${30}d ${20}h ${30}m`; return { commission: BigNumber(pool.current_pool_balance.operator_commission_percentage).div(100), activeStake: BigNumber(pool.current_pool_balance.total_coins), address: pool.current_pool_balance.staking_pool_address, name: naming, shares: pool.current_pool_balance.total_shares, wwwUrl: url, nextUnlockTime: nextUnlockTime, }; })); return list.sort((a, b) => b.activeStake.toNumber() - a.activeStake.toNumber()); } const fetchValidatorsData = async (api, _currencyId) => { const query = GetCurrentDelegatorBalancesData; return await api.apolloClient.query({ query: query, fetchPolicy: "network-only", }); }; const cachedValidators = makeLRUCache(fetchValidatorsData, (_api, currencyId) => currencyId, minutes(30)); //# sourceMappingURL=validators.js.map