From 41670503ca336dbf5afc36fa321fd74ceb24bbbb Mon Sep 17 00:00:00 2001 From: clemlak Date: Fri, 8 Dec 2023 12:26:26 +0400 Subject: [PATCH] test: move strategy tests into dedicated folders --- test/Setup.sol | 6 +- test/TestG3MCreatePool.t.sol | 185 ------------------ test/strategies/G3M/TestG3MAllocate.t.sol | 78 ++++++++ test/strategies/G3M/TestG3MCreatePool.t.sol | 63 ++++++ test/strategies/G3M/TestG3MDeallocate.t.sol | 71 +++++++ test/strategies/G3M/TestG3MLib.t.sol | 26 +++ test/strategies/G3M/TestG3MSwap.t.sol | 83 ++++++++ .../NormalConfiguration.sol | 0 .../TestNormalStrategy.t.sol | 2 +- 9 files changed, 326 insertions(+), 188 deletions(-) delete mode 100644 test/TestG3MCreatePool.t.sol create mode 100644 test/strategies/G3M/TestG3MAllocate.t.sol create mode 100644 test/strategies/G3M/TestG3MCreatePool.t.sol create mode 100644 test/strategies/G3M/TestG3MDeallocate.t.sol create mode 100644 test/strategies/G3M/TestG3MLib.t.sol create mode 100644 test/strategies/G3M/TestG3MSwap.t.sol rename test/strategies/{ => NormalStrategy}/NormalConfiguration.sol (100%) rename test/strategies/{ => NormalStrategy}/TestNormalStrategy.t.sol (96%) diff --git a/test/Setup.sol b/test/Setup.sol index 94d7202b..a4e46344 100644 --- a/test/Setup.sol +++ b/test/Setup.sol @@ -6,7 +6,7 @@ import "forge-std/Test.sol"; // Global test configuration for Portfolio import "./Configuration.sol"; -import "./strategies/NormalConfiguration.sol"; +import "./strategies/NormalStrategy/NormalConfiguration.sol"; // Test helper types import { deploy as deployCoin, Coin } from "./utils/CoinType.sol"; @@ -120,7 +120,9 @@ contract Setup is ISetup, Test, ERC1155TokenReceiver { vm.label(_subjects.positionRenderer, "position-renderer"); _subjects.portfolio = address( - new Portfolio(_subjects.weth, _subjects.registry, _subjects.positionRenderer) + new Portfolio( + _subjects.weth, _subjects.registry, _subjects.positionRenderer + ) ); vm.label(_subjects.portfolio, "portfolio"); diff --git a/test/TestG3MCreatePool.t.sol b/test/TestG3MCreatePool.t.sol deleted file mode 100644 index 3bd47438..00000000 --- a/test/TestG3MCreatePool.t.sol +++ /dev/null @@ -1,185 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-only -pragma solidity ^0.8.4; - -import "./Setup.sol"; - -contract TestG3MCreatePool is Setup { - using FixedPointMathLib for *; - - function test_SF_computeISF() public view { - uint256 x = 0.05 ether; - uint256 y = G3MStrategyLib.computeISFunction(x); - console.log(y); - } - - function test_SF_computeSF() public view { - uint256 w0 = 0.05 ether; - uint256 w1 = 0.9 ether; - uint256 t = 0.5 ether; - uint256 fw0 = G3MStrategyLib.computeISFunction(w0); - console.log(fw0); - uint256 fw1 = G3MStrategyLib.computeISFunction(w1); - console.log(fw1); - uint256 weightX = G3MStrategyLib.computeSFunction(t, fw1 - fw0, fw0); - console.log(weightX); - } - - function deployTokens( - IPortfolio subject, - bool createPair - ) internal returns (MockERC20 token0, MockERC20 token1) { - token0 = new MockERC20("tkn", "tkn", 18); - token1 = new MockERC20("tkn", "tkn", 18); - token0.mint(address(this), type(uint256).max); - token0.approve(address(subject), type(uint256).max); - token1.mint(address(this), type(uint256).max); - token1.approve(address(subject), type(uint256).max); - - if (createPair) { - subject.createPair(address(token0), address(token1)); - } - } - - function test_G3M_createPool() public { - deployTokens(subject(), true); - - address controller = address(0); - uint256 reserveX = 18_600 ether; - uint256 startWeightX = 0.75 ether; - uint256 endWeightX = 0.75 ether; - uint256 startUpdate = block.timestamp; - uint256 endUpdate = block.timestamp; - uint256 initialPrice = 1937.5 ether; - - (bytes memory strategyData, uint256 initialX, uint256 initialY) = - G3MStrategy(g3mStrategy()).getStrategyData( - abi.encode( - controller, - reserveX, - startWeightX, - endWeightX, - startUpdate, - endUpdate, - initialPrice - ) - ); - - uint64 poolId = subject().createPool( - 0, - initialX, - initialY, - 100, - 0, - address(0), - g3mStrategy(), - strategyData - ); - - assertEq(subject().getSpotPrice(poolId), initialPrice); - assertEq(subject().getStrategy(poolId), g3mStrategy()); - } - - function test_G3M_allocate() public { - deployTokens(subject(), true); - - address controller = address(0); - uint256 reserveX = 18_600 ether; - uint256 startWeightX = 0.75 ether; - uint256 endWeightX = 0.75 ether; - uint256 startUpdate = block.timestamp; - uint256 endUpdate = block.timestamp; - uint256 initialPrice = 1937.5 ether; - - (bytes memory strategyData, uint256 initialX, uint256 initialY) = - G3MStrategy(g3mStrategy()).getStrategyData( - abi.encode( - controller, - reserveX, - startWeightX, - endWeightX, - startUpdate, - endUpdate, - initialPrice - ) - ); - - uint64 poolId = subject().createPool( - 0, - initialX, - initialY, - 100, - 0, - address(0), - g3mStrategy(), - strategyData - ); - - subject().allocate( - false, - address(this), - poolId, - 100 ether, - type(uint128).max, - type(uint128).max - ); - } - - function test_G3M_swap() public { - deployTokens(subject(), true); - - address controller = address(0); - uint256 reserveX = 18_600 ether; - uint256 startWeightX = 0.75 ether; - uint256 endWeightX = 0.75 ether; - uint256 startUpdate = block.timestamp; - uint256 endUpdate = block.timestamp; - uint256 initialPrice = 1937.5 ether; - - (bytes memory strategyData, uint256 initialX, uint256 initialY) = - G3MStrategy(g3mStrategy()).getStrategyData( - abi.encode( - controller, - reserveX, - startWeightX, - endWeightX, - startUpdate, - endUpdate, - initialPrice - ) - ); - - uint64 poolId = subject().createPool( - 0, - initialX, - initialY, - 100, - 0, - address(0), - g3mStrategy(), - strategyData - ); - - subject().allocate( - false, - address(this), - poolId, - 100 ether, - type(uint128).max, - type(uint128).max - ); - - Order memory order = Order({ - input: 500 ether, - output: uint128( - G3MStrategy(g3mStrategy()).getAmountOut( - poolId, true, 500 ether, address(this) - ) - ), - useMax: false, - poolId: poolId, - sellAsset: true - }); - - subject().swap(order); - } -} diff --git a/test/strategies/G3M/TestG3MAllocate.t.sol b/test/strategies/G3M/TestG3MAllocate.t.sol new file mode 100644 index 00000000..7d36ea23 --- /dev/null +++ b/test/strategies/G3M/TestG3MAllocate.t.sol @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity ^0.8.4; + +import "../../Setup.sol"; + +contract TestG3MCreatePool is Setup { + using FixedPointMathLib for *; + + address controller = address(0); + uint256 reserveX = 18_600 ether; + uint256 startWeightX = 0.75 ether; + uint256 endWeightX = 0.75 ether; + uint256 startUpdate = block.timestamp; + uint256 endUpdate = block.timestamp; + uint256 initialPrice = 1937.5 ether; + + function deployTokens( + IPortfolio subject, + bool createPair + ) internal returns (MockERC20 token0, MockERC20 token1) { + token0 = new MockERC20("tkn", "tkn", 18); + token1 = new MockERC20("tkn", "tkn", 18); + token0.mint(address(this), type(uint256).max); + token0.approve(address(subject), type(uint256).max); + token1.mint(address(this), type(uint256).max); + token1.approve(address(subject), type(uint256).max); + + if (createPair) { + subject.createPair(address(token0), address(token1)); + } + } + + function test_G3M_allocate() public { + (MockERC20 token0, MockERC20 token1) = deployTokens(subject(), true); + + (bytes memory strategyData, uint256 initialX, uint256 initialY) = + G3MStrategy(g3mStrategy()).getStrategyData( + abi.encode( + controller, + reserveX, + startWeightX, + endWeightX, + startUpdate, + endUpdate, + initialPrice + ) + ); + + uint64 poolId = subject().createPool( + 0, + initialX, + initialY, + 100, + 0, + address(0), + g3mStrategy(), + strategyData + ); + + uint128 liquidity = 100 ether; + (uint256 deltaAsset, uint256 deltaQuote) = subject().allocate( + false, + address(this), + poolId, + liquidity, + type(uint128).max, + type(uint128).max + ); + + assertEq( + ERC1155(address(subject())).balanceOf(address(this), poolId), + liquidity - BURNED_LIQUIDITY + ); + + assertEq(token0.balanceOf(address(subject())), deltaAsset); + assertEq(token1.balanceOf(address(subject())), deltaQuote); + } +} diff --git a/test/strategies/G3M/TestG3MCreatePool.t.sol b/test/strategies/G3M/TestG3MCreatePool.t.sol new file mode 100644 index 00000000..bc76a787 --- /dev/null +++ b/test/strategies/G3M/TestG3MCreatePool.t.sol @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity ^0.8.4; + +import "../../Setup.sol"; + +contract TestG3MCreatePool is Setup { + using FixedPointMathLib for *; + + function deployTokens( + IPortfolio subject, + bool createPair + ) internal returns (MockERC20 token0, MockERC20 token1) { + token0 = new MockERC20("tkn", "tkn", 18); + token1 = new MockERC20("tkn", "tkn", 18); + token0.mint(address(this), type(uint256).max); + token0.approve(address(subject), type(uint256).max); + token1.mint(address(this), type(uint256).max); + token1.approve(address(subject), type(uint256).max); + + if (createPair) { + subject.createPair(address(token0), address(token1)); + } + } + + function test_G3M_createPool() public { + deployTokens(subject(), true); + + address controller = address(0); + uint256 reserveX = 18_600 ether; + uint256 startWeightX = 0.75 ether; + uint256 endWeightX = 0.75 ether; + uint256 startUpdate = block.timestamp; + uint256 endUpdate = block.timestamp; + uint256 initialPrice = 1937.5 ether; + + (bytes memory strategyData, uint256 initialX, uint256 initialY) = + G3MStrategy(g3mStrategy()).getStrategyData( + abi.encode( + controller, + reserveX, + startWeightX, + endWeightX, + startUpdate, + endUpdate, + initialPrice + ) + ); + + uint64 poolId = subject().createPool( + 0, + initialX, + initialY, + 100, + 0, + address(0), + g3mStrategy(), + strategyData + ); + + assertEq(subject().getSpotPrice(poolId), initialPrice); + assertEq(subject().getStrategy(poolId), g3mStrategy()); + } +} diff --git a/test/strategies/G3M/TestG3MDeallocate.t.sol b/test/strategies/G3M/TestG3MDeallocate.t.sol new file mode 100644 index 00000000..e0a6074a --- /dev/null +++ b/test/strategies/G3M/TestG3MDeallocate.t.sol @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity ^0.8.4; + +import "../../Setup.sol"; + +contract TestG3MCreatePool is Setup { + using FixedPointMathLib for *; + + function deployTokens( + IPortfolio subject, + bool createPair + ) internal returns (MockERC20 token0, MockERC20 token1) { + token0 = new MockERC20("tkn", "tkn", 18); + token1 = new MockERC20("tkn", "tkn", 18); + token0.mint(address(this), type(uint256).max); + token0.approve(address(subject), type(uint256).max); + token1.mint(address(this), type(uint256).max); + token1.approve(address(subject), type(uint256).max); + + if (createPair) { + subject.createPair(address(token0), address(token1)); + } + } + + function test_G3M_deallocate() public { + deployTokens(subject(), true); + + address controller = address(0); + uint256 reserveX = 18_600 ether; + uint256 startWeightX = 0.75 ether; + uint256 endWeightX = 0.75 ether; + uint256 startUpdate = block.timestamp; + uint256 endUpdate = block.timestamp; + uint256 initialPrice = 1937.5 ether; + + (bytes memory strategyData, uint256 initialX, uint256 initialY) = + G3MStrategy(g3mStrategy()).getStrategyData( + abi.encode( + controller, + reserveX, + startWeightX, + endWeightX, + startUpdate, + endUpdate, + initialPrice + ) + ); + + uint64 poolId = subject().createPool( + 0, + initialX, + initialY, + 100, + 0, + address(0), + g3mStrategy(), + strategyData + ); + + subject().allocate( + false, + address(this), + poolId, + 100 ether, + type(uint128).max, + type(uint128).max + ); + + // TODO: Deallocate + } +} diff --git a/test/strategies/G3M/TestG3MLib.t.sol b/test/strategies/G3M/TestG3MLib.t.sol new file mode 100644 index 00000000..88543e3a --- /dev/null +++ b/test/strategies/G3M/TestG3MLib.t.sol @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity ^0.8.4; + +import "../../Setup.sol"; + +contract TestG3MLib is Setup { + using FixedPointMathLib for *; + + function test_SF_computeISF() public view { + uint256 x = 0.05 ether; + uint256 y = G3MStrategyLib.computeISFunction(x); + console.log(y); + } + + function test_SF_computeSF() public view { + uint256 w0 = 0.05 ether; + uint256 w1 = 0.9 ether; + uint256 t = 0.5 ether; + uint256 fw0 = G3MStrategyLib.computeISFunction(w0); + console.log(fw0); + uint256 fw1 = G3MStrategyLib.computeISFunction(w1); + console.log(fw1); + uint256 weightX = G3MStrategyLib.computeSFunction(t, fw1 - fw0, fw0); + console.log(weightX); + } +} diff --git a/test/strategies/G3M/TestG3MSwap.t.sol b/test/strategies/G3M/TestG3MSwap.t.sol new file mode 100644 index 00000000..b756910b --- /dev/null +++ b/test/strategies/G3M/TestG3MSwap.t.sol @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity ^0.8.4; + +import "../../Setup.sol"; + +contract TestG3MCreatePool is Setup { + using FixedPointMathLib for *; + + function deployTokens( + IPortfolio subject, + bool createPair + ) internal returns (MockERC20 token0, MockERC20 token1) { + token0 = new MockERC20("tkn", "tkn", 18); + token1 = new MockERC20("tkn", "tkn", 18); + token0.mint(address(this), type(uint256).max); + token0.approve(address(subject), type(uint256).max); + token1.mint(address(this), type(uint256).max); + token1.approve(address(subject), type(uint256).max); + + if (createPair) { + subject.createPair(address(token0), address(token1)); + } + } + + function test_G3M_swap() public { + deployTokens(subject(), true); + + address controller = address(0); + uint256 reserveX = 18_600 ether; + uint256 startWeightX = 0.75 ether; + uint256 endWeightX = 0.75 ether; + uint256 startUpdate = block.timestamp; + uint256 endUpdate = block.timestamp; + uint256 initialPrice = 1937.5 ether; + + (bytes memory strategyData, uint256 initialX, uint256 initialY) = + G3MStrategy(g3mStrategy()).getStrategyData( + abi.encode( + controller, + reserveX, + startWeightX, + endWeightX, + startUpdate, + endUpdate, + initialPrice + ) + ); + + uint64 poolId = subject().createPool( + 0, + initialX, + initialY, + 100, + 0, + address(0), + g3mStrategy(), + strategyData + ); + + subject().allocate( + false, + address(this), + poolId, + 100 ether, + type(uint128).max, + type(uint128).max + ); + + Order memory order = Order({ + input: 500 ether, + output: uint128( + G3MStrategy(g3mStrategy()).getAmountOut( + poolId, true, 500 ether, address(this) + ) + ), + useMax: false, + poolId: poolId, + sellAsset: true + }); + + subject().swap(order); + } +} diff --git a/test/strategies/NormalConfiguration.sol b/test/strategies/NormalStrategy/NormalConfiguration.sol similarity index 100% rename from test/strategies/NormalConfiguration.sol rename to test/strategies/NormalStrategy/NormalConfiguration.sol diff --git a/test/strategies/TestNormalStrategy.t.sol b/test/strategies/NormalStrategy/TestNormalStrategy.t.sol similarity index 96% rename from test/strategies/TestNormalStrategy.t.sol rename to test/strategies/NormalStrategy/TestNormalStrategy.t.sol index 36aa3abb..a9bfc2d6 100644 --- a/test/strategies/TestNormalStrategy.t.sol +++ b/test/strategies/NormalStrategy/TestNormalStrategy.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-only pragma solidity ^0.8.4; -import "../Setup.sol"; +import "../../Setup.sol"; contract TestNormalStrategy is Setup { // Needs default config so a pool is deployed so the default strategy is returned by strategy()