@@ -57,9 +57,11 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
57
57
InvalidVaultImplementation ()
58
58
);
59
59
60
+ uninstall (params.eulerAccount);
61
+
60
62
EulerSwap pool = new EulerSwap {salt: keccak256 (abi.encode (params.eulerAccount, salt))}(params, curveParams);
61
63
62
- checkAndUpdateEulerAccountState (params.eulerAccount, address (pool));
64
+ updateEulerAccountState (params.eulerAccount, address (pool));
63
65
64
66
EulerSwap (pool).activate ();
65
67
@@ -82,6 +84,11 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
82
84
return address (pool);
83
85
}
84
86
87
+ /// @inheritdoc IEulerSwapFactory
88
+ function uninstallPool () external {
89
+ uninstall (_msgSender ());
90
+ }
91
+
85
92
/// @inheritdoc IEulerSwapFactory
86
93
function computePoolAddress (
87
94
IEulerSwap.Params memory poolParams ,
@@ -112,7 +119,7 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
112
119
}
113
120
114
121
/// @inheritdoc IEulerSwapFactory
115
- function poolByHolder (address who ) external view returns (address ) {
122
+ function poolByEulerAccount (address who ) external view returns (address ) {
116
123
return eulerAccountState[who].pool;
117
124
}
118
125
@@ -153,73 +160,51 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
153
160
/// @notice Validates operator authorization for euler account and update the relevant EulerAccountState.
154
161
/// @param eulerAccount The address of the euler account.
155
162
/// @param newOperator The address of the new pool.
156
- function checkAndUpdateEulerAccountState (address eulerAccount , address newOperator ) internal {
163
+ function updateEulerAccountState (address eulerAccount , address newOperator ) internal {
157
164
require (evc.isAccountOperatorAuthorized (eulerAccount, newOperator), OperatorNotInstalled ());
158
165
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);
164
167
165
- // replace pool address in allPools array
166
- _updateInArray (allPools, eulerAccountState[eulerAccount].allPoolsIndex, newOperator);
168
+ address [] storage poolMapArray = poolMap[asset0][asset1];
167
169
168
- eulerAccountState[eulerAccount].pool = newOperator;
170
+ eulerAccountState[eulerAccount] = EulerAccountState ({
171
+ pool: newOperator,
172
+ allPoolsIndex: uint48 (allPools.length ),
173
+ poolMapIndex: uint48 (poolMapArray.length )
174
+ });
169
175
170
- (address oldOpAsset0 , address oldOpAsset1 ) = _getAssets (oldOperator);
176
+ allPools.push (newOperator);
177
+ poolMapArray.push (newOperator);
178
+ }
171
179
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;
180
187
181
- eulerAccountState[eulerAccount].poolMapIndex = uint48 (poolMap[newOpAsset0][newOpAsset1]. length ) ;
188
+ if (pool == address ( 0 )) return ;
182
189
183
- _pushInArray (poolMap[newOpAsset0][newOpAsset1], newOperator);
184
- }
190
+ require (! evc.isAccountOperatorAuthorized (eulerAccount, pool), OldOperatorStillInstalled ());
185
191
186
- emit PoolUninstalled (oldOpAsset0, oldOpAsset1, eulerAccount, oldOperator);
187
- } else {
188
- address [] storage poolMapArray = poolMap[newOpAsset0][newOpAsset1];
192
+ (address asset0 , address asset1 ) = _getAssets (pool);
189
193
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];
195
195
196
- _pushInArray (allPools, newOperator);
197
- _pushInArray (poolMapArray, newOperator);
198
- }
199
- }
196
+ swapAndPop (allPools, eulerAccountState[eulerAccount].allPoolsIndex);
197
+ swapAndPop (poolMapArr, eulerAccountState[eulerAccount].poolMapIndex);
200
198
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];
209
200
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);
216
202
}
217
203
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
221
206
/// @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 {
223
208
arr[index] = arr[arr.length - 1 ];
224
209
arr.pop ();
225
210
}
0 commit comments