-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Reference ]Feat/dynamic fee hook #11
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e3ec155
feat: add sample dynamicFee hook
chefburger 41fe511
chore: redeploy sample dynamic fee hook
chefburger 6a4664b
chore: redeploy the latest contracts
chefburger 427dcdf
fix: return correct selector for beforeInit* hook
chefburger 278c024
Merge pull request #18 from pancakeswap/fix/correct-selector
chefburger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.24; | ||
|
||
import "forge-std/Script.sol"; | ||
import {BaseScript} from "./BaseScript.sol"; | ||
|
||
import {SampleCLDynamicFeeHook} from "../src/pool-cl/dynamic-fee/SampleCLDynamicFeeHook.sol"; | ||
import {ICLPoolManager} from "pancake-v4-core/src/pool-cl/interfaces/ICLPoolManager.sol"; | ||
|
||
/** | ||
* forge script script/04_DeploySampleCLDynamicFeeHook.s.sol:DeploySampleCLDynamicFeeHookScript -vvv \ | ||
* --rpc-url $RPC_URL \ | ||
* --broadcast \ | ||
* --slow \ | ||
* --verify | ||
*/ | ||
contract DeploySampleCLDynamicFeeHookScript is BaseScript { | ||
function run() public { | ||
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); | ||
vm.startBroadcast(deployerPrivateKey); | ||
|
||
address clPoolManager = getAddressFromConfig("clPoolManager"); | ||
emit log_named_address("CLPoolManager", clPoolManager); | ||
|
||
SampleCLDynamicFeeHook hookAddr = new SampleCLDynamicFeeHook(ICLPoolManager(clPoolManager)); | ||
emit log_named_address("SampleCLDynamicFeeHook", address(hookAddr)); | ||
|
||
vm.stopBroadcast(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.24; | ||
|
||
import "forge-std/Script.sol"; | ||
import {BaseScript} from "./BaseScript.sol"; | ||
|
||
import {SampleBinDynamicFeeHook} from "../src/pool-bin/dynamic-fee/SampleBinDynamicFeeHook.sol"; | ||
import {IBinPoolManager} from "pancake-v4-core/src/pool-bin/interfaces/IBinPoolManager.sol"; | ||
|
||
/** | ||
* forge script script/05_DeploySampleBinDynamicFeeHook.s.sol:DeploySampleBinDynamicFeeHookScript -vvv \ | ||
* --rpc-url $RPC_URL \ | ||
* --broadcast \ | ||
* --slow \ | ||
* --verify | ||
*/ | ||
contract DeploySampleBinDynamicFeeHookScript is BaseScript { | ||
function run() public { | ||
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); | ||
vm.startBroadcast(deployerPrivateKey); | ||
|
||
address binPoolManager = getAddressFromConfig("binPoolManager"); | ||
emit log_named_address("BinPoolManager", binPoolManager); | ||
|
||
SampleBinDynamicFeeHook feeHook = new SampleBinDynamicFeeHook(IBinPoolManager(binPoolManager)); | ||
emit log_named_address("SampleBinDynamicFeeHook", address(feeHook)); | ||
|
||
vm.stopBroadcast(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
pragma solidity ^0.8.19; | ||
|
||
import "pancake-v4-core/src/pool-cl/interfaces/ICLHooks.sol"; | ||
import {PoolKey} from "pancake-v4-core/src/types/PoolKey.sol"; | ||
import {PoolId, PoolIdLibrary} from "pancake-v4-core/src/types/PoolId.sol"; | ||
import {Currency} from "pancake-v4-core/src/types/Currency.sol"; | ||
import {IBinPoolManager} from "pancake-v4-core/src/pool-bin/interfaces/IBinPoolManager.sol"; | ||
import {BinPoolManager} from "pancake-v4-core/src/pool-bin/BinPoolManager.sol"; | ||
import {LPFeeLibrary} from "pancake-v4-core/src/libraries/LPFeeLibrary.sol"; | ||
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "pancake-v4-core/src/types/BeforeSwapDelta.sol"; | ||
import {BinBaseHook} from "../BinBaseHook.sol"; | ||
|
||
contract SampleBinDynamicFeeHook is BinBaseHook { | ||
using PoolIdLibrary for PoolKey; | ||
|
||
uint24 DEFAULT_LP_FEE = 3000; | ||
uint24 FREE_LP_FEE = 0; | ||
|
||
bool enableLPFeeOverride = false; | ||
|
||
constructor(IBinPoolManager poolManager) BinBaseHook(poolManager) {} | ||
|
||
function toggleLPFeeOverride() external { | ||
enableLPFeeOverride = !enableLPFeeOverride; | ||
} | ||
|
||
function setDynamicLpFee(PoolKey memory key, uint24 fee) public { | ||
poolManager.updateDynamicLPFee(key, fee); | ||
} | ||
|
||
function getHooksRegistrationBitmap() external pure override returns (uint16) { | ||
return _hooksRegistrationBitmapFrom( | ||
Permissions({ | ||
beforeInitialize: false, | ||
afterInitialize: true, | ||
beforeMint: true, | ||
afterMint: false, | ||
beforeBurn: false, | ||
afterBurn: false, | ||
beforeSwap: true, | ||
afterSwap: false, | ||
beforeDonate: false, | ||
afterDonate: false, | ||
beforeSwapReturnDelta: false, | ||
afterSwapReturnDelta: false, | ||
afterMintReturnDelta: false, | ||
afterBurnReturnDelta: false | ||
}) | ||
); | ||
} | ||
|
||
function afterInitialize(address, PoolKey calldata key, uint24, bytes calldata) | ||
external | ||
override | ||
returns (bytes4) | ||
{ | ||
setDynamicLpFee(key, DEFAULT_LP_FEE); | ||
return this.beforeInitialize.selector; | ||
} | ||
|
||
function beforeMint(address, PoolKey calldata, IBinPoolManager.MintParams calldata, bytes calldata) | ||
external | ||
override | ||
returns (bytes4, uint24) | ||
{ | ||
// if enableLPFeeOverride, the lp fee for the ongoing inner swap will be 0 | ||
if (enableLPFeeOverride) { | ||
return (this.beforeMint.selector, LPFeeLibrary.OVERRIDE_FEE_FLAG & FREE_LP_FEE); | ||
} | ||
|
||
// otherwise, the lp fee will just be the default value | ||
return (this.beforeMint.selector, 0); | ||
} | ||
|
||
function beforeSwap(address, PoolKey calldata, bool, int128, bytes calldata) | ||
external | ||
override | ||
returns (bytes4, BeforeSwapDelta, uint24) | ||
{ | ||
// if enableLPFeeOverride, the lp fee for the ongoing swap will be 0 | ||
if (enableLPFeeOverride) { | ||
return ( | ||
this.beforeSwap.selector, | ||
BeforeSwapDeltaLibrary.ZERO_DELTA, | ||
LPFeeLibrary.OVERRIDE_FEE_FLAG & FREE_LP_FEE | ||
); | ||
} | ||
|
||
return (this.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,80 @@ | ||||||
pragma solidity ^0.8.19; | ||||||
|
||||||
import "pancake-v4-core/src/pool-cl/interfaces/ICLHooks.sol"; | ||||||
import {PoolKey} from "pancake-v4-core/src/types/PoolKey.sol"; | ||||||
import {PoolId, PoolIdLibrary} from "pancake-v4-core/src/types/PoolId.sol"; | ||||||
import {Currency} from "pancake-v4-core/src/types/Currency.sol"; | ||||||
import {ICLPoolManager} from "pancake-v4-core/src/pool-cl/interfaces/ICLPoolManager.sol"; | ||||||
import {CLPoolManager} from "pancake-v4-core/src/pool-cl/CLPoolManager.sol"; | ||||||
import {LPFeeLibrary} from "pancake-v4-core/src/libraries/LPFeeLibrary.sol"; | ||||||
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "pancake-v4-core/src/types/BeforeSwapDelta.sol"; | ||||||
import {CLBaseHook} from "../CLBaseHook.sol"; | ||||||
|
||||||
contract SampleCLDynamicFeeHook is CLBaseHook { | ||||||
using PoolIdLibrary for PoolKey; | ||||||
|
||||||
uint24 DEFAULT_LP_FEE = 3000; | ||||||
uint24 FREE_LP_FEE = 0; | ||||||
|
||||||
bool enableLPFeeOverride = false; | ||||||
|
||||||
constructor(ICLPoolManager poolManager) CLBaseHook(poolManager) {} | ||||||
|
||||||
function toggleLPFeeOverride() external { | ||||||
enableLPFeeOverride = !enableLPFeeOverride; | ||||||
} | ||||||
|
||||||
function setDynamicLpFee(PoolKey memory key, uint24 fee) public { | ||||||
poolManager.updateDynamicLPFee(key, fee); | ||||||
} | ||||||
|
||||||
function getHooksRegistrationBitmap() external pure override returns (uint16) { | ||||||
return _hooksRegistrationBitmapFrom( | ||||||
Permissions({ | ||||||
beforeInitialize: false, | ||||||
afterInitialize: true, | ||||||
beforeAddLiquidity: false, | ||||||
afterAddLiquidity: false, | ||||||
beforeRemoveLiquidity: false, | ||||||
afterRemoveLiquidity: false, | ||||||
beforeSwap: true, | ||||||
afterSwap: false, | ||||||
beforeDonate: false, | ||||||
afterDonate: false, | ||||||
beforeSwapReturnsDelta: false, | ||||||
afterSwapReturnsDelta: false, | ||||||
afterAddLiquidiyReturnsDelta: false, | ||||||
afterRemoveLiquidiyReturnsDelta: false | ||||||
}) | ||||||
); | ||||||
} | ||||||
|
||||||
function afterInitialize( | ||||||
address sender, | ||||||
PoolKey calldata key, | ||||||
uint160 sqrtPriceX96, | ||||||
int24 tick, | ||||||
bytes calldata hookData | ||||||
) external override returns (bytes4) { | ||||||
setDynamicLpFee(key, DEFAULT_LP_FEE); | ||||||
return this.beforeInitialize.selector; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it, do u want me to redeploy the contract ? |
||||||
} | ||||||
|
||||||
function beforeSwap( | ||||||
address sender, | ||||||
PoolKey calldata key, | ||||||
ICLPoolManager.SwapParams calldata params, | ||||||
bytes calldata hookData | ||||||
) external override returns (bytes4, BeforeSwapDelta, uint24) { | ||||||
// if enableLPFeeOverride, the lp fee for the ongoing swap will be 0 | ||||||
if (enableLPFeeOverride) { | ||||||
return ( | ||||||
this.beforeSwap.selector, | ||||||
BeforeSwapDeltaLibrary.ZERO_DELTA, | ||||||
LPFeeLibrary.OVERRIDE_FEE_FLAG & FREE_LP_FEE | ||||||
); | ||||||
} | ||||||
|
||||||
return (this.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0); | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.