Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"permissions": {
"allow": [
"Bash(forge test:*)",
"WebFetch(domain:raw.githubusercontent.com)"
],
"deny": [],
"ask": []
}
}
13 changes: 13 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@
[submodule "packages/foundry/lib/v4-periphery"]
path = packages/foundry/lib/v4-periphery
url = https://github.com/Uniswap/v4-periphery
[submodule "packages/foundry/lib/v3-core"]
path = packages/foundry/lib/v3-core
url = https://github.com/uniswap/v3-core
[submodule "packages/foundry/lib/v2-core"]
path = packages/foundry/lib/v2-core
url = https://github.com/uniswap/v2-core
[submodule "lib/universal-router"]
branch = main
[submodule "lib/universal-router.git"]
branch = main
[submodule "packages/foundry/lib/universal-router"]
path = packages/foundry/lib/universal-router
url = https://github.com/Uniswap/universal-router
12 changes: 6 additions & 6 deletions packages/foundry/contracts/MockOptionToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "../contracts/OpHook.sol";
import "../contracts/IOptionToken.sol";
import {HookMiner} from "lib/uniswap-hooks/lib/v4-periphery/src/utils/HookMiner.sol";
import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";
import {PoolManager} from "v4-core/src/PoolManager.sol";
import {Hooks} from "v4-core/src/libraries/Hooks.sol";
import {PoolKey} from "v4-core/src/types/PoolKey.sol";
import {Currency} from "v4-core/src/types/Currency.sol";
import {HookMiner} from "@uniswap/v4-periphery/src/utils/HookMiner.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolManager} from "@uniswap/v4-core/src/PoolManager.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
Expand Down
45 changes: 32 additions & 13 deletions packages/foundry/contracts/OpHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ pragma solidity ^0.8.26;

import {BaseHook} from "@openzeppelin/uniswap-hooks/src/base/BaseHook.sol";

import {Hooks} from "v4-core/src/libraries/Hooks.sol";
import {IPoolManager, SwapParams} from "v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "v4-core/src/types/PoolKey.sol";
import {PoolIdLibrary} from "v4-core/src/types/PoolId.sol";
import {BeforeSwapDelta, toBeforeSwapDelta} from "v4-core/src/types/BeforeSwapDelta.sol";
import {Currency} from "v4-core/src/types/Currency.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {IPoolManager, SwapParams} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol";
import {BeforeSwapDelta, toBeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol";

import {ERC4626} from "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
Expand All @@ -20,13 +21,15 @@ import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol
import {Pausable} from "@openzeppelin/contracts/utils/Pausable.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";


import {OptionPrice, IUniswapV3Pool} from "./OptionPrice.sol";

import {IOptionToken} from "./IOptionToken.sol";
import {IPermit2} from "./IPermit2.sol";

import {IHooks} from "v4-core/src/interfaces/IHooks.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {console} from "forge-std/console.sol";
import {NonzeroDeltaCount} from "lib/uniswap-hooks/lib/v4-core/src/libraries/NonzeroDeltaCount.sol";

uint160 constant SQRT_PRICE_X96 = 1<<96;
int24 constant TICK_SPACING = type(int16).max;
Expand Down Expand Up @@ -160,14 +163,14 @@ contract OpHook is BaseHook, ERC4626, Ownable, ReentrancyGuard, Pausable {
}
require(amountSpecified < 0, "amountSpecified must be negative");
uint256 amount = uint256(-amountSpecified);
int128 amount_ = int128(int256(amount));
int128 amount_ = SafeCast.toInt128(int256(amount));
uint256 collateralPrice = getCollateralPrice();
uint256 price = _getPrice(collateralPrice, option);

uint256 collateralAmount = calculateCollateral(amount, price);
int128 collateralAmount_ = int128(int256(collateralAmount));
int128 collateralAmount_ = SafeCast.toInt128(int256(collateralAmount));
uint256 cashAmount = Math.mulDiv(amount, 1e36, price);
int128 cashAmount_ = int128(int256(cashAmount));
int128 cashAmount_ = SafeCast.toInt128(int256(cashAmount));
amounts = Amount({
amount:amount,
amount_:amount_,
Expand Down Expand Up @@ -253,11 +256,27 @@ contract OpHook is BaseHook, ERC4626, Ownable, ReentrancyGuard, Pausable {
if (a.cashForOption) {
// Here we JIT create option tokens and let the flash accounting handle transfers
option.mint(a.collateralAmount);
console.log("delta", NonzeroDeltaCount.read());
poolManager.take(a.cashCurrency, address(this), a.amount);
console.log("delta", NonzeroDeltaCount.read());
poolManager.sync(a.optionCurrency);
option.transfer(address(poolManager_), a.collateralAmount);
console.log("delta", NonzeroDeltaCount.read());

console.log("option totalSupply", option.balanceOf(sender));
poolManager.settle();
return (BaseHook.beforeSwap.selector, toBeforeSwapDelta(a.amount_, -a.collateralAmount_), 0);
console.log("option totalSupply", option.balanceOf(sender));
console.log("delta", NonzeroDeltaCount.read());
BeforeSwapDelta delta = toBeforeSwapDelta(a.amount_, -a.collateralAmount_);
console.log("a.amount_", a.amount_);
console.log("a.collateralAmount_", a.collateralAmount_);
// console.log("delta", BeforeSwapDelta.unwrap(delta));
console.log("specifiedDelta", BeforeSwapDeltaLibrary.getSpecifiedDelta(delta));
console.log("unspecifiedDelta", BeforeSwapDeltaLibrary.getUnspecifiedDelta(delta));
// int256 dW = poolManager.currencyDelta(msg.sender, Currency.wrap(address(weth)));
// int256 dO2 = poolManager.currencyDelta(msg.sender, Currency.wrap(address(option2)));
// console.log("ownerDeltas", d0, d1 /*, dW, dO2*/);
return (BaseHook.beforeSwap.selector, delta, 0);
} else {
// Here we have to take the option tokens from the caller and burn them
poolManager.take(a.optionCurrency, address(this), a.amount);
Expand All @@ -268,15 +287,15 @@ contract OpHook is BaseHook, ERC4626, Ownable, ReentrancyGuard, Pausable {
}
}

function _beforeAddLiquidity(address, PoolKey calldata key, SwapParams calldata params, bytes calldata)
function _beforeAddLiquidity(address, PoolKey calldata, SwapParams calldata, bytes calldata)
internal
pure

returns (bytes4, BeforeSwapDelta, uint24){
revert("Cannot Add Liquidity to This Pool ");
}

function _beforeDonate(address, PoolKey calldata key, SwapParams calldata params, bytes calldata)
function _beforeDonate(address, PoolKey calldata, SwapParams calldata, bytes calldata)
internal
pure
returns (bytes4, BeforeSwapDelta, uint24){
Expand Down
30 changes: 30 additions & 0 deletions packages/foundry/foundry.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
{
"lib/v2-core": {
"tag": {
"name": "v1.0.1",
"rev": "4dd59067c76dea4a0e8e4bfdda41877a6b16dedc"
}
},
"packages/foundry/lib/v4-periphery": {
"rev": "60cd93803ac2b7fa65fd6cd351fd5fd4cc8c9db5"
},
"lib/universal-router.git": {
"branch": {
"name": "main",
"rev": "3663f6db6e2fe121753cd2d899699c2dc75dca86"
}
},
"packages/foundry/lib/v3-core": {
"rev": "e3589b192d0be27e100cd0daaf6c97204fdb1899"
},
"lib/v4-periphery": {
"rev": "60cd93803ac2b7fa65fd6cd351fd5fd4cc8c9db5"
},
"lib/v3-core": {
"tag": {
"name": "v1.0.0",
"rev": "e3589b192d0be27e100cd0daaf6c97204fdb1899"
}
},
"lib/universal-router": {
"branch": {
"name": "main",
"rev": "3663f6db6e2fe121753cd2d899699c2dc75dca86"
}
}
}
9 changes: 0 additions & 9 deletions packages/foundry/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ int_types = "long"

# See more config options https://book.getfoundry.sh/reference/config/overview

[profile.default]
optimizer_runs = 44444444
via_ir = true
ffi = true
fs_permissions = [{ access = "read-write", path = ".forge-snapshots/"}, { access = "read", path = "./out"}, {access = "read", path = "./test/bin"}]
solc = "0.8.26"
evm_version = "cancun"
gas_limit = "300000000"
bytecode_hash = "none"

[profile.default.fuzz]
runs = 1000
Expand Down
1 change: 1 addition & 0 deletions packages/foundry/lib/universal-router
Submodule universal-router added at 3663f6
1 change: 1 addition & 0 deletions packages/foundry/lib/v2-core
Submodule v2-core added at 4dd590
1 change: 1 addition & 0 deletions packages/foundry/lib/v3-core
Submodule v3-core added at d8b1c6
8 changes: 6 additions & 2 deletions packages/foundry/remappings.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts
@openzeppelin/uniswap-hooks/=lib/uniswap-hooks/
v4-core/=lib/uniswap-hooks/lib/v4-core/
v4-periphery/=lib/uniswap-hooks/lib/v4-periphery/
@uniswap/v4-periphery/=lib/uniswap-hooks/lib/v4-periphery/
@uniswap/universal-router/=lib/universal-router/
@uniswap/v4-core/=lib/uniswap-hooks/lib/v4-core/
@uniswap/v3-core/=lib/v3-core/
@uniswap/v2-core/=lib/v2-core/
v4-core/=lib/uniswap-hooks/lib/v4-core/
18 changes: 9 additions & 9 deletions packages/foundry/script/DeployLocal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import "../contracts/OpHook.sol";
import "../contracts/IOptionToken.sol";
import "../contracts/MockOptionToken.sol";
import {HookMiner} from "lib/uniswap-hooks/lib/v4-periphery/src/utils/HookMiner.sol";
import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";
import {Hooks} from "v4-core/src/libraries/Hooks.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {IWETH9} from "lib/v4-periphery/src/interfaces/external/IWETH9.sol";


import {BaseHook} from "@openzeppelin/uniswap-hooks/src/base/BaseHook.sol";

import {Hooks} from "v4-core/src/libraries/Hooks.sol";
import {IPoolManager, SwapParams} from "v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "v4-core/src/types/PoolKey.sol";
import {PoolIdLibrary} from "v4-core/src/types/PoolId.sol";
import {BeforeSwapDelta, toBeforeSwapDelta} from "v4-core/src/types/BeforeSwapDelta.sol";
import {Currency} from "v4-core/src/types/Currency.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {IPoolManager, SwapParams} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol";
import {BeforeSwapDelta, toBeforeSwapDelta} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";

import {ERC4626} from "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
Expand All @@ -37,7 +37,7 @@ import {OptionPrice, IUniswapV3Pool} from "../contracts/OptionPrice.sol";
import {IOptionToken} from "../contracts/IOptionToken.sol";
import {IPermit2} from "../contracts/IPermit2.sol";

import {IHooks} from "v4-core/src/interfaces/IHooks.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";

contract MockERC20 is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {
Expand Down
18 changes: 9 additions & 9 deletions packages/foundry/test/EndToEnd.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import {OptionPrice} from "../contracts/OptionPrice.sol";
import {OpHook} from "../contracts/OpHook.sol";

// Uniswap V4 imports
import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";
import {Hooks} from "v4-core/src/libraries/Hooks.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {PoolKey} from "v4-core/src/types/PoolKey.sol";
import {Currency} from "v4-core/src/types/Currency.sol";
import {SwapParams} from "v4-core/src/interfaces/IPoolManager.sol";
import {PoolManager} from "v4-core/src/PoolManager.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {SwapParams} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolManager} from "@uniswap/v4-core/src/PoolManager.sol";
import {HookMiner} from "lib/uniswap-hooks/lib/v4-periphery/src/utils/HookMiner.sol";
import {Constants} from "@uniswap/v4-core/test/utils/Constants.sol";
import {PoolSwapTest} from "v4-core/src/test/PoolSwapTest.sol";
import {IHooks} from "v4-core/src/interfaces/IHooks.sol";
import {PoolSwapTest} from "@uniswap/v4-core/src/test/PoolSwapTest.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";

// import {Deployers} from "v4-core/test/utils/Deployers.sol";
// import {Deployers} from "@uniswap/v4-core/test/utils/Deployers.sol";

contract TestERC20 is IERC20 {
string public name;
Expand Down
Loading
Loading