Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
scab24 committed Sep 4, 2024
1 parent ea528f7 commit 6b02b99
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 57 deletions.
6 changes: 6 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ src = "src"
out = "out"
libs = ["lib"]


solc_version = '0.8.26'
evm_version = "cancun"
optimizer_runs = 800
via_ir = false
ffi = true
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
14 changes: 14 additions & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@ensdomains/=lib/v4-periphery/lib/v4-core/node_modules/@ensdomains/
@openzeppelin/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/
@openzeppelin/contracts/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/contracts/
@uniswap/v4-core/=lib/v4-periphery/lib/v4-core/
ds-test/=lib/v4-periphery/lib/v4-core/lib/forge-std/lib/ds-test/src/
erc4626-tests/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/
forge-gas-snapshot/=lib/v4-periphery/lib/v4-core/lib/forge-gas-snapshot/src/
forge-std/=lib/forge-std/src/
hardhat/=lib/v4-periphery/lib/v4-core/node_modules/hardhat/
openzeppelin-contracts/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/
permit2/=lib/v4-periphery/lib/permit2/
solmate/=lib/v4-periphery/lib/v4-core/lib/solmate/
v4-core/=lib/v4-periphery/lib/v4-core/src/
v4-periphery/=lib/v4-periphery/
19 changes: 0 additions & 19 deletions script/Counter.s.sol

This file was deleted.

14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

131 changes: 131 additions & 0 deletions src/DeltaGammaHedgingHook.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {BaseHook} from "v4-periphery/src/base/hooks/BaseHook.sol";
import {Hooks} from "v4-core/libraries/Hooks.sol";
import {IPoolManager} from "v4-core/interfaces/IPoolManager.sol";
import {PoolKey} from "v4-core/types/PoolKey.sol";
import {BalanceDelta} from "v4-core/types/BalanceDelta.sol";
import {PoolId, PoolIdLibrary} from "v4-core/types/PoolId.sol";
import {Currency, CurrencyLibrary} from "v4-core/types/Currency.sol";
import {TickMath} from "v4-core/libraries/TickMath.sol";
import {FixedPointMathLib} from "solmate/src/utils/FixedPointMathLib.sol";
import {BeforeSwapDelta, toBeforeSwapDelta} from "v4-core/types/BeforeSwapDelta.sol";

contract DeltaGammaHedgingHook is BaseHook {
using PoolIdLibrary for PoolKey;
using CurrencyLibrary for Currency;
using FixedPointMathLib for uint256;

uint256 public constant GAMMA_THRESHOLD = 2;
uint256 public constant PRICE_RANGE_FACTOR = 10000;

mapping(PoolId => int24) public lastTicks;
mapping(PoolId => int256) public deltas;
mapping(PoolId => int256) public gammas;

constructor(IPoolManager _poolManager) BaseHook(_poolManager) {}

function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
return Hooks.Permissions({
beforeInitialize: false,
afterInitialize: false,
beforeAddLiquidity: false,
afterAddLiquidity: false,
beforeRemoveLiquidity: false,
afterRemoveLiquidity: false,
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
beforeSwapReturnDelta: true,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

function beforeSwap(
address sender,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
bytes calldata hookData
) external override returns (bytes4, BeforeSwapDelta, uint24) {
PoolId poolId = key.toId();
if (isLargeSwap(params)) {
(int128 deltaAdjustment, int128 liquidityDelta) = adjustLiquidityForDeltaGammaHedge(key, params);
BeforeSwapDelta beforeSwapDelta = toBeforeSwapDelta(deltaAdjustment, liquidityDelta);
return (BaseHook.beforeSwap.selector, beforeSwapDelta, 0);
} else {

int256 deltaImpact = int256(params.amountSpecified) / 10;
deltas[poolId] += deltaImpact;
}
return (BaseHook.beforeSwap.selector, toBeforeSwapDelta(0, 0), 0);
}

function isLargeSwap(IPoolManager.SwapParams calldata params) internal pure returns (bool) {
return params.amountSpecified > 1000000;
}

function adjustLiquidityForDeltaGammaHedge(PoolKey calldata key, IPoolManager.SwapParams calldata params) internal returns (int128, int128) {
PoolId poolId = key.toId();
int256 gammaImpact = calculateGammaImpact(params);
int128 liquidityDelta = 0;
if (gammaImpact != 0) {
liquidityDelta = int128(distributeGammaHedgingLiquidity(key, gammaImpact));
gammas[poolId] += gammaImpact;
}
int128 deltaAdjustment = int128(adjustDeltaHedge(key, params));
deltas[poolId] += int256(deltaAdjustment);
return (deltaAdjustment, liquidityDelta);
}

function calculateGammaImpact(IPoolManager.SwapParams calldata params) internal pure returns (int256) {
return int256(params.amountSpecified) * int256(GAMMA_THRESHOLD) / 1000000;
}

function distributeGammaHedgingLiquidity(PoolKey calldata key, int256 gammaImpact) internal returns (uint128) {
int24 tickLower = -887272;
int24 tickUpper = 887272;
uint128 liquidity = uint128(uint256(abs(gammaImpact) * 1000));

IPoolManager.ModifyLiquidityParams memory params = IPoolManager.ModifyLiquidityParams({
tickLower: tickLower,
tickUpper: tickUpper,
liquidityDelta: int256(uint256(liquidity)),
salt: bytes32(0)
});

(BalanceDelta callerDelta, BalanceDelta feeDelta) = poolManager.modifyLiquidity(
key,
params,
abi.encode(0)
);


return liquidity;
}

function adjustDeltaHedge(PoolKey calldata key, IPoolManager.SwapParams calldata params) internal returns (int256) {
int256 deltaAdjustment = int256(params.amountSpecified) / 2;
poolManager.swap(
key,
IPoolManager.SwapParams({
zeroForOne: !params.zeroForOne,
amountSpecified: deltaAdjustment,
sqrtPriceLimitX96: 0
}),
abi.encode(0)
);
return deltaAdjustment;
}

function getDeltaGamma(PoolId poolId) public view returns (int256, int256) {
return (deltas[poolId], gammas[poolId]);
}

function abs(int256 x) internal pure returns (int256) {
return x >= 0 ? x : -x;
}
}
24 changes: 0 additions & 24 deletions test/Counter.t.sol

This file was deleted.

Loading

0 comments on commit 6b02b99

Please sign in to comment.