@ledgerhq/coin-canton
Version:
44 lines • 1.83 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.estimateFees = estimateFees;
const config_1 = __importDefault(require("../../config"));
const magnitude = 10n ** 38n;
const cantonCoinTiers = [
// 100n because of rateDenominator for percentages
{ limit: 100n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 100n }, // 1.0%
{ limit: 1000n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 1000n }, // 0.1%
{ limit: 1000000n * 100n * magnitude, rateNumerator: 1n, rateDenominator: 10000n }, // 0.01%
{ limit: null, rateNumerator: 1n, rateDenominator: 100000n }, // 0.001%
];
const feeValue = (currency) => config_1.default.getCoinConfig(currency.id).fee;
async function estimateFees(currency, amount) {
const forcedValue = feeValue(currency);
if (forcedValue !== undefined) {
return Promise.resolve(BigInt(forcedValue) * magnitude);
}
else {
return Promise.resolve(
// Add 1 CC as base fee
1n * magnitude + BigInt(calculateFeeWithTiers(amount, cantonCoinTiers)));
}
}
function calculateFeeWithTiers(value, tiers) {
let remaining = value;
let lastLimit = 0n;
let fee = 0n;
for (const tier of tiers) {
if (remaining <= 0n)
break;
const currentLimit = tier.limit ?? BigInt(2 ** 62); // highest bigint value
const tierCap = currentLimit - lastLimit;
const applicable = remaining < tierCap ? remaining : tierCap;
fee += (applicable * tier.rateNumerator) / tier.rateDenominator;
remaining -= applicable;
lastLimit = currentLimit;
}
return fee;
}
//# sourceMappingURL=estimateFees.js.map