Repository navigation
Allow using custom discriminator #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
91e833e
5bca8e4
e3a785a
aa1de56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,9 @@ | ||
| use borsh::{BorshDeserialize, BorshSerialize}; | ||
|
|
||
| #[derive(BorshSerialize, BorshDeserialize, Clone, Copy)] | ||
| pub enum Context { | ||
| Commit, | ||
| Undelegate, | ||
| Standalone, | ||
| } | ||
|
|
||
| #[derive(BorshSerialize, BorshDeserialize)] | ||
| pub struct CallHandlerArgs { | ||
| pub escrow_index: u8, | ||
| /// This is raw instruction data, it could include discriminator + args | ||
| /// or can be in any other custom format | ||
| pub data: Vec<u8>, | ||
| pub context: Context, | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,6 @@ | ||||||||||||||||||||||||||||||||||||||||
| use anchor_lang::prelude::*; | ||||||||||||||||||||||||||||||||||||||||
| use ephemeral_rollups_sdk::anchor::{delegate, ephemeral}; | ||||||||||||||||||||||||||||||||||||||||
| use ephemeral_rollups_sdk::cpi::DelegateConfig; | ||||||||||||||||||||||||||||||||||||||||
| use ephemeral_rollups_sdk::pda::ephemeral_balance_pda_from_payer; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| declare_id!("3vAK9JQiDsKoQNwmcfeEng4Cnv22pYuj1ASfso7U4ukF"); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
@@ -68,59 +67,42 @@ pub mod test_delegation { | |||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| /// Delegation program call handler | ||||||||||||||||||||||||||||||||||||||||
| pub fn delegation_program_call_handler( | ||||||||||||||||||||||||||||||||||||||||
| ctx: Context<DelegationProgramCallHandler>, | ||||||||||||||||||||||||||||||||||||||||
| hook_args: delegation_program_utils::CallHandlerArgs, | ||||||||||||||||||||||||||||||||||||||||
| #[instruction(discriminator = [1, 0, 1, 0])] | ||||||||||||||||||||||||||||||||||||||||
| pub fn commit_base_action_handler( | ||||||||||||||||||||||||||||||||||||||||
| ctx: Context<CommitBaseActionHandler>, | ||||||||||||||||||||||||||||||||||||||||
| amount: u64, | ||||||||||||||||||||||||||||||||||||||||
| ) -> Result<()> { | ||||||||||||||||||||||||||||||||||||||||
| let expected = ephemeral_balance_pda_from_payer( | ||||||||||||||||||||||||||||||||||||||||
| ctx.accounts.escrow_authority.key, | ||||||||||||||||||||||||||||||||||||||||
| hook_args.escrow_index, | ||||||||||||||||||||||||||||||||||||||||
| msg!("commit_base_action_handler!"); | ||||||||||||||||||||||||||||||||||||||||
| let transfer_ctx = CpiContext::new( | ||||||||||||||||||||||||||||||||||||||||
| ctx.accounts.system_program.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| Transfer { | ||||||||||||||||||||||||||||||||||||||||
| from: ctx.accounts.escrow_account.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| to: ctx.accounts.destination_account.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||
| if &expected != ctx.accounts.escrow_account.key { | ||||||||||||||||||||||||||||||||||||||||
| Err(ProgramError::InvalidAccountData) | ||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||
| }?; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if !ctx.accounts.escrow_account.is_signer { | ||||||||||||||||||||||||||||||||||||||||
| Err(ProgramError::MissingRequiredSignature) | ||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||
| }?; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| match hook_args.context { | ||||||||||||||||||||||||||||||||||||||||
| delegation_program_utils::Context::Commit => { | ||||||||||||||||||||||||||||||||||||||||
| msg!("commit context"); | ||||||||||||||||||||||||||||||||||||||||
| let amount = u64::try_from_slice(&hook_args.data)?; | ||||||||||||||||||||||||||||||||||||||||
| let transfer_ctx = CpiContext::new( | ||||||||||||||||||||||||||||||||||||||||
| ctx.accounts.system_program.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| Transfer { | ||||||||||||||||||||||||||||||||||||||||
| from: ctx.accounts.escrow_account.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| to: ctx.accounts.destination_account.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||
| transfer(transfer_ctx, amount)?; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| delegation_program_utils::Context::Undelegate => { | ||||||||||||||||||||||||||||||||||||||||
| msg!("undelegate context"); | ||||||||||||||||||||||||||||||||||||||||
| let amount = u64::try_from_slice(&hook_args.data)?; | ||||||||||||||||||||||||||||||||||||||||
| let transfer_ctx = CpiContext::new( | ||||||||||||||||||||||||||||||||||||||||
| ctx.accounts.system_program.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| Transfer { | ||||||||||||||||||||||||||||||||||||||||
| from: ctx.accounts.escrow_account.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| to: ctx.accounts.destination_account.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||
| transfer(transfer_ctx, amount)?; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| let counter_data = &mut ctx.accounts.counter.try_borrow_mut_data()?; | ||||||||||||||||||||||||||||||||||||||||
| let mut counter = Counter::try_from_slice(&counter_data)?; | ||||||||||||||||||||||||||||||||||||||||
| counter.count += 1; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| counter_data.copy_from_slice(&counter.try_to_vec()?); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| delegation_program_utils::Context::Standalone => msg!("standalone context"), | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| transfer(transfer_ctx, amount) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| #[instruction(discriminator = [1, 0, 2, 0])] | ||||||||||||||||||||||||||||||||||||||||
| pub fn undelegate_base_action_handler( | ||||||||||||||||||||||||||||||||||||||||
| ctx: Context<UndelegateBaseActionHandler>, | ||||||||||||||||||||||||||||||||||||||||
| amount: u64, | ||||||||||||||||||||||||||||||||||||||||
| ) -> Result<()> { | ||||||||||||||||||||||||||||||||||||||||
| msg!("undelegate_base_action_handler"); | ||||||||||||||||||||||||||||||||||||||||
| let transfer_ctx = CpiContext::new( | ||||||||||||||||||||||||||||||||||||||||
| ctx.accounts.system_program.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| Transfer { | ||||||||||||||||||||||||||||||||||||||||
| from: ctx.accounts.escrow_account.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| to: ctx.accounts.destination_account.to_account_info(), | ||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||
| transfer(transfer_ctx, amount)?; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| let counter_data = &mut ctx.accounts.counter.try_borrow_mut_data()?; | ||||||||||||||||||||||||||||||||||||||||
| let mut counter = Counter::try_from_slice(&counter_data)?; | ||||||||||||||||||||||||||||||||||||||||
| counter.count += 1; | ||||||||||||||||||||||||||||||||||||||||
| counter_data.copy_from_slice(&counter.try_to_vec()?); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
taco-paco marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -194,16 +176,20 @@ pub struct Increment<'info> { | |||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| #[derive(Accounts)] | ||||||||||||||||||||||||||||||||||||||||
| #[instruction(hook_args: delegation_program_utils::CallHandlerArgs)] | ||||||||||||||||||||||||||||||||||||||||
| pub struct DelegationProgramCallHandler<'info> { | ||||||||||||||||||||||||||||||||||||||||
| pub struct CommitBaseActionHandler<'info> { | ||||||||||||||||||||||||||||||||||||||||
| /// CHECK: The authority that owns the escrow account | ||||||||||||||||||||||||||||||||||||||||
| pub escrow_authority: UncheckedAccount<'info>, | ||||||||||||||||||||||||||||||||||||||||
| pub escrow_account: Signer<'info>, | ||||||||||||||||||||||||||||||||||||||||
| /// CHECK: The destination account to transfer lamports to | ||||||||||||||||||||||||||||||||||||||||
| #[account(mut)] | ||||||||||||||||||||||||||||||||||||||||
| pub destination_account: AccountInfo<'info>, | ||||||||||||||||||||||||||||||||||||||||
| pub system_program: Program<'info, System>, | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+179
to
+187
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. escrow_account must be mutable (lamports change on transfer). Without #[account(mut)], Anchor will error at finalize when lamports decrease on the source account. pub struct CommitBaseActionHandler<'info> {
/// CHECK: The authority that owns the escrow account
pub escrow_authority: UncheckedAccount<'info>,
- pub escrow_account: Signer<'info>,
+ #[account(mut)]
+ pub escrow_account: Signer<'info>,
/// CHECK: The destination account to transfer lamports to
#[account(mut)]
pub destination_account: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| #[derive(Accounts)] | ||||||||||||||||||||||||||||||||||||||||
| pub struct UndelegateBaseActionHandler<'info> { | ||||||||||||||||||||||||||||||||||||||||
| /// CHECK: The authority that owns the escrow account | ||||||||||||||||||||||||||||||||||||||||
| pub escrow_authority: UncheckedAccount<'info>, | ||||||||||||||||||||||||||||||||||||||||
| #[account( | ||||||||||||||||||||||||||||||||||||||||
| mut, | ||||||||||||||||||||||||||||||||||||||||
| seeds = [b"balance", &escrow_authority.key().as_ref(), &[hook_args.escrow_index]], | ||||||||||||||||||||||||||||||||||||||||
| seeds::program = delegation_program_utils::ID, | ||||||||||||||||||||||||||||||||||||||||
| bump | ||||||||||||||||||||||||||||||||||||||||
| )] | ||||||||||||||||||||||||||||||||||||||||
| pub escrow_account: Signer<'info>, | ||||||||||||||||||||||||||||||||||||||||
| /// CHECK: The destination account to transfer lamports to | ||||||||||||||||||||||||||||||||||||||||
| #[account(mut)] | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -222,18 +208,4 @@ mod delegation_program_utils { | |||||||||||||||||||||||||||||||||||||||
| use anchor_lang::prelude::*; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| declare_id!("DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh"); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| #[derive(AnchorSerialize, AnchorDeserialize)] | ||||||||||||||||||||||||||||||||||||||||
| pub enum Context { | ||||||||||||||||||||||||||||||||||||||||
| Commit, | ||||||||||||||||||||||||||||||||||||||||
| Undelegate, | ||||||||||||||||||||||||||||||||||||||||
| Standalone, | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| #[derive(AnchorSerialize, AnchorDeserialize)] | ||||||||||||||||||||||||||||||||||||||||
| pub struct CallHandlerArgs { | ||||||||||||||||||||||||||||||||||||||||
| pub escrow_index: u8, | ||||||||||||||||||||||||||||||||||||||||
| pub data: Vec<u8>, | ||||||||||||||||||||||||||||||||||||||||
| pub context: Context, | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.