Skip to content

Commit

Permalink
chore: improve coverage of arrakisV2
Browse files Browse the repository at this point in the history
  • Loading branch information
Gevarist authored Oct 4, 2022
1 parent b22156a commit 95ab2f8
Showing 1 changed file with 268 additions and 0 deletions.
268 changes: 268 additions & 0 deletions test/integration_tests/ArrakisV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
IUniswapV3Pool,
ISwapRouter,
ArrakisV2Resolver,
Position,
} from "../../typechain";
import { Addresses, getAddresses } from "../../src/addresses";
import { Signer } from "ethers";
Expand All @@ -22,9 +23,11 @@ describe("Arrakis V2 integration test!!!", async function () {
let userAddr: string;
let arrakisV2Factory: ArrakisV2Factory;
let vaultV2: ArrakisV2;
let pool: IUniswapV3Pool;
let uniswapV3Factory: IUniswapV3Factory;
let uniswapV3Pool: IUniswapV3Pool;
let arrakisV2Resolver: ArrakisV2Resolver;
let position: Position;
let swapRouter: ISwapRouter;
let wMatic: Contract;
let wEth: Contract;
Expand Down Expand Up @@ -72,6 +75,8 @@ describe("Arrakis V2 integration test!!!", async function () {
user
)) as IUniswapV3Pool;

position = (await ethers.getContract("Position", user)) as Position;

const slot0 = await uniswapV3Pool.slot0();
const tickSpacing = await uniswapV3Pool.tickSpacing();

Expand Down Expand Up @@ -229,6 +234,12 @@ describe("Arrakis V2 integration test!!!", async function () {
// #endregion swap wrapped matic for usdc.

// #endregion get some USDC and WETH tokens from Uniswap V3.

pool = (await ethers.getContractAt(
"IUniswapV3Pool",
await uniswapV3Factory.getPool(addresses.USDC, addresses.WETH, 500),
user
)) as IUniswapV3Pool;
});

it("#0: Deposit token and Mint Arrakis V2 tokens ", async () => {
Expand Down Expand Up @@ -390,6 +401,263 @@ describe("Arrakis V2 integration test!!!", async function () {
// #endregion rebalance to deposit user token into the uniswap v3 pool.
// #region burn token to get back token to user.

// await wMatic.deposit({ value: ethers.utils.parseUnits("1000", 18) });
// await wMatic.approve(swapR.address, ethers.utils.parseUnits("1000", 18));

// await swapR.exactInputSingle({
// tokenIn: addresses.WMATIC,
// tokenOut: addresses.WETH,
// fee: 500,
// recipient: userAddr,
// deadline: ethers.constants.MaxUint256,
// amountIn: ethers.utils.parseUnits("1000", 18),
// amountOutMinimum: ethers.constants.Zero,
// sqrtPriceLimitX96: 0,
// });

// await wEth.approve(swapR.address, ethers.utils.parseEther("0.001"));

// const amountOut = await swapR.callStatic.exactInputSingle({
// tokenIn: wEth.address,
// tokenOut: usdc.address,
// fee: 500,
// recipient: userAddr,
// deadline: ethers.constants.MaxUint256,
// amountIn: ethers.utils.parseEther("0.001"),
// amountOutMinimum: ethers.constants.Zero,
// sqrtPriceLimitX96: 0,
// });

// #region rebalance to do a swap.

const liquidity = pool.positions(
position.getPositionId(vaultV2.address, lowerTick, upperTick)
);

await managerProxyMock.rebalance(
vaultV2.address,
[],
{
removes: [
{
range: { lowerTick, upperTick, feeTier: 500 },
liquidity: (await liquidity)._liquidity,
},
],
swap: {
amountIn: ethers.utils.parseEther("0.001"),
expectedMinReturn: 2626887,
router: swapR.address,
zeroForOne: false,
payload: swapR.interface.encodeFunctionData("exactInputSingle", [
{
tokenIn: wEth.address,
tokenOut: usdc.address,
fee: 500,
recipient: vaultV2.address,
deadline: ethers.constants.MaxUint256,
amountIn: ethers.utils.parseEther("0.001"),
amountOutMinimum: ethers.constants.Zero,
sqrtPriceLimitX96: 0,
},
]),
pool: ethers.constants.AddressZero,
},
deposits: [],
minDeposit0: ethers.constants.Zero,
minDeposit1: ethers.constants.Zero,
},
[]
);

// #endregion rebalance to do a swap.

const burnPayload = await arrakisV2Resolver.standardBurnParams(
result.mintAmount,
vaultV2.address
);
await vaultV2.burn(burnPayload, result.mintAmount, userAddr);

balance = await vaultV2.balanceOf(userAddr);

expect(await usdc.balanceOf(vaultV2.address)).to.be.eq(
(await vaultV2.managerBalance0()).add(await vaultV2.arrakisBalance0())
);
expect(await wEth.balanceOf(vaultV2.address)).to.be.eq(
(await vaultV2.managerBalance1()).add(await vaultV2.arrakisBalance1())
);

expect(balance).to.be.eq(0);

// #endregion burn token to get back token to user.

// #region rebalance to remove the range.

await managerProxyMock.rebalance(
vaultV2.address,
[],
await arrakisV2Resolver.standardRebalance([], vaultV2.address),
[{ lowerTick, upperTick, feeTier: 500 }]
);

// #endregion rebalance to remove the range.

// #region withdraw as manager.

const managerAddr = await vaultV2.manager();

managerProxyMock.fundVaultBalance(vaultV2.address, {
value: ethers.utils.parseEther("1"),
});

const managerT0B = await usdc.balanceOf(managerAddr);
const managerT1B = await wEth.balanceOf(managerAddr);

await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [managerAddr],
});

const managerSigner = await ethers.getSigner(managerAddr);

await vaultV2.connect(managerSigner).withdrawManagerBalance();

await hre.network.provider.request({
method: "hardhat_stopImpersonatingAccount",
params: [managerAddr],
});

const managerT0A = await usdc.balanceOf(managerAddr);
const managerT1A = await wEth.balanceOf(managerAddr);

expect(managerT0A).to.be.gte(managerT0B);
expect(managerT1A).to.be.gt(managerT1B);

// #region withdraw as manager.

// #region withdraw as arrakis treasury.

const arrakisAddr = await vaultV2.arrakisTreasury();

await user.sendTransaction({
to: arrakisAddr,
value: ethers.utils.parseEther("1"),
});

const arrakisT0B = await usdc.balanceOf(arrakisAddr);
const arrakisT1B = await wEth.balanceOf(arrakisAddr);

await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [arrakisAddr],
});

const arrakisAddrSigner = await ethers.getSigner(arrakisAddr);

await vaultV2.connect(arrakisAddrSigner).withdrawArrakisBalance();

await hre.network.provider.request({
method: "hardhat_stopImpersonatingAccount",
params: [arrakisAddr],
});

const arrakisT0A = await usdc.balanceOf(arrakisAddr);
const arrakisT1A = await wEth.balanceOf(arrakisAddr);

expect(arrakisT0A).to.be.gte(arrakisT0B);
expect(arrakisT1A).to.be.gt(arrakisT1B);

// #region withdraw as arrakis treasury.
});

it("#3: Rebalance without swap after mint and burn of Arrakis V2 tokens", async () => {
// #region mint arrakis token by Lp.

await wEth.approve(vaultV2.address, ethers.constants.MaxUint256);
await usdc.approve(vaultV2.address, ethers.constants.MaxUint256);

// #endregion approve weth and usdc token to vault.

// #region user balance of weth and usdc.

const wethBalance = await wEth.balanceOf(userAddr);
const usdcBalance = await usdc.balanceOf(userAddr);

// #endregion user balance of weth and usdc.

// #region mint arrakis vault V2 token.

const result = await arrakisV2Resolver.getMintAmounts(
vaultV2.address,
usdcBalance,
wethBalance
);

await vaultV2.mint(result.mintAmount, userAddr);

let balance = await vaultV2.balanceOf(userAddr);

expect(balance).to.be.eq(result.mintAmount);

// #endregion mint arrakis token by Lp.
// #region rebalance to deposit user token into the uniswap v3 pool.

const rebalanceParams = await arrakisV2Resolver.standardRebalance(
[{ range: { lowerTick, upperTick, feeTier: 500 }, weight: 10000 }],
vaultV2.address
);

await managerProxyMock.rebalance(
vaultV2.address,
[{ lowerTick, upperTick, feeTier: 500 }],
rebalanceParams,
[]
);

// #region do a swap to generate fees.

const swapRouter = "0xE592427A0AEce92De3Edee1F18E0157C05861564";

const swapR: ISwapRouter = (await ethers.getContractAt(
"ISwapRouter",
swapRouter,
user
)) as ISwapRouter;

await wMatic.deposit({ value: ethers.utils.parseUnits("1000", 18) });
await wMatic.approve(swapR.address, ethers.utils.parseUnits("1000", 18));

await swapR.exactInputSingle({
tokenIn: addresses.WMATIC,
tokenOut: addresses.WETH,
fee: 500,
recipient: userAddr,
deadline: ethers.constants.MaxUint256,
amountIn: ethers.utils.parseUnits("1000", 18),
amountOutMinimum: ethers.constants.Zero,
sqrtPriceLimitX96: 0,
});

await wEth.approve(swapR.address, ethers.utils.parseEther("0.001"));

await swapR.exactInputSingle({
tokenIn: wEth.address,
tokenOut: usdc.address,
fee: 500,
recipient: userAddr,
deadline: ethers.constants.MaxUint256,
amountIn: ethers.utils.parseEther("0.001"),
amountOutMinimum: ethers.constants.Zero,
sqrtPriceLimitX96: 0,
});

// #endregion do a swap to generate fess.

// #endregion rebalance to deposit user token into the uniswap v3 pool.
// #region burn token to get back token to user.

// #endregion rebalance to do a swap.

const burnPayload = await arrakisV2Resolver.standardBurnParams(
result.mintAmount,
vaultV2.address
Expand Down

0 comments on commit 95ab2f8

Please sign in to comment.