솔라나 문서프로그램 개발하기Codama

클라이언트 생성하기

Codama는 프로그램의 IDL 파일에서 TypeScript 및 Rust 클라이언트를 생성합니다. 생성된 클라이언트에는 instruction 호출 및 계정 가져오기 기능이 포함되어 있어 프로그램 계정이나 instruction data를 수동으로 직렬화하고 역직렬화할 필요가 없습니다.

Codama는 활발히 개발 중이며 변경될 수 있습니다. 최신 정보는 빠른 참조에 연결된 리포지토리를 참조하세요.

빠른 참조

CLI 명령어

명령어설명
codama initCodama 구성 파일 생성
codama run구성 파일에서 프로그램 클라이언트 생성

클라이언트 생성기

패키지설명
@codama/renderers-js@solana/kit와 호환되는 TypeScript 클라이언트 생성
@codama/renderers-rustsolana_sdk와 호환되는 Rust 클라이언트 생성
@codama/nodes-from-anchorAnchor IDL을 Codama IDL로 변환하여 클라이언트 생성

추가 리소스

리소스설명
CodamaCodama GitHub
Codama CLICodama CLI GitHub
Codama MacrosCodama Macros GitHub. Codama 매크로를 사용하여 네이티브 Rust 프로그램에서 Codama IDL 생성
Codama Macros Example ProgramCodama 매크로 사용을 보여주는 예제 프로그램

예제

이 예제는 Anchor 프로그램 IDL에서 클라이언트를 생성하는 과정을 안내합니다.

프로젝트 파일 설정

따라하려면 example 디렉토리를 새로 만들고 코드 스니펫에서 idl.json 파일과 package.json 파일을 추가하세요.

이 예제는 카운터 프로그램 IDL을 사용합니다. 소스 보기.

Anchor 프로젝트에서 IDL은 anchor build를 실행한 후 target/idl/<program_name>.json에서 찾을 수 있습니다.

Codama 구성 파일 생성하기

Codama는 IDL 파일의 위치, 생성할 클라이언트 언어, 생성된 코드의 출력 위치를 지정하는 구성 파일을 사용합니다. 다음 명령을 실행하여 이 구성을 생성하세요:

Terminal
$
npx codama init

이 명령은 프로젝트 루트에 codama.json 파일을 생성하고 필요한 Codama 종속성을 설치하기 위한 질문을 표시합니다.

example output
Welcome to Codama!
Where is your IDL located? (Supports Codama and Anchor IDLs). … idl.json
Which script preset would you like to use? › Generate JavaScript client, Generate Rust client
[js] Where should the JavaScript code be generated? … clients/js/src/generated
[rust] Where is the Rust client crate located? … clients/rust
[rust] Where should the Rust code be generated? … clients/rust/src/generated
Your configuration requires additional dependencies.
Install command: pnpm install @codama/nodes-from-anchor @codama/renderers-js @codama/renderers-rust
Install dependencies? … yes
Installing
├─ @codama/nodes-from-anchor
├─ @codama/renderers-js
└─ @codama/renderers-rust
Dependencies installed successfully.
Configuration file created.
└─ Path: /example/codama.json

클라이언트 생성하기

구성 파일이 생성되면 다음 명령을 실행하여 클라이언트를 생성하세요:

Terminal
$
npx codama run --all

이 명령은 codama 구성 파일에 지정된 파일 경로에 프로그램 클라이언트를 생성합니다.

codama.json
{
"idl": "idl.json",
"before": [],
"scripts": {
"js": {
"from": "@codama/renderers-js",
"args": ["clients/js/src/generated"]
},
"rust": {
"from": "@codama/renderers-rust",
"args": [
"clients/rust/src/generated",
{
"crateFolder": "clients/rust",
"formatCode": true
}
]
}
}
}

생성된 모든 파일이 해당 코드 스니펫에 표시되지는 않습니다.

프로젝트 파일 설정

따라하려면 example 디렉토리를 새로 만들고 코드 스니펫에서 idl.json 파일과 package.json 파일을 추가하세요.

이 예제는 카운터 프로그램 IDL을 사용합니다. 소스 보기.

Anchor 프로젝트에서 IDL은 anchor build를 실행한 후 target/idl/<program_name>.json에서 찾을 수 있습니다.

Codama 구성 파일 생성하기

Codama는 IDL 파일의 위치, 생성할 클라이언트 언어, 생성된 코드의 출력 위치를 지정하는 구성 파일을 사용합니다. 다음 명령을 실행하여 이 구성을 생성하세요:

Terminal
$
npx codama init

이 명령은 프로젝트 루트에 codama.json 파일을 생성하고 필요한 Codama 종속성을 설치하기 위한 질문을 표시합니다.

example output
Welcome to Codama!
Where is your IDL located? (Supports Codama and Anchor IDLs). … idl.json
Which script preset would you like to use? › Generate JavaScript client, Generate Rust client
[js] Where should the JavaScript code be generated? … clients/js/src/generated
[rust] Where is the Rust client crate located? … clients/rust
[rust] Where should the Rust code be generated? … clients/rust/src/generated
Your configuration requires additional dependencies.
Install command: pnpm install @codama/nodes-from-anchor @codama/renderers-js @codama/renderers-rust
Install dependencies? … yes
Installing
├─ @codama/nodes-from-anchor
├─ @codama/renderers-js
└─ @codama/renderers-rust
Dependencies installed successfully.
Configuration file created.
└─ Path: /example/codama.json

클라이언트 생성하기

구성 파일이 생성되면 다음 명령을 실행하여 클라이언트를 생성하세요:

Terminal
$
npx codama run --all

이 명령은 codama 구성 파일에 지정된 파일 경로에 프로그램 클라이언트를 생성합니다.

codama.json
{
"idl": "idl.json",
"before": [],
"scripts": {
"js": {
"from": "@codama/renderers-js",
"args": ["clients/js/src/generated"]
},
"rust": {
"from": "@codama/renderers-rust",
"args": [
"clients/rust/src/generated",
{
"crateFolder": "clients/rust",
"formatCode": true
}
]
}
}
}

생성된 모든 파일이 해당 코드 스니펫에 표시되지는 않습니다.

idl.json
package.json
{
"address": "8PY1q5J3Aq2z7TBDLBrVjv77mYzjXSCz6iHQaFEFw9hY",
"metadata": {
"name": "counter",
"version": "0.1.0",
"spec": "0.1.0",
"description": "Created with Anchor"
},
"instructions": [
{
"name": "decrement",
"discriminator": [106, 227, 168, 59, 248, 27, 150, 101],
"accounts": [
{
"name": "user",
"writable": true,
"signer": true
},
{
"name": "counter",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [99, 111, 117, 110, 116, 101, 114]
}
]
}
},
{
"name": "vault",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [118, 97, 117, 108, 116]
},
{
"kind": "account",
"path": "user"
}
]
}
},
{
"name": "system_program",
"address": "11111111111111111111111111111111"
}
],
"args": []
},
{
"name": "increment",
"discriminator": [11, 18, 104, 9, 104, 174, 59, 33],
"accounts": [
{
"name": "user",
"writable": true,
"signer": true
},
{
"name": "counter",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [99, 111, 117, 110, 116, 101, 114]
}
]
}
},
{
"name": "vault",
"writable": true,
"pda": {
"seeds": [
{
"kind": "const",
"value": [118, 97, 117, 108, 116]
},
{
"kind": "account",
"path": "user"
}
]
}
},
{
"name": "system_program",
"address": "11111111111111111111111111111111"
}
],
"args": []
}
],
"accounts": [
{
"name": "Counter",
"discriminator": [255, 176, 4, 245, 188, 253, 124, 25]
}
],
"errors": [
{
"code": 6000,
"name": "UnderflowError",
"msg": "Counter cannot be decremented below zero"
}
],
"types": [
{
"name": "Counter",
"type": {
"kind": "struct",
"fields": [
{
"name": "count",
"type": "u64"
}
]
}
}
]
}

생성된 클라이언트 사용하기

생성된 클라이언트에는 프로그램과 상호 작용하기 위한 소스 코드가 포함되어 있지만 package.json (TypeScript용) 또는 Cargo.toml (Rust용) 파일은 포함되어 있지 않습니다.

새로운 package.jsonCargo.toml 파일을 생성하거나 생성된 클라이언트에서 필요한 종속성을 프로젝트의 기존 파일에 추가해야 합니다.

애플리케이션에 통합

클라이언트를 애플리케이션의 소스 디렉토리에 직접 생성한 다음 기존 package.json 또는 Cargo.toml에 필요한 종속성을 추가하세요.

이 접근 방식은 클라이언트 코드를 공유하거나 게시할 필요가 없는 애플리케이션을 구축할 때 사용하세요.

독립형 패키지 또는 크레이트

각 클라이언트에 대해 별도의 TypeScript 패키지나 Rust 크레이트를 만드세요. 예를 들어, clients/js/package.json를 추가하고 clients/rust/Cargo.toml를 추가하세요.

이 접근 방식은 클라이언트를 재사용 가능한 라이브러리(npm 또는 crates.io)로 게시하거나 모노레포에서 여러 애플리케이션 간에 클라이언트를 공유할 때 사용하세요.

Is this page helpful?

목차

페이지 편집

관리자

© 2025 솔라나 재단.
모든 권리 보유.