UNPKG

@ledgerhq/live-common

Version:
194 lines • 7.54 kB
import Prando from "prando"; import { BigNumber } from "bignumber.js"; import { genAccount as genAccountCommon, genOperation, ensureNoNegative, } from "@ledgerhq/coin-framework/mocks/account"; import { getAccountBridge } from "../bridge"; import perFamilyMock from "../generated/mock"; import { PaymentChain } from "@ledgerhq/coin-cardano/types"; /** * @memberof mock/account */ export function genAddingOperationsInAccount(account, count, seed) { const rng = new Prando(seed); const copy = { ...account }; copy.operations = Array(count) .fill(null) .reduce(ops => { const op = genOperation(copy, copy, ops, rng); return ops.concat(op); }, copy.operations); copy.spendableBalance = copy.balance = ensureNoNegative(copy.operations); const perFamilyOperation = perFamilyMock[account.currency.id]; const postSyncAccount = perFamilyOperation && perFamilyOperation.postSyncAccount; if (postSyncAccount) postSyncAccount(copy); return copy; } export function genAccount(id, opts = {}) { return genAccountCommon(id, opts, (account, currency, address) => { switch (currency.family) { case "solana": account.solanaResources = { stakes: [], unstakeReserve: new BigNumber(0), }; break; case "cosmos": account.cosmosResources = { // TODO variation in these delegations: [], redelegations: [], unbondings: [], delegatedBalance: new BigNumber(0), pendingRewardsBalance: new BigNumber(0), unbondingBalance: new BigNumber(0), withdrawAddress: address, sequence: 0, }; break; case "bitcoin": account.bitcoinResources = { utxos: [], walletAccount: undefined, }; break; case "polkadot": account.polkadotResources = { stash: null, controller: null, nonce: 0, lockedBalance: new BigNumber(0), unlockingBalance: new BigNumber(0), unlockedBalance: new BigNumber(0), unlockings: [], nominations: [], numSlashingSpans: 0, }; break; case "tezos": account.tezosResources = { revealed: true, counter: 0, }; break; case "tron": // TODO variation in these. you could use the account.name as a way to split cases account.tronResources = { frozen: { bandwidth: null, energy: null, }, unFrozen: { bandwidth: null, energy: null, }, delegatedFrozen: { bandwidth: null, energy: null, }, legacyFrozen: { bandwidth: null, energy: null, }, votes: [], tronPower: 0, energy: BigNumber(0), bandwidth: { freeUsed: BigNumber(0), freeLimit: BigNumber(opts.bandwidth ? 1 : 0), gainedUsed: BigNumber(0), gainedLimit: BigNumber(0), }, unwithdrawnReward: BigNumber(0), lastWithdrawnRewardDate: null, lastVotedDate: null, cacheTransactionInfoById: {}, }; break; case "cardano": account.cardanoResources = { delegation: { status: true, deposit: "2000000", poolId: "45", ticker: "ADA", name: "Cardano", dRepHex: undefined, rewards: new BigNumber(42), }, externalCredentials: [ { isUsed: false, key: "test", path: { purpose: 1852, coin: 1815, account: 4, chain: PaymentChain.external, index: 0, }, }, ], internalCredentials: [ { isUsed: false, key: "test", path: { purpose: 1852, coin: 1815, account: 4, chain: PaymentChain.internal, index: 0, }, }, ], utxos: [ { hash: "", index: 0, address: "", amount: new BigNumber(10), tokens: [], paymentCredential: { key: "", path: { purpose: 0, coin: 0, account: 0, chain: PaymentChain.internal, index: 0 }, }, }, ], protocolParams: { minFeeA: "", minFeeB: "", stakeKeyDeposit: "", lovelacePerUtxoWord: "", collateralPercent: "", priceMem: "", priceSteps: "", maxTxSize: "", maxValueSize: "", utxoCostPerByte: "", minFeeRefScriptCostPerByte: "", languageView: {}, }, }; break; default: { try { const bridge = getAccountBridge(account); const initAccount = bridge.initAccount; if (initAccount) { initAccount(account); } } catch (e) { // to fix /src/__tests__/cross.ts, skip bridge error if there is no bridge in such currency } } } }, (account, currency, rng) => { const perFamilyOperation = perFamilyMock[currency.id]; const genAccountEnhanceOperations = perFamilyOperation && perFamilyOperation.genAccountEnhanceOperations; if (genAccountEnhanceOperations) { genAccountEnhanceOperations(account, rng); } }); } //# sourceMappingURL=account.js.map