|
1 | | -import { AmountCalculator, ZERO_CENTS, ZERO_SATS } from "@/domain/shared" |
| 1 | +import { PayoutSpeed } from "@/domain/bitcoin/onchain" |
| 2 | +import { AmountCalculator, WalletCurrency, ZERO_CENTS, ZERO_SATS } from "@/domain/shared" |
2 | 3 |
|
3 | 4 | const calc = AmountCalculator() |
4 | 5 |
|
5 | 6 | export const OnChainFees = ({ |
6 | | - thresholdImbalance, |
7 | | - feeRatioAsBasisPoints, |
| 7 | + onchain, |
8 | 8 | }: OnchainWithdrawalConfig): OnChainFeeCalculator => { |
9 | 9 | const withdrawalFee = ({ |
10 | 10 | minerFee, |
11 | 11 | amount, |
12 | | - minBankFee, |
13 | | - imbalance, |
| 12 | + speed, |
| 13 | + feeRate, |
14 | 14 | }: { |
15 | 15 | minerFee: BtcPaymentAmount |
16 | 16 | amount: BtcPaymentAmount |
17 | 17 | minBankFee: BtcPaymentAmount |
18 | | - imbalance: BtcPaymentAmount |
| 18 | + speed: PayoutSpeed |
| 19 | + feeRate: number |
19 | 20 | }) => { |
20 | | - const amountWithImbalanceCalcs = { |
21 | | - amount: imbalance.amount - thresholdImbalance.amount + amount.amount, |
22 | | - currency: amount.currency, |
| 21 | + const satoshis = Number(amount.amount) |
| 22 | + |
| 23 | + const dynamicRate = calculateDynamicFeeRate(satoshis, speed, feeRate) |
| 24 | + const baseMultiplier = calculateBaseMultiplier(speed, feeRate) |
| 25 | + const bankCost = calculateCostToBank(satoshis, speed, feeRate) |
| 26 | + |
| 27 | + const bankFee: BtcPaymentAmount = { |
| 28 | + amount: BigInt(Math.round(satoshis * dynamicRate + bankCost * baseMultiplier)), |
| 29 | + currency: WalletCurrency.Btc, |
23 | 30 | } |
24 | | - const baseAmount = calc.max(calc.min(amountWithImbalanceCalcs, amount), ZERO_SATS) |
25 | | - const bankFee = calc.max( |
26 | | - minBankFee, |
27 | | - calc.mulBasisPoints(baseAmount, feeRatioAsBasisPoints), |
28 | | - ) |
29 | 31 |
|
30 | 32 | return { |
31 | 33 | totalFee: calc.add(bankFee, minerFee), |
32 | 34 | bankFee, |
33 | 35 | } |
34 | 36 | } |
35 | 37 |
|
| 38 | + const createThresholdBasedCalculator = |
| 39 | + ( |
| 40 | + thresholds: readonly { max: number; count: number }[], |
| 41 | + defaultCount: number, |
| 42 | + ): InputCountCalculator => |
| 43 | + (amount: number) => { |
| 44 | + const threshold = thresholds.find(({ max }) => amount < max) |
| 45 | + return threshold?.count ?? defaultCount |
| 46 | + } |
| 47 | + |
| 48 | + const calculateRegularInputCount: InputCountCalculator = createThresholdBasedCalculator( |
| 49 | + onchain.thresholds.regular, |
| 50 | + onchain.thresholds.defaults.regular, |
| 51 | + ) |
| 52 | + |
| 53 | + const calculateBatchInputCount: InputCountCalculator = createThresholdBasedCalculator( |
| 54 | + onchain.thresholds.batch, |
| 55 | + onchain.thresholds.defaults.batch, |
| 56 | + ) |
| 57 | + |
| 58 | + const calculateTransactionSize: TransactionSizeCalculator = (spec) => { |
| 59 | + const { baseSize, inputSize, outputSize } = onchain.transaction |
| 60 | + return baseSize + spec.inputCount * inputSize + spec.outputCount * outputSize |
| 61 | + } |
| 62 | + |
| 63 | + const calculateExponentialDecay = ( |
| 64 | + amount: number, |
| 65 | + minRate: number, |
| 66 | + maxRate: number, |
| 67 | + threshold: number, |
| 68 | + minAmount: number, |
| 69 | + exponentialFactor: number, |
| 70 | + ): number => { |
| 71 | + const span = threshold - minAmount |
| 72 | + const exponent = -((amount - minAmount) / span) * exponentialFactor |
| 73 | + return minRate + (maxRate - minRate) * Math.exp(exponent) |
| 74 | + } |
| 75 | + |
| 76 | + const calculateDecayRate: DecayRateCalculator = (amount, params) => { |
| 77 | + const { threshold, minSats, exponentialFactor } = onchain.decayConstants |
| 78 | + const { minRate, maxRate, divisor } = params |
| 79 | + |
| 80 | + if (amount < threshold) { |
| 81 | + return calculateExponentialDecay( |
| 82 | + amount, |
| 83 | + minRate, |
| 84 | + maxRate, |
| 85 | + threshold, |
| 86 | + minSats, |
| 87 | + exponentialFactor, |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + return divisor / amount |
| 92 | + } |
| 93 | + |
| 94 | + const calculateNormalizedFactor = (feeRate: number): number => { |
| 95 | + const { min, max } = onchain.decayConstants.networkFeeRange |
| 96 | + return (feeRate - min) / (max - min) |
| 97 | + } |
| 98 | + |
| 99 | + const calculateDynamicFeeRate: DynamicRateCalculator = (amount, speed, feeRate) => { |
| 100 | + const { targetRate, ...decayParams } = onchain.decay[speed] |
| 101 | + const decay = calculateDecayRate(amount, decayParams) |
| 102 | + const normalizedFactor = calculateNormalizedFactor(feeRate) |
| 103 | + return decay + normalizedFactor * (targetRate - decay) |
| 104 | + } |
| 105 | + |
| 106 | + const calculateBaseMultiplier: BaseMultiplierCalculator = (speed, feeRate) => { |
| 107 | + const { factors, offsets } = onchain.multiplier |
| 108 | + return factors[speed] / feeRate + offsets[speed] |
| 109 | + } |
| 110 | + |
| 111 | + const createTransactionSpec = ( |
| 112 | + amount: number, |
| 113 | + speed: PayoutSpeed, |
| 114 | + ): TransactionSpecification => { |
| 115 | + const { outputs } = onchain.transaction |
| 116 | + |
| 117 | + if (speed === PayoutSpeed.Slow) { |
| 118 | + return { |
| 119 | + inputCount: calculateBatchInputCount(amount), |
| 120 | + outputCount: outputs.batch, |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return { |
| 125 | + inputCount: calculateRegularInputCount(amount), |
| 126 | + outputCount: outputs.regular, |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + const calculateCostToBank: CostToBankCalculator = (amount, speed, feeRate) => { |
| 131 | + const spec = createTransactionSpec(amount, speed) |
| 132 | + const size = calculateTransactionSize(spec) |
| 133 | + |
| 134 | + return speed === PayoutSpeed.Slow ? Math.round((size * feeRate) / 10) : size * feeRate |
| 135 | + } |
| 136 | + |
36 | 137 | return { |
37 | 138 | withdrawalFee, |
38 | 139 | intraLedgerFees: () => ({ |
|
0 commit comments