-
Notifications
You must be signed in to change notification settings - Fork 75
fix: separate refund claim when there is no token account #633
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
64103df
fix: separate refund claim when no token account
Reinis-FRP fdf2201
fix
Reinis-FRP 3deb865
Merge branch 'master' into reinis-frp/refund-claims
Reinis-FRP 168dab2
fix
Reinis-FRP e1be75d
fix: do not log debug errors
Reinis-FRP 120c198
fix: remove redundant error code
Reinis-FRP 1f31c01
fix
Reinis-FRP 79fa785
Merge branch 'master' into reinis-frp/refund-claims
Reinis-FRP 4c0fb22
test: max refunds on claim accounts
Reinis-FRP 397885a
test: claim account
Reinis-FRP 2361d31
fix: use if instead of require in zero refund claim
Reinis-FRP ec8e213
fix: close claim account and emit event on claiming
Reinis-FRP fdbd8d4
test: separate refund claim tests
Reinis-FRP d6f5e15
Merge branch 'master' into reinis-frp/refund-claims
Reinis-FRP e904f90
fix: emit event when any refunds are deferred
Reinis-FRP c7758ec
fix
Reinis-FRP 79476ba
fix: linting
Reinis-FRP cdb9ac3
fix: consistent comparison
Reinis-FRP d1caae6
Merge branch 'master' into reinis-frp/refund-claims
Reinis-FRP 655219d
fix: merge event data
Reinis-FRP bb1f745
fix: remove reduntant event
Reinis-FRP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| use anchor_lang::prelude::*; | ||
| use anchor_spl::token_interface::{ | ||
| transfer_checked, Mint, TokenAccount, TokenInterface, TransferChecked, | ||
| }; | ||
|
|
||
| use crate::{ | ||
| constants::DISCRIMINATOR_SIZE, | ||
| error::CustomError, | ||
| event::ClaimedRelayerRefund, | ||
| state::{ClaimAccount, State}, | ||
| }; | ||
|
|
||
| #[derive(Accounts)] | ||
| #[instruction(mint: Pubkey, token_account: Pubkey)] | ||
| pub struct InitializeClaimAccount<'info> { | ||
| #[account(mut)] | ||
| pub signer: Signer<'info>, | ||
|
|
||
| #[account( | ||
| init, | ||
| payer = signer, | ||
| space = DISCRIMINATOR_SIZE + ClaimAccount::INIT_SPACE, | ||
| seeds = [b"claim_account", mint.as_ref(), token_account.as_ref()], | ||
| bump | ||
| )] | ||
| pub claim_account: Account<'info, ClaimAccount>, | ||
|
|
||
| pub system_program: Program<'info, System>, | ||
| } | ||
|
|
||
| pub fn initialize_claim_account( | ||
| ctx: Context<InitializeClaimAccount>, | ||
| mint: Pubkey, | ||
| token_account: Pubkey, | ||
| ) -> Result<()> { | ||
| // Store the initializer so only it can receive lamports from closing the account upon claiming the refund. | ||
| ctx.accounts.claim_account.initializer = ctx.accounts.signer.key(); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[event_cpi] | ||
| #[derive(Accounts)] | ||
| pub struct ClaimRelayerRefund<'info> { | ||
| #[account(mut)] | ||
| pub signer: Signer<'info>, | ||
|
|
||
| /// CHECK: We don't need any additional checks as long as this is the same account that initialized the claim account. | ||
| #[account( | ||
| mut, | ||
| address = claim_account.initializer @ CustomError::InvalidClaimInitializer | ||
| )] | ||
| pub initializer: UncheckedAccount<'info>, | ||
|
|
||
| #[account(mut, seeds = [b"state", state.seed.to_le_bytes().as_ref()], bump)] | ||
| pub state: Account<'info, State>, | ||
|
|
||
| #[account( | ||
| mut, | ||
| associated_token::mint = mint, | ||
| associated_token::authority = state, | ||
| associated_token::token_program = token_program | ||
| )] | ||
| pub vault: InterfaceAccount<'info, TokenAccount>, | ||
|
|
||
| // Mint address has been checked when executing the relayer refund leaf and it is part of claim account derivation. | ||
| #[account( | ||
| mint::token_program = token_program, | ||
| )] | ||
| pub mint: InterfaceAccount<'info, Mint>, | ||
|
|
||
| // Token address has been checked when executing the relayer refund leaf and it is part of claim account derivation. | ||
| #[account( | ||
| mut, | ||
| token::mint = mint, | ||
| token::token_program = token_program | ||
| )] | ||
| pub token_account: InterfaceAccount<'info, TokenAccount>, | ||
|
|
||
| #[account( | ||
| mut, | ||
| close = initializer, | ||
| seeds = [b"claim_account", mint.key().as_ref(), token_account.key().as_ref()], | ||
| bump | ||
| )] | ||
| pub claim_account: Account<'info, ClaimAccount>, | ||
|
|
||
| pub token_program: Interface<'info, TokenInterface>, | ||
| } | ||
|
|
||
| pub fn claim_relayer_refund(ctx: Context<ClaimRelayerRefund>) -> Result<()> { | ||
| // Ensure the claim account holds a non-zero amount. | ||
| let claim_amount = ctx.accounts.claim_account.amount; | ||
| if claim_amount == 0 { | ||
| return err!(CustomError::ZeroRefundClaim); | ||
| } | ||
|
|
||
| // Reset the claim amount. | ||
| ctx.accounts.claim_account.amount = 0; | ||
|
|
||
| // Derive the signer seeds for the state required for the transfer form vault. | ||
| let state_seed_bytes = ctx.accounts.state.seed.to_le_bytes(); | ||
| let seeds = &[b"state", state_seed_bytes.as_ref(), &[ctx.bumps.state]]; | ||
| let signer_seeds = &[&seeds[..]]; | ||
|
|
||
| // Transfer the claim amount from the vault to the relayer token account. | ||
| let transfer_accounts = TransferChecked { | ||
| from: ctx.accounts.vault.to_account_info(), | ||
| mint: ctx.accounts.mint.to_account_info(), | ||
| to: ctx.accounts.token_account.to_account_info(), | ||
| authority: ctx.accounts.state.to_account_info(), | ||
| }; | ||
| let cpi_context = CpiContext::new_with_signer( | ||
| ctx.accounts.token_program.to_account_info(), | ||
| transfer_accounts, | ||
| signer_seeds, | ||
| ); | ||
| transfer_checked(cpi_context, claim_amount, ctx.accounts.mint.decimals)?; | ||
|
|
||
| // Emit the ClaimedRelayerRefund event. | ||
| emit_cpi!(ClaimedRelayerRefund { | ||
| l2_token_address: ctx.accounts.mint.key(), | ||
| claim_amount, | ||
| refund_address: ctx.accounts.token_account.key(), | ||
| }); | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you help me understand this bit of syntax a bit better? I also see it in the
lib.rs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the constraint that lifetime for remaining accounts (
'c) is at least as long as'infofor accounts that are referenced by this account array. I think its needed because of manual remaining account processing intry_from_remaining_accountmethod. Without these lifetime constraints the compiler errors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
o that's interesting. ye, I've had other lifetime issues while implementing other things and was able to address it through other syntax but this is more complex here.