Skip to content

Commit

Permalink
feat(cspr): add mock.ts to bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
lawRathod committed Aug 24, 2023
1 parent 56e0f53 commit 5bac45b
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ const getTransactionStatus = async (a: Account, t: Transaction): Promise<Transac
});
}

// This is the worst case scenario (the tx won't cost more than this value)
const estimatedFees = t.fees;

let totalSpent = BigNumber(0);
Expand Down
169 changes: 169 additions & 0 deletions libs/ledger-live-common/src/families/casper/bridge/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import { BigNumber } from "bignumber.js";
import {
AmountRequired,
NotEnoughBalance,
RecipientRequired,
InvalidAddress,
InvalidAddressBecauseDestinationIsAlsoSource,
} from "@ledgerhq/errors";
import type { Account, AccountBridge, AccountLike, CurrencyBridge } from "@ledgerhq/types-live";
import type { Transaction, TransactionStatus } from "../types";
import {
scanAccounts,
signOperation,
broadcast,
sync,
makeAccountBridgeReceive,
} from "../../../bridge/mockHelpers";
import { getEstimatedFees } from "./bridgeHelpers/fee";
import { defaultUpdateTransaction } from "@ledgerhq/coin-framework/bridge/jsHelpers";
import { getAddress, isAddressValid } from "./bridgeHelpers/addresses";
import { isTransferIdValid } from "./bridgeHelpers/transferId";
import { CasperInvalidTransferId, InvalidMinimumAmount, MayBlockAccount } from "../errors";
import {
CASPER_FEES_CSPR,
CASPER_MAX_TRANSFER_ID,
CASPER_MINIMUM_VALID_AMOUNT_CSPR,
CASPER_MINIMUM_VALID_AMOUNT_MOTES,
} from "../consts";
import { getMainAccount } from "../../../account/helpers";

const receive = makeAccountBridgeReceive();

const createTransaction = (): Transaction => ({
family: "casper",
amount: new BigNumber(0),
fees: getEstimatedFees(),
recipient: "",
useAllAmount: false,
});

const getTransactionStatus = async (a: Account, t: Transaction): Promise<TransactionStatus> => {
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<Transaction> => {
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<BigNumber> => {
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<Transaction> = {
createTransaction,
updateTransaction: defaultUpdateTransaction,
prepareTransaction,
getTransactionStatus,
sync,
receive,
signOperation,
broadcast,
estimateMaxSpendable,
};
export default {
currencyBridge,
accountBridge,
};

0 comments on commit 5bac45b

Please sign in to comment.