|
| 1 | +#include "./IHooks.huff" |
| 2 | +#include "./Assert.huff" |
| 3 | +#include "./IAsyncSwap.huff" |
| 4 | + |
| 5 | +//.sSSSSs. .sSSSSs. .sSSSSs. .sSSSSs. SsSSs. |
| 6 | +//SSSSSSSSSs. SSSSSSSSSs. SSSSSSSSSs. SSSSSSSSSs. .sSSS s. SSSSs |
| 7 | +//S SSS SSSSS S SSS SSSSS S SSS SSSS' S SSS SSSS' S SSS SSSs. S SSS |
| 8 | +//S SS SSSS' S SS SSSSS S SS S SS S SS SSSSS S SS |
| 9 | +//S..SSsSSSa. S..SSsSSSSS S..SSsss S..SSsss S..SS SSSSS S..SS |
| 10 | +//S:::S SSSSS S:::S SSSSS S:::SSSS S:::SSSS S::S SSSS S:::S |
| 11 | +//S;;;S SSSSS S;;;S SSSSS S;;;S S;;;S S;S SSS S;;;S |
| 12 | +//S%%%S SSSSS S%%%S SSSSS S%%%S S%%%S SS SS S%%%S |
| 13 | +//SSSSS SSSSS SSSSS SSSSS SSSSS SSSSS SsS SsSSSSSsS |
| 14 | +// |
| 15 | +// r fGr |
| 16 | +// rr GrGGrG |
| 17 | +//rrr GraG |
| 18 | +//r GfrrraGa |
| 19 | +//r GaarrrrrrGr |
| 20 | +// GrarrrrrGrG |
| 21 | +// GaGfra GraG |
| 22 | +// Ga ar ff |
| 23 | +// Ga Gr G |
| 24 | +// fr af Gr |
| 25 | +// Gr Gr G |
| 26 | +//_mmsaki 2025 ________________ |
| 27 | + |
| 28 | +//'########:'##::: ##:'########:'########::'##:::'##: |
| 29 | +// ##.....:: ###:: ##:... ##..:: ##.... ##:. ##:'##:: |
| 30 | +// ##::::::: ####: ##:::: ##:::: ##:::: ##::. ####::: |
| 31 | +// ######::: ## ## ##:::: ##:::: ########::::. ##:::: |
| 32 | +// ##...:::: ##. ####:::: ##:::: ##.. ##:::::: ##:::: |
| 33 | +// ##::::::: ##:. ###:::: ##:::: ##::. ##::::: ##:::: |
| 34 | +// ########: ##::. ##:::: ##:::: ##:::. ##:::: ##:::: |
| 35 | +//........::..::::..:::::..:::::..:::::..:::::..::::: |
| 36 | +// |
| 37 | +#define macro MAIN() = { |
| 38 | + 0x00 calldataload // [calldata] |
| 39 | + 0xE0 shr // [func_sig] |
| 40 | + FUNCTION_DISPATCHER() |
| 41 | + // Returns: // [func_sig] |
| 42 | +} |
| 43 | + |
| 44 | +#define macro CONSTRUCTOR() = takes (0) returns (0) { |
| 45 | + 0x20 // [size] - byte size to copy |
| 46 | + 0x20 codesize sub // [offset, size] - offset in the code to copy from |
| 47 | + 0x00 // [mem, offset, size] - offset in memory to copy to |
| 48 | + codecopy // [] |
| 49 | + 0x00 mload // [poolManager] |
| 50 | + [POOL_MANAGER_LOC] sstore // [] |
| 51 | + 0x20 // [size] - byte size to copy |
| 52 | + 0x40 codesize sub // [offset, size] - offset in the code to copy from |
| 53 | + 0x00 // [mem, offset, size] - offset in memory to copy to |
| 54 | + codecopy // [] |
| 55 | + 0x00 mload // [owner] |
| 56 | + [OWNER_LOC] sstore // [] |
| 57 | + 0x20 // [size] - byte size to copy |
| 58 | + 0x60 codesize sub // [offset, size] - offset in the code to copy from |
| 59 | + 0x00 // [mem, offset, size] - offset in memory to copy to |
| 60 | + codecopy // [] |
| 61 | + 0x00 mload // [owner] |
| 62 | + [CONTRACT_PAUSED_LOC] sstore // [] |
| 63 | +} |
| 64 | + |
| 65 | +//'########::'####::'######::'########:::::'###::::'########::'######::'##::::'##: |
| 66 | +// ##.... ##:. ##::'##... ##: ##.... ##:::'## ##:::... ##..::'##... ##: ##:::: ##: |
| 67 | +// ##:::: ##:: ##:: ##:::..:: ##:::: ##::'##:. ##::::: ##:::: ##:::..:: ##:::: ##: |
| 68 | +// ##:::: ##:: ##::. ######:: ########::'##:::. ##:::: ##:::: ##::::::: #########: |
| 69 | +// ##:::: ##:: ##:::..... ##: ##.....::: #########:::: ##:::: ##::::::: ##.... ##: |
| 70 | +// ##:::: ##:: ##::'##::: ##: ##:::::::: ##.... ##:::: ##:::: ##::: ##: ##:::: ##: |
| 71 | +// ########::'####:. ######:: ##:::::::: ##:::: ##:::: ##::::. ######:: ##:::: ##: |
| 72 | +//........:::....:::......:::..:::::::::..:::::..:::::..::::::......:::..:::::..:: |
| 73 | +// |
| 74 | +#define macro FUNCTION_DISPATCHER() = takes (1) returns (0) { |
| 75 | + /// Takes: [function_selector] |
| 76 | + dup1 [beforeInitialize] eq BEFORE_INITIALIZE jumpi |
| 77 | + dup1 [beforeAddLiquidity] eq BEFORE_ADD_LIQUIDITY jumpi |
| 78 | + dup1 [beforeSwap] eq BEFORE_SWAP jumpi |
| 79 | + dup1 [getHookPermissions] eq GET_HOOK_PERMISSIONS jumpi |
| 80 | + dup1 [poolManager] eq POOL_MANAGER jumpi |
| 81 | + dup1 [pause] eq PAUSE jumpi |
| 82 | + |
| 83 | + no_match jump |
| 84 | + |
| 85 | + BEFORE_INITIALIZE: |
| 86 | + beforeInitialize() |
| 87 | + BEFORE_ADD_LIQUIDITY: |
| 88 | + beforeAddLiquidity() |
| 89 | + BEFORE_SWAP: |
| 90 | + beforeSwap() |
| 91 | + GET_HOOK_PERMISSIONS: |
| 92 | + getHookPermissions() |
| 93 | + POOL_MANAGER: |
| 94 | + poolManager() |
| 95 | + PAUSE: |
| 96 | + pause() |
| 97 | + no_match: |
| 98 | + stop |
| 99 | +} |
| 100 | + |
| 101 | +//:'######::'########::'#######::'########:::::'###:::::'######:::'########: |
| 102 | +//'##... ##:... ##..::'##.... ##: ##.... ##:::'## ##:::'##... ##:: ##.....:: |
| 103 | +// ##:::..::::: ##:::: ##:::: ##: ##:::: ##::'##:. ##:: ##:::..::: ##::::::: |
| 104 | +//. ######::::: ##:::: ##:::: ##: ########::'##:::. ##: ##::'####: ######::: |
| 105 | +//:..... ##:::: ##:::: ##:::: ##: ##.. ##::: #########: ##::: ##:: ##...:::: |
| 106 | +//'##::: ##:::: ##:::: ##:::: ##: ##::. ##:: ##.... ##: ##::: ##:: ##::::::: |
| 107 | +//. ######::::: ##::::. #######:: ##:::. ##: ##:::: ##:. ######::: ########: |
| 108 | +//:......::::::..::::::.......:::..:::::..::..:::::..:::......::::........:: |
| 109 | +// |
| 110 | +#define constant CONTRACT_PAUSED_LOC = FREE_STORAGE_POINTER() |
| 111 | +#define constant OWNER_LOC = FREE_STORAGE_POINTER() |
| 112 | +#define constant POOL_MANAGER_LOC = FREE_STORAGE_POINTER() |
| 113 | + |
| 114 | +//'####:'##::: ##:'########:'########:'########::'########::::'###:::::'######::'########: |
| 115 | +//. ##:: ###:: ##:... ##..:: ##.....:: ##.... ##: ##.....::::'## ##:::'##... ##: ##.....:: |
| 116 | +//: ##:: ####: ##:::: ##:::: ##::::::: ##:::: ##: ##::::::::'##:. ##:: ##:::..:: ##::::::: |
| 117 | +//: ##:: ## ## ##:::: ##:::: ######::: ########:: ######:::'##:::. ##: ##::::::: ######::: |
| 118 | +//: ##:: ##. ####:::: ##:::: ##...:::: ##.. ##::: ##...:::: #########: ##::::::: ##...:::: |
| 119 | +//: ##:: ##:. ###:::: ##:::: ##::::::: ##::. ##:: ##::::::: ##.... ##: ##::: ##: ##::::::: |
| 120 | +//'####: ##::. ##:::: ##:::: ########: ##:::. ##: ##::::::: ##:::: ##:. ######:: ########: |
| 121 | +//....::..::::..:::::..:::::........::..:::::..::..::::::::..:::::..:::......:::........:: |
| 122 | +// |
| 123 | +#define constant ALGORITHM = __FUNC_SIG("ALGORITHM()") // "7cd88050" |
| 124 | +#define constant asyncOrder = __FUNC_SIG("asyncOrder(bytes32,address,bool)") // "b9586732" |
| 125 | +#define constant asyncOrders = __FUNC_SIG("asyncOrders(bytes32)") // "43719eae" |
| 126 | +#define constant calculateHookFee = __FUNC_SIG("calculateHookFee(uint256)") // "aa223bd0" |
| 127 | +#define constant calculatePoolFee = __FUNC_SIG("calculatePoolFee(uint24,uint256)") // "d6a522ca" |
| 128 | +#define constant executeOrder = __FUNC_SIG("executeOrder(((address,address,uint24,int24,address),address,bool,uint256,uint160),bytes)") // "caf6c3b1" |
| 129 | +#define constant executeOrders = __FUNC_SIG("executeOrders(((address,address,uint24,int24,address),address,bool,uint256,uint160)[],bytes)") // "fb249c0a" |
| 130 | +#define constant getHookPermissions = __FUNC_SIG("getHookPermissions()") // "c4e833ce" |
| 131 | +#define constant isExecutor = __FUNC_SIG("isExecutor(bytes32,address,address)") // "6ca34978" |
| 132 | +#define constant pause = __FUNC_SIG("pause()") // "" |
| 133 | + |
| 134 | +//'##::::'##::'#######::'########::'####:'########:'####:'########:'########:: |
| 135 | +// ###::'###:'##.... ##: ##.... ##:. ##:: ##.....::. ##:: ##.....:: ##.... ##: |
| 136 | +// ####'####: ##:::: ##: ##:::: ##:: ##:: ##:::::::: ##:: ##::::::: ##:::: ##: |
| 137 | +// ## ### ##: ##:::: ##: ##:::: ##:: ##:: ######:::: ##:: ######::: ########:: |
| 138 | +// ##. #: ##: ##:::: ##: ##:::: ##:: ##:: ##...::::: ##:: ##...:::: ##.. ##::: |
| 139 | +// ##:.:: ##: ##:::: ##: ##:::: ##:: ##:: ##:::::::: ##:: ##::::::: ##::. ##:: |
| 140 | +// ##:::: ##:. #######:: ########::'####: ##:::::::'####: ########: ##:::. ##: |
| 141 | +//..:::::..:::.......:::........:::....::..::::::::....::........::..:::::..:: |
| 142 | +// |
| 143 | +#define macro onlyPoolManager() = takes (0) returns (1) { |
| 144 | + __RIGHTPAD(__BYTES("NOT POOL MANAGER!")) // [message] |
| 145 | + 0x11 // [message_length, message] |
| 146 | + [POOL_MANAGER_LOC] sload // [poolManager, message_length, message] |
| 147 | + caller // [caller, poolManager, message_length, message] |
| 148 | + eq // [condition, message_length, message] |
| 149 | + REQUIRE() |
| 150 | + 0x01 |
| 151 | + // Returns: [onlyPoolManager] |
| 152 | +} |
| 153 | + |
| 154 | +#define macro onlyOwner() = takes (0) returns (1) { |
| 155 | + __RIGHTPAD(__BYTES("NOT OWNER!")) // [message] |
| 156 | + 0x0a // [message_length, message] |
| 157 | + [OWNER_LOC] sload // [owner, message_length, message] |
| 158 | + caller // [caller, owner, message_length, message] |
| 159 | + eq // [condition, message_length, message] |
| 160 | + REQUIRE() |
| 161 | + 0x01 |
| 162 | + // Returns: [onlyOwner] |
| 163 | +} |
| 164 | + |
| 165 | +#define macro isPaused() = takes (0) returns (1) { |
| 166 | + [CONTRACT_PAUSED_LOC] sload // [paused] |
| 167 | + 0x01 // [0x01, paused] |
| 168 | + eq // [condition] |
| 169 | + // Returns: [is_paused] |
| 170 | +} |
| 171 | + |
| 172 | +#define macro notPaused() = takes (0) returns (1) { |
| 173 | + [CONTRACT_PAUSED_LOC] sload // [paused] |
| 174 | + 0x00 // [0x00, paused] |
| 175 | + eq // [condition] |
| 176 | + // Returns: [not_paused] |
| 177 | +} |
| 178 | + |
| 179 | +//'##::::'##::'#######:::'#######::'##:::'##::'######:: |
| 180 | +// ##:::: ##:'##.... ##:'##.... ##: ##::'##::'##... ##: |
| 181 | +// ##:::: ##: ##:::: ##: ##:::: ##: ##:'##::: ##:::..:: |
| 182 | +// #########: ##:::: ##: ##:::: ##: #####::::. ######:: |
| 183 | +// ##.... ##: ##:::: ##: ##:::: ##: ##. ##::::..... ##: |
| 184 | +// ##:::: ##: ##:::: ##: ##:::: ##: ##:. ##::'##::: ##: |
| 185 | +// ##:::: ##:. #######::. #######:: ##::. ##:. ######:: |
| 186 | +//..:::::..:::.......::::.......:::..::::..:::......::: |
| 187 | +// |
| 188 | +#define macro beforeInitialize() = takes (1) returns (1) { |
| 189 | + // Takes: [function_selector] // [func_sig] |
| 190 | + onlyPoolManager() // [onlyPoolManager, func_sig] <- check caller |
| 191 | + 0x01 eq BEFORE_INITIALIZE_SUCCESS jumpi // [func_sig] |
| 192 | + 0x00 0x00 revert // [] |
| 193 | + |
| 194 | + BEFORE_INITIALIZE_SUCCESS: |
| 195 | + /// return function selector |
| 196 | + __RIGHTPAD([beforeInitialize]) |
| 197 | + 0x00 mstore |
| 198 | + 0x20 0x00 return |
| 199 | +} |
| 200 | + |
| 201 | +/// @params a function selector |
| 202 | +#define macro beforeAddLiquidity() = takes (1) returns (1) { |
| 203 | + // Takes: [function_selector] // [func_sig] |
| 204 | + onlyPoolManager() // [onlyPoolManager] <- check caller |
| 205 | + notPaused() // [notPaused, onlyPoolManager] <- check not paused |
| 206 | + eq BEFORE_ADD_LIQUIDITY_SUCCESS jumpi // [func_sig] |
| 207 | + 0x00 0x00 revert // [] |
| 208 | + |
| 209 | + /// When contract is not paused |
| 210 | + BEFORE_ADD_LIQUIDITY_SUCCESS: |
| 211 | + __RIGHTPAD([beforeAddLiquidity]) // return function selector |
| 212 | + 0x00 mstore |
| 213 | + 0x20 0x00 return |
| 214 | +} |
| 215 | + |
| 216 | +#define macro beforeSwap() = takes (1) returns (1) { |
| 217 | + // Takes: [func_selector] // [func_sig] |
| 218 | + onlyPoolManager() // [onlyPoolManager, func_sig] <- check caller |
| 219 | + notPaused() // [notPaused, onlyPoolManager, func_sig] <- check not paused |
| 220 | + eq BEFORE_SWAP_SUCCESS jumpi // [func_sig] |
| 221 | + 0x00 0x00 revert // [] |
| 222 | + |
| 223 | + BEFORE_SWAP_SUCCESS: |
| 224 | + __RIGHTPAD([beforeSwap]) // return function selector |
| 225 | + 0x00 mstore |
| 226 | + 0x20 0x00 return |
| 227 | +} |
| 228 | + |
| 229 | +//'########::'##::::'##:'########::'##:::::::'####::'######:: |
| 230 | +// ##.... ##: ##:::: ##: ##.... ##: ##:::::::. ##::'##... ##: |
| 231 | +// ##:::: ##: ##:::: ##: ##:::: ##: ##:::::::: ##:: ##:::..:: |
| 232 | +// ########:: ##:::: ##: ########:: ##:::::::: ##:: ##::::::: |
| 233 | +// ##.....::: ##:::: ##: ##.... ##: ##:::::::: ##:: ##::::::: |
| 234 | +// ##:::::::: ##:::: ##: ##:::: ##: ##:::::::: ##:: ##::: ##: |
| 235 | +// ##::::::::. #######:: ########:: ########:'####:. ######:: |
| 236 | +//..::::::::::.......:::........:::........::....:::......::: |
| 237 | + |
| 238 | +#define macro pause() = takes (1) returns (1) { |
| 239 | + // Takes: [func_sig] // [func_sig] |
| 240 | + onlyOwner() // [onlyOwner, func_sig] |
| 241 | + notPaused() CAN_PAUSE jumpi // [onlyOwner, func_sig] |
| 242 | + |
| 243 | + __RIGHTPAD(__BYTES("ALREADY PAUSED!")) // [message] |
| 244 | + 0x0f // [message_length, message] |
| 245 | + 0x00 // [condition, message_length, message] |
| 246 | + REQUIRE() |
| 247 | + |
| 248 | + CAN_PAUSE: |
| 249 | + 0x01 [CONTRACT_PAUSED_LOC] sstore // [paused, onlyOwner, func_sig] |
| 250 | + 0x01 0x00 mstore // [] |
| 251 | + 0x20 0x00 return // [paused] |
| 252 | + // Returns: Pased |
| 253 | +} |
| 254 | + |
| 255 | +#define macro poolManager() = takes (1) returns (1) { |
| 256 | + // Takes: [func_sig] // [func_sig] |
| 257 | + [POOL_MANAGER_LOC] sload // [poolManager, func_sig] |
| 258 | + 0x00 mstore // [func_sig] |
| 259 | + 0x20 0x00 return // [] |
| 260 | +} |
| 261 | + |
| 262 | +#define macro getHookPermissions() = takes (1) returns (1) { |
| 263 | + // Takes: [func_sig] // [func_sig] |
| 264 | + 0x01 0x000 mstore // beforeInitialize: true, |
| 265 | + 0x00 0x020 mstore // afterInitialize: false, |
| 266 | + 0x01 0x040 mstore // beforeAddLiquidity: true, |
| 267 | + 0x00 0x060 mstore // afterAddLiquidity: false, |
| 268 | + 0x00 0x080 mstore // beforeRemoveLiquidity: false, |
| 269 | + 0x00 0x0a0 mstore // afterRemoveLiquidity: false, |
| 270 | + 0x01 0x0c0 mstore // beforeSwap: true, |
| 271 | + 0x00 0x0e0 mstore // afterSwap: false, |
| 272 | + 0x00 0x100 mstore // beforeDonate: false, |
| 273 | + 0x00 0x120 mstore // afterDonate: false, |
| 274 | + 0x01 0x140 mstore // beforeSwapReturnDelta: true, |
| 275 | + 0x00 0x160 mstore // afterSwapReturnDelta: false, |
| 276 | + 0x00 0x180 mstore // afterAddLiquidityReturnDelta: false, |
| 277 | + 0x00 0x1a0 mstore // afterRemoveLiquidityReturnDelta: false |
| 278 | + /// return permissions |
| 279 | + 0x0e 0x20 mul |
| 280 | + 0x00 return |
| 281 | +} |
| 282 | + |
| 283 | +//'########:'########::'######::'########: |
| 284 | +//... ##..:: ##.....::'##... ##:... ##..:: |
| 285 | +//::: ##:::: ##::::::: ##:::..::::: ##:::: |
| 286 | +//::: ##:::: ######:::. ######::::: ##:::: |
| 287 | +//::: ##:::: ##...:::::..... ##:::: ##:::: |
| 288 | +//::: ##:::: ##:::::::'##::: ##:::: ##:::: |
| 289 | +//::: ##:::: ########:. ######::::: ##:::: |
| 290 | +//:::..:::::........:::......::::::..::::: |
| 291 | +// |
| 292 | +// Test: |
| 293 | +// hnc src/AsyncSwap.huff test --sender 0x0000000000000000000000000000000000000000 |
| 294 | +// |
| 295 | +// debug: |
| 296 | +// hnc src/AsyncSwap.huff test --sender 0x0000000000000000000000000000000000000000 \ |
| 297 | +// --debug -m TEST_BEFORE_SWAP |
| 298 | +// |
| 299 | +#[calldata("0xdc98354e")] |
| 300 | +#define test TEST_BEFORE_INITIALIZE() = { |
| 301 | + beforeInitialize() |
| 302 | +} |
| 303 | + |
| 304 | +#[calldata("0x575e24b4")] |
| 305 | +#define test TEST_BEFORE_SWAP() = { |
| 306 | + beforeSwap() |
| 307 | +} |
| 308 | + |
| 309 | +#[calldata("0x259982e5")] |
| 310 | +#define test TEST_BEFORE_ADD_LIQUIDITY() = { |
| 311 | + beforeAddLiquidity() |
| 312 | +} |
| 313 | + |
| 314 | +#[calldata("0xc4e833ce")] |
| 315 | +#define test TEST_GET_HOOK_PERMISSIONS() = { |
| 316 | + getHookPermissions() |
| 317 | +} |
| 318 | + |
| 319 | +#[calldata("0xdc4c90d3")] |
| 320 | +#define test TEST_POOL_MANAGER() = { |
| 321 | + poolManager() |
| 322 | +} |
| 323 | + |
| 324 | +#[calldata("0x575e24b4")] |
| 325 | +#define test TEST_SWAP_WHEN_PAUSED() = { |
| 326 | + // fails when paused |
| 327 | + 0x00 [CONTRACT_PAUSED_LOC] sstore |
| 328 | + MAIN() |
| 329 | +} |
| 330 | + |
| 331 | +#[calldata("0x259982e5")] |
| 332 | +#define test TEST_ADD_LIQ_WHEN_PAUSED() = { |
| 333 | + // fails when paused |
| 334 | + 0x00 [CONTRACT_PAUSED_LOC] sstore |
| 335 | + MAIN() |
| 336 | +} |
0 commit comments