UNPKG

@ledgerhq/coin-celo

Version:
93 lines 4.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const bignumber_js_1 = require("bignumber.js"); const sdk_1 = require("../network/sdk"); const logic_1 = require("../logic"); const getFeesForTransaction = async ({ account, transaction, }) => { const { amount, index } = transaction; const kit = (0, sdk_1.celoKit)(); // A workaround - estimating gas throws an error if value > funds let value = new bignumber_js_1.BigNumber(0); const pendingOperationAmounts = (0, logic_1.getPendingStakingOperationAmounts)(account); const lockedGold = await kit.contracts.getLockedGold(); const nonvotingLockedGoldBalance = await lockedGold.getAccountNonvotingLockedGold(account.freshAddress); // Deduct pending vote operations from the non-voting locked balance const totalNonVotingLockedBalance = nonvotingLockedGoldBalance.minus(pendingOperationAmounts.vote); // Deduct pending lock operations from the spendable balance const totalSpendableBalance = account.spendableBalance.minus(pendingOperationAmounts.lock); if ((transaction.mode === "unlock" || transaction.mode === "vote") && account.celoResources) { value = transaction.useAllAmount ? totalNonVotingLockedBalance : bignumber_js_1.BigNumber.minimum(amount, totalNonVotingLockedBalance); } else if (transaction.mode === "revoke" && account.celoResources) { const vote = (0, logic_1.getVote)(account, transaction.recipient, transaction.index); if (vote) { value = transaction.useAllAmount ? vote.amount : bignumber_js_1.BigNumber.minimum(amount, vote.amount); } } else { value = transaction.useAllAmount ? totalSpendableBalance : bignumber_js_1.BigNumber.minimum(amount, totalSpendableBalance); } let gas = null; if (transaction.mode === "lock") { gas = await lockedGold .lock() .txo.estimateGas({ from: account.freshAddress, value: value.toFixed() }); } else if (transaction.mode === "unlock") { const lockedGold = await kit.contracts.getLockedGold(); gas = await lockedGold.unlock(value).txo.estimateGas({ from: account.freshAddress }); } else if (transaction.mode === "withdraw") { const lockedGold = await kit.contracts.getLockedGold(); gas = await lockedGold.withdraw(index || 0).txo.estimateGas({ from: account.freshAddress }); } else if (transaction.mode === "vote") { const election = await kit.contracts.getElection(); const vote = await election.vote(transaction.recipient, new bignumber_js_1.BigNumber(value)); gas = await vote.txo.estimateGas({ from: account.freshAddress }); } else if (transaction.mode === "revoke") { const election = await kit.contracts.getElection(); const accounts = await kit.contracts.getAccounts(); const voteSignerAccount = await accounts.voteSignerToAccount(account.freshAddress); const revokeTxs = await election.revoke(voteSignerAccount, transaction.recipient, new bignumber_js_1.BigNumber(value)); const revokeTx = revokeTxs.find(transactionObject => { return (transactionObject.txo._method.name === (transaction.index === 0 ? "revokePending" : "revokeActive")); }); if (!revokeTx) return new bignumber_js_1.BigNumber(0); gas = await revokeTx.txo.estimateGas({ from: account.freshAddress }); } else if (transaction.mode === "activate") { const election = await kit.contracts.getElection(); const accounts = await kit.contracts.getAccounts(); const voteSignerAccount = await accounts.voteSignerToAccount(account.freshAddress); const activates = await election.activate(voteSignerAccount); const activate = activates.find(a => a.txo.arguments[0] === transaction.recipient); if (!activate) return new bignumber_js_1.BigNumber(0); gas = await activate.txo.estimateGas({ from: account.freshAddress }); } else if (transaction.mode === "register") { const accounts = await kit.contracts.getAccounts(); gas = await accounts.createAccount().txo.estimateGas({ from: account.freshAddress }); } else { const celoToken = await kit.contracts.getGoldToken(); const celoTransaction = { from: account.freshAddress, to: celoToken.address, data: celoToken.transfer(transaction.recipient, value.toFixed()).txo.encodeABI(), }; gas = await kit.connection.estimateGasWithInflationFactor(celoTransaction); } const gasPrice = new bignumber_js_1.BigNumber(await kit.connection.gasPrice()); return gasPrice.times(gas); }; exports.default = getFeesForTransaction; //# sourceMappingURL=getFeesForTransaction.js.map