UNPKG

@ledgerhq/coin-celo

Version:
68 lines (58 loc) 2.43 kB
import { getAccountRegistrationStatus, getPendingWithdrawals, getVotes } from "../network/sdk"; import { makeSync, mergeOps } from "@ledgerhq/coin-framework/bridge/jsHelpers"; import type { GetAccountShape } from "@ledgerhq/coin-framework/bridge/jsHelpers"; import { encodeAccountId } from "@ledgerhq/coin-framework/account"; import { getEnv } from "@ledgerhq/live-env"; import { getAccountDetails } from "../network"; import { CeloAccount } from "../types/types"; import { celoKit } from "../network/sdk"; import { getAccountShape as evmGetAccountShape } from "@ledgerhq/coin-evm/bridge/synchronization"; const kit = celoKit(); export const getAccountShape: GetAccountShape<CeloAccount> = async (info, config) => { const { address, currency, initialAccount, derivationMode } = info; const oldOperations = initialAccount?.operations || []; const election = await kit.contracts.getElection(); const electionConfig = await election.getConfig(); const lockedGold = await kit.contracts.getLockedGold(); const accountId = encodeAccountId({ type: "js", version: "2", currencyId: currency.id, xpubOrAddress: address, derivationMode, }); const { blockHeight, balance, spendableBalance, operations: newOperations, lockedBalance, nonvotingLockedBalance, } = await getAccountDetails(address, accountId); const accountRegistrationStatus = await getAccountRegistrationStatus(address); const pendingWithdrawals = accountRegistrationStatus ? await getPendingWithdrawals(address) : []; const votes = accountRegistrationStatus ? await getVotes(address) : []; const fromEvm = await evmGetAccountShape(info, config); const operations = mergeOps(oldOperations, newOperations); const shape: Partial<CeloAccount> = { id: accountId, balance, blockHeight, operationsCount: operations.length, spendableBalance, subAccounts: getEnv("ENABLE_CELO_TOKENS") === false ? [] : fromEvm.subAccounts || [], syncHash: fromEvm.syncHash, celoResources: { registrationStatus: accountRegistrationStatus, lockedBalance, nonvotingLockedBalance, pendingWithdrawals, votes, electionAddress: election.address, lockedGoldAddress: lockedGold.address, maxNumGroupsVotedFor: electionConfig.maxNumGroupsVotedFor, }, }; return { ...shape, operations }; }; export const sync = makeSync({ getAccountShape });