From 0903141b64091fcecfaab14f8504bb2eaee96e62 Mon Sep 17 00:00:00 2001 From: clemlak Date: Fri, 22 Mar 2024 16:39:13 +0400 Subject: [PATCH 1/5] fix: add InvalidReservesLength error to PairStrategy --- src/interfaces/IStrategy.sol | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/interfaces/IStrategy.sol b/src/interfaces/IStrategy.sol index 40027f55..0b10cd9a 100644 --- a/src/interfaces/IStrategy.sol +++ b/src/interfaces/IStrategy.sol @@ -23,6 +23,9 @@ interface IStrategy { /// @dev Thrown when an expected delta does not match the actual delta. error DeltaError(uint256 expected, uint256 actual); + /// @dev Thrown when the reserves length is not 2. + error InvalidReservesLength(); + // Setters /** From af737451449b4d302fe19393caa0cd9449095cbd Mon Sep 17 00:00:00 2001 From: clemlak Date: Fri, 22 Mar 2024 16:39:32 +0400 Subject: [PATCH 2/5] fix: check reserves and tokens array length in ConstantSum --- src/ConstantSum/ConstantSum.sol | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ConstantSum/ConstantSum.sol b/src/ConstantSum/ConstantSum.sol index 08a1c27d..37a5f358 100644 --- a/src/ConstantSum/ConstantSum.sol +++ b/src/ConstantSum/ConstantSum.sol @@ -45,7 +45,7 @@ contract ConstantSum is PairStrategy { function init( address, uint256 poolId, - Pool calldata, + Pool calldata pool, bytes calldata data ) public @@ -61,6 +61,10 @@ contract ConstantSum is PairStrategy { (reserves, totalLiquidity, params) = abi.decode(data, (uint256[], uint256, ConstantSumParams)); + if (pool.reserves.length != 2 || reserves.length != 2) { + revert InvalidReservesLength(); + } + internalParams[poolId].price = params.price; internalParams[poolId].swapFee = params.swapFee; From ca780dcb9498536cd533dd7800363b43d1deb367 Mon Sep 17 00:00:00 2001 From: clemlak Date: Fri, 22 Mar 2024 18:35:30 +0400 Subject: [PATCH 3/5] fix: add pool reserves length check in G3M --- src/GeometricMean/GeometricMean.sol | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/GeometricMean/GeometricMean.sol b/src/GeometricMean/GeometricMean.sol index 1dcf1942..44e372bc 100644 --- a/src/GeometricMean/GeometricMean.sol +++ b/src/GeometricMean/GeometricMean.sol @@ -74,7 +74,7 @@ contract GeometricMean is PairStrategy { function init( address, uint256 poolId, - Pool calldata, + Pool calldata pool, bytes calldata data ) external onlyDFMM returns (bool, int256, uint256[] memory, uint256) { InitState memory state; @@ -92,6 +92,10 @@ contract GeometricMean is PairStrategy { data, (uint256, uint256, uint256, uint256, uint256, address) ); + if (pool.reserves.length != 2) { + revert InvalidReservesLength(); + } + if (state.wX >= ONE) { revert InvalidWeightX(); } From 3bd1bc3d94dd1f2447c01b88b89e15c79bb5187a Mon Sep 17 00:00:00 2001 From: clemlak Date: Fri, 22 Mar 2024 18:37:16 +0400 Subject: [PATCH 4/5] fix: add reserves and tokens array length check in LogNormal --- src/LogNormal/LogNormal.sol | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/LogNormal/LogNormal.sol b/src/LogNormal/LogNormal.sol index 1bcd5513..ccdb3e1a 100644 --- a/src/LogNormal/LogNormal.sol +++ b/src/LogNormal/LogNormal.sol @@ -58,7 +58,7 @@ contract LogNormal is PairStrategy { function init( address, uint256 poolId, - Pool calldata, + Pool calldata pool, bytes calldata data ) public @@ -74,6 +74,10 @@ contract LogNormal is PairStrategy { (reserves, totalLiquidity, params) = abi.decode(data, (uint256[], uint256, LogNormalParams)); + if (pool.reserves.length != 2 || reserves.length != 2) { + revert InvalidReservesLength(); + } + internalParams[poolId].mean.lastComputedValue = params.mean; internalParams[poolId].width.lastComputedValue = params.width; internalParams[poolId].swapFee = params.swapFee; From e3f07d2b3d5a313164f2e0093fab166a8557f3c4 Mon Sep 17 00:00:00 2001 From: clemlak Date: Mon, 25 Mar 2024 17:17:43 +0400 Subject: [PATCH 5/5] feat: G3M init pool data now expects array of reserves --- src/GeometricMean/G3MMath.sol | 7 +++++-- src/GeometricMean/GeometricMean.sol | 11 +++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/GeometricMean/G3MMath.sol b/src/GeometricMean/G3MMath.sol index 0e409569..d5aff9e3 100644 --- a/src/GeometricMean/G3MMath.sol +++ b/src/GeometricMean/G3MMath.sol @@ -251,6 +251,9 @@ function computeInitialPoolData( L = computeNextLiquidity(amountX, rY, invariant, L, params); - return - abi.encode(amountX, rY, L, params.wX, params.swapFee, params.controller); + uint256[] memory reserves = new uint256[](2); + reserves[0] = amountX; + reserves[1] = rY; + + return abi.encode(reserves, L, params.wX, params.swapFee, params.controller); } diff --git a/src/GeometricMean/GeometricMean.sol b/src/GeometricMean/GeometricMean.sol index 44e372bc..3d20d9b2 100644 --- a/src/GeometricMean/GeometricMean.sol +++ b/src/GeometricMean/GeometricMean.sol @@ -79,20 +79,15 @@ contract GeometricMean is PairStrategy { ) external onlyDFMM returns (bool, int256, uint256[] memory, uint256) { InitState memory state; - state.reserves = new uint256[](2); - ( - state.reserves[0], - state.reserves[1], + state.reserves, state.totalLiquidity, state.wX, state.swapFee, state.controller - ) = abi.decode( - data, (uint256, uint256, uint256, uint256, uint256, address) - ); + ) = abi.decode(data, (uint256[], uint256, uint256, uint256, address)); - if (pool.reserves.length != 2) { + if (pool.reserves.length != 2 || state.reserves.length != 2) { revert InvalidReservesLength(); }