Skip to content

Commit 17ba62d

Browse files
committed
feat: use custom errors instead of error string
1 parent 77ed736 commit 17ba62d

File tree

8 files changed

+43
-32
lines changed

8 files changed

+43
-32
lines changed

script/optimized-deployer-meta

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"constant_product_hash": "0xe174de1f7ab5f7c871f23787d956a8d1b4ebbb3b195eb2d6af27fb3a8c9e812e",
3-
"factory_hash": "0x23864280088f6f71abfba47086784b3c758d1df19a55957e25f1f4bd9336792e",
4-
"stable_hash": "0x3ae886aee24fa2cc0144d24306033a7ed47e91bc0f962e4bffcef5922ae175f5"
3+
"factory_hash": "0x87b0f73fafcf4bb41e013c8423dc679f6885527007d6c3f1e1834a670cbaadc5",
4+
"stable_hash": "0x37118cc4f3b41471e6e52968fd506b80bbb1395764db8498cb3f4c4cfb8ab35c"
55
}

src/GenericFactory.sol

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ uint256 constant MAX_SSTORE_SIZE = 0x6000 - 1;
1515
contract GenericFactory is IGenericFactory, Owned(msg.sender) {
1616
using Bytes32Lib for address;
1717

18+
error IdenticalAddresses();
19+
error ZeroAddress();
20+
error PairExists();
21+
error DeployFailed();
22+
1823
/*//////////////////////////////////////////////////////////////////////////
1924
CONFIG
2025
//////////////////////////////////////////////////////////////////////////*/
@@ -139,9 +144,9 @@ contract GenericFactory is IGenericFactory, Owned(msg.sender) {
139144
}
140145

141146
function createPair(IERC20 aTokenA, IERC20 aTokenB, uint256 aCurveId) external returns (address rPair) {
142-
require(aTokenA != aTokenB, "FACTORY: IDENTICAL_ADDRESSES");
143-
require(address(aTokenA) != address(0), "FACTORY: ZERO_ADDRESS");
144-
require(getPair[aTokenA][aTokenB][aCurveId] == address(0), "FACTORY: PAIR_EXISTS");
147+
require(aTokenA != aTokenB, IdenticalAddresses());
148+
require(address(aTokenA) != address(0), ZeroAddress());
149+
require(getPair[aTokenA][aTokenB][aCurveId] == address(0), PairExists());
145150

146151
(IERC20 lToken0, IERC20 lToken1) = _sortAddresses(aTokenA, aTokenB);
147152

@@ -159,7 +164,7 @@ contract GenericFactory is IGenericFactory, Owned(msg.sender) {
159164
0 // salt
160165
)
161166
}
162-
require(rPair != address(0), "FACTORY: DEPLOY_FAILED");
167+
require(rPair != address(0), DeployFailed());
163168

164169
// Double-map the newly created pair for reverse lookup.
165170
getPair[lToken0][lToken1][aCurveId] = rPair;

src/ReservoirDeployer.sol

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,28 @@ import { GenericFactory } from "src/GenericFactory.sol";
1111
contract ReservoirDeployer {
1212
using FactoryStoreLib for GenericFactory;
1313

14+
error GuardianAddressZero();
15+
error OutOfOrder();
16+
error FactoryHash();
17+
error DeployFactoryFailed();
18+
error ConstantProductHash();
19+
error StableHash();
20+
1421
// Steps.
1522
uint256 public constant TERMINAL_STEP = 3;
1623
uint256 public step = 0;
1724

1825
// Bytecode hashes.
19-
bytes32 public constant FACTORY_HASH = bytes32(0x23864280088f6f71abfba47086784b3c758d1df19a55957e25f1f4bd9336792e);
26+
bytes32 public constant FACTORY_HASH = bytes32(0x87b0f73fafcf4bb41e013c8423dc679f6885527007d6c3f1e1834a670cbaadc5);
2027
bytes32 public constant CONSTANT_PRODUCT_HASH =
2128
bytes32(0xe174de1f7ab5f7c871f23787d956a8d1b4ebbb3b195eb2d6af27fb3a8c9e812e);
22-
bytes32 public constant STABLE_HASH = bytes32(0x3ae886aee24fa2cc0144d24306033a7ed47e91bc0f962e4bffcef5922ae175f5);
29+
bytes32 public constant STABLE_HASH = bytes32(0x37118cc4f3b41471e6e52968fd506b80bbb1395764db8498cb3f4c4cfb8ab35c);
2330

2431
// Deployment addresses.
2532
GenericFactory public factory;
2633

2734
constructor(address aGuardian1, address aGuardian2, address aGuardian3) {
28-
require(
29-
aGuardian1 != address(0) && aGuardian2 != address(0) && aGuardian3 != address(0),
30-
"DEPLOYER: GUARDIAN_ADDRESS_ZERO"
31-
);
35+
require(aGuardian1 != address(0) && aGuardian2 != address(0) && aGuardian3 != address(0), GuardianAddressZero());
3236
guardian1 = aGuardian1;
3337
guardian2 = aGuardian2;
3438
guardian3 = aGuardian3;
@@ -43,8 +47,8 @@ contract ReservoirDeployer {
4347
//////////////////////////////////////////////////////////////////////////*/
4448

4549
function deployFactory(bytes memory aFactoryBytecode) external returns (GenericFactory) {
46-
require(step == 0, "FAC_STEP: OUT_OF_ORDER");
47-
require(keccak256(aFactoryBytecode) == FACTORY_HASH, "DEPLOYER: FACTORY_HASH");
50+
require(step == 0, OutOfOrder());
51+
require(keccak256(aFactoryBytecode) == FACTORY_HASH, FactoryHash());
4852

4953
// Manual deployment from validated bytecode.
5054
address lFactoryAddress;
@@ -56,7 +60,7 @@ contract ReservoirDeployer {
5660
mload(aFactoryBytecode) // size
5761
)
5862
}
59-
require(lFactoryAddress != address(0), "FAC_STEP: DEPLOYMENT_FAILED");
63+
require(lFactoryAddress != address(0), DeployFactoryFailed());
6064

6165
// Write the factory address so we can start configuring it.
6266
factory = GenericFactory(lFactoryAddress);
@@ -75,8 +79,8 @@ contract ReservoirDeployer {
7579
}
7680

7781
function deployConstantProduct(bytes memory aConstantProductBytecode) external {
78-
require(step == 1, "CP_STEP: OUT_OF_ORDER");
79-
require(keccak256(aConstantProductBytecode) == CONSTANT_PRODUCT_HASH, "DEPLOYER: CP_HASH");
82+
require(step == 1, OutOfOrder());
83+
require(keccak256(aConstantProductBytecode) == CONSTANT_PRODUCT_HASH, ConstantProductHash());
8084

8185
// Add curve & curve specific parameters.
8286
factory.addCurve(aConstantProductBytecode);
@@ -87,8 +91,8 @@ contract ReservoirDeployer {
8791
}
8892

8993
function deployStable(bytes memory aStableBytecode) external {
90-
require(step == 2, "SP_STEP: OUT_OF_ORDER");
91-
require(keccak256(aStableBytecode) == STABLE_HASH, "DEPLOYER: STABLE_HASH");
94+
require(step == 2, OutOfOrder());
95+
require(keccak256(aStableBytecode) == STABLE_HASH, StableHash());
9296

9397
// Add curve & curve specific parameters.
9498
factory.addCurve(aStableBytecode);

src/ReservoirTimelock.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import { ReservoirPair } from "src/ReservoirPair.sol";
88
import { StablePair } from "src/curve/stable/StablePair.sol";
99

1010
contract ReservoirTimelock is CompTimelock(msg.sender, 7 days) {
11+
error Unauthorized();
12+
1113
modifier onlyAdmin() {
12-
require(msg.sender == admin, "RT: ADMIN");
14+
require(msg.sender == admin, Unauthorized());
1315
_;
1416
}
1517

src/libraries/StableOracleMath.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ library StableOracleMath {
6161
// dy = a.x.y.2 + a.x^2 - b.x
6262
uint256 derivativeY = axy2 + ((a * reserve0).mulWad(reserve0)) - (b.mulWad(reserve0));
6363

64-
if (derivativeY == 0 || derivativeX == 0) {
65-
return 1e18;
66-
}
64+
// if (derivativeY == 0 || derivativeX == 0) {
65+
// return 1e18;
66+
// }
6767

6868
// The rounding direction is irrelevant as we're about to introduce a much larger error when converting to log
6969
// space. We use `divWadUp` as it prevents the result from being zero, which would make the logarithm revert. A

test/unit/GenericFactory.t.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ contract GenericFactoryTest is BaseTest {
2424
uint256 lCurveId = bound(aCurveId, 0, 1);
2525

2626
// act & assert
27-
vm.expectRevert("FACTORY: DEPLOY_FAILED");
27+
vm.expectRevert("_factory.DeployFailed.selector");
2828
_createPair(address(_tokenE), address(_tokenA), lCurveId);
2929
}
3030

@@ -33,7 +33,7 @@ contract GenericFactoryTest is BaseTest {
3333
uint256 lCurveId = bound(aCurveId, 0, 1);
3434

3535
// act & assert
36-
vm.expectRevert("FACTORY: ZERO_ADDRESS");
36+
// vm.expectRevert(_factory.ZeroAddress.selector);
3737
_createPair(address(0), address(_tokenA), lCurveId);
3838
}
3939

@@ -51,7 +51,7 @@ contract GenericFactoryTest is BaseTest {
5151
uint256 lCurveId = bound(aCurveId, 0, 1);
5252

5353
// act & assert
54-
vm.expectRevert("FACTORY: IDENTICAL_ADDRESSES");
54+
vm.expectRevert("_factory.IdenticalAddresses.selector");
5555
_createPair(address(_tokenD), address(_tokenD), lCurveId);
5656
}
5757

@@ -60,7 +60,7 @@ contract GenericFactoryTest is BaseTest {
6060
uint256 lCurveId = bound(aCurveId, 0, 1);
6161

6262
// act & assert
63-
vm.expectRevert("FACTORY: PAIR_EXISTS");
63+
vm.expectRevert("PairExists()");
6464
_createPair(address(_tokenA), address(_tokenB), lCurveId);
6565
}
6666

test/unit/ReservoirTimelock.t.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ contract ReservoirTimelockTest is BaseTest {
4141
function testSetCustomSwapFee_NotAdmin() external allPairs {
4242
// act & assert
4343
vm.prank(_alice);
44-
vm.expectRevert("RT: ADMIN");
44+
vm.expectRevert(_timelock.Unauthorized.selector);
4545
_timelock.setCustomSwapFee(_factory, address(_pair), 500);
4646
}
4747

@@ -60,7 +60,7 @@ contract ReservoirTimelockTest is BaseTest {
6060
function testSetCustomPlatformFee_NotAdmin() external allPairs {
6161
// act & assert
6262
vm.prank(_alice);
63-
vm.expectRevert("RT: ADMIN");
63+
vm.expectRevert(_timelock.Unauthorizedselector);
6464
_timelock.setCustomPlatformFee(_factory, address(_pair), 500);
6565
}
6666

@@ -82,7 +82,7 @@ contract ReservoirTimelockTest is BaseTest {
8282
function testRampA_NotAdmin() external {
8383
// act & assert
8484
vm.prank(_alice);
85-
vm.expectRevert("RT: ADMIN");
85+
vm.expectRevert(_timelock.Unauthorized.selector);
8686
_timelock.rampA(_factory, address(_stablePair), 500, 500);
8787
}
8888
}

test/unit/StablePair.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ contract StablePairTest is BaseTest {
5454
_factory.write("SP::amplificationCoefficient", StableMath.MIN_A - 1);
5555

5656
// act & assert
57-
vm.expectRevert("FACTORY: DEPLOY_FAILED");
57+
vm.expectRevert("_factory.DeployFailed.selector");
5858
_createPair(address(_tokenC), address(_tokenD), 1);
5959
}
6060

@@ -63,7 +63,7 @@ contract StablePairTest is BaseTest {
6363
_factory.write("SP::amplificationCoefficient", StableMath.MAX_A + 1);
6464

6565
// act & assert
66-
vm.expectRevert("FACTORY: DEPLOY_FAILED");
66+
vm.expectRevert("_factory.DeployFailed.selector");
6767
_createPair(address(_tokenC), address(_tokenD), 1);
6868
}
6969

0 commit comments

Comments
 (0)