Prerequisites
- EVM compatibility: your project is deployed on peaq, so Ethereum optimization techniques apply.
- Basic knowledge of gas mechanics: you understand that gas costs rise with transaction complexity, storage operations, and on-chain computation.
- Stable contract logic: your contract logic is finished, so you can optimize it without expecting further functional changes.
Instructions for Optimizing Gas Fees
1. Minimize on-chain data storage
- Data compression: store compressed or hashed references instead of raw data sets.
- Hashing and linking: use hashes (for example
Keccak256) to reference large off-chain data, so you write less to storage. - State variable efficiency: consolidate related data into fewer state variables, or use bit-packing to store several flags or small integers in one variable.
2. Batch transactions
- Multi-call transactions: combine related operations into a single transaction, so you pay the per-transaction overhead once.
- Off-chain aggregation: collect user actions off chain and submit them on chain as one 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: storage access is expensive. Use a mapping instead of an array for lookups, and keep arrays short.
- Mark fixed values: declare values that never change as
constantorimmutableso reading them costs less.
4. Audit and test
- Iterative testing: deploy test contracts on agung to measure gas usage, and re-measure after each change.
- Automated tools: use a gas profiler such as
hardhat-gas-reporterto track where the gas goes.
5. Move computation off chain
- Perform complex calculations off chain, with an oracle where you need the result to be trusted, and feed only that result into the contract.

