Created
March 22, 2023 08:51
-
-
Save KONFeature/71d83fb9d29ea7de78d71d90b412440d to your computer and use it in GitHub Desktop.
Test of different way to send event, and their impact on gas usage
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: GNU GPLv3 | |
pragma solidity 0.8.17; | |
import {PRBTest} from "@prb/test/PRBTest.sol"; | |
contract SampleEvent { | |
event LogTransfer(address indexed from, address indexed to, uint256 value); | |
function transfer(address to, uint256 value) public { | |
emit LogTransfer(msg.sender, to, value); | |
} | |
} | |
contract SampleAssemblyEvent { | |
event LogTransfer(address indexed from, address indexed to, uint256 value); | |
/// @dev 'keccak256("LogTransfer(address,address,uint256")' | |
uint256 private constant _LOG_TRANSFER_SELECTOR = | |
0x3c70bac155b1df6b781842a200c9369a387d8d24e7cb6fafecba18a53e2de32b; | |
function transfer(address to, uint256 value) public { | |
assembly { | |
mstore(0, value) | |
log3(0x00, 0x20, _LOG_TRANSFER_SELECTOR, caller(), to) | |
} | |
} | |
function transferLoadOfParams(address to, uint256 value1, uint256 value2, uint256 value3) public { | |
assembly { | |
let fmp := mload(0x40) // Load free mem pointer | |
mstore(fmp, value1) // store first value at start | |
mstore(add(fmp, 0x20), value2) // store second val with offset 0x20 | |
mstore(add(fmp, 0x40), value3) // store third val with offset 0x40 | |
// Send all value from start of (fmp + 0) to (fmp + 0x60), since 96 bytes of data are loaded | |
log3(fmp, add(fmp, 0x60), _LOG_TRANSFER_SELECTOR, caller(), to) | |
} | |
} | |
} | |
/// Testing is event gas cost | |
contract EventGasUsageTest is PRBTest { | |
SampleEvent sampleEvent; | |
SampleAssemblyEvent sampleAssemblyEvent; | |
function setUp() public { | |
sampleEvent = new SampleEvent(); | |
sampleAssemblyEvent = new SampleAssemblyEvent(); | |
} | |
event Test(bytes32 selector); | |
function test_event() public { | |
sampleEvent.transfer(address(1), 10); | |
} | |
function test_eventAssembly() public { | |
sampleAssemblyEvent.transfer(address(1), 10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment