> For the complete documentation index, see [llms.txt](https://docs.rome.builders/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rome.builders/rome-academy/05-call-solana-from-solidity.md).

# 5. Call Solana from Solidity

Use Rome's CPI and Helper precompiles to read Solana accounts, create token accounts and call Solana programs from a Solidity contract.

On Rome a Solidity contract can read Solana accounts and call Solana programs in the same transaction. You will read a Solana token balance, create a token account and learn how to build a program call.

## What you'll learn

* What a precompile is on Rome and which three you will use most.
* Whose signature a Solana program sees when your contract calls it.
* How to read Solana state and create a token account from Solidity.
* The rules that decide whether a Solana call works: declared accounts, one track, atomic only.

## Before you start

* Finish [4. How Rome runs your code](https://docs.rome.builders/rome-academy/04-how-rome-runs-your-code.md). The atomic budget and the 64-account cap matter here.
* Have your Hadrian wallet funded and your deploy setup from lesson 3 working. Faucets are listed in [Faucets](https://docs.rome.builders/resources/faucets.md).
* Install the Rome Solidity interfaces from the `rome-solidity` repo, as described in [Deploy Solidity](https://docs.rome.builders/developer-guides/deploy-solidity.md#using-the-rome-solidity-sdk).

## Precompiles are the door

A precompile is a fixed address that runs native code instead of bytecode. Ethereum has a few, such as `ecrecover`. Rome adds precompiles whose job is to reach Solana. You call them like any contract, through an interface.

You will use three:

| Precompile    | What it gives you                                                                                      |
| ------------- | ------------------------------------------------------------------------------------------------------ |
| CpiProgram    | `invoke` and `invoke_signed` to call any Solana program, plus read-only views of Solana accounts       |
| HelperProgram | Ready-made token and account operations: create a token account, transfer SPL tokens, derive addresses |
| System        | PDA derivation and base58 conversion                                                                   |

The addresses are in [Precompile reference](https://docs.rome.builders/reference/precompiles.md) and [Contract addresses](https://docs.rome.builders/reference/contract-addresses.md). You do not need to type them. Importing `interface.sol` from `rome-solidity` gives you ready-made constants named `CpiProgram`, `HelperProgram` and `SystemProgram`.

## Who signs

When your contract calls a precompile, Solana sees your contract's own identity, not the user's. Every EVM address has a Solana program-derived address, its external-authority PDA. It owns that address's token accounts and signs on its behalf.

This is the same rule you know from ERC-20: a contract cannot move a user's assets unless the user allowed it. Lesson 6 shows how the user allows it on Rome. The rule and the three ways to hold a user's position are in [Integrate a Solana program](https://docs.rome.builders/pages/gpZ5lOamv8YlMrUbFYh1#id-1.-every-evm-address-has-one-solana-identity).

## Step 1: read before you write

Start with reads. They change no state and work in atomic and iterative execution. Read before you mutate: once a cached call has run, legacy-track reads are refused for the rest of the transaction.

```solidity
import "@rome-protocol/rome-solidity/contracts/interface.sol";

contract SolanaPeek {
    // The token account Rome uses for the caller and the chain's gas mint
    function myTokenAccount() external view returns (bytes32) {
        return HelperProgram.ata(msg.sender, SystemProgram.mint_id());
    }

    // The caller's balance in that account
    function myBalance() external view returns (uint64) {
        return HelperProgram.user_balance(msg.sender, SystemProgram.mint_id());
    }
}
```

Solana addresses are `bytes32` here, not 20-byte addresses. The other reads, `account_info`, `account_data_at` and `account_u64_at`, let you inspect any Solana account. See [Reading account data](https://docs.rome.builders/developer-guides/call-solana-from-evm.md#reading-account-data).

## Step 2: use a helper

For common jobs you need no hand-built instruction. The Helper creates token accounts, moves SPL tokens and lamports, and converts between gas and lamports. The signatures are in `contracts/interface.sol`, and a worked contract is in [Transfer lamports](https://docs.rome.builders/developer-guides/call-solana-from-evm.md#transfer-lamports).

```solidity
function createMyAta() external {
    HelperProgram.create_ata(msg.sender, SystemProgram.mint_id());
}
```

A contract that calls the Helper acts as itself. The tokens it moves are its own, not the user's.

## Step 3: call any Solana program

For a program the helpers do not cover, build the account list and the instruction bytes yourself and call `invoke`. Each account is a pubkey plus two flags, signer and writable. The instruction data is whatever the target program expects, usually Borsh.

The full contract is in [Calling custom Solana programs](https://docs.rome.builders/developer-guides/call-solana-from-evm.md#calling-custom-solana-programs). `invoke_signed` adds salted sub-PDAs when the target needs more than one signer from your contract. To get a PDA's address inside Solidity, use `find_program_address` on the System precompile; see [PDA derivation](https://docs.rome.builders/developer-guides/call-solana-from-evm.md#pda-derivation).

## The rules that decide whether it works

* **Declare every account.** Solana needs the full account list before the call runs. The proxy finds it by emulating your transaction first, so each account must be derivable from inputs the contract has. The total must stay within the 64-address cap.
* **Stay atomic.** A call that makes a legacy-track CPI cannot run iteratively. It has to fit one atomic transaction, so keep each action to one token movement and one CPI.
* **Pick one track.** Rome has two families of precompiles that change Solana state: legacy (`CpiProgram`, `HelperProgram`, `Withdraw`) and cached (the `...Cached` set). A transaction uses one family. Only the legacy track can call a custom program. Read the table in [Integrate a Solana program](https://docs.rome.builders/pages/gpZ5lOamv8YlMrUbFYh1#id-4.-one-track-per-contract-and-per-transaction).
* **Keep depth shallow.** Your call to a Solana program uses CPI levels, and so does every program it calls in turn. Solana's depth limit is low. [Constraints](https://docs.rome.builders/core-concepts/constraints.md#cpi-constraints) explains the levels.
* **No calls back into Rome.** A CPI cannot name or call the Rome program. It cannot name the Solana fee payer except in a plain System transfer instruction.

## Check yourself

Add `createMyAta` to `SolanaPeek` and deploy it to Hadrian. Then:

1. Call `myTokenAccount` and `myBalance` with a read call from your wallet address.
2. Send a transaction that calls `createMyAta`.
3. Open the transaction on the Hadrian explorer. See [Via explorer](https://docs.rome.builders/apps-on-rome/via.md).

You should see the Solana instruction behind your call and its compute units. A call that reaches Solana shows a Solana program in the explorer, not only an EVM call.

<details>

<summary>1. A contract calls <code>HelperProgram.transfer_spl</code>. Whose tokens move?</summary>

The contract's own. The signer is the contract's external-authority PDA. To move a user's tokens the user must grant a right first.

</details>

<details>

<summary>2. Why must a call that uses <code>invoke</code> fit one atomic transaction?</summary>

Legacy-track CPIs are refused inside an iterative transaction. If the call needs more than one Solana transaction, it fails.

</details>

<details>

<summary>3. Which reads can you rely on in any execution mode?</summary>

The read-only views: `account_info`, `account_data_at`, `account_u64_at`, `account_lamports`, `pdas_batch_derive` and the Helper reads such as `user_balance`. They have no side effects. Run them before any cached mutation in the same transaction.

</details>

## Next

[6. Tokens on both sides](https://docs.rome.builders/rome-academy/06-tokens-on-both-sides.md)


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rome.builders/rome-academy/05-call-solana-from-solidity.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
