From 299360277bd715d5f8c9a88b02f9e622f478d32c Mon Sep 17 00:00:00 2001 From: saucepoint Date: Mon, 22 Jan 2024 14:22:14 -0500 Subject: [PATCH 1/7] forge fmt --- forge-test/Counter.t.sol | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/forge-test/Counter.t.sol b/forge-test/Counter.t.sol index 03f54023..69f7cf51 100644 --- a/forge-test/Counter.t.sol +++ b/forge-test/Counter.t.sol @@ -30,8 +30,10 @@ contract CounterTest is HookTest, GasSnapshot { HookTest.initHookTestEnv(); // Deploy the hook to an address with the correct flags - uint160 flags = - uint160(Hooks.BEFORE_SWAP_FLAG | Hooks.AFTER_SWAP_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG | Hooks.BEFORE_REMOVE_LIQUIDITY_FLAG); + uint160 flags = uint160( + Hooks.BEFORE_SWAP_FLAG | Hooks.AFTER_SWAP_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG + | Hooks.BEFORE_REMOVE_LIQUIDITY_FLAG + ); (address hookAddress, bytes32 salt) = HookMiner.find(address(this), flags, type(Counter).creationCode, abi.encode(address(manager))); counter = new Counter{salt: salt}(IPoolManager(address(manager))); From b71e7123454d2102b2fc7b4601cabda5a865679e Mon Sep 17 00:00:00 2001 From: saucepoint Date: Mon, 22 Jan 2024 16:28:18 -0500 Subject: [PATCH 2/7] bump mining depth --- forge-test/utils/HookMiner.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forge-test/utils/HookMiner.sol b/forge-test/utils/HookMiner.sol index 31bd8144..ca421b5b 100644 --- a/forge-test/utils/HookMiner.sol +++ b/forge-test/utils/HookMiner.sol @@ -8,7 +8,7 @@ library HookMiner { uint160 constant FLAG_MASK = 0xFFF << 148; // Maximum number of iterations to find a salt, avoid infinite loops - uint256 constant MAX_LOOP = 10_000; + uint256 constant MAX_LOOP = 20_000; /// @notice Find a salt that produces a hook address with the desired `flags` /// @param deployer The address that will deploy the hook. In `forge test`, this will be the test contract `address(this)` or the pranking address From fdf2dd72e2661a5595c14c23b44be2050ac4ab30 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Mon, 22 Jan 2024 16:28:50 -0500 Subject: [PATCH 3/7] initial dynamic fee contracts --- forge-test/DynamicFees.t.sol | 185 ++++++++++++++++++ src/pages/fees/dynamic-fee/AutoDynamicFee.sol | 63 ++++++ .../fees/dynamic-fee/ManualDynamicFee.sol | 53 +++++ .../dynamic-fee/UpdateDynamicFee.solsnippet | 6 + src/pages/fees/dynamic-fee/index.md | 25 +++ 5 files changed, 332 insertions(+) create mode 100644 forge-test/DynamicFees.t.sol create mode 100644 src/pages/fees/dynamic-fee/AutoDynamicFee.sol create mode 100644 src/pages/fees/dynamic-fee/ManualDynamicFee.sol create mode 100644 src/pages/fees/dynamic-fee/UpdateDynamicFee.solsnippet create mode 100644 src/pages/fees/dynamic-fee/index.md diff --git a/forge-test/DynamicFees.t.sol b/forge-test/DynamicFees.t.sol new file mode 100644 index 00000000..e5043c0c --- /dev/null +++ b/forge-test/DynamicFees.t.sol @@ -0,0 +1,185 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.19; + +import "forge-std/Test.sol"; +import {IHooks} from "v4-core/interfaces/IHooks.sol"; +import {Hooks} from "v4-core/libraries/Hooks.sol"; +import {TickMath} from "v4-core/libraries/TickMath.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 {Constants} from "v4-core/../test/utils/Constants.sol"; +import {CurrencyLibrary, Currency} from "v4-core/types/Currency.sol"; +import {FeeLibrary} from "v4-core/libraries/FeeLibrary.sol"; +import {HookTest} from "@v4-by-example/utils/HookTest.sol"; +import {HookMiner} from "./utils/HookMiner.sol"; +import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol"; +import {PoolSwapTest} from "v4-core/test/PoolSwapTest.sol"; +import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol"; + +import {ManualDynamicFee} from "@v4-by-example/pages/fees/dynamic-fee/ManualDynamicFee.sol"; +import {AutoDynamicFee} from "@v4-by-example/pages/fees/dynamic-fee/AutoDynamicFee.sol"; + +contract DynamicFeesTest is HookTest, GasSnapshot { + using FixedPointMathLib for uint256; + using PoolIdLibrary for PoolKey; + using CurrencyLibrary for Currency; + + AutoDynamicFee autoDynamicFee; + ManualDynamicFee manualDynamicFee; + + PoolKey autoDynamicFeePoolKey; + PoolKey manualDynamicFeePoolKey; + + function setUp() public { + // creates the pool manager, test tokens, and other utility routers + HookTest.initHookTestEnv(); + + // Deploy the hook to an address with the correct flags + uint160 flags = uint160(Hooks.BEFORE_SWAP_FLAG); + (address hookAddress, bytes32 salt) = + HookMiner.find(address(this), flags, type(AutoDynamicFee).creationCode, abi.encode(address(manager))); + autoDynamicFee = new AutoDynamicFee{salt: salt}(IPoolManager(address(manager))); + require(address(autoDynamicFee) == hookAddress, "hook address mismatch"); + + (hookAddress, salt) = + HookMiner.find(address(this), uint160(0), type(ManualDynamicFee).creationCode, abi.encode(address(manager))); + manualDynamicFee = new ManualDynamicFee{salt: salt}(IPoolManager(address(manager))); + require(address(manualDynamicFee) == hookAddress, "hook address mismatch"); + + // Create the pools + autoDynamicFeePoolKey = PoolKey( + Currency.wrap(address(token0)), + Currency.wrap(address(token1)), + FeeLibrary.DYNAMIC_FEE_FLAG, + 60, + IHooks(autoDynamicFee) + ); + initializeRouter.initialize(autoDynamicFeePoolKey, Constants.SQRT_RATIO_1_1, ZERO_BYTES); + + manualDynamicFeePoolKey = PoolKey( + Currency.wrap(address(token0)), + Currency.wrap(address(token1)), + FeeLibrary.DYNAMIC_FEE_FLAG, + 60, + IHooks(manualDynamicFee) + ); + initializeRouter.initialize(manualDynamicFeePoolKey, Constants.SQRT_RATIO_1_1, ZERO_BYTES); + + // Provide liquidity to the pool + modifyPositionRouter.modifyLiquidity( + autoDynamicFeePoolKey, + IPoolManager.ModifyLiquidityParams(TickMath.minUsableTick(60), TickMath.maxUsableTick(60), 100000 ether), + ZERO_BYTES + ); + + modifyPositionRouter.modifyLiquidity( + manualDynamicFeePoolKey, + IPoolManager.ModifyLiquidityParams(TickMath.minUsableTick(60), TickMath.maxUsableTick(60), 100000 ether), + ZERO_BYTES + ); + } + + function test_start_autoFee() public { + // Perform a test swap // + int256 amount = 1e18; + bool zeroForOne = true; + BalanceDelta swapDelta = swap(autoDynamicFeePoolKey, amount, zeroForOne, ZERO_BYTES); + // ------------------- // + + // fee on output token, so expect ~0.95e18 output + assertLt(uint256(-int256(swapDelta.amount1())), 0.95e18); + assertGt(uint256(-int256(swapDelta.amount1())), 0.94e18); + assertApproxEqAbs( + uint256(-int256(swapDelta.amount1())), uint256(amount), uint256(amount).mulWadDown(0.05001e18) + ); + } + + function test_floor_autoFee() public { + // skip 496,000 seconds, fee is now floored at 0.05% + skip(496000); + + // Perform a test swap // + int256 amount = 1e18; + bool zeroForOne = true; + BalanceDelta swapDelta = swap(autoDynamicFeePoolKey, amount, zeroForOne, ZERO_BYTES); + // ------------------- // + + assertLt(uint256(-int256(swapDelta.amount1())), 0.9995e18); + assertGt(uint256(-int256(swapDelta.amount1())), 0.9994e18); + assertApproxEqAbs( + uint256(-int256(swapDelta.amount1())), uint256(amount), uint256(amount).mulWadDown(0.00051e18) + ); + } + + function test_start_manualFee() public { + // Perform a test swap // + int256 amount = 1e18; + bool zeroForOne = true; + BalanceDelta swapDelta = swap(manualDynamicFeePoolKey, amount, zeroForOne, ZERO_BYTES); + // ------------------- // + + // fee on output token, so expect ~0.95e18 output + assertLt(uint256(-int256(swapDelta.amount1())), 0.95e18); + assertGt(uint256(-int256(swapDelta.amount1())), 0.94e18); + assertApproxEqAbs( + uint256(-int256(swapDelta.amount1())), uint256(amount), uint256(amount).mulWadDown(0.05001e18) + ); + } + + function test_floor_manualFee() public { + // skip 496,000 seconds, fee is now floored at 0.05% + skip(496000); + + // poke the pool manager + manager.updateDynamicSwapFee(manualDynamicFeePoolKey); + + // Perform a test swap // + int256 amount = 1e18; + bool zeroForOne = true; + BalanceDelta swapDelta = swap(manualDynamicFeePoolKey, amount, zeroForOne, ZERO_BYTES); + // ------------------- // + + assertLt(uint256(-int256(swapDelta.amount1())), 0.9995e18); + assertGt(uint256(-int256(swapDelta.amount1())), 0.9994e18); + assertApproxEqAbs( + uint256(-int256(swapDelta.amount1())), uint256(amount), uint256(amount).mulWadDown(0.00051e18) + ); + } + + function test_staleFee_manualFee() public { + // skip 496,000 seconds, but the fee is still 5% since its stale and wasnt updated + skip(496000); + + // Perform a test swap // + int256 amount = 1e18; + bool zeroForOne = true; + BalanceDelta swapDelta = swap(manualDynamicFeePoolKey, amount, zeroForOne, ZERO_BYTES); + // ------------------- // + + // fee on output token, so expect ~0.95e18 output + assertLt(uint256(-int256(swapDelta.amount1())), 0.95e18); + assertGt(uint256(-int256(swapDelta.amount1())), 0.94e18); + assertApproxEqAbs( + uint256(-int256(swapDelta.amount1())), uint256(amount), uint256(amount).mulWadDown(0.05001e18) + ); + } + + function test_snapshot_autoFee() public { + int256 amount = 1e18; + bool zeroForOne = true; + IPoolManager.SwapParams memory params = IPoolManager.SwapParams({ + zeroForOne: zeroForOne, + amountSpecified: amount, + sqrtPriceLimitX96: zeroForOne ? MIN_PRICE_LIMIT : MAX_PRICE_LIMIT // unlimited impact + }); + + PoolSwapTest.TestSettings memory testSettings = + PoolSwapTest.TestSettings({withdrawTokens: true, settleUsingTransfer: true, currencyAlreadySent: false}); + + snapStart("autodynamic fee"); + swapRouter.swap(autoDynamicFeePoolKey, params, testSettings, ZERO_BYTES); + snapEnd(); + } +} diff --git a/src/pages/fees/dynamic-fee/AutoDynamicFee.sol b/src/pages/fees/dynamic-fee/AutoDynamicFee.sol new file mode 100644 index 00000000..334c892f --- /dev/null +++ b/src/pages/fees/dynamic-fee/AutoDynamicFee.sol @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.19; + +// TODO: update to v4-periphery/BaseHook.sol when its compatible +import {BaseHook} from "@v4-by-example/utils/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 {IDynamicFeeManager} from "v4-core/interfaces/IDynamicFeeManager.sol"; + +/// @notice A time-decaying dynamically fee, updated automatically with beforeSwap() +contract AutoDynamicFee is BaseHook, IDynamicFeeManager { + uint256 public immutable startTimestamp; + + // Start at 5% fee, decaying at rate of 0.00001% per second + // after 495,000 seconds (5.72 days), fee will be a minimum of 0.05% + // NOTE: because fees are uint24, we will lose some precision + uint128 public constant START_FEE = 500000; // represents 5% + uint128 public constant MIN_FEE = 500; // minimum fee of 0.05% + + uint128 public constant decayRate = 1; // 0.00001% per second + + constructor(IPoolManager _poolManager) BaseHook(_poolManager) { + startTimestamp = block.timestamp; + } + + /// @inheritdoc IDynamicFeeManager + function getFee(address, PoolKey calldata) external view override returns (uint24 _currentFee) { + unchecked { + uint256 timeElapsed = block.timestamp - startTimestamp; + _currentFee = timeElapsed > 495000 ? uint24(MIN_FEE) : uint24((START_FEE - (timeElapsed * decayRate)) / 10); + } + } + + /// @dev this example hook contract does not implement any hooks + 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, + noOp: false, + accessLock: false + }); + } + + function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata, bytes calldata) + external + override + returns (bytes4) + { + // poke the poolmanager to update the fee for every swap + poolManager.updateDynamicSwapFee(key); + return BaseHook.beforeSwap.selector; + } +} diff --git a/src/pages/fees/dynamic-fee/ManualDynamicFee.sol b/src/pages/fees/dynamic-fee/ManualDynamicFee.sol new file mode 100644 index 00000000..bef8997a --- /dev/null +++ b/src/pages/fees/dynamic-fee/ManualDynamicFee.sol @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.19; + +// TODO: update to v4-periphery/BaseHook.sol when its compatible +import {BaseHook} from "@v4-by-example/utils/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 {IDynamicFeeManager} from "v4-core/interfaces/IDynamicFeeManager.sol"; + +/// @notice A time-decaying dynamically fee, updated manually with external PoolManager.updateDynamicSwapFee() calls +contract ManualDynamicFee is BaseHook, IDynamicFeeManager { + uint256 public immutable startTimestamp; + + // Start at 5% fee, decaying at rate of 0.00001% per second + // after 495,000 seconds (5.72 days), fee will be a minimum of 0.05% + // NOTE: because fees are uint24, we will lose some precision + uint128 public constant START_FEE = 500000; // represents 5% + uint128 public constant MIN_FEE = 500; // minimum fee of 0.05% + + uint128 public constant decayRate = 1; // 0.00001% per second + + constructor(IPoolManager _poolManager) BaseHook(_poolManager) { + startTimestamp = block.timestamp; + } + + /// @inheritdoc IDynamicFeeManager + function getFee(address, PoolKey calldata) external view override returns (uint24 _currentFee) { + unchecked { + uint256 timeElapsed = block.timestamp - startTimestamp; + _currentFee = timeElapsed > 495000 ? uint24(MIN_FEE) : uint24((START_FEE - (timeElapsed * decayRate)) / 10); + } + } + + /// @dev this example hook contract does not implement any hooks + 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: false, + afterSwap: false, + beforeDonate: false, + afterDonate: false, + noOp: false, + accessLock: false + }); + } +} diff --git a/src/pages/fees/dynamic-fee/UpdateDynamicFee.solsnippet b/src/pages/fees/dynamic-fee/UpdateDynamicFee.solsnippet new file mode 100644 index 00000000..ea580eb3 --- /dev/null +++ b/src/pages/fees/dynamic-fee/UpdateDynamicFee.solsnippet @@ -0,0 +1,6 @@ +import {IPoolManager} from "v4-core/interfaces/IPoolManager.sol"; +import {PoolKey} from "v4-core/types/PoolKey.sol"; + +// poke the poolmanager to update the "dynamic fee" +PoolKey memory key = PoolKey(...); +poolManager.updateDynamicSwapFee(key); diff --git a/src/pages/fees/dynamic-fee/index.md b/src/pages/fees/dynamic-fee/index.md new file mode 100644 index 00000000..8e9c1f1e --- /dev/null +++ b/src/pages/fees/dynamic-fee/index.md @@ -0,0 +1,25 @@ +--- +title: TEMPLATE +version: 0.8.20 +description: TEMPLATE +keywords: [template, example] +--- + +- SUBTITLE + +TEMPLATE INFO + +--- + +## Example TEMPLATE + +TEMPLATE +```solidity +{{{Template}}} +``` + +## Example TEMPLATE-SNIPPET + +```solidity +{{{TemplateSnippet}}} +``` From f16454eef3e678c5b540f9f51a3abaa29e96f097 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Mon, 22 Jan 2024 16:31:12 -0500 Subject: [PATCH 4/7] snapshots --- .forge-snapshots/autodynamic fee.snap | 1 + .forge-snapshots/counter.snap | 2 +- .forge-snapshots/hookFee.snap | 2 +- forge-test/DynamicFees.t.sol | 1 + 4 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 .forge-snapshots/autodynamic fee.snap diff --git a/.forge-snapshots/autodynamic fee.snap b/.forge-snapshots/autodynamic fee.snap new file mode 100644 index 00000000..1716e303 --- /dev/null +++ b/.forge-snapshots/autodynamic fee.snap @@ -0,0 +1 @@ +242938 \ No newline at end of file diff --git a/.forge-snapshots/counter.snap b/.forge-snapshots/counter.snap index 7573abf5..4a86f960 100644 --- a/.forge-snapshots/counter.snap +++ b/.forge-snapshots/counter.snap @@ -1 +1 @@ -339161 \ No newline at end of file +358444 \ No newline at end of file diff --git a/.forge-snapshots/hookFee.snap b/.forge-snapshots/hookFee.snap index c0ced99f..16c3b790 100644 --- a/.forge-snapshots/hookFee.snap +++ b/.forge-snapshots/hookFee.snap @@ -1 +1 @@ -246486 \ No newline at end of file +265804 \ No newline at end of file diff --git a/forge-test/DynamicFees.t.sol b/forge-test/DynamicFees.t.sol index e5043c0c..9abe8be1 100644 --- a/forge-test/DynamicFees.t.sol +++ b/forge-test/DynamicFees.t.sol @@ -167,6 +167,7 @@ contract DynamicFeesTest is HookTest, GasSnapshot { } function test_snapshot_autoFee() public { + skip(100_000); int256 amount = 1e18; bool zeroForOne = true; IPoolManager.SwapParams memory params = IPoolManager.SwapParams({ From c86c9766394f44ca835dfc34f3baed3a23dc0fc8 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Tue, 23 Jan 2024 12:02:57 -0500 Subject: [PATCH 5/7] gas snapshots between dynamic fees --- .forge-snapshots/autodynamic fee.snap | 2 +- .forge-snapshots/manual dynamic fee.snap | 1 + forge-test/DynamicFees.t.sol | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .forge-snapshots/manual dynamic fee.snap diff --git a/.forge-snapshots/autodynamic fee.snap b/.forge-snapshots/autodynamic fee.snap index 1716e303..58d87c97 100644 --- a/.forge-snapshots/autodynamic fee.snap +++ b/.forge-snapshots/autodynamic fee.snap @@ -1 +1 @@ -242938 \ No newline at end of file +242952 \ No newline at end of file diff --git a/.forge-snapshots/manual dynamic fee.snap b/.forge-snapshots/manual dynamic fee.snap new file mode 100644 index 00000000..8b180eb5 --- /dev/null +++ b/.forge-snapshots/manual dynamic fee.snap @@ -0,0 +1 @@ +219217 \ No newline at end of file diff --git a/forge-test/DynamicFees.t.sol b/forge-test/DynamicFees.t.sol index 9abe8be1..37b85bcf 100644 --- a/forge-test/DynamicFees.t.sol +++ b/forge-test/DynamicFees.t.sol @@ -183,4 +183,25 @@ contract DynamicFeesTest is HookTest, GasSnapshot { swapRouter.swap(autoDynamicFeePoolKey, params, testSettings, ZERO_BYTES); snapEnd(); } + + function test_snapshot_manualFee() public { + skip(100_000); + // poke the pool manager + manager.updateDynamicSwapFee(manualDynamicFeePoolKey); + + int256 amount = 1e18; + bool zeroForOne = true; + IPoolManager.SwapParams memory params = IPoolManager.SwapParams({ + zeroForOne: zeroForOne, + amountSpecified: amount, + sqrtPriceLimitX96: zeroForOne ? MIN_PRICE_LIMIT : MAX_PRICE_LIMIT // unlimited impact + }); + + PoolSwapTest.TestSettings memory testSettings = + PoolSwapTest.TestSettings({withdrawTokens: true, settleUsingTransfer: true, currencyAlreadySent: false}); + + snapStart("manual dynamic fee"); + swapRouter.swap(manualDynamicFeePoolKey, params, testSettings, ZERO_BYTES); + snapEnd(); + } } From 034a44ee80122c6d0127bac2d43ccf5b5ef722e8 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Tue, 23 Jan 2024 12:03:19 -0500 Subject: [PATCH 6/7] dynamic fee page --- src/keywords.json | 7 + src/nav.ts | 4 + .../fees/dynamic-fee/ManualDynamicFee.sol | 4 +- src/pages/fees/dynamic-fee/index.html.ts | 184 ++++++++++++++++++ src/pages/fees/dynamic-fee/index.md | 54 ++++- src/pages/fees/dynamic-fee/index.tsx | 29 +++ src/routes.tsx | 5 + src/search.json | 15 +- 8 files changed, 290 insertions(+), 12 deletions(-) create mode 100644 src/pages/fees/dynamic-fee/index.html.ts create mode 100644 src/pages/fees/dynamic-fee/index.tsx diff --git a/src/keywords.json b/src/keywords.json index 5ebfb955..298e092a 100644 --- a/src/keywords.json +++ b/src/keywords.json @@ -45,6 +45,13 @@ "static fee", "hook fee" ], + "/fees/dynamic-fee": [ + "fee", + "fees", + "dynamic fee", + "dynamic", + "poke" + ], "/create-liquidity": [ "liquidity", "LP", diff --git a/src/nav.ts b/src/nav.ts index b995a19b..abb8c262 100644 --- a/src/nav.ts +++ b/src/nav.ts @@ -44,6 +44,10 @@ const FEE_ROUTES: Route[] = [ { path: "fixed-hook-fee", title: "Static Hook Fee" + }, + { + path: "dynamic-fee", + title: "Dynamic Swap Fee" } ] diff --git a/src/pages/fees/dynamic-fee/ManualDynamicFee.sol b/src/pages/fees/dynamic-fee/ManualDynamicFee.sol index bef8997a..01537e32 100644 --- a/src/pages/fees/dynamic-fee/ManualDynamicFee.sol +++ b/src/pages/fees/dynamic-fee/ManualDynamicFee.sol @@ -25,8 +25,10 @@ contract ManualDynamicFee is BaseHook, IDynamicFeeManager { startTimestamp = block.timestamp; } - /// @inheritdoc IDynamicFeeManager + /// @dev Deteremines a Pool's swap fee. Called and cached by PoolManager.updateDynamicFee() function getFee(address, PoolKey calldata) external view override returns (uint24 _currentFee) { + // Linearly decaying fee, y = mx + b + // After 495,000 seconds (5.72 days), fee will be a minimum of 0.05% unchecked { uint256 timeElapsed = block.timestamp - startTimestamp; _currentFee = timeElapsed > 495000 ? uint24(MIN_FEE) : uint24((START_FEE - (timeElapsed * decayRate)) / 10); diff --git a/src/pages/fees/dynamic-fee/index.html.ts b/src/pages/fees/dynamic-fee/index.html.ts new file mode 100644 index 00000000..5fbf863d --- /dev/null +++ b/src/pages/fees/dynamic-fee/index.html.ts @@ -0,0 +1,184 @@ +// metadata +export const version = "0.8.20" +export const title = "Dynamic Fees" +export const description = "Design a v4 pool with a dynamic fee" + +export const keywords = [ + "fee", + "fees", + "dynamic fee", + "dynamic", + "poke", +] + +export const codes = [ + { + fileName: "AutoDynamicFee.sol", + code: "Ly8gU1BEWC1MaWNlbnNlLUlkZW50aWZpZXI6IE1JVApwcmFnbWEgc29saWRpdHkgXjAuOC4xOTsKCi8vIFRPRE86IHVwZGF0ZSB0byB2NC1wZXJpcGhlcnkvQmFzZUhvb2suc29sIHdoZW4gaXRzIGNvbXBhdGlibGUKaW1wb3J0IHtCYXNlSG9va30gZnJvbSAiQHY0LWJ5LWV4YW1wbGUvdXRpbHMvQmFzZUhvb2suc29sIjsKCmltcG9ydCB7SG9va3N9IGZyb20gInY0LWNvcmUvbGlicmFyaWVzL0hvb2tzLnNvbCI7CmltcG9ydCB7SVBvb2xNYW5hZ2VyfSBmc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MgKEhvb2tzLlBlcm1pc3Npb25zIG1lbW9yeSkgewogICAgICAgIHJldHVybiBIb29rcy5QZXJtaXNzaW9ucyh7CiAgICAgICAgICAgIGJlZm9yZUluaXRpYWxpemU6IGZhbHNlLAogICAgICAgICAgICBhZnRlckluaXRpYWxpemU6IGZhbHNlLAogICAgICAgICAgICBiZWZvcmVBZGRMaXF1aWRpdHk6IGZhbHNlLAogICAgICAgICAgICBhZnRlckFkZExpcXVpZGl0eTogZmFsc2UsCiAgICAgICAgICAgIGJlZm9yZVJlbW92ZUxpcXVpZGl0eTogZmFsc2UsCiAgICAgICAgICAgIGFmdGVyUmVtb3ZlTGlxdWlkaXR5OiBmYWxzZSwKICAgICAgICAgICAgYmVmb3JlU3dhcDogZmFsc2UsCiAgICAgICAgICAgIGFmdGVyU3dhcDogZmFsc2UsCiAgICAgICAgICAgIGJlZm9yZURvbmF0ZTogZmFsc2UsCiAgICAgICAgICAgIGFmdGVyRG9uYXRlOiBmYWxzZSwKICAgICAgICAgICAgbm9PcDogZmFsc2UsCiAgICAgICAgICAgIGFjY2Vzc0xvY2s6IGZhbHNlCiAgICAgICAgfSk7CiAgICB9Cn0K", + }, + { + fileName: "UpdateDynamicFee.sol", + code: "aW1wb3J0IHtJUG9vbE1hbmFnZXJ9IGZyb20gInY0LWNvcmUvaW50ZXJmYWNlcy9JUG9vbE1hbmFnZXIuc29sIjsKaW1wb3J0IHtQb29sS2V5fSBmcm9tICJ2NC1jb3JlL3R5cGVzL1Bvb2xLZXkuc29sIjsKCi8vIHBva2UgdGhlIHBvb2xtYW5hZ2VyIHRvIHVwZGF0ZSB0aGUgImR5bmFtaWMgZmVlIgpQb29sS2V5IG1lbW9yeSBrZXkgPSBQb29sS2V5KC4uLik7CnBvb2xNYW5hZ2VyLnVwZGF0ZUR5bmFtaWNTd2FwRmVlKGtleSk7Cg==", + }, +] + +const html = `
    +
  • Design a v4 pool with a dynamic fee
  • +
+

Uniswap v4 pools can support dynamic swap fees, and do not need to adhere to a static fee (0.05% / 0.30% / 1.0%). The hook needs to inherit IDynamicFeeManager and use FeeLibrary.DYNAMIC_FEE_FLAG in its PoolKey.fee.

+

Despite its name, the fee is cached by the PoolManager and an external call PoolManager.updateDynamicFee() is required to change the swap fee.

+

Note: dynamic fees can be computed every swap, but incurs a gas overhead

+
+

Initialize a Dynamic Fee Pool

+
import {FeeLibrary} from "v4-core/libraries/FeeLibrary.sol";
+
+
+poolKey = PoolKey(
+    Currency.wrap(address(token0)),
+    Currency.wrap(address(token1)),
+    FeeLibrary.DYNAMIC_FEE_FLAG, // signal that the pool has a dynamic fee
+    60,
+    IHooks(hook)
+);
+initializeRouter.initialize(poolKey, startingPrice, hookData);
+

Example: Manual Dynamic Fee

+

Implements a time-decaying dynamic fee

+
    +
  • The swap fee starts at 5.0%
  • +
  • The fee decays 0.00001% every second
  • +
  • After 495,000 seconds, the minimum fee is set to 0.05%
  • +
+
    +
  1. Inherit IDynamicFeeManager
  2. +
  3. Implement getFee()
  4. +
  5. Poke PoolManager.updateDynamicFee() to change the fee
  6. +
+
// SPDX-License-Identifier: MIT
+pragma solidity ^0.8.19;
+
+// TODO: update to v4-periphery/BaseHook.sol when its compatible
+import {BaseHook} from "@v4-by-example/utils/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 {IDynamicFeeManager} from "v4-core/interfaces/IDynamicFeeManager.sol";
+
+/// @notice A time-decaying dynamically fee, updated manually with external PoolManager.updateDynamicSwapFee() calls
+contract ManualDynamicFee is BaseHook, IDynamicFeeManager {
+    uint256 public immutable startTimestamp;
+
+    // Start at 5% fee, decaying at rate of 0.00001% per second
+    // after 495,000 seconds (5.72 days), fee will be a minimum of 0.05%
+    // NOTE: because fees are uint24, we will lose some precision
+    uint128 public constant START_FEE = 500000; // represents 5%
+    uint128 public constant MIN_FEE = 500; // minimum fee of 0.05%
+
+    uint128 public constant decayRate = 1; // 0.00001% per second
+
+    constructor(IPoolManager _poolManager) BaseHook(_poolManager) {
+        startTimestamp = block.timestamp;
+    }
+
+    /// @dev Deteremines a Pool's swap fee. Called and cached by PoolManager.updateDynamicFee()
+    function getFee(address, PoolKey calldata) external view override returns (uint24 _currentFee) {
+        // Linearly decaying fee, y = mx + b
+        // After 495,000 seconds (5.72 days), fee will be a minimum of 0.05%
+        unchecked {
+            uint256 timeElapsed = block.timestamp - startTimestamp;
+            _currentFee = timeElapsed > 495000 ? uint24(MIN_FEE) : uint24((START_FEE - (timeElapsed * decayRate)) / 10);
+        }
+    }
+
+    /// @dev this example hook contract does not implement any hooks
+    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: false,
+            afterSwap: false,
+            beforeDonate: false,
+            afterDonate: false,
+            noOp: false,
+            accessLock: false
+        });
+    }
+}
+

Example: Automatic Dynamic Fee

+

Implements an automatically-updated, time-decaying dynamic fee

+

The hook uses beforeSwap to automatically poke the PoolManager, ensuring the fee is always up-to-date

+

incurs +23,000 gas overhead

+
// SPDX-License-Identifier: MIT
+pragma solidity ^0.8.19;
+
+// TODO: update to v4-periphery/BaseHook.sol when its compatible
+import {BaseHook} from "@v4-by-example/utils/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 {IDynamicFeeManager} from "v4-core/interfaces/IDynamicFeeManager.sol";
+
+/// @notice A time-decaying dynamically fee, updated automatically with beforeSwap()
+contract AutoDynamicFee is BaseHook, IDynamicFeeManager {
+    uint256 public immutable startTimestamp;
+
+    // Start at 5% fee, decaying at rate of 0.00001% per second
+    // after 495,000 seconds (5.72 days), fee will be a minimum of 0.05%
+    // NOTE: because fees are uint24, we will lose some precision
+    uint128 public constant START_FEE = 500000; // represents 5%
+    uint128 public constant MIN_FEE = 500; // minimum fee of 0.05%
+
+    uint128 public constant decayRate = 1; // 0.00001% per second
+
+    constructor(IPoolManager _poolManager) BaseHook(_poolManager) {
+        startTimestamp = block.timestamp;
+    }
+
+    /// @inheritdoc IDynamicFeeManager
+    function getFee(address, PoolKey calldata) external view override returns (uint24 _currentFee) {
+        unchecked {
+            uint256 timeElapsed = block.timestamp - startTimestamp;
+            _currentFee = timeElapsed > 495000 ? uint24(MIN_FEE) : uint24((START_FEE - (timeElapsed * decayRate)) / 10);
+        }
+    }
+
+    /// @dev this example hook contract does not implement any hooks
+    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,
+            noOp: false,
+            accessLock: false
+        });
+    }
+
+    function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata, bytes calldata)
+        external
+        override
+        returns (bytes4)
+    {
+        // poke the poolmanager to update the fee for every swap
+        poolManager.updateDynamicSwapFee(key);
+        return BaseHook.beforeSwap.selector;
+    }
+}
+
` + +export default html diff --git a/src/pages/fees/dynamic-fee/index.md b/src/pages/fees/dynamic-fee/index.md index 8e9c1f1e..b0ef5baf 100644 --- a/src/pages/fees/dynamic-fee/index.md +++ b/src/pages/fees/dynamic-fee/index.md @@ -1,25 +1,59 @@ --- -title: TEMPLATE +title: Dynamic Fees version: 0.8.20 -description: TEMPLATE -keywords: [template, example] +description: Design a v4 pool with a dynamic fee +keywords: [fee, fees, dynamic fee, dynamic, poke] --- -- SUBTITLE +- Design a v4 pool with a dynamic fee -TEMPLATE INFO +Uniswap v4 pools can support dynamic swap fees, and do not need to adhere to a static fee (0.05% / 0.30% / 1.0%). The hook needs to inherit `IDynamicFeeManager` and use `FeeLibrary.DYNAMIC_FEE_FLAG` in its `PoolKey.fee`. + +Despite its name, the fee is *cached* by the `PoolManager` and an external call `PoolManager.updateDynamicFee()` is required to change the swap fee. + +**Note: dynamic fees can be computed every swap, but incurs a gas overhead** --- -## Example TEMPLATE +### Initialize a Dynamic Fee Pool -TEMPLATE ```solidity -{{{Template}}} +import {FeeLibrary} from "v4-core/libraries/FeeLibrary.sol"; + + +poolKey = PoolKey( + Currency.wrap(address(token0)), + Currency.wrap(address(token1)), + FeeLibrary.DYNAMIC_FEE_FLAG, // signal that the pool has a dynamic fee + 60, + IHooks(hook) +); +initializeRouter.initialize(poolKey, startingPrice, hookData); ``` -## Example TEMPLATE-SNIPPET +## Example: Manual Dynamic Fee + +*Implements a time-decaying dynamic fee* + +* The swap fee starts at 5.0% +* The fee decays 0.00001% every second +* After 495,000 seconds, the minimum fee is set to 0.05% + + +1) Inherit `IDynamicFeeManager` +2) Implement `getFee()` +3) Poke `PoolManager.updateDynamicFee()` to change the fee +```solidity +{{{ManualDynamicFee}}} +``` + +## Example: Automatic Dynamic Fee + +*Implements an automatically-updated, time-decaying dynamic fee* + +The hook uses `beforeSwap` to automatically poke the PoolManager, ensuring the fee is always up-to-date +*incurs +23,000 gas overhead* ```solidity -{{{TemplateSnippet}}} +{{{AutoDynamicFee}}} ``` diff --git a/src/pages/fees/dynamic-fee/index.tsx b/src/pages/fees/dynamic-fee/index.tsx new file mode 100644 index 00000000..1c63a4f4 --- /dev/null +++ b/src/pages/fees/dynamic-fee/index.tsx @@ -0,0 +1,29 @@ +import React from "react" +import Example from "../../../components/Example" +import html, { version, title, description, codes } from "./index.html" + +interface Path { + path: string + title: string +} + +interface Props { + prev: Path | null + next: Path | null +} + +const ExamplePage: React.FC = ({ prev, next }) => { + return ( + + ) +} + +export default ExamplePage diff --git a/src/routes.tsx b/src/routes.tsx index e87863ef..023ff0a0 100644 --- a/src/routes.tsx +++ b/src/routes.tsx @@ -1,4 +1,5 @@ import component_create_liquidity from "./pages/create-liquidity" +import component_fees_dynamic_fee from "./pages/fees/dynamic-fee" import component_fees_fixed_hook_fee from "./pages/fees/fixed-hook-fee" import component_hooks_custom_curve from "./pages/hooks/custom-curve" import component_hooks_no_op from "./pages/hooks/no-op" @@ -28,6 +29,10 @@ const routes: Route[] = [ path: "/create-liquidity", component: component_create_liquidity }, + { + path: "/fees/dynamic-fee", + component: component_fees_dynamic_fee + }, { path: "/fees/fixed-hook-fee", component: component_fees_fixed_hook_fee diff --git a/src/search.json b/src/search.json index a6562aa2..17493745 100644 --- a/src/search.json +++ b/src/search.json @@ -79,7 +79,8 @@ "/hooks/custom-curve" ], "fee": [ - "/fees/fixed-hook-fee" + "/fees/fixed-hook-fee", + "/fees/dynamic-fee" ], "static fee": [ "/fees/fixed-hook-fee" @@ -87,6 +88,18 @@ "hook fee": [ "/fees/fixed-hook-fee" ], + "fees": [ + "/fees/dynamic-fee" + ], + "dynamic fee": [ + "/fees/dynamic-fee" + ], + "dynamic": [ + "/fees/dynamic-fee" + ], + "poke": [ + "/fees/dynamic-fee" + ], "liquidity": [ "/create-liquidity" ], From 81780e49ab9edcec0fddc80c7608e5b25bd196b4 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Tue, 23 Jan 2024 12:06:26 -0500 Subject: [PATCH 7/7] changelog --- README.md | 2 ++ src/pages/index.tsx | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 737e983c..16f36880 100644 --- a/README.md +++ b/README.md @@ -54,3 +54,5 @@ Generate react, register routes, and rebuild search index ```bash npm run generate ``` + +Update the [changelog](src/pages/index.tsx#L11) \ No newline at end of file diff --git a/src/pages/index.tsx b/src/pages/index.tsx index bf408dfc..2bdaac71 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -8,7 +8,7 @@ import styles from "./index.module.css" import youTube from "../components/youtube.png" import { ROUTES, ROUTES_BY_CATEGORY, TRANSLATIONS } from "../nav" -const UPDATES = ["2024/01/08 - Quoter", "2023/12/15 - Update v4", "2023/12/11 - Custom Curves", "2023/12/03 - Static Hook Fee", "2023/11/28 - Updated pool initialization", "2023/11/28 - NoOp", "2023/11/13 - Make snippets concise", "2023/10/18 - Initial V4 Snippets"] +const UPDATES = ["2024/01/23 - Dynamic Fees", "2024/01/08 - Quoter", "2023/12/15 - Update v4", "2023/12/11 - Custom Curves", "2023/12/03 - Static Hook Fee", "2023/11/28 - Updated pool initialization", "2023/11/28 - NoOp", "2023/11/13 - Make snippets concise", "2023/10/18 - Initial V4 Snippets"] export default function HomePage() { const [query, setQuery] = useState("")