UNPKG

@ledgerhq/live-common

Version:
74 lines 3.13 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useUpdateMaxAmount = exports.ZERO = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const react_1 = require("react"); const bridge_1 = require("../../../bridge"); exports.ZERO = new bignumber_js_1.default(0); const useUpdateMaxAmount = ({ setFromAmount, account, parentAccount, bridge, }) => { const transaction = bridge.transaction; const feesStrategy = transaction?.feesStrategy; const [isMaxEnabled, setIsMaxEnabled] = (0, react_1.useState)(false); const [isMaxLoading, setIsMaxLoading] = (0, react_1.useState)(false); const toggleMax = (0, react_1.useCallback)(() => setIsMaxEnabled(previous => { const next = !previous; if (previous) { setFromAmount(exports.ZERO); setIsMaxLoading(false); } bridge.updateTransaction(tx => { let additionalFees; if (tx.family === "evm" && !tx.subAccountId && next) { additionalFees = new bignumber_js_1.default(5000000000000000); // 0,005 ETH/BNB/MATIC } // do not use useAllAmount for tron because we need to keep some TRX for fees and account alive if (tx.family === "tron" && !tx.subAccountId && next) { return { ...tx, }; } return { ...tx, useAllAmount: next, additionalFees, }; }); return next; }), [setFromAmount]); /* UPDATE from amount to the estimate max spendable on account change when the amount feature is enabled */ (0, react_1.useEffect)(() => { const updateAmountUsingMax = async () => { if (!account) return; const bridge = (0, bridge_1.getAccountBridge)(account, parentAccount); setIsMaxLoading(true); const amount = await bridge.estimateMaxSpendable({ account, parentAccount, transaction, }); setIsMaxLoading(false); if ("currency" in account && account.currency.id === "tron") { // keep 1.1 TRX for fees and 0.1 TRX for keeping the account alive if (account?.spendableBalance.gt(new bignumber_js_1.default(1_200_000))) { setFromAmount(account?.spendableBalance.minus(new bignumber_js_1.default(1_200_000))); } } else { setFromAmount(amount); } }; if (isMaxEnabled) { updateAmountUsingMax(); } }, // eslint-disable-next-line react-hooks/exhaustive-deps [setFromAmount, isMaxEnabled, account, parentAccount, feesStrategy]); return { isMaxEnabled, toggleMax, isMaxLoading }; }; exports.useUpdateMaxAmount = useUpdateMaxAmount; //# sourceMappingURL=useUpdateMaxAmount.js.map