Transaction fees on Ethereum are crazily high recently. A token swap sometimes costs over $1000 gas fees, why?
This article explains the underlying reasons and introduces a new solution that significantly reduces gas fees.
On Ethereum, gas fees are determined by two numbers: (1) gas price X, and (2) gas cost Y. The gas fee of a transaction is just X * Y.
X is a number that a transaction maker offers to the miners for prioritizing the transaction. For example, the 1263 gwei in Figure 1 means X = 1,263,000,000,000. X is chosen by the transaction maker and it can be any number. The larger X is, the more likely the transaction will be processed sooner.
Essentially, X is dynamic depending on how busy the Ethereum network is. Because the Ethereum network has a limited throughput, currently only 30 transactions per second, it can get congested easily. When network congestion happens, there will be many pending transactions in the miners’ queue, and where a transaction is placed in the queue is determined by X. In those scenarios, the transaction maker would need to set a high X to prioritize their transaction.
Y is another number that measures the computational cost of a transaction. Different from X, Y depends on how much computational resources the transaction takes to execute, and it does not change with network congestion. Therefore, Y is a static number determined by the smart contract — the code executed by the transaction.
For example, Uniswap’s smart contract costs 130,000 on average for a token swap transaction. That is, Y = 130,000 for Uniswap. While Y varies in different smart contracts and essentially depends on the code complexity, Y may be greatly reduced through code optimization.
GreenCore is a technology that automatically optimizes smart contract bytecodes towards reducing their gas costs, that is, minimizing Y for a transaction.
Figure 3 depicts a bird’s view of GreenCore, which takes as input the deployed code of a smart contract (a sequence of bytecodes), optimizes them with a Superoptimizer, and outputs another sequence of optimized bytecodes.
GreenCore is based on optimization theories of computer programs. The output code from GreenCore has the same format as the input code and can be deployed with no changes to the blockchain. Moreover, the output code is functionally equivalent to the input code: under all scenarios allowed by the blockchain, the output code always produces the same state transition as the input code (note that the Ethereum blockchain is a state machine).
GreenCore’s cornerstone is an automated Superoptimizer driven by Maximal Concolic Execution, a code verification technique developed by the programming language research community. Maximal Concolic Execution provides the ability to systematically explore every code path, encode path conditions with mathematical formulas, and check path invariants with SMT (Satisfiability Modulo Theories) solvers. The Superoptimizer can then identify optimization opportunities, such as useless computation, redundant computation, cheaper computation and so on, to minimize gas cost on every code path.
Finally, the optimization is realized by iteratively transforming an intermediate graph representation of the input code while respecting the path invariants and state transition invariants.
Please refer to our paper for more details.
Powered by GreenCore, we have developed GreenSwap (https://app.greenswap.tech), a highly optimized exchange based on Uniswap V2 and V3. Compared to Uniswap, GreenSwap reduces the gas cost by as much as 55% (over 30% on average).
Figure 4 shows a transaction record. GreenSwap’s gas cost is less than 60,000 for a transaction (among many other similar cases).
Figure 5 shows the deployed bytecodes of GreenSwap compared with Uniswap. The size of GreenSwap is almost 18X smaller than Uniswap. This indicates that there are abundant optimization opportunities in Uniswap identified by GreenCore. Once GreenSwap is widely deployed and used by DeFi users, it will save a significant amount of transaction fees.
GreenSwap has already supported swapping between ETH/USDC. For the users, the way to use GreenSwap is the same as Uniswap. Figure 6 shows a snapshot. GreenSwap uses the same price oracle as Uniswap V2/V3, and it guarantees the same exchange price as Uniswap for every transaction, but with a much lower transaction fee.