@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
47 lines • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMaxAvailable = getMaxAvailable;
exports.isInsufficientFundsAmountError = isInsufficientFundsAmountError;
const bignumber_js_1 = require("bignumber.js");
/**
* Known "insufficient funds" amount error names used by bridges.
* Covers all NotEnough* variants and Insufficient* errors.
*/
const INSUFFICIENT_FUNDS_AMOUNT_ERROR_NAMES = [
"NotEnoughBalance",
"NotEnoughBalanceFees",
"NotEnoughBalanceSwap",
"NotEnoughBalanceBecauseDestinationNotCreated",
"NotEnoughBalanceInParentAccount",
"NotEnoughBalanceToDelegate",
];
/**
* Maximum amount the user can send (spendable/balance minus estimated fees).
* Returns zero if account is missing or balance would be negative.
*/
function getMaxAvailable(account, estimatedFees) {
if (!account)
return new bignumber_js_1.BigNumber(0);
const spendable = "spendableBalance" in account ? account.spendableBalance : undefined;
const balance = spendable ?? ("balance" in account ? account.balance : undefined) ?? new bignumber_js_1.BigNumber(0);
const safeMax = balance.minus(estimatedFees);
return bignumber_js_1.BigNumber.max(0, safeMax);
}
/**
* Returns true if the transaction status has an amount error that indicates
* insufficient funds (so the CTA can show "Get funds" instead of "Review").
*/
function isInsufficientFundsAmountError(status) {
const amountError = status.errors?.amount;
if (!amountError)
return false;
const errorName = amountError.name;
if (typeof errorName !== "string")
return false;
if (INSUFFICIENT_FUNDS_AMOUNT_ERROR_NAMES.includes(errorName))
return true;
if (errorName.includes("Insufficient"))
return true;
return false;
}
//# sourceMappingURL=amountReview.js.map