Crates.io | solid-grinder |
lib.rs | solid-grinder |
version | 1.1.5 |
source | src |
created_at | 2023-08-10 08:38:59.083965 |
updated_at | 2024-05-16 01:22:41.967351 |
description | A CLI that goes along with building blocks of smart contract. Along with our front-end snippets, this toolbox can reduce L2 gas cost by encoding calldata for dApps development to use as little bytes of calldata as possible. |
homepage | https://github.com/Ratimon/solid-grinder |
repository | https://github.com/Ratimon/solid-grinder |
max_upload_size | |
id | 940747 |
size | 280,295 |
A CLI that goes along with building blocks of smart contract. Along with our front-end snippets, this toolbox can reduce L2 gas cost by encoding calldata for dApps development to use as little bytes of calldata as possible.
[!WARNING] The code is not audited yet. Please use it carefully in production.
This dApp building block is intended to reduce L2 gas costs by a significant amount, using calldata optimization paradigm.
To learn more about calldata optimization, Check out this Technical Article
!!
While security is our top priority, we aim to enhance developer experience, such that the entire protocol is not required to re-written from scratch.
What you need to do is specify how argument is packed into one single calldata, then our CLI will generate required files for you !!
It works by optimizing calldata by using as little bytes of calldata as possible.
Specifically, Our novel components are as follows:
Solidity snippets: one contract to encode call data on chain. Another to decode it. This component has following feat:
Front-end snippets: to atomically connect between encoding and decoding component into single call
uniswap-calldata-optimization-monorepo
CLI: to generate the above solidity snippets (including Encoder and Decoder contracts). The only task requires to do is to specify the data type to pack the calldata while ensuring security.
We have done the benchmark by deploying and interacting the un-optimized version : /UniswapV2Router02.sol
and optimized version /UniswapV2Router02_Optimized.sol
.
Then, we have compared the difference in gas cost in following Txs:
The L2 Fees Paid is then 0.00011025.
The L2 Fees Paid is then 0.00007481
It can be seen that the L1 gas has been saved by ~36% (from 0.000029 ETH to 0.000018 ETH), but the overall cost is higher. However, the gas amount saved can potentially be much higher in case of the high congestion on L1 network.
For example if L1 gas price increases to 100 Gwei and L1 Fee Scalar is adjusted to 1. The figures will be from 0.000424 ETH to 0.000263 ETH.
According to the formula:
$\ \text{Total Gas} = \text{Gas Price}_{\text{Layer2}} \times \text{Gas Used for Execution}_{\text{Layer2}} + \text{Gas Price}_{\text{Layer1}} \times \text{Gas Used for Data}_{\text{Layer2}} \times \text{Scalar Fee}_{\text{Layer1}}
$
The total fee for unoptimized contract is 0.00007481 + 0.000424 = 0.0004988.
The total fee for optimized contract is 0.00011025 + 0.000263 = 0.00034325.
The gas has been saved by 31 %
We provide how the UniswapV2's router is optimized as follows:
UniswapV2Router02.sol
/** ... */
contract UniswapV2Router02 is IUniswapV2Router02 {
/** ... */
function addLiquidity(
address tokenA,
address tokenB,
uint256 amountADesired,
uint256 amountBDesired,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline
) public virtual override ensure(deadline) returns (uint256 amountA, uint256 amountB, uint256 liquidity) {
/** ... */
}
/** ... */
}
UniswapV2Router02_Optimized.sol
which inherits main functionality from UniswapV2Router02_Decoder.g.sol
/** ... */
contract UniswapV2Router02_Optimized is UniswapV2Router02, Ownable, UniswapV2Router02_Decoder {
/** ... */
function addLiquidityCompressed(bytes calldata _payload)
external
payable
returns (uint256 amountA, uint256 amountB, uint256 liquidity)
{
(addLiquidityData memory addLiquidityData,) = _decode_addLiquidityData(_payload, 0);
return UniswapV2Router02.addLiquidity(
addLiquidityData.tokenA,
addLiquidityData.tokenB,
addLiquidityData.amountADesired,
addLiquidityData.amountBDesired,
addLiquidityData.amountAMin,
addLiquidityData.amountBMin,
addLiquidityData.to,
addLiquidityData.deadline
);
}
/** ... */
}
The second one is UniswapV2Router02_Encoder.sol
/** ... */
contract UniswapV2Router02_Encoder {
IAddressTable public immutable addressTable;
/** ... */
function encode_addLiquidityData(
address tokenA,
address tokenB,
uint256 amountADesired,
uint256 amountBDesired,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline
)
external
view
returns (
bytes memory _compressedPayload
)
{
/** ... */
}
/** ... */
}
As shown above, the various input arguments of original contract are compressed into a single calldata via encoder. It is then decoded to be used later in decoder. Thus, nearly half bytes of calldata is reduced.
This can be illustrated by following:
cast calldata "addLiquidity(address,address,uint256,uint256,uint256,uint256,address,uint256)" 0x106EABe0298ec286Adf962994f0Dcf250c4BB763 0xEbfc763Eb9e1d1ab09Eb2f70549b66682AfD9aa5 1200000000000000000000 2500000000000000000000 1000000000000000000000 2000000000000000000000 0x095E7BAea6a6c7c4c2DfeB977eFac326aF552d87 100
0xe8e33700000000000000000000000000106eabe0298ec286adf962994f0dcf250c4bb763000000000000000000000000ebfc763eb9e1d1ab09eb2f70549b66682afd9aa50000000000000000000000000000000000000000000000410d586a20a4c000000000000000000000000000000000000000000000000000878678326eac90000000000000000000000000000000000000000000000000003635c9adc5dea0000000000000000000000000000000000000000000000000006c6b935b8bbd400000000000000000000000000000095e7baea6a6c7c4c2dfeb977efac326af552d870000000000000000000000000000000000000000000000000000000000000064
cast calldata "addLiquidityCompressed(bytes)" 000001000002000000410d586a20a4c00000000000878678326eac9000000000003635c9adc5dea000000000006c6b935b8bbd4000000000030000000064
0x2feccbed0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003e000001000002000000410d586a20a4c00000000000878678326eac9000000000003635c9adc5dea000000000006c6b935b8bbd40000000000300000000640000
Hence, this saves bytes by around 50% of calldata. This figure is quite impactful when implementing on dApp deployed on L2 (like Arbitrum/ Optimism) where L2 users pay their significant portion of L1 security cost of batch submission. The L1 gas could possibly be most of the total gas cost (L1 + 2 gas).
This essentially means that either the fewer bytes of call data sent or the tighter packed call data, the lower gas users will pay on L2.
As a result, our optimized version of UniswapV2's router could potentially save nearly 50% of gas on L2.
[!NOTE] The gas amount saved heavily depends on L1 security cost which can vary, depending on the congestion on L1.
Mathematically, the total gas is the total of the L2 execution fee and the L1 data/security fee, and this figure is differently calculated, depending on different layer 2 chains.
$\ \text{Total Gas Fee}_{\text{Layer2}} = \text{Execution Fee}_{\text{Layer2}} + \text{Data Fee}_{\text{Layer1}}
$
where $\ \text{Execution Fee}_{\text{Layer2}}
$ is :
$\ \text{Execution Fee}_{\text{Layer2}} = \text{Gas Price}_{\text{Layer2}} \times \text{ Gas Used}_{\text{Layer2}}
$
$\ text{Gas Price}_{\text{Layer2}} = \min( \text{Base Fee}_{\text{Layer2}} + \text{Priority Fee}_{\text{Layer2}} , \text{Gas Price Floor} )
$
$\ \text{where} \quad \text{Gas Price Floor} = \begin{cases} 0.1 \, \text{Gwei}, & \text{on Arbitrum One} \\ 0.01 \, \text{Gwei}, & \text{on Nova} \end{cases}
$
and $\ \text{Data Fee}_{\text{Layer1}}
$ is :
$\ \text{Data Fee}_{\text{Layer1}} = \text{Gas Price}_{\text{Layer1}} \times \text{brotli-zero-algorithm}_{\text{txdata}}(txdata) \times 16
$
$\ \text{where } \text{brotli-zero-algorithm}_{\text{txdata}}(txdata)
$ is used to reward users for posting compressible transactions.
Here's the (simple) math:
$\ \text{Total Gas Fee}_{\text{Layer2}} = \text{Execution Fee}_{\text{Layer2}} + \text{Data Fee}_{\text{Laye1}}
$
where $\ \text{Execution Fee}_{\text{Layer2}}
$ is :
$\ \text{Execution Fee}_{\text{Layer2}} = \text{Gas Price}_{\text{Layer2}} \times \text{ Gas Used}_{\text{Layer2}}
$
$\ \text{Gas Price}_{\text{Layer2}} = \text{Base Fee}_{\text{Layer2}} + \text{Priority Fee}_{\text{Layer2}}
$
and $\ \text{Data Fee}_{\text{Layer1}}
$ is :
$\ \text{Data Fee}_{\text{Layer1}} = \text{Gas Price}_{\text{Layer1}} \times \text{ [Tx Data Gas + Fixed Overhead]} \times \text{Dynamic Overhead}
$
where $\ \text{Tx Data Gas}
$ is :
$\ \text{Tx Data Gas} = \text{count-zero-bytes}_{\text{txdata}}(txdata) \times 4 + \text{count-non-zero-bytes}_{\text{txdata}}(txdata) \times 16
$
There are 2 ways: without npm and one with npm
We assume that you already setup your working environment with hardhat + foundry as specified in foundry 's guide or hardhat 's guide and cd
into it
cd my-project;
solid-grinder
using your favorite package manager, e.g., with Yarn:yarn add -D solid-grinder
This will automatically add the solid-grinder
binary and required templates in /templates
package.json
:
"scripts": {
"solid-grinder": "solid-grinder"
}
yarn solid-grinder -V
remappings.txt
with following line:@solid-grinder/=node_modules/solid-grinder/
We assume that you already have a forge project
mkdir my-project;
cd my-project;
forge init;
solid-grinder
crates package:forge install Ratimon/solid-grinder@v0.0.4;
lib/solid-grinder
cd lib/solid-grinder;
cargo build --release;
cp target/release/solid-grinder ../../solid-grinder;
./solid-grinder -V
remappings.txt
with following line:@solid-grinder/=lib/solid-grinder/contracts/
cd lib/solid-grinder;
cp -a cli/templates/. ../../templates/;
For simplicity, we use the UniswapV2's router as mentioned in Benchmarks as an example.
For uint
type, we can, for example, minimize the time period into type of uint40 (5 bytes). This is safe as the upper bound is around 35k years, which is long enough.
For address
type, the bit size is specified as uint24, assuming that the address table can store the maximum of 16,777,216 ids.
The following is the guideline how we can define the arguments' ranges.
// 24-bit, 16,777,216 possible
// 32-bit, 4,294,967,296 possible
// 40-bit, 1,099,511,627,776 => ~35k years
// 72-bit, 4,722 (18 decimals)
// 88-bit, 309m (18 decimals)
// 96-bit, 79b or 79,228,162,514 (18 decimals)
[!IMPORTANT] Now, the tool only generates one function in each iteration. If you intend to optimize two functions, you can still use it two times and then add the second one to the first one.
examples
into your /contracts
├── contracts
│ ├── examples/
decoder
contractyarn solid-grinder gen-decoder --source 'contracts/solc_0_8/examples/uniswapv2/UniswapV2Router02.sol' --output 'contracts/solc_0_8/examples/uniswapv2' --contract-name 'UniswapV2Router02' --function-name 'addLiquidity' --arg-bits '24 24 96 96 96 96 24 40' --compiler-version 'solc_0_8'
encoder
contractyarn solid-grinder gen-encoder --source 'contracts/solc_0_8/examples/uniswapv2/UniswapV2Router02.sol' --output 'contracts/solc_0_8/examples/uniswapv2' --contract-name 'UniswapV2Router02' --function-name 'addLiquidity' --arg-bits '24 24 96 96 96 96 24 40' --compiler-version 'solc_0_8'
[!IMPORTANT] Now, we only support 0.8.X version, and it is required to put
solc_0_8
as argement of--compiler-version
.
forge fmt
import {UniswapV2Router02_Decoder} from "@solid-grinder/solc_0_8/examples/uniswapv2/decoder/UniswapV2Router02_Decoder.g.sol";
[!TIP] It is recommended to manually change original (un-optimized) contract's visibility to public. From user perspective, it is then safe to still include the original version, meaning that users can directly and quickly interact via Etherscan in emergency case (i.e. front-end part is down). This is because it is difficult to interact with the optimized version via Etherscan, because the users have to manually compress arguments into single payload themselves.
/** ... */
contract UniswapV2Router02 is IUniswapV2Router02 {
/** ... */
function addLiquidity(
/** ... */
) public virtual override ensure(deadline) returns (uint256 amountA, uint256 amountB, uint256 liquidity) {
/** ... */
}
/** ... */
}
See our contributing guidelines
.
If you want to say thank you or/and support active development of Solid Grinder: