Skip to content

Commit

Permalink
chore: Refactoring & Improve SDK (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco authored Oct 1, 2024
1 parent 07a2f4a commit 48ccd73
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 10 deletions.
223 changes: 218 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [
resolver = "2"

[workspace.package]
version = "0.0.5"
version = "0.0.6"
authors = ["Magicblock Labs <[email protected]>"]
edition = "2021"
license = "MIT"
Expand All @@ -17,9 +17,11 @@ readme = "./README.md"
keywords = ["solana", "crypto", "delegation", "ephemeral-rollups", "magicblock"]

[workspace.dependencies]
ephemeral-rollups-sdk-attribute-delegate = { path = "sdk/delegate", version = "=0.0.5" }
ephemeral-rollups-sdk-attribute-delegate = { path = "sdk/delegate", version = "=0.0.6" }
ephemeral-rollups-sdk-attribute-commit = { path = "sdk/commit_attribute", version = "=0.0.6" }

## External crates
anchor-lang = "0.30.1"
borsh = "0.10.3"
paste = "^1.0"
solana-program = "^1.16"
Expand Down
6 changes: 6 additions & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ readme = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[features]
default = []
anchor = ["anchor-lang"]

[dependencies]
borsh = { workspace = true }
ephemeral-rollups-sdk-attribute-delegate = { workspace = true }
ephemeral-rollups-sdk-attribute-commit = { workspace = true }
paste = { workspace = true }
solana-program = { workspace = true }
anchor-lang = { workspace = true, optional = true }
17 changes: 17 additions & 0 deletions sdk/commit_attribute/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "ephemeral-rollups-sdk-attribute-commit"
description = "ephemeral-rollups-sdk-attribute-commit"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
proc-macro2 = { workspace = true }
syn = { workspace = true, features = ["full"] }
quote = { workspace = true }

[lib]
proc-macro = true
55 changes: 55 additions & 0 deletions sdk/commit_attribute/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
extern crate proc_macro;
use syn::parse::Parser;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Fields, Field, ItemStruct};

#[proc_macro_attribute]
pub fn commit(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as ItemStruct);

let name = &input.ident;
let expanded = if let Fields::Named(fields_named) = &input.fields {
let mut has_magic_program = false;
let mut has_magic_context = false;

for field in &fields_named.named {
if let Some(ident) = &field.ident {
if ident == "magic_program" {
has_magic_program = true;
} else if ident == "magic_context" {
has_magic_context = true;
}
}
}

let mut new_fields = fields_named.named.clone();

if !has_magic_program {
new_fields.push(Field::parse_named.parse2(quote! {
pub magic_program: Program<'info, MagicProgram>
}).unwrap());
}

if !has_magic_context {
new_fields.push(Field::parse_named.parse2(quote! {
#[account(mut, address = ephemeral_rollups_sdk::consts::MAGIC_CONTEXT_ID)]
/// CHECK:`
pub magic_context: AccountInfo<'info>
}).unwrap());
}

quote! {
#[derive(Accounts)]
pub struct #name<'info> {
#new_fields
}
}
} else {
quote! {
compile_error!("Commit attribute can only be used with structs with named fields");
}
};

TokenStream::from(expanded)
}
Loading

0 comments on commit 48ccd73

Please sign in to comment.