@ledgerhq/coin-celo
Version:
61 lines (53 loc) • 2.07 kB
text/typescript
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 { getAccountDetails } from "../network";
import { CeloAccount } from "../types/types";
import { celoKit } from "../network/sdk";
const kit = celoKit();
export const getAccountShape: GetAccountShape<CeloAccount> = async info => {
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 operations = mergeOps(oldOperations, newOperations);
const shape = {
id: accountId,
balance,
spendableBalance,
operationsCount: operations.length,
blockHeight,
celoResources: {
registrationStatus: accountRegistrationStatus,
lockedBalance,
nonvotingLockedBalance,
pendingWithdrawals,
votes,
electionAddress: election.address,
lockedGoldAddress: lockedGold.address,
maxNumGroupsVotedFor: electionConfig.maxNumGroupsVotedFor,
},
};
return { ...shape, operations };
};
export const sync = makeSync({ getAccountShape });