From 3ff3dbdfad4ca82e1e3027964c9f61be290fdcd0 Mon Sep 17 00:00:00 2001 From: r0ohafza Date: Thu, 26 Jan 2023 17:25:54 -0800 Subject: [PATCH] fix: separate rage trade controllers --- src/rage/DepositPeripheryController.sol | 63 +++++++++ ...er.sol => WithdrawPeripheryController.sol} | 65 +++------ src/tests/DNGMXVaultController.t.sol | 132 ++++++++++++++---- 3 files changed, 188 insertions(+), 72 deletions(-) create mode 100644 src/rage/DepositPeripheryController.sol rename src/rage/{DNGMXVaultController.sol => WithdrawPeripheryController.sol} (52%) diff --git a/src/rage/DepositPeripheryController.sol b/src/rage/DepositPeripheryController.sol new file mode 100644 index 0000000..a793dae --- /dev/null +++ b/src/rage/DepositPeripheryController.sol @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.17; + +import {IController} from "../core/IController.sol"; + +/** + @title Rage Trade delta netural gmx vault controller +*/ +contract DepositPeripheryController is IController { + /* -------------------------------------------------------------------------- */ + /* CONSTANT VARIABLES */ + /* -------------------------------------------------------------------------- */ + + /// @notice depositToken(address,address,uint256) + bytes4 constant DEPOSIT = 0xfb0f97a8; + + /// @notice rage trade delta netural jr vault + address[] public vault; + + /* -------------------------------------------------------------------------- */ + /* CONSTRUCTOR */ + /* -------------------------------------------------------------------------- */ + + /** + @param _vault rage trade delta netural jr vault + */ + constructor(address _vault) { + vault.push(_vault); + } + + /* -------------------------------------------------------------------------- */ + /* PUBLIC FUNCTIONS */ + /* -------------------------------------------------------------------------- */ + + /// @inheritdoc IController + function canCall( + address, + bool, + bytes calldata data + ) + external + view + returns ( + bool, + address[] memory, + address[] memory + ) + { + if (bytes4(data) == DEPOSIT) { + (address token, , ) = abi.decode( + data[4:], + (address, address, uint256) + ); + + address[] memory tokensOut = new address[](1); + tokensOut[0] = token; + + return (true, vault, tokensOut); + } + + return (false, new address[](0), new address[](0)); + } +} diff --git a/src/rage/DNGMXVaultController.sol b/src/rage/WithdrawPeripheryController.sol similarity index 52% rename from src/rage/DNGMXVaultController.sol rename to src/rage/WithdrawPeripheryController.sol index 8348a42..50fae34 100644 --- a/src/rage/DNGMXVaultController.sol +++ b/src/rage/WithdrawPeripheryController.sol @@ -6,15 +6,11 @@ import {IController} from "../core/IController.sol"; /** @title Rage Trade delta netural gmx vault controller */ -contract DNGMXVaultController is IController { - +contract WithdrawPeripheryController is IController { /* -------------------------------------------------------------------------- */ /* CONSTANT VARIABLES */ /* -------------------------------------------------------------------------- */ - /// @notice depositToken(address,address,uint256) - bytes4 constant DEPOSIT = 0xfb0f97a8; - /// @notice redeemToken(address,address,uint256) bytes4 constant REDEEM = 0x0d71bdc3; @@ -40,50 +36,33 @@ contract DNGMXVaultController is IController { /* -------------------------------------------------------------------------- */ /// @inheritdoc IController - function canCall(address, bool, bytes calldata data) + function canCall( + address, + bool, + bytes calldata data + ) external view - returns (bool, address[] memory, address[] memory) + returns ( + bool, + address[] memory, + address[] memory + ) { bytes4 sig = bytes4(data); - if (sig == DEPOSIT) return canDeposit(data[4:]); - if (sig == REDEEM || sig == WITHDRAW) return canWithdraw(data[4:]); + if (sig == REDEEM || sig == WITHDRAW) { + (address token, , ) = abi.decode( + data[4:], + (address, address, uint256) + ); - return (false, new address[](0), new address[](0)); - } + address[] memory tokensIn = new address[](1); + tokensIn[0] = token; - /* -------------------------------------------------------------------------- */ - /* INTERNAL FUNCTIONS */ - /* -------------------------------------------------------------------------- */ - - function canDeposit(bytes calldata data) - internal - view - returns (bool, address[] memory, address[] memory) - { - (address token,,) = abi.decode( - data, (address, address, uint256) - ); + return (true, tokensIn, vault); + } - address[] memory tokensOut = new address[](1); - tokensOut[0] = token; - - return (true, vault, tokensOut); - } - - function canWithdraw(bytes calldata data) - internal - view - returns (bool, address[] memory, address[] memory) - { - (address token,,) = abi.decode( - data, (address, address, uint256) - ); - - address[] memory tokensIn = new address[](1); - tokensIn[0] = token; - - return (true, tokensIn, vault); + return (false, new address[](0), new address[](0)); } -} \ No newline at end of file +} diff --git a/src/tests/DNGMXVaultController.t.sol b/src/tests/DNGMXVaultController.t.sol index b4bd7df..0557e78 100644 --- a/src/tests/DNGMXVaultController.t.sol +++ b/src/tests/DNGMXVaultController.t.sol @@ -2,7 +2,8 @@ pragma solidity ^0.8.17; import {TestBase} from "./utils/Base.t.sol"; -import {DNGMXVaultController} from "../rage/DNGMXVaultController.sol"; +import {DepositPeripheryController} from "../rage/DepositPeripheryController.sol"; +import {WithdrawPeripheryController} from "../rage/WithdrawPeripheryController.sol"; interface DNGMXVault { function withdrawToken( @@ -31,19 +32,29 @@ interface DNGMXVault { } contract TestDNGMXVaultController is TestBase { + DepositPeripheryController depositController; + WithdrawPeripheryController withdrawController; - DNGMXVaultController vaultController; - - address target = makeAddr("target"); + address depositPeriphery = makeAddr("depositPeriphery"); + address withdrawPeriphery = makeAddr("withdrawPeriphery"); address vault = makeAddr("vault"); - function setUp() public { - setupControllerFacade(); - vaultController = new DNGMXVaultController(vault); - controllerFacade.updateController(target, vaultController); + function setUp() public override { + super.setUp(); + depositController = new DepositPeripheryController(vault); + withdrawController = new WithdrawPeripheryController(vault); + controllerFacade.updateController(depositPeriphery, depositController); + controllerFacade.updateController( + withdrawPeriphery, + withdrawController + ); } - function testDeposit(address token, address receiver, uint64 amt) public { + function testDeposit( + address token, + address receiver, + uint64 amt + ) public { // Setup controllerFacade.toggleTokenAllowance(vault); @@ -55,8 +66,11 @@ contract TestDNGMXVaultController is TestBase { ); // Test - (bool canCall, address[] memory tokensIn, address[] memory tokensOut) - = controllerFacade.canCall(target, false, data); + ( + bool canCall, + address[] memory tokensIn, + address[] memory tokensOut + ) = controllerFacade.canCall(depositPeriphery, false, data); // Assert assertTrue(canCall); @@ -64,7 +78,11 @@ contract TestDNGMXVaultController is TestBase { assertEq(tokensOut[0], token); } - function testCannotWithdraw(address token, address receiver, uint64 amt) public { + function testCannotWithdraw( + address token, + address receiver, + uint64 amt + ) public { // Setup bytes memory data = abi.encodeWithSelector( DNGMXVault.withdrawToken.selector, @@ -74,14 +92,21 @@ contract TestDNGMXVaultController is TestBase { ); // Test - (bool canCall,,) - = controllerFacade.canCall(target, false, data); + (bool canCall, , ) = controllerFacade.canCall( + withdrawPeriphery, + false, + data + ); // Assert assertTrue(!canCall); } - function testWithdraw(address token, address receiver, uint64 amt) public { + function testWithdraw( + address token, + address receiver, + uint64 amt + ) public { // Setup controllerFacade.toggleTokenAllowance(token); @@ -93,8 +118,11 @@ contract TestDNGMXVaultController is TestBase { ); // Test - (bool canCall, address[] memory tokensIn, address[] memory tokensOut) - = controllerFacade.canCall(target, false, data); + ( + bool canCall, + address[] memory tokensIn, + address[] memory tokensOut + ) = controllerFacade.canCall(withdrawPeriphery, false, data); // Assert assertTrue(canCall); @@ -102,7 +130,11 @@ contract TestDNGMXVaultController is TestBase { assertEq(tokensOut[0], vault); } - function testRedeem(address token, address receiver, uint64 amt) public { + function testRedeem( + address token, + address receiver, + uint64 amt + ) public { // Setup controllerFacade.toggleTokenAllowance(token); @@ -114,8 +146,11 @@ contract TestDNGMXVaultController is TestBase { ); // Test - (bool canCall, address[] memory tokensIn, address[] memory tokensOut) - = controllerFacade.canCall(target, false, data); + ( + bool canCall, + address[] memory tokensIn, + address[] memory tokensOut + ) = controllerFacade.canCall(withdrawPeriphery, false, data); // Assert assertTrue(canCall); @@ -123,8 +158,11 @@ contract TestDNGMXVaultController is TestBase { assertEq(tokensOut[0], vault); } - function testCannotRedeem(address token, address receiver, uint64 amt) public { - + function testCannotRedeem( + address token, + address receiver, + uint64 amt + ) public { bytes memory data = abi.encodeWithSelector( DNGMXVault.redeemToken.selector, token, @@ -133,29 +171,65 @@ contract TestDNGMXVaultController is TestBase { ); // Test - (bool canCall,,) - = controllerFacade.canCall(target, false, data); + (bool canCall, , ) = controllerFacade.canCall( + withdrawPeriphery, + false, + data + ); // Assert assertTrue(!canCall); } - function testCanNotCall(address token, address receiver, uint64 amt) public { + function testCanNotCallDeposit( + address token, + address receiver, + uint64 amt + ) public { // Setup controllerFacade.toggleTokenAllowance(token); bytes memory data = abi.encodeWithSelector( - DNGMXVault.deposit.selector, + DNGMXVault.depositToken.selector, token, receiver, amt ); // Test - (bool canCall,,) - = controllerFacade.canCall(target, false, data); + (bool canCall, , ) = controllerFacade.canCall( + withdrawPeriphery, + false, + data + ); // Assert assertTrue(!canCall); } -} \ No newline at end of file + + function testCanNotCallWithdraw( + address token, + address receiver, + uint64 amt + ) public { + // Setup + controllerFacade.toggleTokenAllowance(token); + + bytes memory data = abi.encodeWithSelector( + DNGMXVault.withdrawToken.selector, + token, + receiver, + amt + ); + + // Test + (bool canCall, , ) = controllerFacade.canCall( + depositPeriphery, + false, + data + ); + + // Assert + assertTrue(!canCall); + } +}