Skip to content

Commit

Permalink
use real pool for fork tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ewansheldon committed Oct 21, 2024
1 parent 616b6b3 commit 5f5ea08
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 92 deletions.
11 changes: 10 additions & 1 deletion test/foundry/fixtures/ForkConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import {ISwapRouter} from "src/interfaces/ISwapRouter.sol";
import {IUniProxy} from "src/interfaces/IUniProxy.sol";
import {IClearing} from "src/interfaces/IClearing.sol";
import {IHypervisor} from "src/interfaces/IHypervisor.sol";
import {IAdminUSDs} from "src/test_utils/interfaces/IAdminUSDs.sol";

// Fork constants
string constant ENV_RPC_URL = "ARBITRUM_RPC_URL";
string constant DEFAULT_RPC_URL = "https://rpc.ankr.com/arbitrum";

// Token constants
address constant USDS_ADDRESS = 0x2Ea0bE86990E8Dac0D09e4316Bb92086F304622d;
address constant USDC_ADDRESS = 0xaf88d065e77c8cC2239327C5EDb3A432268e5831;
address constant WETH_ADDRESS = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1;
address constant WBTC_ADDRESS = 0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f;
Expand All @@ -24,6 +26,8 @@ address constant PAXG_ADDRESS = 0xfEb4DfC8C4Cf7Ed305bb08065D08eC6ee6728429;
address constant RDNT_ADDRESS = 0x3082CC23568eA640225c2467653dB90e9250AaA0;
address constant SUSHI_ADDRESS = 0xd4d42F0b6DEF4CE0383636770eF773390d85c61A;

IAdminUSDs constant ADMIN_USDS = IAdminUSDs(USDS_ADDRESS);
ERC20 constant USDS = ERC20(USDS_ADDRESS);
ERC20 constant USDC = ERC20(USDC_ADDRESS);
ERC20 constant WETH = ERC20(WETH_ADDRESS);
ERC20 constant WBTC = ERC20(WBTC_ADDRESS);
Expand All @@ -50,6 +54,7 @@ address constant WBTC_WHALE = 0x078f358208685046a11C85e8ad32895DED33A249;
address constant LINK_WHALE = 0x191c10Aa4AF7C30e871E70C95dB0E4eb77237530;
address constant ARB_WHALE = 0xF3FC178157fb3c87548bAA86F9d24BA38E649B58;
address constant GMX_WHALE = 0x908C4D94D34924765f1eDc22A1DD098397c59dD4;
address constant RDNT_WHALE = 0xF977814e90dA44bFA03b6295A0616a897441aceC;
address constant PAXG_WHALE = 0x694321B2f596C0610c03DEac16C7341933Aaa952;
// address constant RDNT_WHALE = ?;
address constant SUSHI_WHALE = 0x3Ef3D8bA38EBe18DB133cEc108f4D14CE00Dd9Ae;
Expand Down Expand Up @@ -80,6 +85,8 @@ address constant UNISWAP_ROUTER_ADDRESS = 0xE592427A0AEce92De3Edee1F18E0157C0586

ISwapRouter constant UNISWAP_ROUTER = ISwapRouter(UNISWAP_ROUTER_ADDRESS);

address constant USD_POOL_ADDRESS = 0x8DEF4Db6697F4885bA4a3f75e9AdB3cEFCca6D6E;

// Gamma constants
address constant UNI_PROXY_ADDRESS = 0x82FcEB07a4D01051519663f6c1c919aF21C27845;
address constant CLEARING_ADDRESS = 0x80a44ce970D9380bDA7677916B860f37b4ba8Ce2;
Expand All @@ -88,12 +95,14 @@ IUniProxy constant UNI_PROXY = IUniProxy(UNI_PROXY_ADDRESS);
IClearing constant CLEARING = IClearing(CLEARING_ADDRESS);

// Hypervisor constants
address constant USDS_HYPERVISOR_ADDRESS = 0x547A116a2622876cE1C8d19d41c683C8f7BeC5c0;
address constant WBTC_HYPERVISOR_ADDRESS = 0x52ee1FFBA696c5E9b0Bc177A9f8a3098420EA691;
address constant LINK_HYPERVISOR_ADDRESS = 0xfA392dbefd2d5ec891eF5aEB87397A89843a8260;
address constant ARB_HYPERVISOR_ADDRESS = 0x330DFC5Bc1a63A1dCf1cD5bc9aD3D5e5E61Bcb6C;
address constant ARB_HYPERVISOR_ADDRESS = 0x6B7635b7d2E85188dB41C3c05B1efa87B143fcE8;
address constant GMX_HYPERVISOR_ADDRESS = 0xF08BDBC590C59cb7B27A8D224E419ef058952b5f;
address constant RDNT_HYPERVISOR_ADDRESS = 0x2BCBDD577616357464CFe307Bc67F9e820A66e80;

IHypervisor constant USDS_HYPERVISOR = IHypervisor(USDS_HYPERVISOR_ADDRESS);
IHypervisor constant WBTC_HYPERVISOR = IHypervisor(WBTC_HYPERVISOR_ADDRESS);
IHypervisor constant LINK_HYPERVISOR = IHypervisor(LINK_HYPERVISOR_ADDRESS);
IHypervisor constant ARB_HYPERVISOR = IHypervisor(ARB_HYPERVISOR_ADDRESS);
Expand Down
136 changes: 45 additions & 91 deletions test/foundry/fixtures/ForkFixture.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ contract ForkFixture is Test {
address YIELD_MANAGER_OWNER = _makeAddr("Yield manager owner");
address PROTOCOL = _makeAddr("Protocol");
address HYPERVISOR_FEE_RECIPIENT = _makeAddr("Hypervisor fee recipient");
address USDS_OWNER = 0xF9d85965c6A40D0C029471d758850e4b4C0d5b17;

// Protocol deployments
USDsMock usds;
IUniswapV3Pool usdsPool;
IHypervisor usdsHypervisor;
TokenManager tokenManager;
SmartVaultManagerV6 smartVaultManager;
SmartVaultYieldManager yieldManager;
Expand All @@ -70,7 +68,7 @@ contract ForkFixture is Test {
_pushCollateralSymbols();
_pushCollateralData();

_deployUsds();
_addUSDLiquidity();
_deployTokenManager();
_deployVaultManager();
_deployYieldManager();
Expand All @@ -90,14 +88,15 @@ contract ForkFixture is Test {
}

function _labelConstants() internal {
vm.label(USDS_ADDRESS, "USDs");
vm.label(USDC_ADDRESS, "USDC");
vm.label(WETH_ADDRESS, "WETH");
vm.label(WBTC_ADDRESS, "WBTC");
vm.label(LINK_ADDRESS, "LINK");
vm.label(ARB_ADDRESS, "ARB");
vm.label(GMX_ADDRESS, "GMX");
vm.label(PAXG_ADDRESS, "PAXG");
// vm.label(RDNT_ADDRESS, "RDNT");
vm.label(RDNT_ADDRESS, "RDNT");
vm.label(SUSHI_ADDRESS, "SUSHI");

vm.label(CL_NATIVE_USD_ADDRESS, "Chainlink ETH/USD");
Expand All @@ -106,19 +105,20 @@ contract ForkFixture is Test {
vm.label(CL_ARB_USD_ADDRESS, "Chainlink ARB/USD");
vm.label(CL_GMX_USD_ADDRESS, "Chainlink GMX/USD");
vm.label(CL_PAXG_USD_ADDRESS, "Chainlink PAXG/USD");
// vm.label(CL_RDNT_USD_ADDRESS, "Chainlink RDNT/USD");
vm.label(CL_RDNT_USD_ADDRESS, "Chainlink RDNT/USD");
vm.label(CL_SUSHI_USD_ADDRESS, "Chainlink SUSHI/USD");

vm.label(UNISWAP_ROUTER_ADDRESS, "Uniswap Router");

vm.label(UNI_PROXY_ADDRESS, "UniProxy");
vm.label(CLEARING_ADDRESS, "Clearing");

vm.label(USDS_HYPERVISOR_ADDRESS, "USDs Hypervisor");
vm.label(WBTC_HYPERVISOR_ADDRESS, "WBTC Hypervisor");
vm.label(LINK_HYPERVISOR_ADDRESS, "LINK Hypervisor");
vm.label(ARB_HYPERVISOR_ADDRESS, "ARB Hypervisor");
vm.label(GMX_HYPERVISOR_ADDRESS, "GMX Hypervisor");
// vm.label(RDNT_HYPERVISOR_ADDRESS, "RDNT Hypervisor");
vm.label(RDNT_HYPERVISOR_ADDRESS, "RDNT Hypervisor");
}

function _pushCollateralSymbols() internal {
Expand All @@ -130,7 +130,7 @@ contract ForkFixture is Test {
collateralSymbols.push(ARB_SYMBOL);
collateralSymbols.push(GMX_SYMBOL);
collateralSymbols.push(PAXG_SYMBOL);
// collateralSymbols.push(RDNT_SYMBOL);
collateralSymbols.push(RDNT_SYMBOL);
collateralSymbols.push(SUSHI_SYMBOL);
}

Expand All @@ -155,8 +155,8 @@ contract ForkFixture is Test {
WBTC,
CL_WBTC_USD,
WBTC_HYPERVISOR,
abi.encodePacked(WBTC_ADDRESS, UNISWAP_FEE, WETH_ADDRESS, UNISWAP_FEE, USDC_ADDRESS),
abi.encodePacked(USDC_ADDRESS, UNISWAP_FEE, WETH_ADDRESS, UNISWAP_FEE, WBTC_ADDRESS)
abi.encodePacked(WBTC_ADDRESS, UNISWAP_FEE, USDC_ADDRESS),
abi.encodePacked(USDC_ADDRESS, UNISWAP_FEE, WBTC_ADDRESS)
);

collateralData[LINK_SYMBOL] = CollateralData(
Expand All @@ -183,6 +183,14 @@ contract ForkFixture is Test {
abi.encodePacked(USDC_ADDRESS, UNISWAP_FEE, WETH_ADDRESS, RAMSES_FEE, GMX_ADDRESS)
);

collateralData[RDNT_SYMBOL] = CollateralData(
RDNT,
CL_RDNT_USD,
RDNT_HYPERVISOR,
abi.encodePacked(RDNT_ADDRESS, RAMSES_FEE, WETH_ADDRESS, UNISWAP_FEE, USDC_ADDRESS),
abi.encodePacked(USDC_ADDRESS, UNISWAP_FEE, WETH_ADDRESS, RAMSES_FEE, RDNT_ADDRESS)
);

collateralData[PAXG_SYMBOL] =
CollateralData(PAXG, CL_PAXG_USD, IHypervisor(address(0)), new bytes(0), new bytes(0));

Expand All @@ -192,29 +200,6 @@ contract ForkFixture is Test {
// TODO: RDNT configurations not clear
}

function _deployUsds() internal {
// deploy USDs
usds = new USDsMock();
vm.label(address(usds), "USDs");

// deploy USDs Uniswap pool
IUniswapV3Factory factory = IUniswapV3Factory(IPeripheryImmutableState(UNISWAP_ROUTER_ADDRESS).factory());
usdsPool = IUniswapV3Pool(factory.createPool(USDC_ADDRESS, address(usds), UNISWAP_FEE));
vm.label(address(usdsPool), "USDs/USDC Uniswap Pool");

// deal tokens to this contract
usds.grantRole(usds.MINTER_ROLE(), address(this));
usds.mint(address(this), 1_000_000 * 10 ** usds.decimals());
_deal(USDC, USDC_WHALE, address(this));

// seed the pool with liquidity
_addUsdsLiquidity();

// deploy USDs/USDC hypervisor
usdsHypervisor = IHypervisor(_deployUsdsHypervisor());
vm.label(address(usdsHypervisor), "USDs/USDC Hypervisor");
}

function _deployTokenManager() internal {
// deploy TokenManager
tokenManager = new TokenManager(NATIVE, CL_NATIVE_USD_ADDRESS);
Expand Down Expand Up @@ -242,7 +227,7 @@ contract ForkFixture is Test {
smartVaultManager.initialize(
COLLATERAL_RATE,
PROTOCOL_FEE_RATE,
address(usds),
USDS_ADDRESS,
PROTOCOL,
address(tokenManager),
address(smartVaultDeployer),
Expand All @@ -258,19 +243,21 @@ contract ForkFixture is Test {
smartVaultManager.setWethAddress(WETH_ADDRESS);

smartVaultIndex.setVaultManager(address(smartVaultManager));
usds.grantRole(usds.DEFAULT_ADMIN_ROLE(), address(smartVaultManager));
usds.grantRole(usds.BURNER_ROLE(), address(smartVaultManager));
// grant default admin role to smart vault manager
vm.startPrank(USDS_OWNER);
ADMIN_USDS.grantRole(0x00, address(smartVaultManager));
ADMIN_USDS.grantRole(ADMIN_USDS.BURNER_ROLE(), address(smartVaultManager));
vm.stopPrank();
}

function _deployYieldManager() internal {
// deploy SmartVaultYieldManager
vm.prank(YIELD_MANAGER_OWNER);
yieldManager = new SmartVaultYieldManager(
address(usds),
USDS_ADDRESS,
USDC_ADDRESS,
WETH_ADDRESS,
UNI_PROXY_ADDRESS,
address(usdsHypervisor),
USDS_HYPERVISOR_ADDRESS,
UNISWAP_ROUTER_ADDRESS
);

Expand Down Expand Up @@ -307,64 +294,31 @@ contract ForkFixture is Test {
vault = SmartVaultV4(payable(smartVault));
}

function _addUsdsLiquidity() internal {
(address token0, address token1) =
address(usds) < USDC_ADDRESS ? (address(usds), USDC_ADDRESS) : (USDC_ADDRESS, address(usds));

uint256 price = (10 ** ERC20(token1).decimals() * 1 << 192) / 10 ** ERC20(token0).decimals();
uint160 sqrtPriceX96 = uint160(FullMath.sqrt(price));

int24 tick = TickMath.getTickAtSqrtRatio(sqrtPriceX96);

IUniswapV3Pool(usdsPool).initialize(sqrtPriceX96);
function _addUSDLiquidity() internal {
vm.startPrank(USDS_OWNER);
uint256 usdAmount = 1_000_000 * 10 ** USDS.decimals();
ADMIN_USDS.setSupplyLimit(10_000_000e18);
ADMIN_USDS.mint(address(this), usdAmount);
vm.stopPrank();
_deal(USDC, USDC_WHALE, address(this));

int24 tickLower = tick - 100 - tick % IUniswapV3Pool(usdsPool).tickSpacing();
int24 tickUpper = tick + 100 + tick % IUniswapV3Pool(usdsPool).tickSpacing();
IUniswapV3Pool pool = IUniswapV3Pool(USD_POOL_ADDRESS);
(,int24 tick,,,,,) = pool.slot0();
int24 tickSpacing = pool.tickSpacing();
// int24 tickLower = (tick - tick) / tickSpacing * tickSpacing;
// int24 tickLower = tick / tickSpacing * tickSpacing - tickSpacing;
int24 tickLower = (tick - 500) / tickSpacing * tickSpacing;
// int24 tickUpper = (tick + tick) / tickSpacing * tickSpacing;
// int24 tickUpper = tick / tickSpacing * tickSpacing + tickSpacing;
int24 tickUpper = (tick + 500) / tickSpacing * tickSpacing;

uint128 liquidity = LiquidityAmounts.getLiquidityForAmounts(
sqrtPriceX96,
TickMath.getSqrtRatioAtTick(tickLower),
TickMath.getSqrtRatioAtTick(tickUpper),
100_000 * 10 ** ERC20(token0).decimals(),
100_000 * 10 ** ERC20(token1).decimals()
);
IUniswapV3Pool(usdsPool).mint(address(this), tickLower, tickUpper, liquidity, "");
pool.mint(address(this), tickLower, tickUpper, 1e19, "");

IUniswapV3Pool(usdsPool).increaseObservationCardinalityNext(100);
pool.increaseObservationCardinalityNext(100);
}

function uniswapV3MintCallback(uint256 amount0Owed, uint256 amount1Owed, bytes calldata) external {
usds.transfer(msg.sender, amount0Owed);
USDS.transfer(msg.sender, amount0Owed);
USDC.transfer(msg.sender, amount1Owed);
}

// TODO: investigate whether there is a simpler way to do this without using raw bytecode
function _deployUsdsHypervisor() internal returns (address hypervisor) {
bytes memory constructorParams = abi.encode(usdsPool, address(this), "USDs-USDC Hypervisor", "USDs-USDC");
bytes memory bytecodeWithParams = bytes.concat(HYPERVISOR_CODE, constructorParams);

assembly {
hypervisor := create(0, add(bytecodeWithParams, 0x20), mload(bytecodeWithParams))
}

vm.assertNotEq(hypervisor, address(0));
IHypervisor(hypervisor).setWhitelist(UNI_PROXY_ADDRESS);
vm.prank(IClearing(UNI_PROXY_ADDRESS).owner());
CLEARING.addPosition(hypervisor, 1);

IHypervisor(hypervisor).rebalance(
-276350, // base lower
-276300, // base upper
-276280, // limit lower
-276230, // limit upper
HYPERVISOR_FEE_RECIPIENT,
[uint256(0), uint256(0), uint256(0), uint256(0)],
[uint256(0), uint256(0), uint256(0), uint256(0)]
);
vm.warp(block.timestamp + 3601);

usds.approve(hypervisor, 1_000 * 10 ** usds.decimals());
USDC.approve(hypervisor, 1_000 * 10 ** USDC.decimals());
UNI_PROXY.deposit(1000e18, 1000e6, msg.sender, hypervisor, [uint256(0), uint256(0), uint256(0), uint256(0)]);
}
}

0 comments on commit 5f5ea08

Please sign in to comment.