From 07a2f4aa12b3a0d7adafbd3527f3c590309830f6 Mon Sep 17 00:00:00 2001 From: Gabriele Picco Date: Sun, 29 Sep 2024 20:46:51 +0700 Subject: [PATCH] Add magic context (#3) * feat: Add Magic Context * fix: Fix commit CPI * chore: Refactoring --- Cargo.lock | 4 ++-- Cargo.toml | 4 ++-- sdk/src/consts.rs | 4 ++++ sdk/src/ephem.rs | 49 +++++++++++++++++++++++++++-------------- sdk/ts/package.json | 2 +- sdk/ts/src/constants.ts | 1 + 6 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4deffd5..2478062 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -560,7 +560,7 @@ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "ephemeral-rollups-sdk" -version = "0.0.4" +version = "0.0.5" dependencies = [ "borsh 0.10.3", "ephemeral-rollups-sdk-attribute-delegate", @@ -570,7 +570,7 @@ dependencies = [ [[package]] name = "ephemeral-rollups-sdk-attribute-delegate" -version = "0.0.4" +version = "0.0.5" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 6ab4bf5..86f8180 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ members = [ resolver = "2" [workspace.package] -version = "0.0.4" +version = "0.0.5" authors = ["Magicblock Labs "] edition = "2021" license = "MIT" @@ -17,7 +17,7 @@ readme = "./README.md" keywords = ["solana", "crypto", "delegation", "ephemeral-rollups", "magicblock"] [workspace.dependencies] -ephemeral-rollups-sdk-attribute-delegate = { path = "sdk/delegate", version = "=0.0.4" } +ephemeral-rollups-sdk-attribute-delegate = { path = "sdk/delegate", version = "=0.0.5" } ## External crates borsh = "0.10.3" diff --git a/sdk/src/consts.rs b/sdk/src/consts.rs index d3d57e9..2ca9228 100644 --- a/sdk/src/consts.rs +++ b/sdk/src/consts.rs @@ -7,6 +7,10 @@ pub const DELEGATION_PROGRAM_ID: Pubkey = pubkey!("DELeGGvXpWV2fqJUhqcF5ZSYMS4JT /// The magic program ID. pub const MAGIC_PROGRAM_ID: Pubkey = pubkey!("Magic11111111111111111111111111111111111111"); + +/// The magic context ID. +pub const MAGIC_CONTEXT_ID: Pubkey = pubkey!("MagicContext1111111111111111111111111111111"); + /// /// The seed of the authority account PDA. pub const DELEGATION_RECORD: &[u8] = b"delegation"; diff --git a/sdk/src/ephem.rs b/sdk/src/ephem.rs index 98c5e2b..ad39028 100644 --- a/sdk/src/ephem.rs +++ b/sdk/src/ephem.rs @@ -1,6 +1,7 @@ use solana_program::account_info::AccountInfo; use solana_program::entrypoint::ProgramResult; use solana_program::instruction::{AccountMeta, Instruction}; +use solana_program::msg; use solana_program::program::invoke; /// CPI to trigger a commit for one or more accounts in the ER @@ -8,12 +9,13 @@ use solana_program::program::invoke; pub fn commit_accounts<'a, 'info>( payer: &'a AccountInfo<'info>, account_infos: Vec<&'a AccountInfo<'info>>, + magic_context: &'a AccountInfo<'info>, magic_program: &'a AccountInfo<'info>, ) -> ProgramResult { - let mut accounts = vec![payer]; - accounts.extend(account_infos.iter()); - let ix = create_schedule_commit_ix(magic_program, &accounts, false); - invoke(&ix, &accounts.into_iter().cloned().collect::>()) + let ix = create_schedule_commit_ix(payer, &account_infos, magic_context, magic_program, false); + let mut all_accounts = vec![payer.clone(), magic_context.clone()]; + all_accounts.extend(account_infos.into_iter().cloned()); + invoke(&ix, &all_accounts) } /// CPI to trigger a commit and undelegate one or more accounts in the ER @@ -21,17 +23,20 @@ pub fn commit_accounts<'a, 'info>( pub fn commit_and_undelegate_accounts<'a, 'info>( payer: &'a AccountInfo<'info>, account_infos: Vec<&'a AccountInfo<'info>>, + magic_context: &'a AccountInfo<'info>, magic_program: &'a AccountInfo<'info>, ) -> ProgramResult { - let mut accounts = vec![payer]; - accounts.extend(account_infos.iter()); - let ix = create_schedule_commit_ix(magic_program, &accounts, true); - invoke(&ix, &accounts.into_iter().cloned().collect::>()) + let ix = create_schedule_commit_ix(payer, &account_infos, magic_context, magic_program, true); + let mut all_accounts = vec![payer.clone(), magic_context.clone()]; + all_accounts.extend(account_infos.into_iter().cloned()); + invoke(&ix, &all_accounts) } pub fn create_schedule_commit_ix<'a, 'info>( - magic_program: &'a AccountInfo<'info>, + payer: &'a AccountInfo<'info>, account_infos: &[&'a AccountInfo<'info>], + magic_context: &'a AccountInfo<'info>, + magic_program: &'a AccountInfo<'info>, allow_undelegation: bool, ) -> Instruction { let instruction_data = if allow_undelegation { @@ -39,13 +44,23 @@ pub fn create_schedule_commit_ix<'a, 'info>( } else { vec![1, 0, 0, 0] }; - let account_metas = account_infos - .iter() - .map(|x| AccountMeta { - pubkey: *x.key, - is_signer: x.is_signer, - is_writable: x.is_writable, - }) - .collect::>(); + let mut account_metas = vec![ + AccountMeta { + pubkey: *payer.key, + is_signer: true, + is_writable: true, + }, + AccountMeta { + pubkey: *magic_context.key, + is_signer: false, + is_writable: true, + }, + ]; + account_metas.extend(account_infos.iter().map(|x| AccountMeta { + pubkey: *x.key, + is_signer: x.is_signer, + is_writable: x.is_writable, + })); + msg!("Keys: {:?}", account_metas); Instruction::new_with_bytes(*magic_program.key, &instruction_data, account_metas) } diff --git a/sdk/ts/package.json b/sdk/ts/package.json index ca10545..db9f53d 100644 --- a/sdk/ts/package.json +++ b/sdk/ts/package.json @@ -1,6 +1,6 @@ { "name": "@magicblock-labs/ephemeral-rollups-sdk", - "version": "0.0.4", + "version": "0.0.5", "author": "MagicBlock Labs", "license": "MIT", "publishConfig": { diff --git a/sdk/ts/src/constants.ts b/sdk/ts/src/constants.ts index c92e054..bda11d1 100644 --- a/sdk/ts/src/constants.ts +++ b/sdk/ts/src/constants.ts @@ -2,3 +2,4 @@ export const DELEGATION_PROGRAM_ID = "DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh"; export const MAGIC_PROGRAM_ID = "Magic11111111111111111111111111111111111111"; +export const MAGIC_CONTEXT_ID = "MagicContext1111111111111111111111111111111";