Skip to content

Commit

Permalink
feat: remove all twap
Browse files Browse the repository at this point in the history
  • Loading branch information
kassandraoftroy authored Oct 3, 2022
1 parent 1b6f52a commit b22156a
Show file tree
Hide file tree
Showing 23 changed files with 23 additions and 930 deletions.
64 changes: 17 additions & 47 deletions contracts/ArrakisV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
BurnLiquidity,
UnderlyingOutput
} from "./structs/SArrakisV2.sol";
import {Twap} from "./libraries/Twap.sol";
import {Position} from "./libraries/Position.sol";
import {Pool} from "./libraries/Pool.sol";
import {Manager} from "./libraries/Manager.sol";
Expand Down Expand Up @@ -335,16 +334,15 @@ contract ArrakisV2 is IUniswapV3MintCallback, ArrakisV2Storage {
nonReentrant
{
// Burns
uint256 totalFee0 = 0;
uint256 totalFee1 = 0;
uint256 aggregator0 = 0;
uint256 aggregator1 = 0;
for (uint256 i = 0; i < rebalanceParams_.removes.length; i++) {
address poolAddr = factory.getPool(
address(token0),
address(token1),
rebalanceParams_.removes[i].range.feeTier
);
IUniswapV3Pool pool = IUniswapV3Pool(poolAddr);
Twap.checkDeviation(pool, twapDuration, maxTwapDeviation);

Withdraw memory withdraw = _withdraw(
pool,
Expand All @@ -353,20 +351,20 @@ contract ArrakisV2 is IUniswapV3MintCallback, ArrakisV2Storage {
rebalanceParams_.removes[i].liquidity
);

totalFee0 += withdraw.fee0;
totalFee1 += withdraw.fee1;
aggregator0 += withdraw.fee0;
aggregator1 += withdraw.fee1;
}

if (totalFee0 > 0 || totalFee1 > 0) {
_applyFees(totalFee0, totalFee1);
(totalFee0, totalFee1) = UniswapV3Amounts.subtractAdminFees(
totalFee0,
totalFee1,
if (aggregator0 > 0 || aggregator1 > 0) {
_applyFees(aggregator0, aggregator1);
(aggregator0, aggregator1) = UniswapV3Amounts.subtractAdminFees(
aggregator0,
aggregator1,
Manager.getManagerFeeBPS(manager),
arrakisFeeBPS
);

emit LogFeesEarnRebalance(totalFee0, totalFee1);
emit LogFeesEarnRebalance(aggregator0, aggregator1);
}

// Swap
Expand Down Expand Up @@ -402,22 +400,6 @@ contract ArrakisV2 is IUniswapV3MintCallback, ArrakisV2Storage {
uint256 balance1After = token1.balanceOf(address(this));

if (rebalanceParams_.swap.zeroForOne) {
require(
FullMath.mulDiv(
rebalanceParams_.swap.expectedMinReturn,
10**ERC20(address(token0)).decimals(),
rebalanceParams_.swap.amountIn
) >
FullMath.mulDiv(
Twap.getPrice0(
IUniswapV3Pool(rebalanceParams_.swap.pool),
twapDuration
),
maxSlippage,
10000
),
"S"
);
require(
(balance1After >=
balance1Before +
Expand All @@ -428,22 +410,6 @@ contract ArrakisV2 is IUniswapV3MintCallback, ArrakisV2Storage {
"SF"
);
} else {
require(
FullMath.mulDiv(
rebalanceParams_.swap.expectedMinReturn,
10**ERC20(address(token1)).decimals(),
rebalanceParams_.swap.amountIn
) >
FullMath.mulDiv(
Twap.getPrice1(
IUniswapV3Pool(rebalanceParams_.swap.pool),
twapDuration
),
maxSlippage,
10000
),
"S"
);
require(
(balance0After >=
balance0Before +
Expand All @@ -458,6 +424,8 @@ contract ArrakisV2 is IUniswapV3MintCallback, ArrakisV2Storage {
}

// Mints.
aggregator0 = 0;
aggregator1 = 0;
for (uint256 i = 0; i < rebalanceParams_.deposits.length; i++) {
IUniswapV3Pool pool = IUniswapV3Pool(
factory.getPool(
Expand All @@ -473,16 +441,18 @@ contract ArrakisV2 is IUniswapV3MintCallback, ArrakisV2Storage {
);
require(exist, "NR");

Twap.checkDeviation(pool, twapDuration, maxTwapDeviation);

pool.mint(
(uint256 amt0, uint256 amt1) = pool.mint(
address(this),
rebalanceParams_.deposits[i].range.lowerTick,
rebalanceParams_.deposits[i].range.upperTick,
rebalanceParams_.deposits[i].liquidity,
""
);
aggregator0 += amt0;
aggregator1 += amt1;
}
require(aggregator0 >= rebalanceParams_.minDeposit0, "RF");
require(aggregator1 >= rebalanceParams_.minDeposit1, "RF");

emit LogRebalance(rebalanceParams_);
}
Expand Down
151 changes: 0 additions & 151 deletions contracts/ArrakisV2Resolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {Underlying as UnderlyingHelper} from "./libraries/Underlying.sol";
import {UniswapV3Amounts} from "./libraries/UniswapV3Amounts.sol";
import {Twap} from "./libraries/Twap.sol";
import {Manager} from "./libraries/Manager.sol";
import {Position as PositionHelper} from "./libraries/Position.sol";
import {FullMath} from "@arrakisfi/v3-lib-0.8/contracts/FullMath.sol";
Expand All @@ -34,7 +33,6 @@ import {
Rebalance,
SwapPayload
} from "./structs/SArrakisV2.sol";
import {RebalanceWithSwap} from "./structs/SArrakisV2Resolver.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";

contract ArrakisV2Resolver is IArrakisV2Resolver {
Expand Down Expand Up @@ -145,155 +143,6 @@ contract ArrakisV2Resolver is IArrakisV2Resolver {
}
}

// solhint-disable-next-line function-max-lines, code-complexity
function rebalanceWithSwap(RebalanceWithSwap calldata rebalanceWithSwap_)
external
view
returns (Rebalance memory rebalanceParams)
{
uint256 amount0;
uint256 amount1;
address token0Addr;
address token1Addr;
{
Range[] memory ranges = helper.ranges(rebalanceWithSwap_.vaultV2);

token0Addr = address(rebalanceWithSwap_.vaultV2.token0());
token1Addr = address(rebalanceWithSwap_.vaultV2.token1());

(amount0, amount1) = helper.totalUnderlying(
rebalanceWithSwap_.vaultV2
);

PositionLiquidity[] memory pl = new PositionLiquidity[](
ranges.length
);
uint256 numberOfPosLiq;

for (uint256 i = 0; i < ranges.length; i++) {
uint128 liquidity;
{
(liquidity, , , , ) = IUniswapV3Pool(
rebalanceWithSwap_.vaultV2.factory().getPool(
token0Addr,
token1Addr,
ranges[i].feeTier
)
).positions(
PositionHelper.getPositionId(
address(rebalanceWithSwap_.vaultV2),
ranges[i].lowerTick,
ranges[i].upperTick
)
);
}

if (liquidity > 0) numberOfPosLiq++;

pl[i] = PositionLiquidity({
liquidity: liquidity,
range: ranges[i]
});
}

rebalanceParams.removes = new PositionLiquidity[](numberOfPosLiq);
uint256 j;

for (uint256 i = 0; i < pl.length; i++) {
if (pl[i].liquidity > 0) {
rebalanceParams.removes[j] = pl[i];
j++;
}
}

SwapPayload memory swap;
swap.zeroForOne = rebalanceWithSwap_.zeroForOne;
swap.pool = address(rebalanceWithSwap_.pool);
swap.router = address(swapRouter);

if (rebalanceWithSwap_.zeroForOne) {
swap.amountIn = FullMath.mulDiv(
amount0,
rebalanceWithSwap_.swapRatio,
10000
);
swap.expectedMinReturn = FullMath.mulDiv(
swap.amountIn,
Twap.getPrice0(
rebalanceWithSwap_.pool,
rebalanceWithSwap_.vaultV2.twapDuration()
),
10 **
ERC20(address(rebalanceWithSwap_.vaultV2.token0()))
.decimals()
);
} else {
swap.amountIn = FullMath.mulDiv(
amount1,
rebalanceWithSwap_.swapRatio,
10000
);
swap.expectedMinReturn = FullMath.mulDiv(
swap.amountIn,
Twap.getPrice1(
rebalanceWithSwap_.pool,
rebalanceWithSwap_.vaultV2.twapDuration()
),
10 **
ERC20(address(rebalanceWithSwap_.vaultV2.token1()))
.decimals()
);
}

swap.payload = abi.encodeWithSelector(
ISwapRouter.exactInputSingle.selector,
ISwapRouter.ExactInputSingleParams({
tokenIn: address(rebalanceWithSwap_.vaultV2.token0()),
tokenOut: address(rebalanceWithSwap_.vaultV2.token1()),
fee: rebalanceWithSwap_.pool.fee(),
recipient: address(rebalanceWithSwap_.vaultV2),
deadline: block.number + 600, // 10 minutes
amountIn: swap.amountIn,
amountOutMinimum: swap.expectedMinReturn,
sqrtPriceLimitX96: 0
})
);
rebalanceParams.swap = swap;
}

// TODO check if sum of weight is < 10000

_requireWeightUnder100(rebalanceWithSwap_.rangeWeights);

rebalanceParams.deposits = new PositionLiquidity[](
rebalanceWithSwap_.rangeWeights.length
);

for (uint256 i = 0; i < rebalanceWithSwap_.rangeWeights.length; i++) {
RangeWeight memory rangeWeight = rebalanceWithSwap_.rangeWeights[i];
(uint160 sqrtPriceX96, , , , , , ) = IUniswapV3Pool(
rebalanceWithSwap_.vaultV2.factory().getPool(
token0Addr,
token1Addr,
rangeWeight.range.feeTier
)
).slot0();

uint128 liquidity = LiquidityAmounts.getLiquidityForAmounts(
sqrtPriceX96,
TickMath.getSqrtRatioAtTick(rangeWeight.range.lowerTick),
TickMath.getSqrtRatioAtTick(rangeWeight.range.upperTick),
FullMath.mulDiv(amount0, rangeWeight.weight, 10000),
FullMath.mulDiv(amount1, rangeWeight.weight, 10000)
);

rebalanceParams.deposits[i] = PositionLiquidity({
liquidity: liquidity,
range: rangeWeight.range
});
}
}

// solhint-disable-next-line function-max-lines
function standardBurnParams(uint256 amountToBurn_, IArrakisV2 vaultV2_)
external
Expand Down
17 changes: 0 additions & 17 deletions contracts/__mocks__/functions/MockFArrakisV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
IUniswapV3Factory
} from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol";
import {Position as PositionHelper} from "../../libraries/Position.sol";
import {Twap} from "../../libraries/Twap.sol";
import {Underlying as UnderlyingHelper} from "../../libraries/Underlying.sol";
import {UniswapV3Amounts} from "../../libraries/UniswapV3Amounts.sol";
import {Pool} from "../../libraries/Pool.sol";
Expand All @@ -29,22 +28,6 @@ contract MockFArrakisV2 {
return UniswapV3Amounts.computeFeesEarned(computeFeesEarned_);
}

function getTwap(IUniswapV3Pool pool_, uint24 twapDuration_)
external
view
returns (int24)
{
return Twap.getTwap(pool_, twapDuration_);
}

function checkDeviation(
IUniswapV3Pool pool_,
uint24 twapDuration_,
int24 maxTwapDeviation_
) external view {
Twap.checkDeviation(pool_, twapDuration_, maxTwapDeviation_);
}

// function totalUnderlying(UnderlyingPayload calldata underlyingPayload_)
// external
// view
Expand Down
49 changes: 0 additions & 49 deletions contracts/__mocks__/libraries/TwapMock.sol

This file was deleted.

Loading

0 comments on commit b22156a

Please sign in to comment.