From 98857be30501c10be8939b6f025ce4f5b9dd3fa5 Mon Sep 17 00:00:00 2001 From: Louise Poole Date: Mon, 15 Apr 2024 16:18:48 +0100 Subject: [PATCH 1/4] Fix balancer price and capabilities --- evm/lib/forge-std | 2 +- evm/lib/openzeppelin-contracts | 2 +- evm/src/balancer-v2/BalancerV2SwapAdapter.sol | 12 ++---------- evm/src/interfaces/ISwapAdapter.sol | 2 +- 4 files changed, 5 insertions(+), 13 deletions(-) diff --git a/evm/lib/forge-std b/evm/lib/forge-std index f73c73d..e4aef94 160000 --- a/evm/lib/forge-std +++ b/evm/lib/forge-std @@ -1 +1 @@ -Subproject commit f73c73d2018eb6a111f35e4dae7b4f27401e9421 +Subproject commit e4aef94c1768803a16fe19f7ce8b65defd027cfd diff --git a/evm/lib/openzeppelin-contracts b/evm/lib/openzeppelin-contracts index 932fddf..11dc5e3 160000 --- a/evm/lib/openzeppelin-contracts +++ b/evm/lib/openzeppelin-contracts @@ -1 +1 @@ -Subproject commit 932fddf69a699a9a80fd2396fd1a2ab91cdda123 +Subproject commit 11dc5e3809ebe07d5405fe524385cbe4f890a08b diff --git a/evm/src/balancer-v2/BalancerV2SwapAdapter.sol b/evm/src/balancer-v2/BalancerV2SwapAdapter.sol index 13b2f2d..2b5f149 100644 --- a/evm/src/balancer-v2/BalancerV2SwapAdapter.sol +++ b/evm/src/balancer-v2/BalancerV2SwapAdapter.sol @@ -100,7 +100,7 @@ contract BalancerV2SwapAdapter is ISwapAdapter { sellAmount = uint256(assetDeltas[0]); } - function priceBatch( + function price( bytes32 poolId, address sellToken, address buyToken, @@ -112,15 +112,6 @@ contract BalancerV2SwapAdapter is ISwapAdapter { } } - function price(bytes32, address, address, uint256[] memory) - external - pure - override - returns (Fraction[] memory) - { - revert NotImplemented("BalancerV2SwapAdapter.price"); - } - function swap( bytes32 poolId, address sellToken, @@ -199,6 +190,7 @@ contract BalancerV2SwapAdapter is ISwapAdapter { capabilities = new Capability[](2); capabilities[0] = Capability.SellOrder; capabilities[1] = Capability.BuyOrder; + capabilities[2] = Capability.PriceFunction; } function getTokens(bytes32 poolId) diff --git a/evm/src/interfaces/ISwapAdapter.sol b/evm/src/interfaces/ISwapAdapter.sol index 50fed77..ab3bacc 100644 --- a/evm/src/interfaces/ISwapAdapter.sol +++ b/evm/src/interfaces/ISwapAdapter.sol @@ -37,7 +37,7 @@ interface ISwapAdapter is ISwapAdapterTypes { address sellToken, address buyToken, uint256[] memory specifiedAmounts - ) external view returns (Fraction[] memory prices); + ) external returns (Fraction[] memory prices); /** * @notice Simulates swapping tokens on a given pool. From 306293764cb9aee25189ef26b2571d580865a10f Mon Sep 17 00:00:00 2001 From: Louise Poole Date: Mon, 15 Apr 2024 16:56:19 +0100 Subject: [PATCH 2/4] Fix tests --- evm/src/balancer-v2/BalancerV2SwapAdapter.sol | 2 +- evm/test/BalancerV2SwapAdapter.t.sol | 20 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/evm/src/balancer-v2/BalancerV2SwapAdapter.sol b/evm/src/balancer-v2/BalancerV2SwapAdapter.sol index 2b5f149..040aba9 100644 --- a/evm/src/balancer-v2/BalancerV2SwapAdapter.sol +++ b/evm/src/balancer-v2/BalancerV2SwapAdapter.sol @@ -187,7 +187,7 @@ contract BalancerV2SwapAdapter is ISwapAdapter { override returns (Capability[] memory capabilities) { - capabilities = new Capability[](2); + capabilities = new Capability[](3); capabilities[0] = Capability.SellOrder; capabilities[1] = Capability.BuyOrder; capabilities[2] = Capability.PriceFunction; diff --git a/evm/test/BalancerV2SwapAdapter.t.sol b/evm/test/BalancerV2SwapAdapter.t.sol index 06ccfe0..a336af8 100644 --- a/evm/test/BalancerV2SwapAdapter.t.sol +++ b/evm/test/BalancerV2SwapAdapter.t.sol @@ -40,14 +40,15 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes { function testPrice() public { uint256[] memory amounts = new uint256[](2); - amounts[0] = 100; - amounts[1] = 200; - vm.expectRevert( - abi.encodeWithSelector( - NotImplemented.selector, "BalancerV2SwapAdapter.price" - ) - ); - adapter.price(B_80BAL_20WETH_POOL_ID, BAL, WETH, amounts); + amounts[0] = amount0; + amounts[1] = amount1; + + Fraction[] memory prices = adapter.price(B_80BAL_20WETH_POOL_ID, BAL, WETH, amounts); + + for (uint256 i = 0; i < prices.length; i++) { + assertGt(prices[i].numerator, 0); + assertGt(prices[i].denominator, 0); + } } function testPriceSingleFuzz() public { @@ -206,9 +207,10 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes { { Capability[] memory res = adapter.getCapabilities(pool, t0, t1); - assertEq(res.length, 2); + assertEq(res.length, 3); assertEq(uint256(res[0]), uint256(Capability.SellOrder)); assertEq(uint256(res[1]), uint256(Capability.BuyOrder)); + assertEq(uint256(res[2]), uint256(Capability.PriceFunction)); } function testGetTokens() public { From 45dc7aa45778c9301be90f2859ea79a724d0df79 Mon Sep 17 00:00:00 2001 From: Louise Poole Date: Mon, 15 Apr 2024 17:41:16 +0100 Subject: [PATCH 3/4] Fix prices function --- evm/src/balancer-v2/BalancerV2SwapAdapter.sol | 1 + evm/test/BalancerV2SwapAdapter.t.sol | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/evm/src/balancer-v2/BalancerV2SwapAdapter.sol b/evm/src/balancer-v2/BalancerV2SwapAdapter.sol index 040aba9..e93702b 100644 --- a/evm/src/balancer-v2/BalancerV2SwapAdapter.sol +++ b/evm/src/balancer-v2/BalancerV2SwapAdapter.sol @@ -106,6 +106,7 @@ contract BalancerV2SwapAdapter is ISwapAdapter { address buyToken, uint256[] memory specifiedAmounts ) external returns (Fraction[] memory calculatedPrices) { + calculatedPrices = new Fraction[](specifiedAmounts.length); for (uint256 i = 0; i < specifiedAmounts.length; i++) { calculatedPrices[i] = priceSingle(poolId, sellToken, buyToken, specifiedAmounts[i]); diff --git a/evm/test/BalancerV2SwapAdapter.t.sol b/evm/test/BalancerV2SwapAdapter.t.sol index a336af8..3be35a5 100644 --- a/evm/test/BalancerV2SwapAdapter.t.sol +++ b/evm/test/BalancerV2SwapAdapter.t.sol @@ -40,8 +40,8 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes { function testPrice() public { uint256[] memory amounts = new uint256[](2); - amounts[0] = amount0; - amounts[1] = amount1; + amounts[0] = 1e18; + amounts[1] = 2e18; Fraction[] memory prices = adapter.price(B_80BAL_20WETH_POOL_ID, BAL, WETH, amounts); From cf7516df254e7498c0e712463fd16b5c3fe8787a Mon Sep 17 00:00:00 2001 From: Louise Poole Date: Mon, 15 Apr 2024 17:42:30 +0100 Subject: [PATCH 4/4] Format --- evm/test/BalancerV2SwapAdapter.t.sol | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/evm/test/BalancerV2SwapAdapter.t.sol b/evm/test/BalancerV2SwapAdapter.t.sol index 3be35a5..20f4f8d 100644 --- a/evm/test/BalancerV2SwapAdapter.t.sol +++ b/evm/test/BalancerV2SwapAdapter.t.sol @@ -43,7 +43,8 @@ contract BalancerV2SwapAdapterTest is Test, ISwapAdapterTypes { amounts[0] = 1e18; amounts[1] = 2e18; - Fraction[] memory prices = adapter.price(B_80BAL_20WETH_POOL_ID, BAL, WETH, amounts); + Fraction[] memory prices = + adapter.price(B_80BAL_20WETH_POOL_ID, BAL, WETH, amounts); for (uint256 i = 0; i < prices.length; i++) { assertGt(prices[i].numerator, 0);