> For the complete documentation index, see [llms.txt](https://spire-docs.gitbook.io/spire/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://spire-docs.gitbook.io/spire/pylon/features/l2-reads-from-appchain.md).

# L2 Reads from Appchain

Pylon enables your appchain contracts to read data from the settlement L2 chain using the `l2Read` function. This allows you to access L2 state without leaving your appchain, enabling [seamless cross-chain data access](/spire/pylon/features.md#seamless-devex).

### How It Works

The `l2Read` function is available through the [`SettlementPort`](/spire/pylon/architecture-overview.md#port-contracts) contract deployed on your appchain. It takes a target address on the L2 and encoded function call data, then returns the result from the L2 contract.

### Basic Usage

Here's how to use `l2Read` in your appchain contract:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.29;

interface IL2Reader {
    function l2Read(address _to, bytes calldata _data) external view returns (bytes memory);
}

contract MyAppContract {
    address public immutable settlementPort;
    
    constructor(address _settlementPort) {
        settlementPort = _settlementPort;
    }
    
    function getL2TokenBalance(address tokenContract, address account) external view returns (uint256) {
        // Encode the balanceOf function call
        bytes memory data = abi.encodeWithSignature("balanceOf(address)", account);
        
        // Perform the L2 read
        bytes memory result = IL2Reader(settlementPort).l2Read(tokenContract, data);
        
        // Decode the result
        return abi.decode(result, (uint256));
    }
    
    function getL2ContractData(address targetContract, bytes calldata callData) external view returns (bytes memory) {
        // Direct L2 read with custom call data
        return IL2Reader(settlementPort).l2Read(targetContract, callData);
    }
}
```

### Key Points

* **View Function**: `l2Read` is a `view` function, so it doesn't modify state
* **Synchronous**: The read appears synchronous to your contract, but the data is [pre-fetched by the sequencer](/spire/pylon/architecture-overview.md#priming-transactions)
* **Error Handling**: The function will revert if the L2 call fails

### Common Use Cases

* Reading token balances on L2
* Checking L2 contract state
* Accessing L2 price feeds
* Reading L2 governance data
* Cross-chain data validation

### Integration Example

```solidity
function processL2Data(address l2Contract) external view returns (bool) {
    try IL2Reader(settlementPort).l2Read(l2Contract, abi.encodeWithSignature("getStatus()")) returns (bytes memory result) {
        bool status = abi.decode(result, (bool));
        return status;
    } catch {
        // Handle L2 read failure
        return false;
    }
}
```

This pattern enables your appchain contracts to seamlessly integrate with L2 data while maintaining the performance benefits of your appchain.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://spire-docs.gitbook.io/spire/pylon/features/l2-reads-from-appchain.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
