> 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/rome/05-call-solana-from-solidity.md).

# 5. Solidity에서 Solana 호출하기

Rome의 CPI 및 Helper 프리컴파일을 사용해 Solana 계정을 읽고, 토큰 계정을 생성하고, Solidity 컨트랙트에서 Solana 프로그램을 호출합니다.

Rome에서는 Solidity 계약이 같은 트랜잭션에서 Solana 계정을 읽고 Solana 프로그램을 호출할 수 있습니다. Solana 토큰 잔액을 읽고, 토큰 계정을 생성하며, 프로그램 호출을 만드는 방법을 배우게 됩니다.

## 배울 내용

* Rome에서 프리컴파일이 무엇인지, 그리고 가장 많이 사용할 세 가지가 무엇인지.
* 계약이 Solana 프로그램을 호출할 때 Solana 프로그램이 보는 서명이 누구의 것인지.
* Solidity에서 Solana 상태를 읽고 토큰 계정을 생성하는 방법.
* Solana 호출이 작동하는지 결정하는 규칙: 선언된 계정, 하나의 트랙, 원자적 실행만.

## 시작하기 전에

* 마치기 [4. Rome이 코드를 실행하는 방식](https://docs.rome.builders/ko/rome/04-how-rome-runs-your-code.md). 원자적 예산과 64개 계정 상한이 여기서 중요합니다.
* Hadrian 지갑에 자금이 충전되어 있고, 3강의 배포 설정이 작동하는지 확인하세요. Faucet은 다음에 나열되어 있습니다 [파우셋](https://docs.rome.builders/ko/resources/faucets.md).
* 다음에서 Rome Solidity 인터페이스를 설치하세요 `rome-solidity` 리포지토리에서, 다음에 설명된 대로 [Solidity 배포](https://docs.rome.builders/ko/developer-guides/deploy-solidity.md#using-the-rome-solidity-sdk).

## 프리컴파일은 문입니다

프리컴파일은 바이트코드 대신 네이티브 코드를 실행하는 고정 주소입니다. Ethereum에는 몇 가지가 있으며, 예를 들어 `ecrecover`. Rome은 Solana에 도달하는 역할을 하는 프리컴파일을 추가합니다. 인터페이스를 통해 다른 계약처럼 호출하면 됩니다.

세 가지를 사용하게 됩니다:

| 사전 컴파일            | 제공되는 것                                                                         |
| ----------------- | ------------------------------------------------------------------------------ |
| \`CpiProgram\`    | `invoke` 그리고 `invoke_signed` 모든 Solana 프로그램을 호출하고, Solana 계정을 읽기 전용으로 조회할 수 있음 |
| \`HelperProgram\` | 미리 준비된 토큰 및 계정 작업: 토큰 계정 생성, SPL 토큰 전송, 주소 도출                                  |
| System            | PDA 도출 및 base58 변환                                                             |

주소는 다음에 있습니다 [사전 컴파일 참조](https://docs.rome.builders/ko/reference/precompiles.md) 그리고 [계약 주소](https://docs.rome.builders/ko/reference/contract-addresses.md). 직접 입력할 필요는 없습니다. 다음을 임포트하면 `의 CPI 프리컴파일` 에서 `rome-solidity` 다음과 같은 미리 정의된 상수를 사용할 수 있습니다 `` `CpiProgram` ``, `` `HelperProgram` `` 그리고 `SystemProgram`.

## 누가 서명하는가

계약이 프리컴파일을 호출하면 Solana는 사용자가 아니라 계약 자체의 신원을 봅니다. 모든 EVM 주소에는 Solana 프로그램이 도출한 주소, 즉 외부 권한 PDA가 있습니다. 이 PDA가 해당 주소의 토큰 계정을 소유하고 그 대신 서명합니다.

이는 ERC-20에서 알고 있는 것과 같은 규칙입니다. 사용자가 허용하지 않으면 계약은 사용자의 자산을 이동시킬 수 없습니다. 6강에서는 사용자가 Rome에서 이를 허용하는 방법을 보여줍니다. 이 규칙과 사용자의 포지션을 보유하는 세 가지 방법은 다음에 있습니다 [Solana 프로그램 통합](https://docs.rome.builders/pages/059b03f494e36e3c2923aca0e51d7e6fd10b3744#id-1.-every-evm-address-has-one-solana-identity).

## 1단계: 쓰기 전에 읽기

읽기부터 시작하세요. 읽기는 상태를 변경하지 않으며 원자적 실행과 반복 실행에서 모두 작동합니다. 변경하기 전에 읽으세요: 한 번 캐시된 호출이 실행되면, 그 트랜잭션의 나머지 동안 레거시 트랙 읽기는 거부됩니다.

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

contract SolanaPeek {
    // Rome이 호출자와 체인의 가스 민트에 사용하는 토큰 계정
    function myTokenAccount() external view returns (bytes32) {
        return HelperProgram.ata(msg.sender, SystemProgram.mint_id());
    }

    // 해당 계정에서 호출자의 잔액
    function myBalance() external view returns (uint64) {
        return HelperProgram.user_balance(msg.sender, SystemProgram.mint_id());
    }
}
```

Solana 주소는 `bytes32` 여기이며, 20바이트 주소가 아닙니다. 다른 읽기 함수인 `account_info`, `account_data_at` 그리고 `account_u64_at`, 는 어떤 Solana 계정이든 검사할 수 있게 해줍니다. 다음을 참조하세요 [계정 데이터 읽기](https://docs.rome.builders/ko/developer-guides/call-solana-from-evm.md#reading-account-data).

## 2단계: 헬퍼 사용

일반적인 작업에는 직접 만든 instruction이 필요 없습니다. Helper는 토큰 계정을 생성하고, SPL 토큰과 lamport를 이동시키며, gas와 lamport 사이를 변환합니다. 서명은 다음에 있습니다 `contracts/interface.sol`, 그리고 완성 예제 계약은 다음에 있습니다 [Lamport 전송](https://docs.rome.builders/ko/developer-guides/call-solana-from-evm.md#transfer-lamports).

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

Helper를 호출하는 계약은 자기 자신으로서 동작합니다. 이동시키는 토큰은 사용자의 것이 아니라 계약 자신의 것입니다.

## 3단계: 임의의 Solana 프로그램 호출

헬퍼가 다루지 않는 프로그램의 경우, 계정 목록과 instruction 바이트를 직접 구성한 뒤 다음을 호출하세요 `invoke`. 각 계정은 pubkey와 두 플래그, 즉 signer와 writable로 구성됩니다. instruction 데이터는 대상 프로그램이 기대하는 형식이면 되며, 보통 Borsh입니다.

전체 계약은 다음에 있습니다 [사용자 정의 Solana 프로그램 호출](https://docs.rome.builders/ko/developer-guides/call-solana-from-evm.md#calling-custom-solana-programs). `invoke_signed` 대상이 계약으로부터 둘 이상의 서명을 필요로 할 때 salted sub-PDA를 추가합니다. Solidity 안에서 PDA의 주소를 얻으려면 다음을 사용하세요 `find_program_address` System 프리컴파일의 다음을 사용하세요; 다음을 참조하세요 [PDA 도출](https://docs.rome.builders/ko/developer-guides/call-solana-from-evm.md#pda-derivation).

## 작동 여부를 결정하는 규칙

* **모든 계정을 선언하세요.** Solana는 호출이 실행되기 전에 전체 계정 목록이 필요합니다. 프록시는 먼저 트랜잭션을 에뮬레이션하여 이를 찾으므로, 각 계정은 계약이 가진 입력으로부터 도출 가능해야 합니다. 전체는 64개 주소 상한 내에 있어야 합니다.
* **원자적으로 유지하세요.** 레거시 트랙 CPI를 만드는 호출은 반복적으로 실행할 수 없습니다. 하나의 원자적 트랜잭션 안에 들어가야 하므로, 각 작업은 토큰 이동 1회와 CPI 1회로 제한하세요.
* **하나의 트랙을 선택하세요.** Rome에는 Solana 상태를 변경하는 두 계열의 프리컴파일이 있습니다: legacy (`` `CpiProgram` ``, `` `HelperProgram` ``, `출금`)와 cached(다음의 `...Cached` 세트). 하나의 트랜잭션은 한 계열만 사용합니다. 사용자 정의 프로그램을 호출할 수 있는 것은 legacy 트랙뿐입니다. 다음의 표를 읽어보세요 [Solana 프로그램 통합](https://docs.rome.builders/pages/059b03f494e36e3c2923aca0e51d7e6fd10b3744#id-4.-one-track-per-contract-and-per-transaction).
* **깊이는 얕게 유지하세요.** Solana 프로그램에 대한 호출은 CPI 레벨을 사용하며, 그 프로그램이 차례로 호출하는 모든 프로그램도 마찬가지입니다. Solana의 깊이 제한은 낮습니다. [제약 사항](https://docs.rome.builders/ko/core-concepts/constraints.md#cpi-constraints) 레벨을 설명합니다.
* **Rome으로 다시 호출하지 마세요.** CPI는 Rome 프로그램의 이름을 지정하거나 호출할 수 없습니다. 단순한 System 전송 instruction을 제외하고는 Solana 수수료 지불자를 지정할 수도 없습니다.

## 자신을 점검해 보세요

추가하세요 `createMyAta` 를 `SolanaPeek` 그리고 Hadrian에 배포하세요. 그런 다음:

1. 호출하십시오 `myTokenAccount` 그리고 `myBalance` 지갑 주소에서 읽기 호출을 사용하세요.
2. 다음을 호출하는 트랜잭션을 보내세요 `createMyAta`.
3. Hadrian 탐색기에서 해당 트랜잭션을 여세요. 다음을 참조하세요 [Via 탐색기](https://docs.rome.builders/ko/rome-1/via.md).

호출 뒤에 있는 Solana instruction과 그 compute unit을 볼 수 있어야 합니다. Solana에 도달하는 호출은 탐색기에서 EVM 호출만이 아니라 Solana 프로그램도 표시합니다.

<details>

<summary>1. 계약이 다음을 호출합니다 <code>HelperProgram.transfer_spl</code>. 누구의 토큰이 이동하나요?</summary>

계약 자신의 토큰입니다. 서명자는 계약의 external-authority PDA입니다. 사용자의 토큰을 이동하려면 먼저 사용자가 권한을 부여해야 합니다.

</details>

<details>

<summary>2. 다음을 사용하는 호출이 왜 <code>invoke</code> 하나의 원자적 트랜잭션에 들어가야 하나요?</summary>

레거시 트랙 CPI는 반복 트랜잭션 내에서 거부됩니다. 호출에 Solana 트랜잭션이 두 개 이상 필요하면 실패합니다.

</details>

<details>

<summary>3. 어떤 읽기를 모든 실행 모드에서 신뢰할 수 있나요?</summary>

읽기 전용 뷰: `account_info`, `account_data_at`, `account_u64_at`, `account_lamports`, `pdas_batch_derive` 그리고 다음과 같은 Helper 읽기 함수: `user_balance`. 이들은 부작용이 없습니다. 같은 트랜잭션에서 캐시된 변경 작업보다 먼저 실행하세요.

</details>

## 다음

[6. 양쪽의 토큰](https://docs.rome.builders/ko/rome/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/ko/rome/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.
