@ledgerhq/coin-celo
Version:
120 lines • 6.41 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("@ledgerhq/coin-framework/account/index");
const bignumber_js_1 = require("bignumber.js");
const sdk_1 = require("../network/sdk");
const logic_1 = require("../logic");
const buildTransaction_1 = __importDefault(require("./buildTransaction"));
const constants_1 = require("../constants");
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);
const tokenAccount = (0, index_1.findSubAccountById)(account, transaction.subAccountId || "");
const isTokenTransaction = tokenAccount?.type === "TokenAccount";
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 if (isTokenTransaction) {
value = transaction.useAllAmount ? tokenAccount.balance : transaction.amount;
const block = await kit.connection.web3.eth.getBlock("latest");
const baseFee = BigInt(block.baseFeePerGas || constants_1.MAX_PRIORITY_FEE_PER_GAS);
const maxFeePerGas = baseFee + constants_1.MAX_PRIORITY_FEE_PER_GAS;
let token;
if (constants_1.CELO_STABLE_TOKENS.includes(tokenAccount.token.id)) {
token = await kit.contracts.getStableToken((0, constants_1.getStableTokenEnum)(tokenAccount.token.id));
}
else {
token = await kit.contracts.getErc20(tokenAccount.token.contractAddress);
}
const celoTransaction = {
from: account.freshAddress,
to: transaction.recipient,
data: token.transfer(transaction.recipient, value.toFixed()).txo.encodeABI(),
maxFeePerGas: maxFeePerGas.toString(),
maxPriorityFeePerGas: await kit.connection.getMaxPriorityFeePerGas(),
value: value.toFixed(),
};
gas = Number(((await kit.connection.estimateGasWithInflationFactor(celoTransaction)) *
constants_1.MAX_FEES_THRESHOLD_MULTIPLIER).toFixed());
}
else {
// Send
const tx = await (0, buildTransaction_1.default)(account, transaction);
gas = tx.gas ? Number(tx.gas) : 0;
}
const gasPrice = new bignumber_js_1.BigNumber(await kit.connection.gasPrice());
return gasPrice.times(gas);
};
exports.default = getFeesForTransaction;
//# sourceMappingURL=getFeesForTransaction.js.map