Skip to content

Commit

Permalink
fix: separate rage trade controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
r0ohafza committed Jan 27, 2023
1 parent 3eb07f6 commit 17c8f8e
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 57 deletions.
63 changes: 63 additions & 0 deletions src/rage/DepositPeripheryController.sol
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -40,14 +36,21 @@ 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:]);

return (false, new address[](0), new address[](0));
Expand All @@ -57,33 +60,20 @@ contract DNGMXVaultController is IController {
/* INTERNAL FUNCTIONS */
/* -------------------------------------------------------------------------- */

function canDeposit(bytes calldata data)
internal
view
returns (bool, address[] memory, address[] memory)
{
(address token,,) = abi.decode(
data, (address, address, uint256)
);

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)
returns (
bool,
address[] memory,
address[] memory
)
{
(address token,,) = abi.decode(
data, (address, address, uint256)
);
(address token, , ) = abi.decode(data, (address, address, uint256));

address[] memory tokensIn = new address[](1);
tokensIn[0] = token;

return (true, tokensIn, vault);
}
}
}
132 changes: 103 additions & 29 deletions src/tests/DNGMXVaultController.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);

Expand All @@ -55,16 +66,23 @@ 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);
assertEq(tokensIn[0], vault);
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,
Expand All @@ -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);

Expand All @@ -93,16 +118,23 @@ 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);
assertEq(tokensIn[0], token);
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);

Expand All @@ -114,17 +146,23 @@ 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);
assertEq(tokensIn[0], token);
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,
Expand All @@ -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);
}
}

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);
}
}

0 comments on commit 17c8f8e

Please sign in to comment.