Skip to content

Commit

Permalink
feat: Add undelegation trough CPI (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco authored Sep 3, 2024
1 parent 34e2a77 commit ad9b6ba
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 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.1"
version = "0.0.2"
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.1" }
ephemeral-rollups-sdk-attribute-delegate = { path = "sdk/delegate", version = "=0.0.2" }

## External crates
borsh = "0.10.3"
Expand Down
24 changes: 21 additions & 3 deletions sdk/src/er.rs → sdk/src/ephem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,33 @@ pub fn commit_accounts<'a, 'info>(
) -> ProgramResult {
let mut accounts = vec![payer];
accounts.extend(account_infos.iter());
let ix = create_schedule_commit_ix(magic_program, &accounts);
let ix = create_schedule_commit_ix(magic_program, &accounts, false);
invoke(&ix, &accounts.into_iter().cloned().collect::<Vec<_>>())
}

/// 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_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<_>>())
}

pub fn create_schedule_commit_ix<'a, 'info>(
magic_program: &'a AccountInfo<'info>,
account_infos: &[&'a AccountInfo<'info>],
allow_undelegation: bool,
) -> Instruction {
let instruction_data = vec![1, 0, 0, 0];
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 {
Expand All @@ -30,4 +48,4 @@ pub fn create_schedule_commit_ix<'a, 'info>(
})
.collect::<Vec<AccountMeta>>();
Instruction::new_with_bytes(*magic_program.key, &instruction_data, account_metas)
}
}
2 changes: 1 addition & 1 deletion sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod anchor;
pub mod consts;
pub mod cpi;
pub mod delegate_args;
pub mod er;
pub mod ephem;
pub mod pda;
pub mod types;
pub mod utils;
Expand Down
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.1",
"version": "0.0.2",
"author": "MagicBlock Labs",
"license": "MIT",
"publishConfig": {
Expand Down

0 comments on commit ad9b6ba

Please sign in to comment.