Skip to content

Commit 32002b8

Browse files
authored
Add include_idl readme (#86)
* Add crate details * Add readme * Update crate name * Add cli crate details * Typos
1 parent bf9c43d commit 32002b8

File tree

10 files changed

+98
-31
lines changed

10 files changed

+98
-31
lines changed

Cargo.lock

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include-idl-cli/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
[package]
2-
name = "include-idl-cli"
2+
name = "solana-include-idl-cli"
3+
description = "CLI to parse IDL files from a program binary"
34
version = "0.1.0"
45
edition = "2021"
6+
license-file = "Apache-2.0"
57

68
[dependencies]
79
clap = { version = "4.5.7", features = ["derive"] }
810
goblin = { version = "0.8.2" }
9-
include-idl = { path = "../include-idl", features = ["parse"] }
11+
solana-include-idl = { version = "^0", features = ["parse"], path = "../include-idl" }

include-idl-cli/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# `solana-include-idl-cli`
2+
3+
A simple CLI to read an IDL file stored on a Solana program binary.
4+
5+
```console
6+
Usage: solana-include-idl-cli parse <PATH>
7+
8+
Arguments:
9+
<PATH> Path to the program binary
10+
11+
Options:
12+
-h, --help Print help
13+
```

include-idl-cli/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clap::{Error, Parser, Subcommand};
2-
use include_idl::parse::parse_idl_from_program_binary;
2+
use solana_include_idl::parse::parse_idl_from_program_binary;
33
use std::path::PathBuf;
44

55
#[derive(Parser)]
@@ -11,8 +11,9 @@ struct Cli {
1111

1212
#[derive(Subcommand)]
1313
enum Commands {
14+
/// Read IDL from a solana program binary.
1415
Parse {
15-
/// Read IDL from a solana program binary
16+
/// Path to the program binary.
1617
path: PathBuf,
1718
},
1819
}

include-idl/Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[package]
2-
name = "include-idl"
2+
name = "solana-include-idl"
3+
description = "A collection of macro and helpers to manage IDL stored on the program binary"
34
version = "0.1.0"
45
edition = "2021"
6+
license-file = "Apache-2.0"
57

68
[features]
7-
shrink = ["flate2"]
8-
parse = ["flate2", "goblin", "serde_json"]
9+
shrink = ["dep:flate2"]
10+
parse = ["dep:flate2", "dep:goblin", "dep:serde_json"]
911

1012
[dependencies]
1113
flate2 = { version = "1.0", optional = true }

include-idl/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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).

include-idl/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ pub use shrink::compress_idl;
2121
///
2222
/// This macro takes two arguments:
2323
///
24-
/// - `type`: The type of the IDL file. This should be one of the variants of the [`IdlType``] enum.
24+
/// - `type`: The type of the IDL file. This should be one of the variants of the [`IdlType`] enum.
2525
/// - `file`: The path to the IDL file.
2626
///
2727
/// # Example
2828
///
2929
/// Include the following in your `lib.rs` file:
3030
///
3131
/// ```ignore
32+
/// use include_idl::{include_idl, parse::IdlType};
33+
///
3234
/// include_idl!(IdlType::Codama, concat!(env!("OUT_DIR"), "/codama.idl.zip"));
3335
/// ```
3436
#[macro_export]

programs/asset/program/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ num-traits = "^0.2"
2525
solana-security-txt = "1.1.1"
2626
solana-program = "~1.16"
2727
thiserror = "^1.0"
28-
include-idl = { path = "../../../include-idl" }
28+
solana-include-idl = { path = "../../../include-idl" }
2929

3030
[build-dependencies]
31-
include-idl = { path = "../../../include-idl", features = ["shrink"] }
31+
solana-include-idl = { path = "../../../include-idl", features = ["shrink"] }

programs/asset/program/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22
use std::path::PathBuf;
33
use std::process::Command;
44

5-
use include_idl::compress_idl;
5+
use solana_include_idl::compress_idl;
66

77
fn main() {
88
// Run shank to generate the IDL

programs/asset/program/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub mod instruction;
1212
pub mod processor;
1313
pub mod utils;
1414

15-
use include_idl::{include_idl, parse::IdlType};
15+
use solana_include_idl::{include_idl, parse::IdlType};
1616
pub use solana_program;
1717
use solana_security_txt::security_txt;
1818

0 commit comments

Comments
 (0)