Skip to content

Commit

Permalink
Add magic context (#3)
Browse files Browse the repository at this point in the history
* feat: Add Magic Context

* fix: Fix commit CPI

* chore: Refactoring
  • Loading branch information
GabrielePicco authored Sep 29, 2024
1 parent bf9fbc5 commit 07a2f4a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 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.4"
version = "0.0.5"
authors = ["Magicblock Labs <[email protected]>"]
edition = "2021"
license = "MIT"
Expand All @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions sdk/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
49 changes: 32 additions & 17 deletions sdk/src/ephem.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,66 @@
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
#[inline(always)]
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::<Vec<_>>())
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
#[inline(always)]
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::<Vec<_>>())
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 {
vec![2, 0, 0, 0]
} 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::<Vec<AccountMeta>>();
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)
}
2 changes: 1 addition & 1 deletion sdk/ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@magicblock-labs/ephemeral-rollups-sdk",
"version": "0.0.4",
"version": "0.0.5",
"author": "MagicBlock Labs",
"license": "MIT",
"publishConfig": {
Expand Down
1 change: 1 addition & 0 deletions sdk/ts/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const DELEGATION_PROGRAM_ID =
"DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh";

export const MAGIC_PROGRAM_ID = "Magic11111111111111111111111111111111111111";
export const MAGIC_CONTEXT_ID = "MagicContext1111111111111111111111111111111";

0 comments on commit 07a2f4a

Please sign in to comment.