From 5bac45b09451786b104e0afce57e6c86ae43b68d Mon Sep 17 00:00:00 2001 From: Prateek Rathod Date: Thu, 24 Aug 2023 20:13:39 +0530 Subject: [PATCH] feat(cspr): add mock.ts to bridge --- .../src/families/casper/bridge/account.ts | 1 - .../src/families/casper/bridge/mock.ts | 169 ++++++++++++++++++ 2 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 libs/ledger-live-common/src/families/casper/bridge/mock.ts diff --git a/libs/ledger-live-common/src/families/casper/bridge/account.ts b/libs/ledger-live-common/src/families/casper/bridge/account.ts index 04a8b35d1f93..ff1970ed2eb3 100644 --- a/libs/ledger-live-common/src/families/casper/bridge/account.ts +++ b/libs/ledger-live-common/src/families/casper/bridge/account.ts @@ -106,7 +106,6 @@ const getTransactionStatus = async (a: Account, t: Transaction): Promise ({ + family: "casper", + amount: new BigNumber(0), + fees: getEstimatedFees(), + recipient: "", + useAllAmount: false, +}); + +const getTransactionStatus = async (a: Account, t: Transaction): Promise => { + const errors: TransactionStatus["errors"] = {}; + const warnings: TransactionStatus["warnings"] = {}; + + const { balance, spendableBalance } = a; + const { address } = getAddress(a); + const { recipient, useAllAmount } = t; + let { amount } = t; + + if (!recipient) errors.recipient = new RecipientRequired(); + else if (!isAddressValid(recipient)) + errors.recipient = new InvalidAddress("", { + currencyName: a.currency.name, + }); + else if (recipient.toLowerCase() === address.toLowerCase()) + errors.recipient = new InvalidAddressBecauseDestinationIsAlsoSource(); + + if (!isAddressValid(address)) + errors.sender = new InvalidAddress("", { + currencyName: a.currency.name, + }); + else if (!isTransferIdValid(t.transferId)) { + errors.sender = new CasperInvalidTransferId("", { + maxTransferId: CASPER_MAX_TRANSFER_ID, + }); + } + + const estimatedFees = t.fees; + + let totalSpent = BigNumber(0); + + if (useAllAmount) { + totalSpent = a.spendableBalance; + amount = totalSpent.minus(estimatedFees); + if (amount.lte(0) || totalSpent.gt(balance)) { + errors.amount = new NotEnoughBalance(); + } + } + + if (!useAllAmount) { + totalSpent = amount.plus(estimatedFees); + if (amount.eq(0)) { + errors.amount = new AmountRequired(); + } + + if (totalSpent.gt(a.spendableBalance)) { + errors.amount = new NotEnoughBalance(); + } + } + + if (amount.lt(CASPER_MINIMUM_VALID_AMOUNT_MOTES) && !errors.amount) + errors.amount = new InvalidMinimumAmount("", { + minAmount: `${CASPER_MINIMUM_VALID_AMOUNT_CSPR} CSPR`, + }); + + if (spendableBalance.minus(totalSpent).minus(estimatedFees).lt(CASPER_MINIMUM_VALID_AMOUNT_MOTES)) + warnings.amount = new MayBlockAccount("", { + minAmount: `${CASPER_MINIMUM_VALID_AMOUNT_CSPR + CASPER_FEES_CSPR} CSPR`, + }); + + return { + errors, + warnings, + estimatedFees, + amount, + totalSpent, + }; +}; + +const prepareTransaction = async (a: Account, t: Transaction): Promise => { + const { address } = getAddress(a); + const { recipient } = t; + + if (recipient && address) { + if (t.useAllAmount) { + const amount = a.spendableBalance.minus(t.fees); + return { ...t, amount }; + } + } + + return t; +}; + +const estimateMaxSpendable = async ({ + account, + parentAccount, + transaction, +}: { + account: AccountLike; + parentAccount?: Account | null | undefined; + transaction?: Transaction | null | undefined; +}): Promise => { + const a = getMainAccount(account, parentAccount); + let balance = a.spendableBalance; + + if (balance.eq(0)) return balance; + + const estimatedFees = transaction?.fees ?? getEstimatedFees(); + + if (balance.lte(estimatedFees)) return new BigNumber(0); + + balance = balance.minus(estimatedFees); + + return balance; +}; +const preload = async () => ({}); + +const hydrate = () => {}; + +const currencyBridge: CurrencyBridge = { + preload, + hydrate, + scanAccounts, +}; +const accountBridge: AccountBridge = { + createTransaction, + updateTransaction: defaultUpdateTransaction, + prepareTransaction, + getTransactionStatus, + sync, + receive, + signOperation, + broadcast, + estimateMaxSpendable, +}; +export default { + currencyBridge, + accountBridge, +};