> 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/ja/gaido/call-solana-from-evm.md).

# EVMからSolanaを呼ぶ

RomeのCPIとヘルパーprecompileを使って、SolidityからSolanaプログラムを呼び出します。

Rome の precompile は、Solidity コントラクトが Solana プログラムを直接呼び出せるようにします。このガイドではその仕組みを説明します。

## 前提条件

* 公開されている Rome Solidity インターフェースは [`rome-solidity`](https://github.com/rome-protocol/rome-solidity) リポジトリ（GitHub からインストールし、npm ではありません — 参照 [Solidityをデプロイ](https://docs.rome.builders/ja/gaido/deploy-solidity.md#using-the-rome-solidity-sdk)) — precompile インターフェースは次にあります [`contracts/interface.sol`](https://github.com/rome-protocol/rome-solidity/blob/master/contracts/interface.sol)
* デプロイ済みの Rome コントラクト（参照 [Solidityをデプロイ](https://docs.rome.builders/ja/gaido/deploy-solidity.md))

各インターフェースをそれぞれの precompile アドレスにバインドします:

```solidity
import {ICrossProgramInvocation, ISystemProgram, IHelperProgram}
    from "@rome-protocol/rome-solidity/contracts/interface.sol";

ICrossProgramInvocation constant CpiProgram    = ICrossProgramInvocation(0xFF00000000000000000000000000000000000008);
ISystemProgram          constant SystemProgram = ISystemProgram(0xFF00000000000000000000000000000000000007);
IHelperProgram          constant Helper        = IHelperProgram(0xFF00000000000000000000000000000000000009);
```

## CpiProgram precompile は

`CpiProgram` (`0xFF…08`) は CPI（`invoke` / `invoke_signed`) に加え、クロス状態読み取りショートカット（`account_info`, `account_data_at`, `account_u64_at`, `account_lamports`, `pdas_batch_derive`):

```solidity
// Solana プログラムを呼び出す
CpiProgram.invoke(programId, accounts, instructionData);

// PDA 署名で呼び出す: あなたのコントラクトの外部権限 PDA が署名し、さらに
// `salts` の各エントリごとに salt 付きの sub-PDA を 1 つずつ使用します（Rome は ["EXTERNAL_AUTHORITY", contract, salt] を導出します）
CpiProgram.invoke_signed(programId, accounts, data, salts);
```

## lamportsを送金する

単純な lamport と SPL の送金には、HelperProgram precompile を使います — 手作業で CPI を組み立てる必要はありません。これを呼び出すコントラクトは **それ自身として**: 以下の送金はコントラクト自身の lamports とトークン（その外部権限 PDA が署名します）を移動するのであり、ユーザーのものではありません。コントラクトがユーザーの SPL トークンを自分が管理するトークンアカウントへ引き出し、その所有者を知るには、ユーザーが SPL デリゲーションを付与します。ERC-20 `transfer` または `transferFrom` をコントラクトへ送っても、ラッパーのエスクロー台帳に記録されるだけで、コントラクトに支出元のトークンアカウントは与えられません — 参照 [Solana プログラムを統合する](https://docs.rome.builders/ja/gaido/integrate-solana-program.md).

```solidity
contract Transfers {
    IHelperProgram constant Helper = IHelperProgram(0xFF00000000000000000000000000000000000009);

    // このコントラクトの PDA から EVM アドレスの PDA へ lamports を送る
    function transferSol(address to, uint64 lamports) external {
        Helper.transfer_lamports(to, lamports);
    }

    // このコントラクトの PDA 所有トークンアカウントから SPL トークンを送る
    function transferSpl(address to, uint64 tokens, bytes32 mint) external {
        Helper.transfer_spl(to, tokens, mint);
    }

    // ミント用に呼び出し元の ATA を作成する
    function createAta(bytes32 mint) external {
        Helper.create_ata(msg.sender, mint);
    }
}
```

`transfer_spl` にはいくつかのオーバーロードがあります（ `transferFrom` フロー用のデリゲート版を含む）； `interface.sol`. キャッシュ済みトラックでは、代わりに `ISplCached` (`0xff…05`) / `IAssociatedSplCached` (`0xff…06`) を使ってください — コントラクトは一貫して 1 つのトラックを使い、レガシートラックだけが `invoke` カスタムプログラムを使用できます（参照 [Solana プログラムを統合する](https://docs.rome.builders/ja/gaido/integrate-solana-program.md)).

## アカウントデータの読み取り

CpiProgram の読み取りショートカットを使って、任意の Solana アカウントのデータを読み取れます:

```solidity
(
    uint64 lamports,
    bytes32 owner,
    bool isSigner,
    bool isWritable,
    bool executable,
    bytes memory data
) = CpiProgram.account_info(accountPubkey);
```

## PDAの導出

System precompile を使って Solidity から Program Derived Address を見つけます:

```solidity
ISystemProgram.Seed[] memory seeds = new ISystemProgram.Seed[](2);
seeds[0] = ISystemProgram.Seed("my-program-seed");
seeds[1] = ISystemProgram.Seed(abi.encodePacked(someValue));

(bytes32 pda, uint8 bump) = SystemProgram.find_program_address(targetProgramId, seeds);
```

## Base58 変換

相互変換します `bytes32` と base58（Solana のアドレス形式）の間で:

```solidity
bytes memory base58Str = SystemProgram.bytes32_to_base58(pubkey);
bytes32 pubkey = SystemProgram.base58_to_bytes32(base58Bytes);
```

## カスタムSolanaプログラムの呼び出し

任意の Solana プログラムを呼び出すには、アカウント一覧と instruction data を自分で構築します:

```solidity
contract CustomCPI {
    ICrossProgramInvocation constant CpiProgram = ICrossProgramInvocation(0xFF00000000000000000000000000000000000008);
    bytes32 constant MY_PROGRAM = 0x0000000000000000000000000000000000000000000000000000000000000000; // your Solana program ID

    function callMyProgram(bytes32 account1, bytes32 account2, bytes calldata ixData) external {
        ICrossProgramInvocation.AccountMeta[] memory accounts = new ICrossProgramInvocation.AccountMeta[](2);
        accounts[0] = ICrossProgramInvocation.AccountMeta(account1, false, true);
        accounts[1] = ICrossProgramInvocation.AccountMeta(account2, false, false);

        CpiProgram.invoke(MY_PROGRAM, accounts, ixData);
    }
}
```

## 主な制約

1. **すべてのアカウントは事前に宣言しなければなりません。** Solana トランザクションには、CPI が触れるすべてのアカウントを含める必要があります — CPI 内で動的にアカウントを見つけることはできません。
2. **CPI の深さ制限: 4 レベル。** Rome EVM → 対象 → 対象の呼び出し → さらに 1 段。呼び出し深度を計画してください。
3. **Solana の pubkey は `bytes32`,** 20 バイトの Ethereum アドレスではありません。
4. **instruction data は生のバイト列です** 対象プログラムが期待する形式（通常は Borsh、リトルエンディアン）である必要があります。

## 次は何か

* **実際のアプリで見る** — [cardo](https://github.com/rome-protocol/cardo) ユーザーのウォレットから直接 precompile に CPI 呼び出しを送り、アダプタコントラクトは使いません; [rome-dex](https://github.com/rome-protocol/rome-dex) （dual-lane AMM）も同様で、さらにカストディ不要のルーターコントラクトもあります; [aerarium](https://github.com/rome-protocol/aerarium) は、Solidity から Solana プログラムを呼び出さないレンディングアプリです。
* [Solana プログラムを統合する](https://docs.rome.builders/ja/gaido/integrate-solana-program.md) — ユーザーの Solana アイデンティティ、それとして署名すること、1 トラックのルール、そして向こう側の実際のプログラムに対するアカウント準備
* [プリコンパイルリファレンス](https://docs.rome.builders/ja/rifarensu/precompiles.md) — すべての precompile のすべてのセレクタ
* [Solana から EVM を呼び出す](https://docs.rome.builders/ja/gaido/call-evm-from-solana.md) — 逆方向: Solana ウォレットから EVM コントラクトを操作する
* [トークン相互運用](https://docs.rome.builders/ja/ji-ben-gai-nian/token-interop.md) — ERC-20 と SPL トークンがどう連携するか
* [制約](https://docs.rome.builders/ja/ji-ben-gai-nian/constraints.md) — CPI の深さとその他の制限


---

# 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/ja/gaido/call-solana-from-evm.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.
