Created
April 5, 2024 18:03
-
-
Save frangio/55af98288f9d3adbf70994471c30b45f to your computer and use it in GitHub Desktop.
Testing potential gas savings for Uniswap v4 Swap events
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
[PASS] test_std() (gas: 4661) | |
Traces: | |
[4661] SwapTest::test_std() | |
├─ emit Swap(id: 0x0000000000000000000000000000000000000000000000000000000000000000, sender: 0x0000000000000000000000000000000000000001, amount0: 2, amount1: 3, sqrtPri | |
ceX96: 4, liquidity: 5, tick: 6, fee: 7) | |
├─ emit Gasused(: 3160) | |
└─ ← () | |
[PASS] test_nonstd() (gas: 3137) | |
Traces: | |
[3137] SwapTest::test_nonstd() | |
├─ data: 0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000002000000000 | |
00000000000000000000003000000000000000000000000000000000000000400000000000000000000000000000005000006000007 | |
├─ emit Gasused(: 1702) | |
└─ ← () | |
[PASS] test_nonstd_asm() (gas: 2913) | |
Traces: | |
[2913] SwapTest::test_nonstd_asm() | |
├─ data: 0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000002000000000 | |
00000000000000000000003000000000000000000000000000000000000000400000000000000000000000000000005000006000007 | |
├─ emit Gasused(: 1544) | |
└─ ← () | |
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
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.13; | |
import {Test} from "forge-std/Test.sol"; | |
contract SwapTest is Test { | |
event Gasused(uint); | |
event Swap( | |
bytes32 indexed id, | |
address indexed sender, | |
int128 amount0, | |
int128 amount1, | |
uint160 sqrtPriceX96, | |
uint128 liquidity, | |
int24 tick, | |
uint24 fee | |
); | |
function test1() public { | |
uint g0 = gasleft(); | |
emit Swap(0, address(1), 2, 3, 4, 5, 6, 7); | |
uint g1 = gasleft(); | |
emit Gasused(g0 - g1); | |
} | |
enum Event { | |
Swap | |
} | |
function emit_Swap(bytes32 id, address sender, int128 amount0, int128 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick, uint24 fee) internal { | |
bytes memory e = abi.encodePacked(uint8(Event.Swap), id, sender, amount0, amount1, sqrtPriceX96, liquidity, tick, fee); | |
assembly { | |
log0(add(e, 0x20), mload(e)) | |
} | |
} | |
function test2() public { | |
uint g0 = gasleft(); | |
emit_Swap(0, address(1), 2, 3, 4, 5, 6, 7); | |
uint g1 = gasleft(); | |
emit Gasused(g0 - g1); | |
} | |
function emit_Swap_asm(bytes32 id, address sender, int128 amount0, int128 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick, uint24 fee) internal { | |
uint8 selector = uint8(Event.Swap); | |
assembly { | |
let e := mload(0x40) | |
mstore(add(e, 95), fee) | |
mstore(add(e, 92), tick) | |
mstore(add(e, 89), liquidity) | |
mstore(add(e, 73), sqrtPriceX96) | |
mstore(add(e, 53), amount1) | |
mstore(add(e, 37), amount0) | |
mstore(add(e, 21), sender) | |
mstore(add(e, 1), id) | |
mstore8(e, selector) | |
log0(e, 127) | |
} | |
} | |
function test3() public { | |
uint g0 = gasleft(); | |
emit_Swap_asm(0, address(1), 2, 3, 4, 5, 6, 7); | |
uint g1 = gasleft(); | |
emit Gasused(g0 - g1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment