From 48ccd7312c29b28a605f1cba00f0f6beb0bc8a2a Mon Sep 17 00:00:00 2001 From: Gabriele Picco Date: Tue, 1 Oct 2024 11:21:18 +0700 Subject: [PATCH] chore: Refactoring & Improve SDK (#4) --- Cargo.lock | 223 +++++++++++++++++++++++++++++++- Cargo.toml | 6 +- sdk/Cargo.toml | 6 + sdk/commit_attribute/Cargo.toml | 17 +++ sdk/commit_attribute/src/lib.rs | 55 ++++++++ sdk/delegate/Cargo.toml | 5 +- sdk/src/anchor.rs | 30 +++++ sdk/src/cpi.rs | 2 +- sdk/ts/package.json | 2 +- 9 files changed, 336 insertions(+), 10 deletions(-) create mode 100644 sdk/commit_attribute/Cargo.toml create mode 100644 sdk/commit_attribute/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 2478062..43ce065 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,6 +25,184 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "anchor-attribute-access-control" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47fe28365b33e8334dd70ae2f34a43892363012fe239cf37d2ee91693575b1f8" +dependencies = [ + "anchor-syn", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "anchor-attribute-account" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c288d496168268d198d9b53ee9f4f9d260a55ba4df9877ea1d4486ad6109e0f" +dependencies = [ + "anchor-syn", + "bs58 0.5.1", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "anchor-attribute-constant" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49b77b6948d0eeaaa129ce79eea5bbbb9937375a9241d909ca8fb9e006bb6e90" +dependencies = [ + "anchor-syn", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "anchor-attribute-error" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d20bb569c5a557c86101b944721d865e1fd0a4c67c381d31a44a84f07f84828" +dependencies = [ + "anchor-syn", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "anchor-attribute-event" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cebd8d0671a3a9dc3160c48598d652c34c77de6be4d44345b8b514323284d57" +dependencies = [ + "anchor-syn", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "anchor-attribute-program" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efb2a5eb0860e661ab31aff7bb5e0288357b176380e985bade4ccb395981b42d" +dependencies = [ + "anchor-lang-idl", + "anchor-syn", + "anyhow", + "bs58 0.5.1", + "heck", + "proc-macro2", + "quote", + "serde_json", + "syn 1.0.109", +] + +[[package]] +name = "anchor-derive-accounts" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04368b5abef4266250ca8d1d12f4dff860242681e4ec22b885dcfe354fd35aa1" +dependencies = [ + "anchor-syn", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "anchor-derive-serde" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0bb0e0911ad4a70cab880cdd6287fe1e880a1a9d8e4e6defa8e9044b9796a6c" +dependencies = [ + "anchor-syn", + "borsh-derive-internal 0.10.3", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "anchor-derive-space" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ef415ff156dc82e9ecb943189b0cb241b3a6bfc26a180234dc21bd3ef3ce0cb" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "anchor-lang" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6620c9486d9d36a4389cab5e37dc34a42ed0bfaa62e6a75a2999ce98f8f2e373" +dependencies = [ + "anchor-attribute-access-control", + "anchor-attribute-account", + "anchor-attribute-constant", + "anchor-attribute-error", + "anchor-attribute-event", + "anchor-attribute-program", + "anchor-derive-accounts", + "anchor-derive-serde", + "anchor-derive-space", + "arrayref", + "base64 0.21.7", + "bincode", + "borsh 0.10.3", + "bytemuck", + "getrandom 0.2.15", + "solana-program", + "thiserror", +] + +[[package]] +name = "anchor-lang-idl" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31cf97b4e6f7d6144a05e435660fcf757dbc3446d38d0e2b851d11ed13625bba" +dependencies = [ + "anchor-lang-idl-spec", + "anyhow", + "heck", + "serde", + "serde_json", + "sha2 0.10.8", +] + +[[package]] +name = "anchor-lang-idl-spec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bdf143115440fe621bdac3a29a1f7472e09f6cd82b2aa569429a0c13f103838" +dependencies = [ + "anyhow", + "serde", +] + +[[package]] +name = "anchor-syn" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f99daacb53b55cfd37ce14d6c9905929721137fd4c67bbab44a19802aecb622f" +dependencies = [ + "anyhow", + "bs58 0.5.1", + "heck", + "proc-macro2", + "quote", + "serde", + "serde_json", + "sha2 0.10.8", + "syn 1.0.109", + "thiserror", +] + [[package]] name = "anyhow" version = "1.0.86" @@ -357,6 +535,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" +[[package]] +name = "bs58" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4" +dependencies = [ + "tinyvec", +] + [[package]] name = "bumpalo" version = "3.16.0" @@ -560,17 +747,28 @@ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "ephemeral-rollups-sdk" -version = "0.0.5" +version = "0.0.6" dependencies = [ + "anchor-lang", "borsh 0.10.3", + "ephemeral-rollups-sdk-attribute-commit", "ephemeral-rollups-sdk-attribute-delegate", "paste", "solana-program", ] +[[package]] +name = "ephemeral-rollups-sdk-attribute-commit" +version = "0.0.6" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "ephemeral-rollups-sdk-attribute-delegate" -version = "0.0.5" +version = "0.0.6" dependencies = [ "proc-macro2", "quote", @@ -650,6 +848,15 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "hmac" version = "0.8.1" @@ -1254,7 +1461,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20a6ef2db80dceb124b7bf81cca3300804bf427d2711973fc3df450ed7dfb26d" dependencies = [ "block-buffer 0.10.4", - "bs58", + "bs58 0.4.0", "bv", "either", "generic-array", @@ -1301,7 +1508,7 @@ dependencies = [ "borsh 0.10.3", "borsh 0.9.3", "borsh 1.5.1", - "bs58", + "bs58 0.4.0", "bv", "bytemuck", "cc", @@ -1345,7 +1552,7 @@ version = "1.18.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c55c196c8050834c391a34b58e3c9fd86b15452ef1feeeafa1dbeb9d2291dfec" dependencies = [ - "bs58", + "bs58 0.4.0", "proc-macro2", "quote", "rustversion", @@ -1493,6 +1700,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + [[package]] name = "version_check" version = "0.9.5" diff --git a/Cargo.toml b/Cargo.toml index 86f8180..ea333e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ members = [ resolver = "2" [workspace.package] -version = "0.0.5" +version = "0.0.6" authors = ["Magicblock Labs "] edition = "2021" license = "MIT" @@ -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" diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 681fb99..7cda4c3 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -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 } diff --git a/sdk/commit_attribute/Cargo.toml b/sdk/commit_attribute/Cargo.toml new file mode 100644 index 0000000..3763b26 --- /dev/null +++ b/sdk/commit_attribute/Cargo.toml @@ -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 diff --git a/sdk/commit_attribute/src/lib.rs b/sdk/commit_attribute/src/lib.rs new file mode 100644 index 0000000..bab4333 --- /dev/null +++ b/sdk/commit_attribute/src/lib.rs @@ -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) +} \ No newline at end of file diff --git a/sdk/delegate/Cargo.toml b/sdk/delegate/Cargo.toml index 04cf0a1..83b49f2 100644 --- a/sdk/delegate/Cargo.toml +++ b/sdk/delegate/Cargo.toml @@ -8,10 +8,13 @@ homepage = { workspace = true } license = { workspace = true } edition = { workspace = true } +[features] +default = [] + [lib] proc-macro = true [dependencies] proc-macro2 = { workspace = true } syn = { workspace = true, features = ["full"] } -quote = { workspace = true } +quote = { workspace = true } \ No newline at end of file diff --git a/sdk/src/anchor.rs b/sdk/src/anchor.rs index 956f8fa..3ffa1a1 100644 --- a/sdk/src/anchor.rs +++ b/sdk/src/anchor.rs @@ -1 +1,31 @@ +#[cfg(feature = "anchor")] pub use ephemeral_rollups_sdk_attribute_delegate::delegate; + +#[cfg(feature = "anchor")] +pub use ephemeral_rollups_sdk_attribute_commit::commit; + +#[cfg(feature = "anchor")] +use solana_program::pubkey::Pubkey; + +#[cfg(feature = "anchor")] +extern crate anchor_lang; + +#[cfg(feature = "anchor")] +pub struct DelegationProgram; + +#[cfg(feature = "anchor")] +impl anchor_lang::Id for DelegationProgram { + fn id() -> Pubkey { + crate::consts::DELEGATION_PROGRAM_ID + } +} + +#[cfg(feature = "anchor")] +pub struct MagicProgram; + +#[cfg(feature = "anchor")] +impl anchor_lang::Id for MagicProgram { + fn id() -> Pubkey { + crate::consts::MAGIC_PROGRAM_ID + } +} diff --git a/sdk/src/cpi.rs b/sdk/src/cpi.rs index fc4591f..19e8228 100644 --- a/sdk/src/cpi.rs +++ b/sdk/src/cpi.rs @@ -10,7 +10,6 @@ use crate::consts::BUFFER; use crate::types::DelegateAccountArgs; use crate::utils::{close_pda, create_pda, seeds_with_bump}; -/// Delegate an account #[allow(clippy::too_many_arguments)] #[inline(always)] pub fn delegate_account<'a, 'info>( @@ -26,6 +25,7 @@ pub fn delegate_account<'a, 'info>( valid_until: i64, commit_frequency_ms: u32, ) -> ProgramResult { + let buffer_seeds: &[&[u8]] = &[BUFFER, pda.key.as_ref()]; let (_, delegate_account_bump) = Pubkey::find_program_address(pda_seeds, owner_program.key); diff --git a/sdk/ts/package.json b/sdk/ts/package.json index db9f53d..759778f 100644 --- a/sdk/ts/package.json +++ b/sdk/ts/package.json @@ -1,6 +1,6 @@ { "name": "@magicblock-labs/ephemeral-rollups-sdk", - "version": "0.0.5", + "version": "0.0.6", "author": "MagicBlock Labs", "license": "MIT", "publishConfig": {