diff --git a/Cargo.lock b/Cargo.lock index 0498016..2906afa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -388,12 +388,12 @@ version = "0.6.1" dependencies = [ "borsh 0.10.3", "bytemuck", - "include-idl", "nifty-asset-types", "nitrate", "num-derive 0.3.3", "num-traits", "shank", + "solana-include-idl", "solana-program", "solana-security-txt", "thiserror", @@ -1897,24 +1897,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "include-idl" -version = "0.1.0" -dependencies = [ - "flate2", - "goblin 0.8.2", - "serde_json", -] - -[[package]] -name = "include-idl-cli" -version = "0.1.0" -dependencies = [ - "clap 4.5.7", - "goblin 0.8.2", - "include-idl", -] - [[package]] name = "index_list" version = "0.2.11" @@ -3962,6 +3944,24 @@ dependencies = [ "syn 2.0.52", ] +[[package]] +name = "solana-include-idl" +version = "0.1.0" +dependencies = [ + "flate2", + "goblin 0.8.2", + "serde_json", +] + +[[package]] +name = "solana-include-idl-cli" +version = "0.1.0" +dependencies = [ + "clap 4.5.7", + "goblin 0.8.2", + "solana-include-idl", +] + [[package]] name = "solana-loader-v4-program" version = "1.16.25" diff --git a/include-idl-cli/Cargo.toml b/include-idl-cli/Cargo.toml index 5ec5d6c..ee08994 100644 --- a/include-idl-cli/Cargo.toml +++ b/include-idl-cli/Cargo.toml @@ -1,9 +1,11 @@ [package] -name = "include-idl-cli" +name = "solana-include-idl-cli" +description = "CLI to parse IDL files from a program binary" version = "0.1.0" edition = "2021" +license-file = "Apache-2.0" [dependencies] clap = { version = "4.5.7", features = ["derive"] } goblin = { version = "0.8.2" } -include-idl = { path = "../include-idl", features = ["parse"] } +solana-include-idl = { version = "^0", features = ["parse"], path = "../include-idl" } diff --git a/include-idl-cli/README.md b/include-idl-cli/README.md new file mode 100644 index 0000000..09d1d02 --- /dev/null +++ b/include-idl-cli/README.md @@ -0,0 +1,13 @@ +# `solana-include-idl-cli` + +A simple CLI to read an IDL file stored on a Solana program binary. + +```console +Usage: solana-include-idl-cli parse + +Arguments: + Path to the program binary + +Options: + -h, --help Print help +``` \ No newline at end of file diff --git a/include-idl-cli/src/main.rs b/include-idl-cli/src/main.rs index f6cb75b..1048cc4 100644 --- a/include-idl-cli/src/main.rs +++ b/include-idl-cli/src/main.rs @@ -1,5 +1,5 @@ use clap::{Error, Parser, Subcommand}; -use include_idl::parse::parse_idl_from_program_binary; +use solana_include_idl::parse::parse_idl_from_program_binary; use std::path::PathBuf; #[derive(Parser)] @@ -11,8 +11,9 @@ struct Cli { #[derive(Subcommand)] enum Commands { + /// Read IDL from a solana program binary. Parse { - /// Read IDL from a solana program binary + /// Path to the program binary. path: PathBuf, }, } diff --git a/include-idl/Cargo.toml b/include-idl/Cargo.toml index 8b10438..2466c30 100644 --- a/include-idl/Cargo.toml +++ b/include-idl/Cargo.toml @@ -1,11 +1,13 @@ [package] -name = "include-idl" +name = "solana-include-idl" +description = "A collection of macro and helpers to manage IDL stored on the program binary" version = "0.1.0" edition = "2021" +license-file = "Apache-2.0" [features] -shrink = ["flate2"] -parse = ["flate2", "goblin", "serde_json"] +shrink = ["dep:flate2"] +parse = ["dep:flate2", "dep:goblin", "dep:serde_json"] [dependencies] flate2 = { version = "1.0", optional = true } diff --git a/include-idl/README.md b/include-idl/README.md new file mode 100644 index 0000000..b0f7501 --- /dev/null +++ b/include-idl/README.md @@ -0,0 +1,47 @@ +# `solana-include-idl` + +A collection of macro and helpers to manage IDLs stored on the program binary. + +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. + +## Usage + +The crate provides a macro that includes the type and contents of the IDL file in separate ELF sections on the program binary. + +* `.idl.type` contains the type of the IDL file. +* `.idl.data` contains the IDL file itself. + +The macro takes two arguments: + +* `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`). +* `file`: The path to the IDL file. + +```rust +use include_idl::{include_idl, parse::IdlType}; + +include_idl!(IdlType::Codama, concat!(env!("OUT_DIR"), "/codama.idl.zip")); +``` + +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. + +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. + +```rust +use std::env; +use std::path::PathBuf; +use include_idl::compress_idl; + +fn main() { + // Get the IDL path. + let idl_path = PathBuf::from("../api").join("idl.json"); + // Compress the IDL file to a zip file. + let out_dir = env::var("OUT_DIR").unwrap(); + let dest_path = PathBuf::from(out_dir).join("codama.idl.zip"); + + compress_idl(&idl_path, &dest_path); +} +``` + +### Generating an IDL + +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). \ No newline at end of file diff --git a/include-idl/src/lib.rs b/include-idl/src/lib.rs index 8a07ca0..3aaffb6 100644 --- a/include-idl/src/lib.rs +++ b/include-idl/src/lib.rs @@ -21,7 +21,7 @@ pub use shrink::compress_idl; /// /// This macro takes two arguments: /// -/// - `type`: The type of the IDL file. This should be one of the variants of the [`IdlType``] enum. +/// - `type`: The type of the IDL file. This should be one of the variants of the [`IdlType`] enum. /// - `file`: The path to the IDL file. /// /// # Example @@ -29,6 +29,8 @@ pub use shrink::compress_idl; /// Include the following in your `lib.rs` file: /// /// ```ignore +/// use include_idl::{include_idl, parse::IdlType}; +/// /// include_idl!(IdlType::Codama, concat!(env!("OUT_DIR"), "/codama.idl.zip")); /// ``` #[macro_export] diff --git a/programs/asset/program/Cargo.toml b/programs/asset/program/Cargo.toml index bd88542..d792e02 100644 --- a/programs/asset/program/Cargo.toml +++ b/programs/asset/program/Cargo.toml @@ -25,7 +25,7 @@ num-traits = "^0.2" solana-security-txt = "1.1.1" solana-program = "~1.16" thiserror = "^1.0" -include-idl = { path = "../../../include-idl" } +solana-include-idl = { path = "../../../include-idl" } [build-dependencies] -include-idl = { path = "../../../include-idl", features = ["shrink"] } +solana-include-idl = { path = "../../../include-idl", features = ["shrink"] } diff --git a/programs/asset/program/build.rs b/programs/asset/program/build.rs index f75abff..1ce564d 100644 --- a/programs/asset/program/build.rs +++ b/programs/asset/program/build.rs @@ -2,7 +2,7 @@ use std::env; use std::path::PathBuf; use std::process::Command; -use include_idl::compress_idl; +use solana_include_idl::compress_idl; fn main() { // Run shank to generate the IDL diff --git a/programs/asset/program/src/lib.rs b/programs/asset/program/src/lib.rs index dde84e9..f9dc4b0 100644 --- a/programs/asset/program/src/lib.rs +++ b/programs/asset/program/src/lib.rs @@ -12,7 +12,7 @@ pub mod instruction; pub mod processor; pub mod utils; -use include_idl::{include_idl, parse::IdlType}; +use solana_include_idl::{include_idl, parse::IdlType}; pub use solana_program; use solana_security_txt::security_txt;