@ledgerhq/coin-tron
Version:
Ledger Tron Coin integration
59 lines • 3.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const constants_1 = require("../logic/constants");
const network_1 = require("../network");
const utils_1 = require("./utils");
// see : https://developers.tron.network/docs/bandwith#section-bandwidth-points-consumption
// 1. cost around 200 Bandwidth, if not enough check Free Bandwidth
// 2. If not enough, will cost some TRX
// 3. normal transfert cost around 0.002 TRX
const getFeesFromBandwidth = (account, transaction) => {
const { freeUsed, freeLimit, gainedUsed, gainedLimit } = (0, utils_1.extractBandwidthInfo)(transaction.networkInfo);
const available = freeLimit.minus(freeUsed).plus(gainedLimit).minus(gainedUsed);
const estimatedBandwidthCost = (0, utils_1.getEstimatedBlockSize)(account, transaction);
if (available.lt(estimatedBandwidthCost)) {
return constants_1.STANDARD_FEES_NATIVE; // cost is around 0.002 TRX
}
return new bignumber_js_1.default(0); // no fee
};
// Special case: If activated an account, cost around 0.1 TRX
const getFeesFromAccountActivation = async (account, transaction, tokenAccount) => {
const recipientAccounts = await (0, network_1.fetchTronAccount)(transaction.recipient);
const recipientAccount = recipientAccounts[0];
const { gainedUsed, gainedLimit } = (0, utils_1.extractBandwidthInfo)(transaction.networkInfo);
const available = gainedLimit.minus(gainedUsed);
const estimatedBandwidthCost = (0, utils_1.getEstimatedBlockSize)(account, transaction);
const hasTRC20 = Boolean(tokenAccount &&
recipientAccount?.trc20?.some(trc20 => tokenAccount.token.contractAddress in trc20));
if (!recipientAccount && !hasTRC20 && available.lt(estimatedBandwidthCost)) {
// if we have a token account but the recipient is either not active or the account does not have a trc20 balance for the given token.
if (tokenAccount && tokenAccount.token.tokenType === "trc20") {
return constants_1.ACTIVATION_FEES_TRC_20; // cost is 27.6009 TRX
}
// if no token account then we are sending tron use the default activation fees.
if (!tokenAccount) {
return constants_1.ACTIVATION_FEES; // cost is around 1 TRX
}
}
// if account is activated and it does already have a trc20 balance for given token.
if (tokenAccount && tokenAccount.token.tokenType === "trc20") {
return constants_1.STANDARD_FEES_TRC_20; // cost is 13.3959 TRX
}
return new bignumber_js_1.default(0); // no fee
};
const getEstimatedFees = async (account, transaction, tokenAccount) => {
const feesFromAccountActivation = transaction.mode === "send"
? await getFeesFromAccountActivation(account, transaction, tokenAccount)
: new bignumber_js_1.default(0);
if (feesFromAccountActivation.gt(0)) {
return feesFromAccountActivation;
}
const feesFromBandwidth = getFeesFromBandwidth(account, transaction);
return feesFromBandwidth;
};
exports.default = getEstimatedFees;
//# sourceMappingURL=getEstimateFees.js.map