@@ -16,22 +16,34 @@ import {
1616 HOOKS_AFTER_SWAP_RETURNS_DELTA_OFFSET,
1717 HOOKS_AFTER_MINT_RETURNS_DELTA_OFFSET,
1818 HOOKS_AFTER_BURN_RETURNS_DELTA_OFFSET
19- } from "pancake-v4 -core/src/pool-bin/interfaces/IBinHooks.sol " ;
20- import {PoolKey} from "pancake-v4 -core/src/types/PoolKey.sol " ;
21- import {BeforeSwapDelta } from "pancake-v4- core/src/types/BeforeSwapDelta .sol " ;
22- import {BalanceDelta } from "pancake-v4- core/src/types/BalanceDelta .sol " ;
23- import {IHooks} from "pancake-v4 -core/src/interfaces/IHooks.sol " ;
24- import {IVault} from "pancake-v4 -core/src/interfaces/IVault.sol " ;
25- import {IBinHooks} from "pancake-v4 -core/src/pool-bin/interfaces/IBinHooks.sol " ;
26- import {IBinPoolManager} from "pancake-v4 -core/src/pool-bin/interfaces/IBinPoolManager.sol " ;
27- import {BinPoolManager} from "pancake-v4 -core/src/pool-bin/BinPoolManager.sol " ;
19+ } from "infinity -core/src/pool-bin/interfaces/IBinHooks.sol " ;
20+ import {PoolKey} from "infinity -core/src/types/PoolKey.sol " ;
21+ import {BalanceDelta } from "infinity- core/src/types/BalanceDelta .sol " ;
22+ import {BeforeSwapDelta } from "infinity- core/src/types/BeforeSwapDelta .sol " ;
23+ import {IHooks} from "infinity -core/src/interfaces/IHooks.sol " ;
24+ import {IVault} from "infinity -core/src/interfaces/IVault.sol " ;
25+ import {IBinHooks} from "infinity -core/src/pool-bin/interfaces/IBinHooks.sol " ;
26+ import {IBinPoolManager} from "infinity -core/src/pool-bin/interfaces/IBinPoolManager.sol " ;
27+ import {BinPoolManager} from "infinity -core/src/pool-bin/BinPoolManager.sol " ;
2828
29+ /// @notice BaseHook abstract contract for Bin pool hooks to inherit
2930abstract contract BinBaseHook is IBinHooks {
31+ /// @notice The sender is not the pool manager
3032 error NotPoolManager ();
33+
34+ /// @notice The sender is not the vault
3135 error NotVault ();
36+
37+ /// @notice The sender is not this contract
3238 error NotSelf ();
39+
40+ /// @notice The pool key does not include this hook
3341 error InvalidPool ();
42+
43+ /// @notice The delegation of lockAcquired failed
3444 error LockFailure ();
45+
46+ /// @notice The method is not implemented
3547 error HookNotImplemented ();
3648
3749 struct Permissions {
@@ -45,10 +57,10 @@ abstract contract BinBaseHook is IBinHooks {
4557 bool afterSwap;
4658 bool beforeDonate;
4759 bool afterDonate;
48- bool beforeSwapReturnsDelta ;
49- bool afterSwapReturnsDelta ;
50- bool afterMintReturnsDelta ;
51- bool afterBurnReturnsDelta ;
60+ bool beforeSwapReturnDelta ;
61+ bool afterSwapReturnDelta ;
62+ bool afterMintReturnDelta ;
63+ bool afterBurnReturnDelta ;
5264 }
5365
5466 /// @notice The address of the pool manager
@@ -86,8 +98,7 @@ abstract contract BinBaseHook is IBinHooks {
8698 _;
8799 }
88100
89- /// @dev Helper function when the hook needs to get a lock from the vault. See
90- /// https://github.com/pancakeswap/pancake-v4-hooks oh hooks which perform vault.lock()
101+ /// @dev Delegate calls to corresponding methods according to callback data
91102 function lockAcquired (bytes calldata data ) external virtual vaultOnly returns (bytes memory ) {
92103 (bool success , bytes memory returnData ) = address (this ).call (data);
93104 if (success) return returnData;
@@ -99,78 +110,186 @@ abstract contract BinBaseHook is IBinHooks {
99110 }
100111 }
101112
102- function beforeInitialize (address , PoolKey calldata , uint24 , bytes calldata ) external virtual returns (bytes4 ) {
103- revert HookNotImplemented ();
113+ /// @inheritdoc IBinHooks
114+ function beforeInitialize (address sender , PoolKey calldata key , uint24 activeId )
115+ external
116+ virtual
117+ poolManagerOnly
118+ returns (bytes4 )
119+ {
120+ return _beforeInitialize (sender, key, activeId);
104121 }
105122
106- function afterInitialize (address , PoolKey calldata , uint24 , bytes calldata ) external virtual returns (bytes4 ) {
123+ function _beforeInitialize (address , PoolKey calldata , uint24 ) internal virtual returns (bytes4 ) {
107124 revert HookNotImplemented ();
108125 }
109126
110- function beforeMint (address , PoolKey calldata , IBinPoolManager.MintParams calldata , bytes calldata )
127+ /// @inheritdoc IBinHooks
128+ function afterInitialize (address sender , PoolKey calldata key , uint24 activeId )
111129 external
112130 virtual
131+ poolManagerOnly
132+ returns (bytes4 )
133+ {
134+ return _afterInitialize (sender, key, activeId);
135+ }
136+
137+ function _afterInitialize (address , PoolKey calldata , uint24 ) internal virtual returns (bytes4 ) {
138+ revert HookNotImplemented ();
139+ }
140+
141+ /// @inheritdoc IBinHooks
142+ function beforeMint (
143+ address sender ,
144+ PoolKey calldata key ,
145+ IBinPoolManager.MintParams calldata params ,
146+ bytes calldata hookData
147+ ) external virtual poolManagerOnly returns (bytes4 , uint24 ) {
148+ return _beforeMint (sender, key, params, hookData);
149+ }
150+
151+ function _beforeMint (address , PoolKey calldata , IBinPoolManager.MintParams calldata , bytes calldata )
152+ internal
153+ virtual
113154 returns (bytes4 , uint24 )
114155 {
115156 revert HookNotImplemented ();
116157 }
117158
118- function afterMint (address , PoolKey calldata , IBinPoolManager.MintParams calldata , BalanceDelta, bytes calldata )
119- external
159+ /// @inheritdoc IBinHooks
160+ function afterMint (
161+ address sender ,
162+ PoolKey calldata key ,
163+ IBinPoolManager.MintParams calldata params ,
164+ BalanceDelta delta ,
165+ bytes calldata hookData
166+ ) external virtual poolManagerOnly returns (bytes4 , BalanceDelta) {
167+ return _afterMint (sender, key, params, delta, hookData);
168+ }
169+
170+ function _afterMint (address , PoolKey calldata , IBinPoolManager.MintParams calldata , BalanceDelta, bytes calldata )
171+ internal
120172 virtual
121173 returns (bytes4 , BalanceDelta)
122174 {
123175 revert HookNotImplemented ();
124176 }
125177
126- function beforeBurn (address , PoolKey calldata , IBinPoolManager.BurnParams calldata , bytes calldata )
127- external
178+ /// @inheritdoc IBinHooks
179+ function beforeBurn (
180+ address sender ,
181+ PoolKey calldata key ,
182+ IBinPoolManager.BurnParams calldata params ,
183+ bytes calldata hookData
184+ ) external virtual poolManagerOnly returns (bytes4 ) {
185+ return _beforeBurn (sender, key, params, hookData);
186+ }
187+
188+ function _beforeBurn (address , PoolKey calldata , IBinPoolManager.BurnParams calldata , bytes calldata )
189+ internal
128190 virtual
129191 returns (bytes4 )
130192 {
131193 revert HookNotImplemented ();
132194 }
133195
134- function afterBurn (address , PoolKey calldata , IBinPoolManager.BurnParams calldata , BalanceDelta, bytes calldata )
135- external
196+ /// @inheritdoc IBinHooks
197+ function afterBurn (
198+ address sender ,
199+ PoolKey calldata key ,
200+ IBinPoolManager.BurnParams calldata params ,
201+ BalanceDelta delta ,
202+ bytes calldata hookData
203+ ) external virtual poolManagerOnly returns (bytes4 , BalanceDelta) {
204+ return _afterBurn (sender, key, params, delta, hookData);
205+ }
206+
207+ function _afterBurn (address , PoolKey calldata , IBinPoolManager.BurnParams calldata , BalanceDelta, bytes calldata )
208+ internal
136209 virtual
137210 returns (bytes4 , BalanceDelta)
138211 {
139212 revert HookNotImplemented ();
140213 }
141214
142- function beforeSwap (address , PoolKey calldata , bool , int128 , bytes calldata )
143- external
215+ /// @inheritdoc IBinHooks
216+ function beforeSwap (
217+ address sender ,
218+ PoolKey calldata key ,
219+ bool swapForY ,
220+ int128 amountSpecified ,
221+ bytes calldata hookData
222+ ) external virtual poolManagerOnly returns (bytes4 , BeforeSwapDelta, uint24 ) {
223+ return _beforeSwap (sender, key, swapForY, amountSpecified, hookData);
224+ }
225+
226+ function _beforeSwap (address , PoolKey calldata , bool , int128 , bytes calldata )
227+ internal
144228 virtual
145229 returns (bytes4 , BeforeSwapDelta, uint24 )
146230 {
147231 revert HookNotImplemented ();
148232 }
149233
150- function afterSwap (address , PoolKey calldata , bool , int128 , BalanceDelta, bytes calldata )
151- external
234+ /// @inheritdoc IBinHooks
235+ function afterSwap (
236+ address sender ,
237+ PoolKey calldata key ,
238+ bool swapForY ,
239+ int128 amountSpecified ,
240+ BalanceDelta delta ,
241+ bytes calldata hookData
242+ ) external virtual poolManagerOnly returns (bytes4 , int128 ) {
243+ return _afterSwap (sender, key, swapForY, amountSpecified, delta, hookData);
244+ }
245+
246+ function _afterSwap (address , PoolKey calldata , bool , int128 , BalanceDelta, bytes calldata )
247+ internal
152248 virtual
153249 returns (bytes4 , int128 )
154250 {
155251 revert HookNotImplemented ();
156252 }
157253
158- function beforeDonate (address , PoolKey calldata , uint256 , uint256 , bytes calldata )
159- external
254+ /// @inheritdoc IBinHooks
255+ function beforeDonate (
256+ address sender ,
257+ PoolKey calldata key ,
258+ uint256 amount0 ,
259+ uint256 amount1 ,
260+ bytes calldata hookData
261+ ) external virtual poolManagerOnly returns (bytes4 ) {
262+ return _beforeDonate (sender, key, amount0, amount1, hookData);
263+ }
264+
265+ function _beforeDonate (address , PoolKey calldata , uint256 , uint256 , bytes calldata )
266+ internal
160267 virtual
161268 returns (bytes4 )
162269 {
163270 revert HookNotImplemented ();
164271 }
165272
166- function afterDonate (address , PoolKey calldata , uint256 , uint256 , bytes calldata )
167- external
273+ /// @inheritdoc IBinHooks
274+ function afterDonate (
275+ address sender ,
276+ PoolKey calldata key ,
277+ uint256 amount0 ,
278+ uint256 amount1 ,
279+ bytes calldata hookData
280+ ) external virtual poolManagerOnly returns (bytes4 ) {
281+ return _afterDonate (sender, key, amount0, amount1, hookData);
282+ }
283+
284+ function _afterDonate (address , PoolKey calldata , uint256 , uint256 , bytes calldata )
285+ internal
168286 virtual
169287 returns (bytes4 )
170288 {
171289 revert HookNotImplemented ();
172290 }
173291
292+ /// @dev Helper function to construct the hook registration map
174293 function _hooksRegistrationBitmapFrom (Permissions memory permissions ) internal pure returns (uint16 ) {
175294 return uint16 (
176295 (permissions.beforeInitialize ? 1 << HOOKS_BEFORE_INITIALIZE_OFFSET : 0 )
@@ -183,10 +302,10 @@ abstract contract BinBaseHook is IBinHooks {
183302 | (permissions.afterSwap ? 1 << HOOKS_AFTER_SWAP_OFFSET : 0 )
184303 | (permissions.beforeDonate ? 1 << HOOKS_BEFORE_DONATE_OFFSET : 0 )
185304 | (permissions.afterDonate ? 1 << HOOKS_AFTER_DONATE_OFFSET : 0 )
186- | (permissions.beforeSwapReturnsDelta ? 1 << HOOKS_BEFORE_SWAP_RETURNS_DELTA_OFFSET : 0 )
187- | (permissions.afterSwapReturnsDelta ? 1 << HOOKS_AFTER_SWAP_RETURNS_DELTA_OFFSET : 0 )
188- | (permissions.afterMintReturnsDelta ? 1 << HOOKS_AFTER_MINT_RETURNS_DELTA_OFFSET : 0 )
189- | (permissions.afterBurnReturnsDelta ? 1 << HOOKS_AFTER_BURN_RETURNS_DELTA_OFFSET : 0 )
305+ | (permissions.beforeSwapReturnDelta ? 1 << HOOKS_BEFORE_SWAP_RETURNS_DELTA_OFFSET : 0 )
306+ | (permissions.afterSwapReturnDelta ? 1 << HOOKS_AFTER_SWAP_RETURNS_DELTA_OFFSET : 0 )
307+ | (permissions.afterMintReturnDelta ? 1 << HOOKS_AFTER_MINT_RETURNS_DELTA_OFFSET : 0 )
308+ | (permissions.afterBurnReturnDelta ? 1 << HOOKS_AFTER_BURN_RETURNS_DELTA_OFFSET : 0 )
190309 );
191310 }
192311}
0 commit comments