In Layer 2 (L2) blockchain ecosystems, optimizing calldata is more than just compression—it's about smart data reduction. Here's how we do it:
Short IDs for Addresses: Instead of using full-length addresses, we assign shorter IDs. This is effective because the active addresses are far fewer than all possible addresses. This is inspired from a PoC Vitalik did recently on dictionary based calldata reduction. https://github.com/ethereum/research/blob/master/rollup_compression/dicts.py
Compaction Over Standard ABI Encoding: Standard ABI encoding is inefficient, padding data to 32 bytes. We tackle this by packing data more densely and using a custom parser.
Pattern Recognition with Custom Decompressors: We analyze common data patterns and use custom Decompressors to compress and decompress this data efficiently. This method also offloads repetitive data to on-chain storage. This effectively moves information about repetitive data and the "structure" away from the calldata into the Decompressor contract.
Precision Reduction in Gas Limits: Fields like gas limits, which can tolerate slight imprecision, are compressed by reducing their precision. Note this this is lossy compression, but this fine (with a certain range) for fields like callGasLimit and verificationGasLimit since unused gas is returned to the payer (atleast as of EPv0.6)
Remove redundant information such as handleOps selector from calldata: We simply send the calldata to the CompressionMiddleware's fallback function, which saves us 4 bytes of calldata per transaction.
In the provided Solidity code:
Sender Address Compression:
Nonce Compression:
Pre-Verification Gas Compression:
Verification Gas Limit Compression:
Call Gas Limit Compression:
Max Priority Fee Per Gas Compression:
Max Fee Per Gas Compression:
Calldata Compression:
PaymasterAndData Compression:
calldata
.Signature Compression:
BatchedSessionRouterDecompressor (op.signature):
batchedSessionRouter
and sessionKeyManager
.sessionValidationModule
using an on-chain dictionary.SessionData
structure.BiconomyVerifyingPaymasterDecompressor (op.paymasterAndData):
paymasterId
using an on-chain dictionary.<2 bytes - length><signature>
format.RageTradeSubmitDelayedOrderCalldataDecompressor (op.callData) :
abi.encodeCall
.These strategies collectively reduce the calldata size, leading to cost-efficient transactions on L2 blockchains.
forge test
The UserOperation from the following transaction was chosen: https://optimistic.etherscan.io/tx/0x38a6f56d0a1190fc94b7d6a5e501873427b5dd4b4806e2718b7fa69b0bde5ccf.
This is transaction from RageTrade that utilizes:
PerpsV2MarketDelayedIntent
to submit an order.Theoretical
function calldataCost(bytes memory data) internal pure returns (uint256 cost) {
// 4 for 0 bytes and 16 for non zero bytes
for (uint256 i = 0; i < data.length; i++) {
if (uint8(data[i]) == 0) {
cost += 4;
} else {
cost += 16;
}
}
}
Source: https://docs.optimism.io/stack/transactions/transaction-fees
Mainnet
Sent a transaction with the calldata to an EOA on Optimism Mainnet, so that calldata cost is incurred but no execution cost is incurred. Note that in the original transaction, 97% of the total gas cost is contributed by the L1 Calldata Posting cost, therefore these transactions should be representative of actual transactions when it comes to gas.
0x1fad948c000000000000000000000000000000000000000000000000000000000000004000000000000000000000000080213f829c8543eda6b1f0f303e94b8e504b53d8000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000002a757ab30726a7d839f5bdf2fd790b4cf2eadd5e0000000000000000000000000000000000000000001c3fa800000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000e7af700000000000000000000000000000000000000000000000000000000000249f00000000000000000000000000000000000000000000000000000000002e71e9f000000000000000000000000000000000000000000000000000000000098fe1e00000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000004c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c400004680000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000ea09d97b4084d859328ec4bf8ebcf9ecca26f1d0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006485f05ab50000000000000000000000000000000000000000000000255f1898ded107e72c000000000000000000000000000000000000000000000003fe58dd566c1e36fc72616765000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011400000f79b7faf42eebadba19acc07cd08af4478900000000000000000000000006759c4726202f40275ed9267bbe5e5dc691c738000000000000000000000000000000000000000000000000000000006572c9a8000000000000000000000000000000000000000000000000000000006572c2a000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000041319e0110ef3d5983b757bd952662bdaaa4b2a9ae460509d09d3442c29e21e15064eaaf64c5fd44b1c858c69e971818dda495192f210ec6c07a245b0e86a0028b1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000d09967410f8c76752a104c9848b57ebba5500000000000000000000000000000000000000000000000000000000000002e0000000000000000000000000000002fbffedd9b33f4e7156f2de8d48945e7489000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000659a4e11000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008be2d79c4cfe3d7fb660d9cc1991bfd0d4267e100000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001a00000000000000000000000000000000000000000000000000000000000000014aec3d80511d3758da3d4855f7649e01f48bb412e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000049b1b6f367a3afd8c538350ee36cf685179c2f62b0da2f7a5008546b815fc68e4068826b594a1849812e8c6f4753d9118d05255238162f09e4770efc7b98cf95215a890b22ab475cf7f46080839cedf61db2b261ab5db777ff1160aea9b8e917021a2d7f4510a201313c560c905e07de58937e5fbfc0875bab6a4a04db6f6f8af0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004103052d4fd3dff2d9c38d9e816856babb7f6d3f983d0542666d011484e8bee5d06ca95a7364cc1667afe5f03484d635069806dccb146bb5469b1cc570c30e30271b00000000000000000000000000000000000000000000000000000000000000
Theoretical Gas Cost: 15280.
Mainnet Cost: $0.34. https://optimistic.etherscan.io/tx/0x04755b9540d06649091b99ac6e61088e2b565b960587fd9b2d5035d95ec8df2c
0x0000000000000000000000000000000000000000000000001c3fa80000000000000000000000000000000000000000000002e71e9f1f130003e80000640000006485f05ab50000000000000000000000000000000000000000000000255f1898ded107e72c000000000000000000000000000000000000000000000003fe58dd566c1e36fc726167650000000000000000000000000000000000000000000000000000000000000051000000006572c9a800006572c2a00041319e0110ef3d5983b757bd952662bdaaa4b2a9ae460509d09d3442c29e21e15064eaaf64c5fd44b1c858c69e971818dda495192f210ec6c07a245b0e86a0028b1b000001c3018000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e80000659a4e1100000000000000000014aec3d80511d3758da3d4855f7649e01f48bb412e00c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000049b1b6f367a3afd8c538350ee36cf685179c2f62b0da2f7a5008546b815fc68e4068826b594a1849812e8c6f4753d9118d05255238162f09e4770efc7b98cf95215a890b22ab475cf7f46080839cedf61db2b261ab5db777ff1160aea9b8e917021a2d7f4510a201313c560c905e07de58937e5fbfc0875bab6a4a04db6f6f8af000000000000000000000000000000000000000000000000000003052d4fd3dff2d9c38d9e816856babb7f6d3f983d0542666d011484e8bee5d06ca95a7364cc1667afe5f03484d635069806dccb146bb5469b1cc570c30e30271b
Theoretical Gas Cost: 6924 (54% reduction)
Mainnet Cost: $0.18 (47% reduction). https://optimistic.etherscan.io/tx/0xbd0429aea338f8da2cb44adf6b105cb76541d2098a52981d8796a5b623a76b34
The results indicate that the reduction in calldata with this algorithm translates very well to actual real life results.