@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
127 lines • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = require("bignumber.js");
const errors_1 = require("@ledgerhq/errors");
const mockHelpers_1 = require("../../../bridge/mockHelpers");
const fee_1 = require("@ledgerhq/coin-casper/bridge/bridgeHelpers/fee");
const jsHelpers_1 = require("@ledgerhq/ledger-wallet-framework/bridge/jsHelpers");
const addresses_1 = require("@ledgerhq/coin-casper/bridge/bridgeHelpers/addresses");
const validateMemo_1 = require("@ledgerhq/coin-casper/logic/validateMemo");
const errors_2 = require("@ledgerhq/coin-casper/errors");
const consts_1 = require("../consts");
const account_1 = require("../../../account");
const validateAddress_1 = require("../../../bridge/validateAddress");
const receive = (0, mockHelpers_1.makeAccountBridgeReceive)();
const createTransaction = () => ({
family: "casper",
amount: new bignumber_js_1.BigNumber(0),
fees: (0, fee_1.getEstimatedFees)(),
recipient: "",
useAllAmount: false,
});
const getTransactionStatus = async (a, t) => {
const errors = {};
const warnings = {};
const { balance, spendableBalance } = a;
const { address } = (0, addresses_1.getAddress)(a);
const { recipient, useAllAmount } = t;
let { amount } = t;
if (!recipient)
errors.recipient = new errors_1.RecipientRequired();
else if (!(0, addresses_1.isAddressValid)(recipient))
errors.recipient = new errors_1.InvalidAddress("", {
currencyName: a.currency.name,
});
else if (recipient.toLowerCase() === address.toLowerCase())
errors.recipient = new errors_1.InvalidAddressBecauseDestinationIsAlsoSource();
if (!(0, addresses_1.isAddressValid)(address))
errors.sender = new errors_1.InvalidAddress("", {
currencyName: a.currency.name,
});
else if (!(0, validateMemo_1.validateMemo)(t.transferId)) {
errors.sender = new errors_2.CasperInvalidTransferId("", {
maxTransferId: consts_1.CASPER_MAX_TRANSFER_ID,
});
}
const estimatedFees = t.fees;
let totalSpent = (0, bignumber_js_1.BigNumber)(0);
if (useAllAmount) {
totalSpent = a.spendableBalance;
amount = totalSpent.minus(estimatedFees);
if (amount.lte(0) || totalSpent.gt(balance)) {
errors.amount = new errors_1.NotEnoughBalance();
}
}
if (!useAllAmount) {
totalSpent = amount.plus(estimatedFees);
if (amount.eq(0)) {
errors.amount = new errors_1.AmountRequired();
}
if (totalSpent.gt(a.spendableBalance)) {
errors.amount = new errors_1.NotEnoughBalance();
}
}
if (amount.lt(consts_1.CASPER_MINIMUM_VALID_AMOUNT_MOTES) && !errors.amount)
errors.amount = new errors_2.InvalidMinimumAmount("", {
minAmount: `${consts_1.CASPER_MINIMUM_VALID_AMOUNT_CSPR} CSPR`,
});
if (spendableBalance.minus(totalSpent).minus(estimatedFees).lt(consts_1.CASPER_MINIMUM_VALID_AMOUNT_MOTES))
warnings.amount = new errors_2.MayBlockAccount("", {
minAmount: `${consts_1.CASPER_MINIMUM_VALID_AMOUNT_CSPR + consts_1.CASPER_FEES_CSPR} CSPR`,
});
return {
errors,
warnings,
estimatedFees,
amount,
totalSpent,
};
};
const prepareTransaction = async (a, t) => {
const { address } = (0, addresses_1.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, }) => {
const a = (0, account_1.getMainAccount)(account, parentAccount);
let balance = a.spendableBalance;
if (balance.eq(0))
return balance;
const estimatedFees = transaction?.fees ?? (0, fee_1.getEstimatedFees)();
if (balance.lte(estimatedFees))
return new bignumber_js_1.BigNumber(0);
balance = balance.minus(estimatedFees);
return balance;
};
const preload = async () => ({});
const hydrate = () => { };
const currencyBridge = {
preload,
hydrate,
scanAccounts: mockHelpers_1.scanAccounts,
};
const accountBridge = {
createTransaction,
updateTransaction: jsHelpers_1.updateTransaction,
prepareTransaction,
getTransactionStatus,
sync: mockHelpers_1.sync,
receive,
signOperation: mockHelpers_1.signOperation,
signRawOperation: mockHelpers_1.signRawOperation,
broadcast: mockHelpers_1.broadcast,
estimateMaxSpendable,
getSerializedAddressParameters: jsHelpers_1.getSerializedAddressParameters,
validateAddress: validateAddress_1.validateAddress,
};
exports.default = {
currencyBridge,
accountBridge,
};
//# sourceMappingURL=mock.js.map