Prerequisites
- EVM Compatibility: Your project is deployed on peaq network, which has EVM compatibility, allowing you to apply common Ethereum-based optimization techniques.
- Basic Knowledge of Gas Mechanics: You understand that gas fees increase with transaction complexity, storage operations, and on-chain computations.
- Developer-Level Access: You have the ability to modify and deploy smart contracts, review their code, and adjust transaction creation parameters.
- Stable Contract Logic: Your smart contract logic is largely finalized, enabling you to focus on gas optimization, without expecting major functional changes.
Instructions for Optimizing Gas Fees
1. Minimize On-Chain Data Storage
- Data Compression: Instead of storing raw large data sets, try to store compressed or hashed references.
- Hashing and Linking: Use hashes (e.g.,
Keccak256
) to reference large off-chain data, reducing on-chain storage operations. - State Variable Efficiency: Consolidate related data into fewer state variables or use bit-packing techniques to store multiple flags or small integers in a single variable.
2. Batch Transactions
- Multi-Call Transactions: Combine multiple related operations into a single transaction when possible, reducing overhead and the total gas spent on repeated transaction components.
- Use Off-Chain Aggregation: Aggregate user actions off-chain and submit them in bulk on-chain as a single batched update.
3. Optimize Smart Contract Logic
- Simplify Computations: Remove redundant loops, pre-calculate results off-chain, and use efficient algorithms.
- Use Mappings and Arrays Wisely: Accessing storage variables is expensive. Consider using more gas-efficient data structures (e.g., mapping instead of arrays for lookups) and keep arrays as short as possible.
- Leverage Immutable Variables and Constants: Mark values that do not change as
constant
orimmutable
to reduce gas costs associated with lookups.
4. Regularly Audit and Test
- Iterative Testing: Deploy test contracts on testnets to measure gas usage, iterating on code changes to verify improvements.
- Automated Tools: Use gas profiling and analytics tools (e.g.,
hardhat-gas-reporter
) to identify and track optimization progress.
5. Leverage Layered Architectures
- Off-Chain Computations and Oracles: Perform complex calculations off-chain and feed only the necessary results into the contract.
- Rollups or Sidechains: While peaq EVM may offer certain scaling capabilities, consider hybrid approaches that further reduce mainnet gas consumption.