Skip to content

Commit

Permalink
fix: custom errors everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
xenide committed Jan 10, 2025
1 parent 19a8653 commit 2293b6b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions script/optimized-deployer-meta
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"constant_product_hash": "0x174b1bf04c1051e9d50d64a3c3b35666a4473b5994955635039d64ef9da69e97",
"constant_product_hash": "0x0bc4266f837b782234f212cbda9a118d71e9203f7e314496449c337eeb02717c",
"factory_hash": "0x03107ee57e3054504c8ec584092f9d955bec0cbd1225fa1a8fb090a2084335aa",
"stable_hash": "0xc94b58df187b1b9b93b67504d44fbe2d8083ccfa39b23bd724037901d6c6dbf9"
"stable_hash": "0x24060eb744ee36cdb90d8c1ae84a51b5c15e06f587fea20a292b4ad3a4451553"
}
4 changes: 2 additions & 2 deletions script/unoptimized-deployer-meta
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"constant_product_hash": "0x4cca74e4aab53779dc7f4f91ab12a3460cc34bae518a286b4ecd7fd16c0f193c",
"constant_product_hash": "0x1fa3539b5f6e4bb831ed1528317d1fa452da1156b63aac7a58b51abd8aa941b9",
"factory_hash": "0xc5e6cd61c86f3ff115d416548099141eb8fa9ede05aebf8f748e14f0b2cecfb4",
"stable_hash": "0x17c57223683f732b0c73ae6a258077c8a1053ebfecb97f8f60518a75f75a6253"
"stable_hash": "0x7475e24687ae955c068d89b8004668ddb38115eefeb101e04416c07f9f92e7bd"
}
4 changes: 2 additions & 2 deletions src/ReservoirDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ contract ReservoirDeployer {
// Bytecode hashes.
bytes32 public constant FACTORY_HASH = bytes32(0x03107ee57e3054504c8ec584092f9d955bec0cbd1225fa1a8fb090a2084335aa);
bytes32 public constant CONSTANT_PRODUCT_HASH =
bytes32(0x174b1bf04c1051e9d50d64a3c3b35666a4473b5994955635039d64ef9da69e97);
bytes32 public constant STABLE_HASH = bytes32(0xc94b58df187b1b9b93b67504d44fbe2d8083ccfa39b23bd724037901d6c6dbf9);
bytes32(0x0bc4266f837b782234f212cbda9a118d71e9203f7e314496449c337eeb02717c);
bytes32 public constant STABLE_HASH = bytes32(0x24060eb744ee36cdb90d8c1ae84a51b5c15e06f587fea20a292b4ad3a4451553);

// Deployment addresses.
GenericFactory public factory;
Expand Down
11 changes: 7 additions & 4 deletions src/libraries/ConstantProductMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pragma solidity ^0.8.0;
library ConstantProductMath {
uint256 public constant FEE_ACCURACY = 1_000_000; // 100%

error CPM_InsufficientInputAmt();
error CPM_InsufficientLiquidity();

/// @dev the function assumes that the following args are within the respective bounds as enforced by ReservoirPair
/// and therefore should not overflow. If overflow happens the transaction will revert
/// aAmountIn <= uint104
Expand All @@ -15,8 +18,8 @@ library ConstantProductMath {
pure
returns (uint256 rAmountOut)
{
require(aAmountIn > 0, "CP: INSUFFICIENT_INPUT_AMOUNT");
require(aReserveIn > 0 && aReserveOut > 0, "CP: INSUFFICIENT_LIQUIDITY");
require(aAmountIn > 0, CPM_InsufficientInputAmt());
require(aReserveIn > 0 && aReserveOut > 0, CPM_InsufficientLiquidity());

uint256 lAmountInWithFee = aAmountIn * (FEE_ACCURACY - aSwapFee);
uint256 lNumerator = lAmountInWithFee * aReserveOut;
Expand All @@ -35,8 +38,8 @@ library ConstantProductMath {
pure
returns (uint256 rAmountIn)
{
require(aAmountOut > 0, "CP: INSUFFICIENT_OUTPUT_AMOUNT");
require(aReserveIn > 0 && aReserveOut > 0, "CP: INSUFFICIENT_LIQUIDITY");
require(aAmountOut > 0, CPM_InsufficientInputAmt());
require(aReserveIn > 0 && aReserveOut > 0, CPM_InsufficientLiquidity());

uint256 lNumerator = aReserveIn * aAmountOut * FEE_ACCURACY;
uint256 lDenominator = (aReserveOut - aAmountOut) * (FEE_ACCURACY - aSwapFee);
Expand Down
15 changes: 10 additions & 5 deletions src/libraries/LogExpMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ library LogExpMath {
int256 constant x11 = 6_250_000_000_000_000_000; // 2ˆ-4
int256 constant a11 = 106_449_445_891_785_942_956; // eˆ(x11)

error LEM_InvalidExponent();
error LEM_OutOfBounds();
error LEM_YOutOfBounds();
error LEM_ProductOutOfBounds();

/**
* @dev Exponentiation (x^y) with unsigned 18 decimal fixed point base and exponent.
*
Expand All @@ -107,15 +112,15 @@ library LogExpMath {
// x^y = exp(y * ln(x)).

// The ln function takes a signed value, so we need to make sure x fits in the signed 256 bit range.
require(x < 2 ** 255, "EM: X_OUT_OF_BOUNDS");
require(x < 2 ** 255, LEM_OutOfBounds());
int256 x_int256 = int256(x);

// We will compute y * ln(x) in a single step. Depending on the value of x, we can either use ln or ln_36. In
// both cases, we leave the division by ONE_18 (due to fixed point multiplication) to the end.

// This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 256 bit
// range.
require(y < MILD_EXPONENT_BOUND, "EM: Y_OUT_OF_BOUNDS");
require(y < MILD_EXPONENT_BOUND, LEM_YOutOfBounds());
int256 y_int256 = int256(y);

int256 logx_times_y;
Expand All @@ -136,7 +141,7 @@ library LogExpMath {
// Finally, we compute exp(y * ln(x)) to arrive at x^y
require(
MIN_NATURAL_EXPONENT <= logx_times_y && logx_times_y <= MAX_NATURAL_EXPONENT,
"EM: PRODUCT_OUT_OF_BOUNDS"
LEM_ProductOutOfBounds()
);

return uint256(exp(logx_times_y));
Expand All @@ -150,7 +155,7 @@ library LogExpMath {
*/
function exp(int256 x) internal pure returns (int256) {
unchecked {
require(x >= MIN_NATURAL_EXPONENT && x <= MAX_NATURAL_EXPONENT, "EM: INVALID_EXPONENT");
require(x >= MIN_NATURAL_EXPONENT && x <= MAX_NATURAL_EXPONENT, LEM_InvalidExponent());
if (x < 0) {
// We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since
// it
Expand Down Expand Up @@ -330,7 +335,7 @@ library LogExpMath {
function ln(int256 a) internal pure returns (int256) {
unchecked {
// The real natural logarithm is not defined for negative numbers or zero.
require(a > 0, "EM: OUT_OF_BOUNDS");
require(a > 0, LEM_OutOfBounds());

if (LN_36_LOWER_BOUND < a && a < LN_36_UPPER_BOUND) {
return _ln_36(a) / ONE_18;
Expand Down
6 changes: 3 additions & 3 deletions test/differential/LogCompression.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pragma solidity ^0.8.0;

import "forge-std/Test.sol";

import { LogCompression } from "src/libraries/LogCompression.sol";
import { LogCompression, LogExpMath } from "src/libraries/LogCompression.sol";

contract LogCompressionTest is Test {
address private _balancerLogCompression = address(200);
Expand Down Expand Up @@ -53,7 +53,7 @@ contract LogCompressionTest is Test {
assertEq(lResLargest, 1_353_060); // 135.3060

// once the input exceeds the max input above, it reverts
vm.expectRevert("EM: OUT_OF_BOUNDS");
vm.expectRevert(LogExpMath.LEM_OutOfBounds.selector);
LogCompression.toLowResLog(2 ** 255);
}

Expand All @@ -62,7 +62,7 @@ contract LogCompressionTest is Test {
int256 lResSmallest = LogCompression.toLowResLog(1);
assertEq(lResSmallest, -414_465); // -41.4465

vm.expectRevert("EM: OUT_OF_BOUNDS");
vm.expectRevert(LogExpMath.LEM_OutOfBounds.selector);
LogCompression.toLowResLog(0);
}
}

0 comments on commit 2293b6b

Please sign in to comment.