> 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/ko/developer-guides/call-solana-from-evm.md).

# EVM에서 Solana 호출

Rome의 CPI와 헬퍼 프리컴파일을 통해 Solidity에서 Solana 프로그램을 호출하세요.

Rome의 프리컴파일을 사용하면 Solidity 계약이 Solana 프로그램을 직접 호출할 수 있습니다. 이 가이드에서는 그 메커니즘을 다룹니다.

## 필수 조건

* 공개 저장소의 Rome Solidity 인터페이스 [`rome-solidity`](https://github.com/rome-protocol/rome-solidity) 저장소(GitHub에서 설치하며, npm이 아님 — 참조 [Solidity 배포](https://docs.rome.builders/ko/developer-guides/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/ko/developer-guides/deploy-solidity.md))

각 인터페이스를 해당 프리컴파일 주소에 바인딩합니다:

```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 프리컴파일

`` `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 서명으로 호출: 계약의 external-authority PDA가 서명하며, 추가로 하나
// `salts`의 각 항목마다 salted sub-PDA가 하나씩 생성됩니다(Rome는 ["EXTERNAL_AUTHORITY", contract, salt]를 파생합니다)
CpiProgram.invoke_signed(programId, accounts, data, salts);
```

## Lamport 전송

단순한 lamport 및 SPL 전송에는 HelperProgram 프리컴파일을 사용하세요 — 직접 CPI를 구성할 필요가 없습니다. 이를 호출하는 계약은 다음과 같이 동작합니다 **자체적으로**: 아래 전송은 사용자의 것이 아니라 계약 자체의 lamport와 토큰(계약의 external-authority PDA가 서명함)을 이동합니다. 계약이 사용자의 SPL 토큰을 자신이 제어하는 토큰 계정으로 가져오고, 그것이 누구의 것인지 알기 위해서는 사용자가 SPL 위임을 부여합니다. ERC-20 `transfer` 또는 `transferFrom` 계약에 대한 전송은 래퍼의 에스크로 원장에만 적립되며, 계약에 지출할 토큰 계정을 제공하지 않습니다 — 참조 [Solana 프로그램 통합](https://docs.rome.builders/ko/developer-guides/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` 의 대리자 변형 포함); 정확한 시그니처는 참조 `의 CPI 프리컴파일`. 캐시된 트랙에서는 다음을 사용하세요 `ISplCached` (`0xff…05`) / `IAssociatedSplCached` (`0xff…06`) 대신 — 계약은 하나의 트랙만 일관되게 사용하며, 레거시 트랙만 다음을 할 수 있습니다 `invoke` 사용자 지정 프로그램(참조 [Solana 프로그램 통합](https://docs.rome.builders/ko/developer-guides/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 프리컴파일을 통해 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; // 여러분의 Solana 프로그램 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 → 대상 → 대상의 호출 → 한 번 더. 호출 깊이를 계획하세요.
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/ko/developer-guides/integrate-solana-program.md) — 사용자의 Solana 신원, 그것으로 서명하는 방식, one-track 규칙, 그리고 반대편의 실제 프로그램을 위한 계정 프로비저닝
* [사전 컴파일 참조](https://docs.rome.builders/ko/reference/precompiles.md) — 모든 precompile의 모든 selector
* [Solana에서 EVM 호출](https://docs.rome.builders/ko/developer-guides/call-evm-from-solana.md) — 반대 방향: Solana 지갑으로 EVM 계약을 구동하기
* [토큰 상호운용성](https://docs.rome.builders/ko/core-concepts/token-interop.md) — ERC-20과 SPL 토큰이 함께 작동하는 방식
* [제약 사항](https://docs.rome.builders/ko/core-concepts/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/ko/developer-guides/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.
