Sec3 logo — Solana smart contract security firm
Back to Blog
Security

Why gas fees are crazily high on Ethereum and what's the rescue?

Sec3 Research Team

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.

Why gas fees are so high?

Figure 1. Ethereum Gas Tracker (etherscan.io, Aug 14 2021) showing gas prices of 878-1263 gwei. An ERC20 transfer cost $185-$267, a Uniswap swap cost $571-$821, and adding/removing LP cost $499-$718.

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.

Figure 2. A Uniswap swap transaction on Etherscan: 2.74 ETH swapped for 8,517 USDT. Transaction fee: 0.0121 ETH ($37.77). Gas price: 92.55 Gwei. Gas limit: 187,657. Gas used: 130,709 (69.65%).

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: an EVM bytecode optimization technology

Figure 3. Bird's-eye view of GreenCore Superoptimizer. Input bytecodes flow through the Superoptimizer which analyzes each code path, identifying useless computation, redundant computation, and cheaper computation opportunities. Goal: minimize gas on every path. Constraint: produce the same state transition on the storage.

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.

Example: GreenSwap, a gas-optimized Uniswap

Powered by GreenCore, we have developed GreenSwap, 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. A GreenSwap transaction on Etherscan: 0.312623 USDC transferred. Transaction fee: 0.00272 ETH ($8.46). Gas price: 46.99 Gwei. Gas limit: 147,412. Gas used: 57,927 (39.3%).

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. Deployed bytecodes comparison — GreenSwap: 1,199 bytes vs. Uniswap: 22,398 bytes (nearly 18x smaller).

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.

Figure 6. GreenSwap UI (08/26/2021) — a swap interface swapping 0.01 ETH (~$31.08) for 30.9895 USDC, showing the same exchange rate as Uniswap V2 (1 USDC = 0.0003227 ETH) but with lower 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.

Related Posts

Security

IDL Guesser

The Solana ecosystem thrives on innovation, but many Anchor-based programs do not publish up-to-date IDLs, which complicates the analysis of such programs and their transactions. To tackle this, we developed and open-sourced a prototype tool called IDL Guesser. This tool aims to automatically recover instruction definitions, required accounts (including signer/writable flags), and parameter information directly from closed-source Solana program binaries. This blog outlines the approach behind IDL Guesser and discusses potential areas for future improvement.

Read more
Security

All About Anchor Account Size

Smart contracts using Anchor require developers to allocate space for new accounts and specify the account size. Anchor provides guidelines for calculating the size based on the account structure, but many developers use std::mem::size_of instead, as they don't have to manually update the size when making changes to the account structure. Are they equivalent? In this blog post, we conduct a systematic comparison of the results produced by std::mem::size_of and the Anchor space reference.

Read more