# Integrating Storage Proofs

This guide will walk you through the process of integrating Storage Proofs into your decentralized applications (dApps). By following these steps, you'll be able to leverage verifiable cross-chain data in your smart contracts.

## Prerequisites

* Basic knowledge of Solidity and smart contract development
* Familiarity with the Herodotus Storage Proof API
* An active Herodotus API key

## 1. Set Up Your Development Environment

1. Ensure you have a development environment set up for Ethereum smart contract development (e.g., Hardhat, Truffle, or Foundry).
2. Install the necessary dependencies, including OpenZeppelin contracts for standard implementations.

## 2. Implement the IFactsRegistry Interface

Create an interface in your smart contract to interact with the Herodotus Facts Registry:

```solidity
interface IFactsRegistry {
    function accountField(
        address account,
        uint256 blockNumber,
        uint8 field
    ) external view returns (bytes32);

    function accountStorageSlotValues(
        address account,
        uint256 blockNumber,
        bytes32 slot
    ) external view returns (bytes32);
}
```

## 3. Set Up Your Smart Contract

Create your smart contract and include the IFactsRegistry interface:

```solidity
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";

contract MyStorageProofContract is Ownable {
    IFactsRegistry public factsRegistry;

    constructor(address _factsRegistry) {
        factsRegistry = IFactsRegistry(_factsRegistry);
    }

    // Contract logic goes here
}
```

## 4. Implement Storage Proof Verification

Add functions to your contract that use the Facts Registry to verify data:

```solidity
function verifyBalance(address account, uint256 blockNumber) public view returns (uint256) {
    bytes32 balanceField = factsRegistry.accountField(account, blockNumber, 1); // 1 represents BALANCE
    return uint256(balanceField);
}

function verifyStorageSlot(address account, uint256 blockNumber, bytes32 slot) public view returns (bytes32) {
    return factsRegistry.accountStorageSlotValues(account, blockNumber, slot);
}
```

## 5. Use Verified Data in Your Contract Logic

Implement your contract's core logic using the verified data:

```solidity
function executeBasedOnVerifiedBalance(address account, uint256 blockNumber, uint256 threshold) public {
    uint256 verifiedBalance = verifyBalance(account, blockNumber);
    
    if (verifiedBalance > threshold) {
        // Execute some logic
    } else {
        // Execute alternative logic
    }
}
```

## 6. Deploy and Interact

1. Deploy your contract to the desired network, providing the address of the appropriate Facts Registry contract for that network.
2. Interact with your contract, ensuring that the necessary Storage Proofs have been submitted and processed before calling functions that rely on verified data.

## Best Practices

1. **Error Handling**: Implement proper error handling for cases where proofs might not be available yet.
2. **Gas Optimization**: Be mindful of gas costs when verifying multiple pieces of data.
3. **Updating Facts Registry**: Implement a function to update the Facts Registry address in case of upgrades.
4. **Timeouts**: Consider implementing timeouts for data verification to ensure your contract doesn't rely on outdated information.

## Using Turbo

For a more streamlined integration, consider using Turbo. Turbo abstracts away much of the complexity of working with Storage Proofs. Here's a basic example:

```solidity
import "@herodotus/turbo/contracts/ITurboSwap.sol";

contract MyTurboContract {
    ITurboSwap public turboSwap;

    constructor(address _turboSwap) {
        turboSwap = ITurboSwap(_turboSwap);
    }

    function verifyBalanceWithTurbo(uint256 chainId, uint256 blockNumber, address account) public returns (bytes32) {
        return turboSwap.accounts(chainId, blockNumber, account, AccountFields.BALANCE);
    }
}
```

Learn more about how you can use Turbo to significantly simplify your integration process [here](/solutions/turbo.md).

## Conclusion

Integrating Storage Proofs into your dApps allows you to leverage verified cross-chain data, opening up new possibilities for decentralized applications. As you become more comfortable with the basics, explore more complex proof types and consider using Turbo for even easier integration.

Remember to always test thoroughly and consider the security implications of relying on external data in your smart contracts. Happy building!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.storageproofs.com/guides/integrating-storage-proofs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
