Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prelim pinocchio support #69

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions shank-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "shank-cli"
description = "Shank CLI that extracts solita compatible IDL from your Rust Solana program code annotated with shank macro attributes"
authors = ["Metaplex Maintainers <[email protected]>"]
repository = "https://github.com/metaplex-foundation/shank"
license = "Apache-2.0"
version = "0.4.2"
description = "Shank CLI that extracts solita compatible IDL from your Rust Solana program code annotated with shank macro attributes"
edition = "2018"
license = "Apache-2.0"
name = "shank-cli"
repository = "https://github.com/metaplex-foundation/shank"
version = "0.5.0"

[[bin]]
name = "shank"
Expand All @@ -17,4 +17,4 @@ anyhow = "1.0.48"
clap = { version = "3.0.14", features = ["derive"] }
fern = { version = "0.6.0", features = ["colored"] }
log = "0.4.14"
shank_idl = { version= "0.4.2", path = "../shank-idl" }
shank_idl = { version = "0.5.0", path = "../shank-idl" }
12 changes: 6 additions & 6 deletions shank-idl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
name = "shank_idl"
version = "0.4.2"
description = "Converts information extracted via shank derive macros to a solita compatible IDL."
authors = ["Metaplex Maintainers <[email protected]>"]
repository = "https://github.com/metaplex-foundation/shank"
license = "Apache-2.0"
description = "Converts information extracted via shank derive macros to a solita compatible IDL."
edition = "2018"
license = "Apache-2.0"
name = "shank_idl"
repository = "https://github.com/metaplex-foundation/shank"
version = "0.5.0"

[dependencies]
anyhow = "1.0.48"
cargo_toml = "0.17"
heck = "0.3.3"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.72"
shank_macro_impl = { version = "0.4.2", path = "../shank-macro-impl" }
shank_macro_impl = { version = "0.5.0", path = "../shank-macro-impl" }
shellexpand = "2.1.0"
10 changes: 5 additions & 5 deletions shank-macro-impl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "shank_macro_impl"
version = "0.4.2"
description = "Implements and tests shank derive macros"
authors = ["Metaplex Maintainers <[email protected]>"]
repository = "https://github.com/metaplex-foundation/shank"
license = "Apache-2.0"
description = "Implements and tests shank derive macros"
edition = "2018"
license = "Apache-2.0"
name = "shank_macro_impl"
repository = "https://github.com/metaplex-foundation/shank"
version = "0.5.0"

[dependencies]
anyhow = "1.0.48"
Expand Down
21 changes: 13 additions & 8 deletions shank-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
[package]
name = "shank_macro"
version = "0.4.2"
description = "Provides macros used to annotate Solana Rust programs in order to extract an IDL with the shank CLI"
authors = ["Metaplex Maintainers <[email protected]>"]
repository = "https://github.com/metaplex-foundation/shank"
license = "Apache-2.0"
description = "Provides macros used to annotate Solana Rust programs in order to extract an IDL with the shank CLI"
edition = "2018"
license = "Apache-2.0"
name = "shank_macro"
repository = "https://github.com/metaplex-foundation/shank"
version = "0.5.0"

[lib]
proc-macro = true
doctest = false
proc-macro = true

[dependencies]
proc-macro2 = "1.0.32"
quote = "1.0.10"
shank_macro_impl = { version = "0.4.2", path = "../shank-macro-impl" }
shank_render = { version = "0.4.2", path = "../shank-render" }
shank_macro_impl = { version = "0.5.0", path = "../shank-macro-impl" }
shank_render = { version = "0.5.0", path = "../shank-render" }
syn = "1.0.82"

[features]
default = ["solana-program"]
pinocchio = ["shank_render/pinocchio"]
solana-program = ["shank_render/solana-program"]
17 changes: 11 additions & 6 deletions shank-render/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
[package]
name = "shank_render"
version = "0.4.2"
description = "Renders implementaions derived from shank macros"
authors = ["Metaplex Maintainers <[email protected]>"]
repository = "https://github.com/metaplex-foundation/shank"
license = "Apache-2.0"
description = "Renders implementaions derived from shank macros"
edition = "2018"
license = "Apache-2.0"
name = "shank_render"
repository = "https://github.com/metaplex-foundation/shank"
version = "0.5.0"

[dependencies]
proc-macro2 = "1.0.46"
quote = "1.0.21"
shank_macro_impl = { version = "0.4.2", path = "../shank-macro-impl" }
shank_macro_impl = { version = "0.5.0", path = "../shank-macro-impl" }

[dev-dependencies]
prettyplease = "0.1.21"

[features]
default = ["solana-program"]
pinocchio = []
solana-program = []
8 changes: 7 additions & 1 deletion shank-render/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ pub fn render_contexts_impl(
.map(generate_context)
.collect::<Vec<TokenStream>>();

let account_info_type = if cfg!(feature = "pinocchio") {
quote! { pinocchio::account_info::AccountInfo }
} else {
quote! { solana_program::account_info::AccountInfo<'a> }
};

Ok(quote! {
pub mod accounts {
use super::*;

pub struct Context<'a, T> {
pub accounts: T,
pub remaining_accounts: &'a [solana_program::account_info::AccountInfo<'a>],
pub remaining_accounts: &'a [#account_info_type],
}

#(#contexts)*
Expand Down
52 changes: 37 additions & 15 deletions shank-render/src/context/render_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,47 @@ use quote::quote;
use shank_macro_impl::{instruction::InstructionVariant, syn};

pub(crate) fn generate_context(variant: &InstructionVariant) -> TokenStream {
// Define the account type path based on feature flags
let account_info_type = if cfg!(feature = "pinocchio") {
quote! { pinocchio::account_info::AccountInfo }
} else {
quote! { solana_program::account_info::AccountInfo<'a> }
};

// Define the ProgramError type path based on feature flags
let program_error_type = if cfg!(feature = "pinocchio") {
quote! { pinocchio::program_error::ProgramError }
} else {
quote! { solana_program::sysvar::slot_history::ProgramError> }
};

// Define how to retrieve the key of the account
let key_retrieval = if cfg!(feature = "pinocchio") {
quote! { key() }
} else {
quote! { key }
};

// accounts fields
let struct_fields = variant.accounts.iter().map(|account| {
let account_name = syn::parse_str::<syn::Ident>(&account.name).unwrap();
if account.optional {
quote! {
pub #account_name: Option<&'a solana_program::account_info::AccountInfo<'a>>
}
} else {
quote! {
pub #account_name:&'a solana_program::account_info::AccountInfo<'a>
}
let account_name = syn::parse_str::<syn::Ident>(&account.name).unwrap();
if account.optional {
quote! {
pub #account_name: Option<&'a #account_info_type>
}
});
} else {
quote! {
pub #account_name: &'a #account_info_type
}
}
});

// accounts initialization
let account_fields = variant.accounts.iter().enumerate().map(|(index, account)| {
let account_name = syn::parse_str::<syn::Ident>(&account.name).unwrap();
if account.optional {
quote! {
#account_name: if accounts[#index].key == &crate::ID { None } else { Some(&accounts[#index]) }
#account_name: if accounts[#index].#key_retrieval == &crate::ID { None } else { Some(&accounts[#index]) }
}
} else {
quote! {
Expand All @@ -31,21 +52,22 @@ pub(crate) fn generate_context(variant: &InstructionVariant) -> TokenStream {
}
});

let expected = variant.accounts.len(); // number of expected accounts
let expected = variant.accounts.len();
let name =
syn::parse_str::<syn::Ident>(&format!("{}Accounts", variant.ident))
.unwrap();

// Use the same account type in the impl block
quote! {
pub struct #name<'a> {
#(#struct_fields,)*
}
impl<'a> #name<'a> {
pub fn context(
accounts: &'a [solana_program::account_info::AccountInfo<'a>]
) -> Result<Context<'a, Self>, solana_program::sysvar::slot_history::ProgramError> {
accounts: &'a [#account_info_type]
) -> Result<Context<'a, Self>, #program_error_type> {
if accounts.len() < #expected {
return Err(solana_program::sysvar::slot_history::ProgramError::NotEnoughAccountKeys);
return Err(#program_error_type::NotEnoughAccountKeys);
}

Ok(Context {
Expand Down
16 changes: 10 additions & 6 deletions shank/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
[package]
name = "shank"
description = "Exposes macros to annotate Rust programs to extract solita compatible IDL in order to generate program SDKs"
authors = ["Metaplex Maintainers <[email protected]>"]
repository = "https://github.com/metaplex-foundation/shank"
license = "Apache-2.0"
version = "0.4.2"
description = "Exposes macros to annotate Rust programs to extract solita compatible IDL in order to generate program SDKs"
edition = "2018"
license = "Apache-2.0"
name = "shank"
repository = "https://github.com/metaplex-foundation/shank"
version = "0.5.0"

[features]
default = ["solana-program"]
pinocchio = ["shank_macro/pinocchio"]
solana-program = ["shank_macro/solana-program"]

[dependencies]
shank_macro = { version = "0.4.2", path = "../shank-macro" }
shank_macro = { version = "0.5.0", path = "../shank-macro" }
Loading