@ledgerhq/coin-tron
Version:
Ledger Tron Coin integration
73 lines • 3.91 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 logic_1 = require("../logic");
const constants_1 = require("../logic/constants");
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.270 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.270 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, logic_1.getAccount)(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 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
}
const transactionIntent = {
intentType: "transaction",
type: transaction.mode,
sender: account.freshAddress,
recipient: transaction.recipient,
amount: BigInt(transaction.amount.toString()),
asset: tokenAccount?.token.tokenType === "trc20"
? {
type: "trc20",
assetReference: tokenAccount.token.contractAddress,
}
: { type: "trc10", assetReference: tokenAccount?.token.id },
};
// 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") {
const estimatedFees = await (0, logic_1.estimateFees)(transactionIntent);
return new bignumber_js_1.default(estimatedFees.toString());
}
}
// 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