Fee Calculation¶
This section defines the function used to compute the fees associated with reference scripts.
Calculation of Fees for Reference Scripts¶
The function defined in this section calculates the fee for reference scripts in a
transaction. It takes as input the total size of the reference scripts in
bytes—calculated by refScriptsSize
(see Functions used in UTxO rules)—and uses a function
(scriptsCostAux) that is piece-wise linear in the size, where the
linear constant multiple grows with each refScriptCostStride bytes.
In addition, scriptsCost depends on the following constants, which
are bundled with the protocol parameters (see the
Protocol Parameter Definitions section).
-
refScriptCostMultiplier: a rational number, the growth factor or step multiplier that determines how much the price per byte increases after each increment; -
refScriptCostStride: an integer, the size in bytes at which the price per byte grows linearly; -
minFeeRefScriptCoinsPerByte: a rational number, the base fee or initial price per byte.
For background on this method of fee calculation, see Kuleshevich24.
scriptsCost : (pp : PParams) → ℕ → Coin scriptsCost pp scriptSize = scriptsCostAux 0ℚ minFeeRefScriptCoinsPerByte scriptSize (<′-wellFounded scriptSize) where minFeeRefScriptCoinsPerByte refScriptCostMultiplier : ℚ minFeeRefScriptCoinsPerByte = PParams.minFeeRefScriptCoinsPerByte pp refScriptCostMultiplier = PParams.refScriptCostMultiplier pp refScriptCostStride : ℕ⁺ refScriptCostStride = PParams.refScriptCostStride pp scriptsCostAux : ℚ -- accumulator → ℚ -- current tier price → (n : ℕ) -- remaining script size → Acc _<′_ n → Coin scriptsCostAux acl curTierPrice n (acc rs) = case n ≤? fromℕ⁺ refScriptCostStride of λ where (yes _) → ∣ floor (acl + (fromℕ n * curTierPrice)) ∣ (no p) → scriptsCostAux (acl + (fromℕ (fromℕ⁺ refScriptCostStride) * curTierPrice)) (refScriptCostMultiplier * curTierPrice) (n - fromℕ⁺ refScriptCostStride) (rs $ <⇒<′ (suc∸≤ (≤-trans (s<s z≤n) (≰⇒> p)) (ℕ⁺->0 refScriptCostStride))) where suc∸≤ : ∀ {n m : ℕ} → n > 0 → m > 0 → n ∸ m < n suc∸≤ {n} {.suc m} p (s≤s q) = ≤-trans (+-monoʳ-≤ 1 (∸-monoʳ-≤ n (s<s q))) (≤-reflexive (m+[n∸m]≡n p))
References¶
[Kuleshevich24] Alexey Kuleshevich. Changes to the fee calculation due to Reference Scripts. 2024.