Skip to content

Commit 31923d5

Browse files
update
1 parent e5c722b commit 31923d5

File tree

4 files changed

+52
-61
lines changed

4 files changed

+52
-61
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.poolByHolder(eulerAccount);
60+
address pool = factory.poolByEulerAccount(eulerAccount);
6161

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

src/EulerSwapFactory.sol

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
5757
InvalidVaultImplementation()
5858
);
5959

60+
uninstall(params.eulerAccount);
61+
6062
EulerSwap pool = new EulerSwap{salt: keccak256(abi.encode(params.eulerAccount, salt))}(params, curveParams);
6163

62-
checkAndUpdateEulerAccountState(params.eulerAccount, address(pool));
64+
updateEulerAccountState(params.eulerAccount, address(pool));
6365

6466
EulerSwap(pool).activate();
6567

@@ -82,6 +84,11 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
8284
return address(pool);
8385
}
8486

87+
/// @inheritdoc IEulerSwapFactory
88+
function uninstallPool() external {
89+
uninstall(_msgSender());
90+
}
91+
8592
/// @inheritdoc IEulerSwapFactory
8693
function computePoolAddress(
8794
IEulerSwap.Params memory poolParams,
@@ -112,7 +119,7 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
112119
}
113120

114121
/// @inheritdoc IEulerSwapFactory
115-
function poolByHolder(address who) external view returns (address) {
122+
function poolByEulerAccount(address who) external view returns (address) {
116123
return eulerAccountState[who].pool;
117124
}
118125

@@ -153,73 +160,51 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
153160
/// @notice Validates operator authorization for euler account and update the relevant EulerAccountState.
154161
/// @param eulerAccount The address of the euler account.
155162
/// @param newOperator The address of the new pool.
156-
function checkAndUpdateEulerAccountState(address eulerAccount, address newOperator) internal {
163+
function updateEulerAccountState(address eulerAccount, address newOperator) internal {
157164
require(evc.isAccountOperatorAuthorized(eulerAccount, newOperator), OperatorNotInstalled());
158165

159-
(address newOpAsset0, address newOpAsset1) = _getAssets(newOperator);
160-
address oldOperator = eulerAccountState[eulerAccount].pool;
161-
162-
if (oldOperator != address(0)) {
163-
require(!evc.isAccountOperatorAuthorized(eulerAccount, oldOperator), OldOperatorStillInstalled());
166+
(address asset0, address asset1) = _getAssets(newOperator);
164167

165-
// replace pool address in allPools array
166-
_updateInArray(allPools, eulerAccountState[eulerAccount].allPoolsIndex, newOperator);
168+
address[] storage poolMapArray = poolMap[asset0][asset1];
167169

168-
eulerAccountState[eulerAccount].pool = newOperator;
170+
eulerAccountState[eulerAccount] = EulerAccountState({
171+
pool: newOperator,
172+
allPoolsIndex: uint48(allPools.length),
173+
poolMapIndex: uint48(poolMapArray.length)
174+
});
169175

170-
(address oldOpAsset0, address oldOpAsset1) = _getAssets(oldOperator);
176+
allPools.push(newOperator);
177+
poolMapArray.push(newOperator);
178+
}
171179

172-
// if deploying new pool for same assets pair, just update poolMap without pop()
173-
// else, we need to go the traditional path, reduce the array size, update eulerAccount poolMapIndex and push new one
174-
if (oldOpAsset0 == newOpAsset0 && oldOpAsset1 == newOpAsset1) {
175-
_updateInArray(
176-
poolMap[newOpAsset0][newOpAsset1], eulerAccountState[eulerAccount].poolMapIndex, newOperator
177-
);
178-
} else {
179-
_removeFromArray(poolMap[oldOpAsset0][oldOpAsset1], eulerAccountState[eulerAccount].poolMapIndex);
180+
/// @notice Uninstalls the pool associated with the given Euler account
181+
/// @dev This function removes the pool from the factory's tracking and emits a PoolUninstalled event
182+
/// @dev The function checks if the operator is still installed and reverts if it is
183+
/// @dev If no pool exists for the account, the function returns without any action
184+
/// @param eulerAccount The address of the Euler account whose pool should be uninstalled
185+
function uninstall(address eulerAccount) internal {
186+
address pool = eulerAccountState[eulerAccount].pool;
180187

181-
eulerAccountState[eulerAccount].poolMapIndex = uint48(poolMap[newOpAsset0][newOpAsset1].length);
188+
if (pool == address(0)) return;
182189

183-
_pushInArray(poolMap[newOpAsset0][newOpAsset1], newOperator);
184-
}
190+
require(!evc.isAccountOperatorAuthorized(eulerAccount, pool), OldOperatorStillInstalled());
185191

186-
emit PoolUninstalled(oldOpAsset0, oldOpAsset1, eulerAccount, oldOperator);
187-
} else {
188-
address[] storage poolMapArray = poolMap[newOpAsset0][newOpAsset1];
192+
(address asset0, address asset1) = _getAssets(pool);
189193

190-
eulerAccountState[eulerAccount] = EulerAccountState({
191-
pool: newOperator,
192-
allPoolsIndex: uint48(allPools.length),
193-
poolMapIndex: uint48(poolMapArray.length)
194-
});
194+
address[] storage poolMapArr = poolMap[asset0][asset1];
195195

196-
_pushInArray(allPools, newOperator);
197-
_pushInArray(poolMapArray, newOperator);
198-
}
199-
}
196+
swapAndPop(allPools, eulerAccountState[eulerAccount].allPoolsIndex);
197+
swapAndPop(poolMapArr, eulerAccountState[eulerAccount].poolMapIndex);
200198

201-
/// @notice Updates an element at a specific index in an array
202-
/// @dev Directly modifies the array element at the given index with a new value
203-
/// @param arr The storage array to update
204-
/// @param index The index of the element to update
205-
/// @param _newValue The new value to set at the specified index
206-
function _updateInArray(address[] storage arr, uint256 index, address _newValue) internal {
207-
arr[index] = _newValue;
208-
}
199+
delete eulerAccountState[eulerAccount];
209200

210-
/// @notice Adds a new element to the end of an array
211-
/// @dev Uses the push operation to append a new value to the array
212-
/// @param arr The storage array to append to
213-
/// @param _newValue The new value to append to the array
214-
function _pushInArray(address[] storage arr, address _newValue) internal {
215-
arr.push(_newValue);
201+
emit PoolUninstalled(asset0, asset1, eulerAccount, pool);
216202
}
217203

218-
/// @notice Removes an element from an array at a specific index
219-
/// @dev Uses the swap-and-pop pattern to remove an element while maintaining array order
220-
/// @param arr The storage array to remove from
204+
/// @notice Swaps the element at the given index with the last element and removes the last element
205+
/// @param arr The storage array to modify
221206
/// @param index The index of the element to remove
222-
function _removeFromArray(address[] storage arr, uint256 index) internal {
207+
function swapAndPop(address[] storage arr, uint256 index) internal {
223208
arr[index] = arr[arr.length - 1];
224209
arr.pop();
225210
}

src/interfaces/IEulerSwapFactory.sol

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ interface IEulerSwapFactory {
2222
external
2323
returns (address);
2424

25+
/// @notice Uninstalls the pool associated with the Euler account
26+
/// @dev This function removes the pool from the factory's tracking and emits a PoolUninstalled event
27+
/// @dev The function can only be called by the Euler account that owns the pool
28+
/// @dev If no pool is installed for the caller, the function returns without any action
29+
function uninstallPool() external;
30+
2531
/// @notice Compute the address of a new EulerSwap pool with the given parameters
2632
/// @dev The pool address is deterministically generated using CREATE2 with a salt derived from
2733
/// the euler account address and provided salt parameter. This allows the pool address to be
@@ -82,7 +88,7 @@ interface IEulerSwapFactory {
8288
/// @dev Returns the pool address from the EulerAccountState mapping for the given holder
8389
/// @param who The address of the holder to query
8490
/// @return The address of the pool associated with the holder
85-
function poolByHolder(address who) external view returns (address);
91+
function poolByEulerAccount(address who) external view returns (address);
8692

8793
/// @notice Returns the total number of deployed pools
8894
/// @dev Returns the length of the allPools array

test/EulerSwapFactoryTest.t.sol

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ contract EulerSwapFactoryTest is EulerSwapTestBase {
6363
vm.prank(holder);
6464
evc.batch(items);
6565

66-
address eulerSwap = eulerSwapFactory.poolByHolder(holder);
66+
address eulerSwap = eulerSwapFactory.poolByEulerAccount(holder);
6767

6868
uint256 allPoolsLengthAfter = eulerSwapFactory.poolsLength();
6969
assertEq(allPoolsLengthAfter - allPoolsLengthBefore, 1);
@@ -95,7 +95,7 @@ contract EulerSwapFactoryTest is EulerSwapTestBase {
9595
evc.batch(items);
9696

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

@@ -122,11 +122,11 @@ contract EulerSwapFactoryTest is EulerSwapTestBase {
122122
vm.prank(holder);
123123
evc.batch(items);
124124

125-
address pool = eulerSwapFactory.poolByHolder(holder);
125+
address pool = eulerSwapFactory.poolByEulerAccount(holder);
126126
assertEq(pool, predictedAddress);
127127

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

132132
salt = bytes32(uint256(1234567));
@@ -155,7 +155,7 @@ contract EulerSwapFactoryTest is EulerSwapTestBase {
155155
vm.prank(holder);
156156
evc.batch(items);
157157

158-
pool = eulerSwapFactory.poolByHolder(holder);
158+
pool = eulerSwapFactory.poolByEulerAccount(holder);
159159
assertEq(pool, predictedAddress);
160160
}
161161

@@ -241,7 +241,7 @@ contract EulerSwapFactoryTest is EulerSwapTestBase {
241241
evc.batch(items);
242242

243243
// Get the deployed pool and its assets
244-
address pool = eulerSwapFactory.poolByHolder(holder);
244+
address pool = eulerSwapFactory.poolByEulerAccount(holder);
245245
address asset0 = EulerSwap(pool).asset0();
246246
address asset1 = EulerSwap(pool).asset1();
247247

0 commit comments

Comments
 (0)