circle-info
This is the first version of our Documentation, it will be improved and extended soon.
Page cover

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:

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:

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:

Saves gas on every lookup.

4. Pack Variables Efficiently

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

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.

Last updated

Was this helpful?