跳到主要内容

33 篇文档带有标签「GAS 优化」

查看所有标签

Alternatives to OpenZeppelin

While OpenZeppelin is a widely used and respected smart contract library, there are alternative libraries that offer improved gas efficiency. Two notable examples are Solmate and Solady. These libraries have been tested and recommended by developers for their focus on gas optimization.

Better Increment

Incrementing operations are fundamental in contract development due to their frequent use in counting and looping mechanisms. Each method of incrementing has subtle nuances that may affect gas consumption and readability:

Bytes32 与 String

In Solidity, the type of data storage you choose can affect the gas cost of your contract operations, especially when storing or modifying state variables. The Ethereum Virtual Machine (EVM) charges gas for all operations, and the choice between using bytes32 and string types can impact these costs.

Clone vs New/Create2

In factory contracts, we often need to create several child contracts, and there are three common ways to do this:

Comparison Operators

In the Ethereum Virtual Machine (EVM), the selection of comparison operators influences the efficiency and gas consumption of smart contracts. Opting for ` (greater than) over ≤ (less than or equal to) and ≥ (greater than or equal to) is notably more gas-efficient. This is due to the absence of direct opcode instructions for ≤ and ≥` in the EVM's design, which requires additional operations to achieve these comparisons.

Efficient Initialization

In Solidity, how you initialize state variables can have a impact on the deployment cost of your contracts, specifically in terms of gas usage. The Ethereum Virtual Machine (EVM) requires gas for every operation, including the initialization of variables.

Efficient Use of Storage Pointers

This tutorial explores how using storage pointers instead of copying data to memory can result in substantial gas savings. Storage pointers allow developers to directly reference storage without unnecessary copying of data, leading to more efficient smart contract execution.

ERC20Permit

In standard ERC20, users typically need to execute two separate transactions:

Fixed-Size/Dynamic Arrays

In Solidity, the way you manage and interact with arrays can impact the gas cost of your smart contract operations. This tutorial demonstrates the difference in gas usage between fixed-size arrays and dynamic arrays when they are filled with values. This understanding can help developers make more cost-effective decisions when designing smart contracts.

Memory 与 Calldata

1. memory : 通常用于修饰函数参数和函数内的临时变量。 此类变量存储在内存中,不会永久保存在区块链上。

MethodId Optimization

When transactions are executed by the Ethereum Virtual Machine (EVM), the accompanying calldata, which specifies the contract function to be executed, incurs gas fees. These fees are calculated based on the calldata size, with 0 bytes costing 4 gas units and non-0 bytes costing 16 gas units. This pricing structure encourages the efficient use of calldata to reduce transaction costs, especially in contracts with high transaction volumes or complex operations.

MethodId Sort Optimization

In the contract, all functions are organized into an array and systematically sorted by their MethodID, a unique identifier for each function. This organization not only streamlines the management of function calls but also facilitates quick access by imposing a structured order that can be efficiently searched during function executions.

Modifiers vs Internal Functions

In Solidity, the choice between using modifiers and internal functions can impact the gas costs associated with contract operations. This article explores the differences in gas usage between modifiers and internal functions when performing typical operations. 了解这些差异可以帮助开发者优化他们的智能合约,以优化合约功能并控制成本。

Non-Zero Balances

Initializing a storage variable from zero to a non-zero value is one of the most gas-intensive operations a contract can perform. It requires a total of 22,100 gas, including 20,000 gas for changing the value from zero to non-zero and 2,100 gas for cold storage access.

Optimize Timestamps and Block Numbers

In Solidity, the way data is stored can significantly impact the gas costs associated with deploying and interacting with smart contracts. Gas costs can quickly become a major concern, especially in applications that handle a large number of transactions.

Predicting Contract Addresses Using Account Nonce

In Solidity, predicting contract addresses before their deployment can save substantial gas, especially when deploying interdependent contracts. This method eliminates the need for setter functions and storage variables, which are costly in terms of gas usage. We can use the LibRLP library from Solady to deterministically compute the addresses based on the deployer's nonce.

ResetVariable

In Solidity, using the delete keyword to remove a state variable resets it to the default value for its type. The default values differ according to the data type:

SSTORE2

The cost of executing transactions on the Ethereum network can be very high, especially when interacting with smart contract storage using the SSTORE opcode. To mitigate these costs, developers can leverage alternative methods like SSTORE2 for more efficient data handling.

Unchecked

We know that before the Solidity version 0.8, it was necessary to manually import the SafeMath library to ensure data safety and avoid overflow, thereby preventing overflow attacks.

Use Monolithic Contract Architecture

This tutorial explores how making the architecture of your smart contracts monolithic, rather than having several contracts that communicate with each other, can result in gas savings. Inter-contract calls can be expensive, and by consolidating logic into a single contract, you can avoid these costs, albeit with some trade-offs in terms of complexity and modularity.

Using Payable

In Solidity, leveraging the payable keyword can be a subtle yet effective way to optimize gas usage. In this article, we explore two distinct scenarios where using payable can lead to gas savings: in constructors and admin functions.

Using selfdestruct in Constructors

In Solidity, optimizing gas usage is crucial for creating efficient smart contracts. One technique involves using the selfdestruct function within the constructor for contracts designed for one-time use. This approach can reduce gas costs by eliminating the contract from the blockchain once its purpose is fulfilled.

UUPS vs Transparent Proxy

When designing upgradable smart contracts, gas efficiency is critical for users interacting with the contract. There are two common upgrade patterns: UUPS (Universal Upgradeable Proxy Standard) and the Transparent Upgradeable Proxy. While both enable upgradability, the UUPS pattern is generally more gas efficient for users.

位图和位运算

在区块链上存储数据的成本极高。 很多项目创新性地使用了一些巧妙的方法来降低gas费用。 我们今天将会讨论那些常见于龙头项目的源代码中的方法。

低成本重入保护

在智能合约中使用修饰符进行重入检查可以通过确认合约当前是否正在执行来提升安全性。 通常,布尔型标志用于控制访问权限,只有当合约尚未激活时才允许函数运行。

在路由器类合约中实现多调用

在 Solidity 中,可以通过将多个状态修改调用批量处理为单个交易,以在路由器类合约中实现多调用功能,显著降低燃气成本。 这种技术在像 Uniswap 和 Compound 平台的合约中非常有价值。

局部变量

在许多常见的DeFi项目中,我们经常遇到需要定义许多新的局部变量和更新现有全局变量的各种复杂计算。 众所周知,修改存储比在内存中进行更改的成本要高得多。

映射 vs 动态数组

在 Solidity 中,不同的数据结构会显著影响因合约操作而产生的 gas 成本。 本文探讨了在执行插入、删除和检索等典型操作时,映射 和 动态数组 在 gas 使用上的差异。 了解这些差异可以帮助开发者优化他们的智能合约,以优化合约功能并控制成本。

短路运算

在Solidity中,每一个需要上链的操作都需要消耗gas,短路运算是一种编码技巧,它仅在第一个参数没有确定结果时才评估逻辑操作的第二个参数,从而显著减少不必要的gas消耗,提高效率。

错误

在Solidity中,开发者可以以三种主要形式定义错误: revert、 require 和 assert。 从功能角度来看,这些方法的主要区别有两个: