|
| 1 | +# `solana-include-idl` |
| 2 | + |
| 3 | +A collection of macro and helpers to manage IDLs stored on the program binary. |
| 4 | + |
| 5 | +IDL files describe the structure and API of a Solana program, facilitating easier integration and interaction with various client applications. This crate automates the task of publishing the IDL file by storing it as a separate ELF section within the program binary. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +The crate provides a macro that includes the type and contents of the IDL file in separate ELF sections on the program binary. |
| 10 | + |
| 11 | +* `.idl.type` contains the type of the IDL file. |
| 12 | +* `.idl.data` contains the IDL file itself. |
| 13 | + |
| 14 | +The macro takes two arguments: |
| 15 | + |
| 16 | +* `type`: The type of the IDL file. This should be one of the variants of the `IdlType` enum (e.g., `IdlType::Anchor` or `IdlType::Codama`). |
| 17 | +* `file`: The path to the IDL file. |
| 18 | + |
| 19 | +```rust |
| 20 | +use include_idl::{include_idl, parse::IdlType}; |
| 21 | + |
| 22 | +include_idl!(IdlType::Codama, concat!(env!("OUT_DIR"), "/codama.idl.zip")); |
| 23 | +``` |
| 24 | + |
| 25 | +In general, the macro is used in combination with a `build.rs` build script to compress the IDL file, reducing the space required on the program binary. |
| 26 | + |
| 27 | +To specify a build script, add a `build = "build.rs"` entry to your `Cargo.toml` file under the `[package]` section. Below is a `build.rs` example file that compresses an existing IDL file. |
| 28 | + |
| 29 | +```rust |
| 30 | +use std::env; |
| 31 | +use std::path::PathBuf; |
| 32 | +use include_idl::compress_idl; |
| 33 | + |
| 34 | +fn main() { |
| 35 | + // Get the IDL path. |
| 36 | + let idl_path = PathBuf::from("../api").join("idl.json"); |
| 37 | + // Compress the IDL file to a zip file. |
| 38 | + let out_dir = env::var("OUT_DIR").unwrap(); |
| 39 | + let dest_path = PathBuf::from(out_dir).join("codama.idl.zip"); |
| 40 | + |
| 41 | + compress_idl(&idl_path, &dest_path); |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### Generating an IDL |
| 46 | + |
| 47 | +If you are using [Anchor](https://www.anchor-lang.com), this step is alredy done for your. If you are writing a native Solana program or using a framework that does not export and IDL, you can use [Codama](https://github.com/codama-idl/codama?tab=readme-ov-file#from-program-to-codama). |
0 commit comments