UNPKG

@ledgerhq/coin-canton

Version:
38 lines 1.56 kB
import coinConfig from "../../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) => coinConfig.getCoinConfig(currency.id).fee; export 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