# Gas Optimization Tips

Reading prices from IFÁ Labs is already gas-efficient (simple `SLOAD` operations), but in larger protocols, small savings add up — especially on L2s like Base where transaction volume is high.

Here are practical tips tailored to oracle integrations.

#### 1. Batch Multiple Reads

If your contract needs several prices (e.g., multiple collateral types), use the batch function when available:

```solidity
bytes32[] memory assetIds = new bytes32[](3);
// populate array
(IIfaPriceFeed.PriceFeed[] memory infos, bool[] memory exists) = oracle.getAssetsInfo(assetIds);
```

One call is cheaper than multiple separate `getAssetInfo` calls.

#### 2. Cache Prices in Storage (When Appropriate)

For infrequent updates (stablecoins change slowly), read once and store:

```solidity
mapping(bytes32 => int256) public cachedPrices;
uint256 public lastCacheUpdate;

function updateCache(bytes32[] calldata assetIds) external {
    // ... batch read and store
    lastCacheUpdate = block.timestamp;
}
```

Subsequent operations use cheap storage reads instead of external view calls.

#### 3. Use Constants for Asset IDs and Oracle Address

Avoid runtime computation:

```solidity
address public constant ORACLE = 0xA9F17344689C2c2328F94464998db1d3e35B80dC;
bytes32 public constant USDT_ID = 0x6ca0cef6107263f3b09a51448617b659278cff744f0e702c24a2f88c91e65a0d;
```

Saves gas on every lookup.

#### 4. Pack Variables Efficiently

When storing oracle-related data, pack smaller types into single slots:

```solidity
struct OracleData {
    int256 price;      // 32 bytes
    uint128 timestamp; // 16 bytes
    uint128 decimal;   // fits in remaining space
}
```

Reduces `SLOAD` costs.

#### 5. Minimize External Calls in Hot Paths

Place oracle reads outside tight loops or high-frequency functions when possible.

#### Quick Gas Checklist

* Use `constant`/`immutable` for oracle address and IDs.
* Batch reads where possible.
* Cache if update frequency allows.
* Avoid unnecessary existence checks in trusted contexts.
* Test with tools like Foundry's gas reports.

These optimizations keep your protocol lean while maintaining security.

Next: See a **Complete Example Contract** putting it all together.
