BTC
ETH
HTX
SOL
BNB
View Market
简中
繁中
English
日本語
한국어
ภาษาไทย
Tiếng Việt

xDeFi Labs: All aspects of Ethereum Gas

星球君的朋友们
Odaily资深作者
This article is about 2979 words, reading the full article takes about 5 minutes
How does xDeFi&Labs optimize gas?
AI Summary
Expand
How does xDeFi&Labs optimize gas?

secondary title

1. The consumption logic of Ethereum blockchain Gas: GasPrice and GasLimit

On the ETH blockchain system, no matter what kind of operation is performed, gasPrice and gaslimit are the prices that you must pay and must understand when operating.

The unit of gasPrice is often gwei, because 1wei = 10^-18 eth, which is the smallest native unit of eth. 1gwei = 10^9wei = 10^-9eth, which is the value obtained by all those who want to operate on eth each time they compete for packaged transaction resources in a block: that is, the one with the highest price packs it, otherwise it cannot be packaged.

The block of Eth has experienced two temporary "full blocks" during the 1CO mania at the end of 2017 and the 312 crash at the beginning of 2020, that is, when users use it, they need to compete for block space. There are too many users, and the quotation is too low to be packaged. The transaction failed after a certain period of time, but because of the widespread use of ERC-20USDT in 2019 and the widespread use of Uniswap in 2020, it led to normal full blocks (the total gas consumption gasLimit exceeds the 12,500,000 that a block can bear.

secondary title

2. How to ensure lower Gas at the level of Solidity programming calculation

Solidity's writing style is a bit like javascript, and the specific execution level depends on compiling into EVM-runable opcodes. Regardless of what kind of opcode it is compiled into to save gas, at least at the Solidity level, you can find opportunities to save as much gas as possible.

A Integer Computational Thinking

Solidity does not have floating-point numbers defined by IEEE-754: this kind of floating-point number calculation originally guided the CPU register design. For the consensus of "universal, deterministic, and consistent", the most common data type is uint256. And use integers to simulate decimals first, and then simulate the behavior of floating-point numbers, but overall, programmers need to have integer thinking.

Assuming you have a convenient math library, you will find that a.div(2) is actually not as fast as a>>1, because the multiplication and division of the math library is still done by calling such less expensive operations.

Fortunately, the pioneers of eth development (such as openzeppelin, gnosis, balancer, etc.) have produced some mathematical libraries in the process of implementing the project for others to use. These operations include four arithmetic operations, power extraction, and exponential logarithm operations.

Web linksWeb links

B avoid loops unless you know your computational complexity

When the loop condition is open, such as the second condition of the for loop is empty, or when the while statement judges the condition to be true, it is easy to be unable to estimate the gas consumption of the calculation in the loop body, which will make the gas consumption unacceptable, or Metamask (and other tools) cannot correctly estimate the gasLimit, causing the eth contract call to fail.

Web linksWeb linkssecondary titleWeb links

Web links

according toWeb linksGiven the clues, the Ethereum Yellow Paper gives a detailed fee breakdown.

This article mentions the cost difference between Gtxdatazero and Gtxdatanonzero. The logic of this core difference is: the more 00s in the eth address (note that 0s appear in pairs), no matter whether the address is a contract or an ordinary person transfers The transfer-out address will save more or less 0.4-5% of the gas fee.

In addition, referring to the operation codes corresponding to various operations on the EVM, you can also avoid high-gas operations as much as possible and use low-gas operations to build solidity code.

Web linksWeb linkssecondary title

4. GasToken family: another practical operation

This is a practical case of building refundable gas for destruction.

GST and GST2 were established by some researchers based on the characteristics of Gsclear and Gssuicide refunding gas fees. Among them, GST is based on releasing storage (Gsclear’s refund) and GST2 is based on releasing contracts (Gssuicide). Generally speaking, people will think that GST2 The second generation will be more reasonable, but in the process of technological development, in fact, GST2 may not always have an advantage over GST: if the optimization of state storage appears in the ETH 1.x technical route, GST may also usher in a revival.

1inch CHI gastoken is based on GST2, and the number of 00 contained in the token address is increased, but because of 1inch's more active commercial operation, this operation is gradually becoming widely known.

secondary title

5. How xDeFi&Labs optimize gas

Considering the millions of dollars in the audit cost of the zksnarks layer2 solution, the xDeFi&Labs team temporarily chose to verify the product logic first.

In the process of product optimization, the user's transaction has already involved the logic of a**b, where b is a decimal represented by uint256 and assumes the role of a decimal in disguise. In the process of financial product design, the balance design of the pool can quickly reduce the value of b from any decimal between [1,49] to {1,2,3,4,6,8,12} Wait for a set of positive integers to save thousands to tens of thousands of gas costs. When calling the math library, the fractional part of the Balancer math library bpow is not actually called, but only bpowi is called.

During the token issuance process of XDEX, xDeFi&Labs ensured the competitiveness of its 0x00 address according to the method mentioned in Chapter 3 (it is calculated that 12 zeros is the only feasible solution now, exceeding the 11 zeros used by TokenLon, ETH2. The 8 zeros of the 0 payment address, and the 6 zeros of GST2, are consistent with the 12 zeros of 1inch CHI).

In the design process of xHalfLife, in order to confirm how much income users can withdraw from the vesting account, the original version uses a circular calculation method, and the number of cycles involves block height differences in extreme cases. We can know that a week covers thousands to tens of thousands of block height differences. If each loop body consumes hundreds of gas, then when a user withdraws money, a single contract invokes tens of millions of gas consumption It is obviously unacceptable (probably hundreds of times the transfer cost). Therefore, from the three functions of fundStream, BalanceOf, and Withdraw, the loop is removed, and a **b simplification method is continued. At this time, we use Keep clearer logic, and leave the simplified loop code in the github code comments. When calling, considering the user's experience (Sushi's income is jumping every block, and the first version of xHalfLife code may not be able to do this), the possibility of b being a decimal is reserved.

DeFi
ETH
invest
Welcome to Join Odaily Official Community