blockful-io / swaplace-contracts

Swaplace is an open-source, ownerless and feeless token swap protocol
https://app.swaplace.xyz/
MIT License
33 stars 33 forks source link

[ISSUE-121] docs: research gas and cost efficiency for event emission #146

Closed 0xjoaovpsantos closed 8 months ago

0xjoaovpsantos commented 9 months ago

docs: research gas and cost efficiency for event emission

closes #121

How I calculated the gas usage?

uint256 startGas = gasleft();

// code

gasUsed = startGas - gasleft();

Prepare a spreadsheet with the gas usage based on the current and proposed events emitted and set for a comparison.

Estimate the gas when calling createSwap and record their gas usage.

event SwapCreated(
    uint256 indexed id,
    address indexed owner,
    uint256 indexed expiry
);
emit SwapCreated(swapId, msg.sender, swap.expiry);
//keccak-256(SwapCreated(uint256,address,uint256))
bytes32 private constant EVENT_SWAP_CREATED_SIGNATURE =
    0xecac413765eac3982e66e569ad3a200e6b3f188ac4d7d44cc5da667779ca8d05;
uint256 swapExpiry = swap.expiry;
assembly {
    log4(0x00, 0x00, EVENT_SWAP_CREATED_SIGNATURE, swapId, caller(), swapExpiry)
}

Take measures of the gas usage.

Add the allowed field in the events. Take Measure.

event SwapCreated(
    uint256 indexed id,
    address indexed owner,
    uint256 indexed expiry,
    address allowed
);
emit SwapCreated(swapId, msg.sender, swap.expiry, swap.allowed);
//keccak-256(SwapCreated(uint256,address,uint256,address))
bytes32 private constant EVENT_SWAP_CREATED_SIGNATURE =
    0x43a58bfac3282e5ce3bfd714c5bc0ddff8d6f2cd049db0b02a25d7cdd5026efb;
uint256 swapExpiry = swap.expiry;
address allowed = swap.allowed;
assembly {
    mstore(0x00, allowed)
    log4(0x00, 0x20, EVENT_SWAP_CREATED_SIGNATURE, swapId, caller(), swapExpiry)
}

Add the bidding and asking field in the events. Take Measure.

event SwapCreated(
    uint256 indexed id,
    address indexed owner,
    uint256 indexed expiry,
    address allowed,
    ISwap.Asset[] biding,
    ISwap.Asset[] asking
);
emit SwapCreated(swapId, msg.sender, swap.expiry, swap.allowed, swap.biding, swap.asking);

Add the price of ETH at $2.200 and calculate the gas cost in dollars.

Conclusion

0xneves commented 8 months ago

Let's go @0xjoaovpsantos 🔥

Hitting some scores: 1 - Using assembly to optimize will turn legibility in the opposite direction we expect. 2 - Allowed should be indexed instead of expiry as it is more important for tracking enrolled users in the swap. 2.1 - Event emission parameters should follow the same order as the Swap struct. 3 - The comparison table was lacking from the response so I had to test before and after changes. 3.1 - Adding allowed in the createSwap event will add up to ~147 gas. 4 - Logging the bidding and asking assets will slightly increase gas consumption. 4.1 - Logging structs in assembly is a pain in the ass, I went after the resolution, and it is just not worth it.

I applied this PR resolution in my fork while comparing the gas differences. Please feel free to take a look, and commit in your branch, and we can move forward with this PR conclusion.

Nice job 😄

0xjoaovpsantos commented 8 months ago

Thanky you @0xneves !!!!