> ## Documentation Index
> Fetch the complete documentation index at: https://docs.peaq.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Optimizing Gas Usage

On EVM networks, **gas fees** represent the **computational cost** of executing transactions and smart contract operations. Lowering these fees can help developers and users alike.
Common strategies include **reducing unnecessary computations**, **optimizing data storage**, **leveraging efficient data structures**, **batching transactions**, and **offloading computation
off-chain** when possible. Ultimately, the goal is to use fewer computational resources per transaction, thus lowering the gas cost and improving overall **efficiency**.

## Prerequisites

1. **EVM Compatibility:** Your project is deployed on peaq network, which has EVM compatibility, allowing you to apply common Ethereum-based **optimization** techniques.
2. **Basic Knowledge of Gas Mechanics:** You understand that gas fees increase with transaction complexity, storage operations, and on-chain computations.
3. **Developer-Level Access:** You have the ability to modify and deploy smart contracts, review their code, and adjust transaction creation parameters.
4. **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` or `immutable` 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.

## Summary

By applying these strategies—reducing on-chain complexity, storing less data and opting for hashed data, batching operations, and thoroughly testing changes—you can **significantly
lower gas fees** on the peaq EVM network without compromising your decentralized application's functionality.
