Commit 287d0a7f authored by Kelvin Fichter's avatar Kelvin Fichter

fix(ctb): break out clamp func in Metering

Breaks out the base fee clamping function in ResourceMetering to
deduplicate some code.
parent 1d9e8c1d
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -3,9 +3,8 @@ pragma solidity 0.8.15; ...@@ -3,9 +3,8 @@ pragma solidity 0.8.15;
import { Initializable } from "@openzeppelin/contracts/proxy/utils/Initializable.sol"; import { Initializable } from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol"; import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
import { SignedMath } from "@openzeppelin/contracts/utils/math/SignedMath.sol";
import { FixedPointMathLib } from "@rari-capital/solmate/src/utils/FixedPointMathLib.sol";
import { Burn } from "../libraries/Burn.sol"; import { Burn } from "../libraries/Burn.sol";
import { Arithmetic } from "../libraries/Arithmetic.sol";
/** /**
* @custom:upgradeable * @custom:upgradeable
...@@ -49,6 +48,11 @@ abstract contract ResourceMetering is Initializable { ...@@ -49,6 +48,11 @@ abstract contract ResourceMetering is Initializable {
*/ */
int256 public constant MINIMUM_BASE_FEE = 10_000; int256 public constant MINIMUM_BASE_FEE = 10_000;
/**
* @notice Maximum base fee value, cannot go higher than this.
*/
int256 public constant MAXIMUM_BASE_FEE = int256(uint256(type(uint128).max));
/** /**
* @notice Initial base fee value. * @notice Initial base fee value.
*/ */
...@@ -89,12 +93,10 @@ abstract contract ResourceMetering is Initializable { ...@@ -89,12 +93,10 @@ abstract contract ResourceMetering is Initializable {
// Update base fee by adding the base fee delta and clamp the resulting value between // Update base fee by adding the base fee delta and clamp the resulting value between
// min and max. // min and max.
int256 newBaseFee = SignedMath.min( int256 newBaseFee = Arithmetic.clamp(
SignedMath.max( int256(uint256(params.prevBaseFee)) + baseFeeDelta,
int256(uint256(params.prevBaseFee)) + baseFeeDelta, MINIMUM_BASE_FEE,
int256(MINIMUM_BASE_FEE) MAXIMUM_BASE_FEE
),
int256(uint256(type(uint128).max))
); );
// If we skipped more than one block, we also need to account for every empty block. // If we skipped more than one block, we also need to account for every empty block.
...@@ -104,20 +106,14 @@ abstract contract ResourceMetering is Initializable { ...@@ -104,20 +106,14 @@ abstract contract ResourceMetering is Initializable {
// Update the base fee by repeatedly applying the exponent 1-(1/change_denominator) // Update the base fee by repeatedly applying the exponent 1-(1/change_denominator)
// blockDiff - 1 times. Simulates multiple empty blocks. Clamp the resulting value // blockDiff - 1 times. Simulates multiple empty blocks. Clamp the resulting value
// between min and max. // between min and max.
newBaseFee = SignedMath.min( newBaseFee = Arithmetic.clamp(
SignedMath.max( Arithmetic.cdexp(
int256( newBaseFee,
(newBaseFee * BASE_FEE_MAX_CHANGE_DENOMINATOR,
( int256(blockDiff - 1)
FixedPointMathLib.powWad(
1e18 - (1e18 / BASE_FEE_MAX_CHANGE_DENOMINATOR),
int256((blockDiff - 1) * 1e18)
)
)) / 1e18
),
int256(MINIMUM_BASE_FEE)
), ),
int256(uint256(type(uint128).max)) MINIMUM_BASE_FEE,
MAXIMUM_BASE_FEE
); );
} }
......
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
import { SignedMath } from "@openzeppelin/contracts/utils/math/SignedMath.sol";
import { FixedPointMathLib } from "@rari-capital/solmate/src/utils/FixedPointMathLib.sol";
/**
* @title Arithmetic
* @notice Even more math than before.
*/
library Arithmetic {
/**
* @notice Clamps a value between a minimum and maximum.
*
* @param _value The value to clamp.
* @param _min The minimum value.
* @param _max The maximum value.
*
* @return The clamped value.
*/
function clamp(
int256 _value,
int256 _min,
int256 _max
) internal pure returns (int256) {
return SignedMath.min(SignedMath.max(_value, _min), _max);
}
/**
* @notice (c)oefficient (d)enominator (exp)onentiation function.
* Returns the result of: c * (1 - 1/d)^exp.
*
* @param _coefficient Coefficient of the function.
* @param _denominator Fractional denominator.
* @param _exponent Power function exponent.
*
* @return Result of c * (1 - 1/d)^exp.
*/
function cdexp(
int256 _coefficient,
int256 _denominator,
int256 _exponent
) internal pure returns (int256) {
return
(_coefficient *
(FixedPointMathLib.powWad(1e18 - (1e18 / _denominator), _exponent * 1e18))) / 1e18;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment