Skip to content

Commit

Permalink
Adds GMX long position test (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
obatirou authored Sep 22, 2022
1 parent 37de777 commit 3a180cd
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export ALCHEMY_KEY=XXXXX
export ALCHEMY_KEY_ARBITRUM=YYYYY
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
run: |
cat << EOF >> .env
export ALCHEMY_KEY=${{ secrets.ALCHEMY_KEY }}
export ALCHEMY_KEY_ARBITRUM=${{ secrets.ALCHEMY_KEY_ARBITRUM }}
EOF
- name: Source env file
Expand Down
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ remappings = [
]
[profile.default.rpc_endpoints]
eth = 'https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_KEY}'
arbi = 'https://arb-mainnet.g.alchemy.com/v2/${ALCHEMY_KEY_ARBITRUM}'
2 changes: 1 addition & 1 deletion test/Call.fork.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import "./calls/SupplyBorrowMorpho.sol";
import "./calls/DepositBorrowAave.sol";

import "./Utils.sol";
import "./Constants.sol";
import "./ConstantsEthereum.sol";

import "./ILens.sol";

Expand Down
7 changes: 7 additions & 0 deletions test/ConstantsArbitrum.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.15;

address constant WETH = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1;
address constant DAI = 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1;
address constant USDC = 0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8;
address constant ZERO_ADDRESS = address(0);
File renamed without changes.
109 changes: 109 additions & 0 deletions test/GMX.forkArbi.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "foundry-huff/HuffDeployer.sol";
import "forge-std/Test.sol";
import "./Utils.sol";
import "./calls/CallNvm.sol";
import "./calls/GMXLong.sol";
import "./calls/IGMX.sol";
import "./ConstantsArbitrum.sol";

contract GMXTest is Test {
address nvm;
address doubleSwapHuff;
address owner;
uint256 balance = 1000 * 1e6;
CallNvm callNvm;
GMXLong gmxLong;
bytes gmxLongNvmBytecode;

// ===== Set up =====
function setUp() public {
vm.createSelectFork(vm.rpcUrl("arbi"));
owner = address(this);
nvm = HuffDeployer.deploy("NVM");
callNvm = new CallNvm();
gmxLong = new GMXLong();
gmxLongNvmBytecode = type(GMXLong).runtimeCode;
}

receive() external payable {}

function testGMXLongNative() public {
// PositionRouter
IGMXPositionRouter positionRouter = IGMXPositionRouter(
0x3D6bA331e3D9702C5e8A8d254e5d8a285F223aba
);
// Admin of the Position Router
address positionRouterAdmin = address(
0x5F799f365Fa8A2B60ac0429C48B153cA5a6f0Cf8
);
// Deal USDC and ETH to contract that will open a long position
deal(USDC, address(gmxLong), 1000000000 * 10**6);
deal(address(gmxLong), 1000000000 * 10**18);
// opens long position
gmxLong.gmxLong();
// Keeper that will execute the requests
address keeper = address(123);
// balance before for the keeper
uint256 balanceBefore = keeper.balance;
// Set keeper address as a keeper for the positionRouter
// Only admin fuction
changePrank(positionRouterAdmin);
positionRouter.setPositionKeeper(keeper, true);
// Execute transaction
// The transaction is identified by a requestKey
changePrank(keeper);
bytes32 key = positionRouter.getRequestKey(
address(gmxLong),
uint256(1)
);
positionRouter.executeIncreasePosition(key, payable(keeper));
// Balance of the keeper after
uint256 balanceAfter = keeper.balance;
// Verify keeper received fees
assertEq(balanceAfter > balanceBefore, true);
}

function testGMXLongNvm() public {
// PositionRouter
IGMXPositionRouter positionRouter = IGMXPositionRouter(
0x3D6bA331e3D9702C5e8A8d254e5d8a285F223aba
);
// Admin of the Position Router
address positionRouterAdmin = address(
0x5F799f365Fa8A2B60ac0429C48B153cA5a6f0Cf8
);
// Deal USDC and ETH to contract that will open a long position
deal(USDC, address(callNvm), 1000000000 * 10**6);
deal(address(callNvm), 1000000000 * 10**18);
// replace "gmxLong" selector and bypass calldata size check
bytes memory finalBytecode = Utils
.replaceSelectorBypassCalldataSizeCheck(
gmxLongNvmBytecode,
hex"9507c78f"
);
// CallNvm
callNvm.callNvm(nvm, finalBytecode);
// Keeper that will execute the requests
address keeper = address(123);
// balance before for the keeper
uint256 balanceBefore = keeper.balance;
// Set keeper address as a keeper for the positionRouter
// Only admin fuction
changePrank(positionRouterAdmin);
positionRouter.setPositionKeeper(keeper, true);
// Execute transaction
// The transaction is identified by a requestKey
changePrank(keeper);
bytes32 key = positionRouter.getRequestKey(
address(callNvm),
uint256(1)
);
positionRouter.executeIncreasePosition(key, payable(keeper));
// Balance of the keeper after
uint256 balanceAfter = keeper.balance;
// Verify keeper received fees
assertEq(balanceAfter > balanceBefore, true);
}
}
2 changes: 1 addition & 1 deletion test/calls/DepositBorrowAave.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.15;
import "./IERC20.sol";
import "./ILendingPool.sol";
import "../Constants.sol";
import "../ConstantsEthereum.sol";

contract DepositBorrowAave {
ILendingPool private constant lendingPool =
Expand Down
37 changes: 37 additions & 0 deletions test/calls/GMXLong.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.15;
import "./IERC20.sol";

import "./IGMX.sol";

import "./../ConstantsArbitrum.sol";

contract GMXLong {

uint256 private constant amountUSDC = 1000 * 10**6;
IGMXRouter private constant GMXRouter = IGMXRouter(0xaBBc5F99639c9B6bCb58544ddf04EFA6802F4064);
IGMXPositionRouter private constant positionRouter = IGMXPositionRouter(0x3D6bA331e3D9702C5e8A8d254e5d8a285F223aba);

constructor(){}

function gmxLong() public {
IERC20(USDC).approve(address(GMXRouter), type(uint256).max);
IERC20(USDC).approve(address(positionRouter), type(uint256).max);
GMXRouter.approvePlugin(address(positionRouter));
// USD amounts are multiplied by (10 ** 30)
address[] memory path = new address[](2);
path[0] = USDC;
path[1] = WETH;
positionRouter.createIncreasePosition{value: 3000_000_000_000_000 }(
path, // [tokenIn, collateralToken] _path
WETH, // _indexToken (token we want to long)
100 * 10**6, // _amountIn
0, // minOut
600 * 10**30, // _sizeDelta the USD value of the change in position size
true, // _isLong
3000 * 10**30, // _acceptablePrice
3000_000_000_000_000, // _executionFee
bytes32(0)); // _referralCode
}
receive() external payable {}
}
24 changes: 24 additions & 0 deletions test/calls/IGMX.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.15;

interface IGMXPositionRouter {
function createIncreasePosition(
address[] memory _path,
address _indexToken,
uint256 _amountIn,
uint256 _minOut,
uint256 _sizeDelta,
bool _isLong,
uint256 _acceptablePrice,
uint256 _executionFee,
bytes32 _referralCode
) external payable;

function executeIncreasePosition(bytes32 _key, address payable _executionFeeReceiver) external returns (bool);
function getRequestKey(address _account, uint256 _index) external pure returns (bytes32);
function setPositionKeeper(address _account, bool _isActive) external;
}

interface IGMXRouter {
function approvePlugin(address _plugin) external;
}
2 changes: 1 addition & 1 deletion test/calls/SupplyBorrowMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.15;

import "./IERC20.sol";
import "./IMorpho.sol";
import "../Constants.sol";
import "../ConstantsEthereum.sol";

contract SupplyBorrowMorpho {
IMorpho private constant morpho =
Expand Down

0 comments on commit 3a180cd

Please sign in to comment.