Skip to content

Commit

Permalink
fix: use unchecked at safe places and custom errors to reduce bytec…
Browse files Browse the repository at this point in the history
…ode size
  • Loading branch information
xenide committed Jan 9, 2025
1 parent 1732564 commit 19a8653
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
solc = "0.8.28"
evm_version = "cancun"
via_ir = true
bytecode_hash = "none"
bytecode_hash = "ipfs"
optimizer_runs = 1_000_000
gas_limit = "18446744073709551615"
libs = ['lib']
Expand Down
6 changes: 3 additions & 3 deletions script/optimized-deployer-meta
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"constant_product_hash": "0xddd075b1901512190892da217db5c4d266a6831226e14669e690aeb663dd9cf3",
"factory_hash": "0x87b0f73fafcf4bb41e013c8423dc679f6885527007d6c3f1e1834a670cbaadc5",
"stable_hash": "0xa52a6e20c3811c44bda8bc9f7988dd62da3c611711900f85d465b64bf97066af"
"constant_product_hash": "0x174b1bf04c1051e9d50d64a3c3b35666a4473b5994955635039d64ef9da69e97",
"factory_hash": "0x03107ee57e3054504c8ec584092f9d955bec0cbd1225fa1a8fb090a2084335aa",
"stable_hash": "0xc94b58df187b1b9b93b67504d44fbe2d8083ccfa39b23bd724037901d6c6dbf9"
}
6 changes: 3 additions & 3 deletions script/unoptimized-deployer-meta
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"constant_product_hash": "0x903212fc8445706a81c9527fb7fdbb940f030132904209befdddbc617d8bbc47",
"factory_hash": "0x09a9ce1ed77c95be4842dddd771939e048b8bfe2837863be3a2766b1c13ea5a2",
"stable_hash": "0x54471d136002bf755956dcaefd3f0450ba1596532ca57cc84f6c9d774002f4d3"
"constant_product_hash": "0x4cca74e4aab53779dc7f4f91ab12a3460cc34bae518a286b4ecd7fd16c0f193c",
"factory_hash": "0xc5e6cd61c86f3ff115d416548099141eb8fa9ede05aebf8f748e14f0b2cecfb4",
"stable_hash": "0x17c57223683f732b0c73ae6a258077c8a1053ebfecb97f8f60518a75f75a6253"
}
6 changes: 3 additions & 3 deletions src/ReservoirDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ contract ReservoirDeployer {
uint256 public step = 0;

// Bytecode hashes.
bytes32 public constant FACTORY_HASH = bytes32(0x87b0f73fafcf4bb41e013c8423dc679f6885527007d6c3f1e1834a670cbaadc5);
bytes32 public constant FACTORY_HASH = bytes32(0x03107ee57e3054504c8ec584092f9d955bec0cbd1225fa1a8fb090a2084335aa);
bytes32 public constant CONSTANT_PRODUCT_HASH =
bytes32(0xddd075b1901512190892da217db5c4d266a6831226e14669e690aeb663dd9cf3);
bytes32 public constant STABLE_HASH = bytes32(0xa52a6e20c3811c44bda8bc9f7988dd62da3c611711900f85d465b64bf97066af);
bytes32(0x174b1bf04c1051e9d50d64a3c3b35666a4473b5994955635039d64ef9da69e97);
bytes32 public constant STABLE_HASH = bytes32(0xc94b58df187b1b9b93b67504d44fbe2d8083ccfa39b23bd724037901d6c6dbf9);

// Deployment addresses.
GenericFactory public factory;
Expand Down
11 changes: 8 additions & 3 deletions src/curve/stable/StablePair.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ contract StablePair is ReservoirPair {

constructor(IERC20 aToken0, IERC20 aToken1) ReservoirPair(aToken0, aToken1, PAIR_SWAP_FEE_NAME) {
uint64 lImpreciseA = factory.read(AMPLIFICATION_COEFFICIENT_NAME).toUint64();
require(lImpreciseA >= StableMath.MIN_A && lImpreciseA <= StableMath.MAX_A, InvalidA());
unchecked {
// StableMath.MIN_A <= lImpreciseA <= StableMath.MAX_A
require(lImpreciseA - StableMath.MIN_A <= StableMath.MAX_A - StableMath.MIN_A, InvalidA());
}

ampData.initialA = lImpreciseA * uint64(StableMath.A_PRECISION);
ampData.futureA = ampData.initialA;
Expand All @@ -51,8 +54,10 @@ contract StablePair is ReservoirPair {
}

function rampA(uint64 aFutureARaw, uint64 aFutureATime) external onlyFactory {
require(aFutureARaw >= StableMath.MIN_A && aFutureARaw <= StableMath.MAX_A, InvalidA());

unchecked {
// StableMath.MIN_A <= aFutureRaw <= StableMath.MAX_A
require(aFutureARaw - StableMath.MIN_A <= StableMath.MAX_A - StableMath.MIN_A, InvalidA());
}
uint64 lFutureAPrecise = aFutureARaw * uint64(StableMath.A_PRECISION);

uint256 duration = aFutureATime - block.timestamp;
Expand Down
7 changes: 5 additions & 2 deletions src/libraries/StableMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ library StableMath {
/// @dev Maximum fee, which is 100%.
uint256 private constant ONE_HUNDRED_PERCENT = 1_000_000;

error SM_COMPUTE_DID_NOT_CONVERGE();
error SM_GETY_DID_NOT_CONVERGE();

/// @notice Calculates the amount of output tokens for an exact in trade
/// @param amountIn input amount, assumed to be type(uint104).max or less
/// @param reserve0 reserves of token0, assumed to be type(uint104).max or less
Expand Down Expand Up @@ -135,7 +138,7 @@ library StableMath {
}
}

revert("SM: COMPUTE_DID_NOT_CONVERGE");
revert SM_COMPUTE_DID_NOT_CONVERGE();
}

/// @notice Calculate the new balance of one token given the balance of the other token
Expand Down Expand Up @@ -163,6 +166,6 @@ library StableMath {
return y;
}
}
revert("SM: GETY_DID_NOT_CONVERGE");
revert SM_GETY_DID_NOT_CONVERGE();
}
}

0 comments on commit 19a8653

Please sign in to comment.