Skip to content

Commit b728963

Browse files
more parity
1 parent d6b1d03 commit b728963

File tree

4 files changed

+114
-53
lines changed

4 files changed

+114
-53
lines changed

script/DeployPool.s.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ contract DeployPool is ScriptUtil {
5757
evc.batch(items);
5858
vm.stopBroadcast();
5959

60-
(address pool,,) = factory.getEulerAccountState(eulerAccount);
60+
address pool = factory.poolByHolder(eulerAccount);
6161

6262
string memory outputScriptFileName = "DeployPool_output.json";
6363

src/EulerSwapFactory.sol

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import {GenericFactory} from "evk/GenericFactory/GenericFactory.sol";
1010
/// @custom:security-contact [email protected]
1111
/// @author Euler Labs (https://www.eulerlabs.com/)
1212
contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
13+
/// @dev An array to store all pools addresses.
14+
address[] private allPools;
1315
/// @dev Vaults must be deployed by this factory
1416
address public immutable evkFactory;
15-
/// @dev An array to store all pools addresses.
16-
address[] public allPools;
1717
/// @dev Mapping between euler account and EulerAccountState
1818
mapping(address eulerAccount => EulerAccountState state) private eulerAccountState;
1919
mapping(address asset0 => mapping(address asset1 => address[])) private poolMap;
@@ -40,6 +40,7 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
4040
error OldOperatorStillInstalled();
4141
error OperatorNotInstalled();
4242
error InvalidVaultImplementation();
43+
error SliceOutOfBounds();
4344

4445
constructor(address evc, address evkFactory_) EVCUtil(evc) {
4546
evkFactory = evkFactory_;
@@ -105,36 +106,48 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
105106
);
106107
}
107108

109+
/// @inheritdoc IEulerSwapFactory
108110
function EVC() external view override(EVCUtil, IEulerSwapFactory) returns (address) {
109111
return address(evc);
110112
}
111113

112114
/// @inheritdoc IEulerSwapFactory
113-
function allPoolsLength() external view returns (uint256) {
115+
function poolByHolder(address who) external view returns (address) {
116+
return eulerAccountState[who].pool;
117+
}
118+
119+
/// @inheritdoc IEulerSwapFactory
120+
function poolsLength() external view returns (uint256) {
114121
return allPools.length;
115122
}
116123

117124
/// @inheritdoc IEulerSwapFactory
118-
function getEulerAccountState(address eulerAccount) external view returns (address, uint48, uint48) {
119-
return (
120-
eulerAccountState[eulerAccount].pool,
121-
eulerAccountState[eulerAccount].allPoolsIndex,
122-
eulerAccountState[eulerAccount].poolMapIndex
123-
);
125+
function poolsSlice(uint256 start, uint256 end) external view returns (address[] memory) {
126+
return _getSlice(allPools, start, end);
124127
}
125128

126129
/// @inheritdoc IEulerSwapFactory
127-
function getAllPoolsListSlice(uint256 _start, uint256 _end) external view returns (address[] memory) {
128-
uint256 length = allPools.length;
129-
if (_end == type(uint256).max) _end = length;
130-
if (_end < _start || _end > length) revert InvalidQuery();
131-
132-
address[] memory allPoolsList = new address[](_end - _start);
133-
for (uint256 i; i < _end - _start; ++i) {
134-
allPoolsList[i] = allPools[_start + i];
135-
}
130+
function pools() external view returns (address[] memory) {
131+
return _getSlice(allPools, 0, type(uint256).max);
132+
}
133+
134+
/// @inheritdoc IEulerSwapFactory
135+
function poolsByPairLength(address asset0, address asset1) external view returns (uint256) {
136+
return poolMap[asset0][asset1].length;
137+
}
136138

137-
return allPoolsList;
139+
/// @inheritdoc IEulerSwapFactory
140+
function poolsByPairSlice(address asset0, address asset1, uint256 start, uint256 end)
141+
external
142+
view
143+
returns (address[] memory)
144+
{
145+
return _getSlice(poolMap[asset0][asset1], start, end);
146+
}
147+
148+
/// @inheritdoc IEulerSwapFactory
149+
function poolsByPair(address asset0, address asset1) external view returns (address[] memory) {
150+
return _getSlice(poolMap[asset0][asset1], 0, type(uint256).max);
138151
}
139152

140153
/// @notice Validates operator authorization for euler account and update the relevant EulerAccountState.
@@ -218,4 +231,24 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
218231
function _getAssets(address pool) internal view returns (address, address) {
219232
return (EulerSwap(pool).asset0(), EulerSwap(pool).asset1());
220233
}
234+
235+
/// @notice Returns a slice of an array of addresses
236+
/// @dev Creates a new memory array containing elements from start to end index
237+
/// If end is type(uint256).max, it will return all elements from start to the end of the array
238+
/// @param arr The storage array to slice
239+
/// @param start The starting index of the slice (inclusive)
240+
/// @param end The ending index of the slice (exclusive)
241+
/// @return A new memory array containing the requested slice of addresses
242+
function _getSlice(address[] storage arr, uint256 start, uint256 end) internal view returns (address[] memory) {
243+
uint256 length = arr.length;
244+
if (end == type(uint256).max) end = length;
245+
if (end < start || end > length) revert SliceOutOfBounds();
246+
247+
address[] memory slice = new address[](end - start);
248+
for (uint256 i; i < end - start; ++i) {
249+
slice[i] = arr[start + i];
250+
}
251+
252+
return slice;
253+
}
221254
}

src/interfaces/IEulerSwapFactory.sol

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@ interface IEulerSwapFactory {
2222
external
2323
returns (address);
2424

25-
/// @notice Retrieves the state information for a given Euler account
26-
/// @dev Returns the current pool address, index in allPools array, and index in poolMap array
27-
/// for the specified Euler account
28-
/// @param eulerAccount The address of the Euler account to query
29-
/// @return pool The address of the current pool associated with the Euler account
30-
/// @return allPoolsIndex The index of the pool in the allPools array
31-
/// @return poolMapIndex The index of the pool in the poolMap array for its asset pair
32-
function getEulerAccountState(address eulerAccount) external view returns (address, uint48, uint48);
33-
3425
/// @notice Compute the address of a new EulerSwap pool with the given parameters
3526
/// @dev The pool address is deterministically generated using CREATE2 with a salt derived from
3627
/// the euler account address and provided salt parameter. This allows the pool address to be
@@ -44,17 +35,54 @@ interface IEulerSwapFactory {
4435
IEulerSwap.CurveParams memory curveParams,
4536
bytes32 salt
4637
) external view returns (address);
38+
39+
/// @notice Returns the address of the Ethereum Vault Connector (EVC) contract
40+
/// @return The address of the EVC contract
4741
function EVC() external view returns (address);
48-
/// @notice Get the length of `allPools` array.
49-
/// @return `allPools` length.
50-
function allPoolsLength() external view returns (uint256);
51-
/// @notice Get the address of the pool at the given index in the `allPools` array.
52-
/// @param index The index of the pool to retrieve.
53-
/// @return The address of the pool at the given index.
54-
function allPools(uint256 index) external view returns (address);
55-
/// @notice Get a slice of the deployed pools array.
56-
/// @param start Start index of the slice.
57-
/// @param end End index of the slice.
58-
/// @return An array containing the slice of the deployed pools.
59-
function getAllPoolsListSlice(uint256 start, uint256 end) external view returns (address[] memory);
42+
43+
/// @notice Returns a slice of all deployed pools
44+
/// @dev Returns a subset of the pools array from start to end index
45+
/// @param start The starting index of the slice (inclusive)
46+
/// @param end The ending index of the slice (exclusive)
47+
/// @return An array containing the requested slice of pool addresses
48+
function poolsSlice(uint256 start, uint256 end) external view returns (address[] memory);
49+
50+
/// @notice Returns all deployed pools
51+
/// @dev Returns the complete array of all pool addresses
52+
/// @return An array containing all pool addresses
53+
function pools() external view returns (address[] memory);
54+
55+
/// @notice Returns the number of pools for a specific asset pair
56+
/// @dev Returns the length of the pool array for the given asset pair
57+
/// @param asset0 The address of the first asset
58+
/// @param asset1 The address of the second asset
59+
/// @return The number of pools for the specified asset pair
60+
function poolsByPairLength(address asset0, address asset1) external view returns (uint256);
61+
62+
/// @notice Returns a slice of pools for a specific asset pair
63+
/// @dev Returns a subset of the pools array for the given asset pair from start to end index
64+
/// @param asset0 The address of the first asset
65+
/// @param asset1 The address of the second asset
66+
/// @param start The starting index of the slice (inclusive)
67+
/// @param end The ending index of the slice (exclusive)
68+
/// @return An array containing the requested slice of pool addresses for the asset pair
69+
function poolsByPairSlice(address asset0, address asset1, uint256 start, uint256 end) external view returns (address[] memory);
70+
71+
/// @notice Returns all pools for a specific asset pair
72+
/// @dev Returns the complete array of pool addresses for the given asset pair
73+
/// @param asset0 The address of the first asset
74+
/// @param asset1 The address of the second asset
75+
/// @return An array containing all pool addresses for the specified asset pair
76+
function poolsByPair(address asset0, address asset1) external view returns (address[] memory);
77+
78+
/// @notice Returns the pool address associated with a specific holder
79+
/// @dev Returns the pool address from the EulerAccountState mapping for the given holder
80+
/// @param who The address of the holder to query
81+
/// @return The address of the pool associated with the holder
82+
function poolByHolder(address who) external view returns (address);
83+
84+
/// @notice Returns the total number of deployed pools
85+
/// @dev Returns the length of the allPools array
86+
/// @return The total number of pools deployed through the factory
87+
function poolsLength() external view returns (uint256);
6088
}

test/EulerSwapFactoryTest.t.sol

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ contract EulerSwapFactoryTest is EulerSwapTestBase {
1717
}
1818

1919
function testDeployPool() public {
20-
uint256 allPoolsLengthBefore = eulerSwapFactory.allPoolsLength();
20+
uint256 allPoolsLengthBefore = eulerSwapFactory.poolsLength();
2121

2222
// test when new pool not set as operator
2323

@@ -61,15 +61,15 @@ contract EulerSwapFactoryTest is EulerSwapTestBase {
6161
vm.prank(holder);
6262
evc.batch(items);
6363

64-
(address eulerSwap,,) = eulerSwapFactory.getEulerAccountState(holder);
64+
address eulerSwap = eulerSwapFactory.poolByHolder(holder);
6565

66-
uint256 allPoolsLengthAfter = eulerSwapFactory.allPoolsLength();
66+
uint256 allPoolsLengthAfter = eulerSwapFactory.poolsLength();
6767
assertEq(allPoolsLengthAfter - allPoolsLengthBefore, 1);
6868

69-
address[] memory poolsList = eulerSwapFactory.getAllPoolsListSlice(0, type(uint256).max);
69+
address[] memory poolsList = eulerSwapFactory.pools();
7070
assertEq(poolsList.length, 1);
7171
assertEq(poolsList[0], eulerSwap);
72-
assertEq(eulerSwapFactory.allPools(0), address(eulerSwap));
72+
assertEq(poolsList[0], address(eulerSwap));
7373

7474
// test when deploying second pool while old pool is still set as operator
7575

@@ -93,7 +93,7 @@ contract EulerSwapFactoryTest is EulerSwapTestBase {
9393
evc.batch(items);
9494

9595
// test deploying new pool for same assets pair as old one
96-
(address oldPool,,) = eulerSwapFactory.getEulerAccountState(holder);
96+
address oldPool = eulerSwapFactory.poolByHolder(holder);
9797
salt = bytes32(uint256(123456));
9898
predictedAddress = predictPoolAddress(address(eulerSwapFactory), poolParams, curveParams, salt);
9999

@@ -120,11 +120,11 @@ contract EulerSwapFactoryTest is EulerSwapTestBase {
120120
vm.prank(holder);
121121
evc.batch(items);
122122

123-
(address pool,,) = eulerSwapFactory.getEulerAccountState(holder);
123+
address pool = eulerSwapFactory.poolByHolder(holder);
124124
assertEq(pool, predictedAddress);
125125

126126
// test deploying new pool for different assets pair as old one
127-
(oldPool,,) = eulerSwapFactory.getEulerAccountState(holder);
127+
oldPool = eulerSwapFactory.poolByHolder(holder);
128128
poolParams = IEulerSwap.Params(address(eTST), address(eTST3), holder, 1e18, 1e18, 1e18, 1e18, 0);
129129

130130
salt = bytes32(uint256(1234567));
@@ -153,13 +153,13 @@ contract EulerSwapFactoryTest is EulerSwapTestBase {
153153
vm.prank(holder);
154154
evc.batch(items);
155155

156-
(pool,,) = eulerSwapFactory.getEulerAccountState(holder);
156+
pool = eulerSwapFactory.poolByHolder(holder);
157157
assertEq(pool, predictedAddress);
158158
}
159159

160-
function testInvalidGetAllPoolsListSliceQuery() public {
161-
vm.expectRevert(EulerSwapFactory.InvalidQuery.selector);
162-
eulerSwapFactory.getAllPoolsListSlice(1, 0);
160+
function testInvalidPoolsSliceQuery() public {
161+
vm.expectRevert(EulerSwapFactory.SliceOutOfBounds.selector);
162+
eulerSwapFactory.poolsSlice(1, 0);
163163
}
164164

165165
function testDeployWithAssetsOutOfOrderOrEqual() public {

0 commit comments

Comments
 (0)