From 7f692a05d6837ecdbb370220dc051a920abb3fcc Mon Sep 17 00:00:00 2001 From: Shahul Hameed <10547529+shahthepro@users.noreply.github.com> Date: Tue, 2 Jul 2024 09:29:04 +0530 Subject: [PATCH] Add `AbstractForkTest` --- test/AbstractForkTest.sol | 18 ++++++++++++++ test/OethARM.t.sol | 23 ++++++++++-------- test/OethLiquidityManager.t.sol | 19 ++++++++------- test/Proxy.t.sol | 42 ++++++++++++++++++++------------- test/UniswapV2.t.sol | 13 +++++----- 5 files changed, 74 insertions(+), 41 deletions(-) create mode 100644 test/AbstractForkTest.sol diff --git a/test/AbstractForkTest.sol b/test/AbstractForkTest.sol new file mode 100644 index 0000000..1745659 --- /dev/null +++ b/test/AbstractForkTest.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.23; + +import {Test} from "forge-std/Test.sol"; + +import {DeployManager} from "script/deploy/DeployManager.sol"; + +abstract contract AbstractForkTest is Test { + DeployManager internal deployManager; + + constructor() { + deployManager = new DeployManager(); + + // Run deployments + deployManager.setUp(); + deployManager.run(); + } +} diff --git a/test/OethARM.t.sol b/test/OethARM.t.sol index 086c594..72e7d0c 100644 --- a/test/OethARM.t.sol +++ b/test/OethARM.t.sol @@ -2,32 +2,30 @@ pragma solidity ^0.8.23; import {Test, console2} from "forge-std/Test.sol"; +import {AbstractForkTest} from "./AbstractForkTest.sol"; import {IERC20} from "contracts/Interfaces.sol"; import {OEthARM} from "contracts/OethARM.sol"; import {Proxy} from "contracts/Proxy.sol"; +import {Addresses} from "contracts/utils/Addresses.sol"; -contract OethARMTest is Test { - IERC20 weth = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); - IERC20 oeth = IERC20(0x856c4Efb76C1D1AE02e20CEB03A2A6a08b0b8dC3); +contract OethARMTest is AbstractForkTest { + IERC20 constant weth = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); + IERC20 constant oeth = IERC20(0x856c4Efb76C1D1AE02e20CEB03A2A6a08b0b8dC3); IERC20 BAD_TOKEN = IERC20(makeAddr("bad token")); - address operator = makeAddr("operator"); + address constant operator = Addresses.STRATEGIST; Proxy proxy; OEthARM oethARM; function setUp() public { - OEthARM implementation = new OEthARM(); - proxy = new Proxy(); - proxy.initialize(address(implementation), address(this), ""); - oethARM = OEthARM(address(proxy)); + proxy = Proxy(deployManager.getDeployment("OETH_ARM")); + oethARM = OEthARM(deployManager.getDeployment("OETH_ARM")); _dealWETH(address(oethARM), 100 ether); _dealOETH(address(oethARM), 100 ether); - // Set operator - oethARM.setOperator(operator); vm.label(address(weth), "WETH"); vm.label(address(oeth), "OETH"); @@ -146,6 +144,8 @@ contract OethARMTest is Test { } function test_collectTokens() external { + vm.startPrank(Addresses.TIMELOCK); + oethARM.transferToken(address(weth), address(this), weth.balanceOf(address(oethARM))); assertGt(weth.balanceOf(address(this)), 50 ether); assertEq(weth.balanceOf(address(oethARM)), 0); @@ -153,6 +153,8 @@ contract OethARMTest is Test { oethARM.transferToken(address(oeth), address(this), oeth.balanceOf(address(oethARM))); assertGt(oeth.balanceOf(address(this)), 50 ether); assertLt(oeth.balanceOf(address(oethARM)), 3); + + vm.stopPrank(); } function _dealOETH(address to, uint256 amount) internal { @@ -167,6 +169,7 @@ contract OethARMTest is Test { /* Operator Tests */ function test_setOperator() external { + vm.prank(Addresses.TIMELOCK); oethARM.setOperator(address(this)); assertEq(oethARM.operator(), address(this)); } diff --git a/test/OethLiquidityManager.t.sol b/test/OethLiquidityManager.t.sol index 039129f..4d7faf9 100644 --- a/test/OethLiquidityManager.t.sol +++ b/test/OethLiquidityManager.t.sol @@ -1,13 +1,15 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.23; -import {Test, console2} from "forge-std/Test.sol"; +import {console2} from "forge-std/Test.sol"; +import {AbstractForkTest} from "./AbstractForkTest.sol"; import {IERC20, IOethARM, IOETHVault} from "contracts/Interfaces.sol"; import {OEthARM} from "contracts/OethARM.sol"; import {Proxy} from "contracts/Proxy.sol"; +import {Addresses} from "contracts/utils/Addresses.sol"; -contract OethLiquidityManagerTest is Test { +contract OethLiquidityManagerTest is AbstractForkTest { address constant RANDOM_ADDRESS = 0xfEEDBeef00000000000000000000000000000000; address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; @@ -20,16 +22,14 @@ contract OethLiquidityManagerTest is Test { Proxy proxy; OEthARM oethARM; + address constant operator = Addresses.STRATEGIST; + function setUp() public { vm.label(WETH, "WETH"); vm.label(OETH, "OETH"); - OEthARM implementation = new OEthARM(); - proxy = new Proxy(); - proxy.initialize(address(implementation), address(this), ""); - oethARM = OEthARM(address(proxy)); - - oethARM.setOperator(address(this)); + proxy = Proxy(deployManager.getDeployment("OETH_ARM")); + oethARM = OEthARM(deployManager.getDeployment("OETH_ARM")); } function test_withdrawal() external { @@ -38,6 +38,7 @@ contract OethLiquidityManagerTest is Test { // put some WETH in the vault _dealWEth(address(vault), 10 ether); + vm.startPrank(operator); (uint256 requestId, uint256 queued) = oethARM.requestWithdrawal(1 ether); // Snapshot WETH balance @@ -50,6 +51,8 @@ contract OethLiquidityManagerTest is Test { // Ensure the balance increased. assertGt(weth.balanceOf(address(oethARM)), startBalance, "Withdrawal did not increase WETH balance"); + + vm.stopPrank(); } /* diff --git a/test/Proxy.t.sol b/test/Proxy.t.sol index 0b1148c..9f8cff7 100644 --- a/test/Proxy.t.sol +++ b/test/Proxy.t.sol @@ -2,37 +2,42 @@ pragma solidity ^0.8.23; import {Vm} from "forge-std/Vm.sol"; -import {Test, console2} from "forge-std/Test.sol"; +import {console2} from "forge-std/Test.sol"; +import {AbstractForkTest} from "./AbstractForkTest.sol"; import {OEthARM} from "contracts/OethARM.sol"; import {Proxy} from "contracts/Proxy.sol"; +import {Addresses} from "contracts/utils/Addresses.sol"; -contract ProxyTest is Test { +contract ProxyTest is AbstractForkTest { address constant RANDOM_ADDRESS = 0xfEEDBeef00000000000000000000000000000000; Proxy proxy; OEthARM oethARM; - address weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; - address oeth = 0x856c4Efb76C1D1AE02e20CEB03A2A6a08b0b8dC3; + address constant weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; + address constant oeth = 0x856c4Efb76C1D1AE02e20CEB03A2A6a08b0b8dC3; + + address constant owner = Addresses.TIMELOCK; + address constant operator = Addresses.STRATEGIST; function setUp() public { - // Deploy a OSwap contract implementation and a proxy. - OEthARM implementation = new OEthARM(); - proxy = new Proxy(); - proxy.initialize(address(implementation), address(this), ""); + vm.label(weth, "WETH"); + vm.label(oeth, "OETH"); - oethARM = OEthARM(address(proxy)); + proxy = Proxy(deployManager.getDeployment("OETH_ARM")); + oethARM = OEthARM(deployManager.getDeployment("OETH_ARM")); } function test_upgrade() external { OEthARM newImplementation1 = new OEthARM(); + vm.prank(owner); proxy.upgradeTo(address(newImplementation1)); assertEq(proxy.implementation(), address(newImplementation1)); // Ensure ownership was preserved. - assertEq(proxy.owner(), address(this)); - assertEq(oethARM.owner(), address(this)); + assertEq(proxy.owner(), owner); + assertEq(oethARM.owner(), owner); // Ensure the storage was preserved through the upgrade. assertEq(address(oethARM.token0()), oeth); @@ -41,24 +46,27 @@ contract ProxyTest is Test { function test_upgradeAndCall() external { OEthARM newImplementation2 = new OEthARM(); - bytes memory data = abi.encodeWithSignature("setOperator(address)", address(this)); + bytes memory data = abi.encodeWithSignature("setOperator(address)", address(0x123)); + + vm.prank(owner); proxy.upgradeToAndCall(address(newImplementation2), data); assertEq(proxy.implementation(), address(newImplementation2)); // Ensure ownership was preserved. - assertEq(proxy.owner(), address(this)); - assertEq(oethARM.owner(), address(this)); + assertEq(proxy.owner(), owner); + assertEq(oethARM.owner(), owner); // Ensure the post upgrade code was run - assertEq(oethARM.operator(), address(this)); + assertEq(oethARM.operator(), address(0x123)); } function test_setOwner() external { - assertEq(proxy.owner(), address(this)); - assertEq(oethARM.owner(), address(this)); + assertEq(proxy.owner(), owner); + assertEq(oethARM.owner(), owner); // Update the owner. address newOwner = RANDOM_ADDRESS; + vm.prank(owner); proxy.setOwner(newOwner); assertEq(proxy.owner(), newOwner); assertEq(oethARM.owner(), newOwner); diff --git a/test/UniswapV2.t.sol b/test/UniswapV2.t.sol index e50f8c1..153a906 100644 --- a/test/UniswapV2.t.sol +++ b/test/UniswapV2.t.sol @@ -2,25 +2,26 @@ pragma solidity ^0.8.23; import {Test, console2} from "forge-std/Test.sol"; +import {AbstractForkTest} from "./AbstractForkTest.sol"; import {IERC20} from "contracts/Interfaces.sol"; import {OEthARM} from "contracts/OethARM.sol"; import {Proxy} from "contracts/Proxy.sol"; +import {Addresses} from "contracts/utils/Addresses.sol"; // Tests for the Uniswap V2 Router compatible interface of OSwap. -contract UniswapV2Test is Test { +contract UniswapV2Test is AbstractForkTest { IERC20 weth = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); IERC20 oeth = IERC20(0x856c4Efb76C1D1AE02e20CEB03A2A6a08b0b8dC3); + address constant operator = Addresses.STRATEGIST; + Proxy proxy; OEthARM oethARM; function setUp() public { - OEthARM implementation = new OEthARM(); - proxy = new Proxy(); - proxy.initialize(address(implementation), address(this), ""); - - oethARM = OEthARM(address(proxy)); + proxy = Proxy(deployManager.getDeployment("OETH_ARM")); + oethARM = OEthARM(deployManager.getDeployment("OETH_ARM")); // Add liquidity to the test contract. _dealWETH(address(this), 120 ether);