-
Notifications
You must be signed in to change notification settings - Fork 38
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
solana: add recovery flow #418
Draft
kcsongor
wants to merge
9
commits into
main
Choose a base branch
from
solana/recovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b312d0d
solana: pass token_program to ATA (support token22)
kcsongor 508af61
solana: add dummy transfer hook
kcsongor 1123458
solana/transfer: use hook helper to transfer tokens
kcsongor 9d49b04
solana/sdk: support transfer hook
kcsongor 29fb437
solana: add network feature flags to dummy transfer hook program
kcsongor 4b55b84
solana: add counter to dummy transfer hook
kcsongor 637c03e
wip: transfer before burn/mint to trigger hooks
kcsongor a3ccab3
solana: populate an address lookup table to reduce tx size
kcsongor 18ebdc1
solana: add recovery flow
kcsongor 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,27 @@ | ||
[package] | ||
name = "dummy-transfer-hook" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "dummy_transfer_hook" | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
no-log-ix-name = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
mainnet = [] | ||
solana-devnet = [] | ||
tilt-devnet = [] | ||
tilt-devnet2 = [ "tilt-devnet" ] | ||
|
||
[dependencies] | ||
anchor-lang.workspace = true | ||
anchor-spl.workspace = true | ||
solana-program.workspace = true | ||
spl-tlv-account-resolution = "0.6.3" | ||
spl-transfer-hook-interface = "0.6.3" |
This file contains 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,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains 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,171 @@ | ||
use anchor_lang::prelude::*; | ||
use anchor_spl::{ | ||
associated_token::AssociatedToken, | ||
token_interface::{Mint, TokenAccount, TokenInterface}, | ||
}; | ||
use spl_tlv_account_resolution::state::ExtraAccountMetaList; | ||
|
||
declare_id!("BgabMDLaxsyB7eGMBt9L22MSk9KMrL4zY2iNe14kyFP5"); | ||
|
||
/// Index of the sender token account in the accounts passed to the transfer hook | ||
pub const SENDER_TOKEN_ACCOUNT_INDEX: u8 = 0; | ||
/// Index of the mint account in the accounts passed to the transfer hook | ||
pub const MINT_ACCOUNT_INDEX: u8 = 1; | ||
/// Index of the destination token account in the accounts passed to the transfer hook | ||
pub const DESTINATION_TOKEN_ACCOUNT_INDEX: u8 = 2; | ||
/// Index of the authority account in the accounts passed to the transfer hook | ||
pub const AUTHORITY_ACCOUNT_INDEX: u8 = 3; | ||
|
||
/// Number of extra accounts in the ExtraAccountMetaList account | ||
pub const EXTRA_ACCOUNTS_LEN: u8 = 2; | ||
|
||
#[program] | ||
pub mod dummy_transfer_hook { | ||
use spl_tlv_account_resolution::{ | ||
account::ExtraAccountMeta, seeds::Seed, state::ExtraAccountMetaList, | ||
}; | ||
use spl_transfer_hook_interface::instruction::{ExecuteInstruction, TransferHookInstruction}; | ||
|
||
use super::*; | ||
|
||
pub fn initialize_extra_account_meta_list( | ||
ctx: Context<InitializeExtraAccountMetaList>, | ||
) -> Result<()> { | ||
let account_metas = vec![ | ||
ExtraAccountMeta::new_with_seeds( | ||
&[ | ||
Seed::Literal { | ||
bytes: "dummy_account".as_bytes().to_vec(), | ||
}, | ||
// owner field of the sender token account | ||
Seed::AccountData { | ||
account_index: SENDER_TOKEN_ACCOUNT_INDEX, | ||
data_index: 32, | ||
length: 32, | ||
}, | ||
], | ||
false, // is_signer | ||
false, // is_writable | ||
)?, | ||
ExtraAccountMeta::new_with_seeds( | ||
&[Seed::Literal { | ||
bytes: "counter".as_bytes().to_vec(), | ||
}], | ||
false, // is_signer | ||
true, // is_writable | ||
)?, | ||
]; | ||
|
||
assert_eq!(EXTRA_ACCOUNTS_LEN as usize, account_metas.len()); | ||
|
||
// initialize ExtraAccountMetaList account with extra accounts | ||
ExtraAccountMetaList::init::<ExecuteInstruction>( | ||
&mut ctx.accounts.extra_account_meta_list.try_borrow_mut_data()?, | ||
&account_metas, | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn transfer_hook(ctx: Context<TransferHook>, _amount: u64) -> Result<()> { | ||
ctx.accounts.counter.count += 1; | ||
Ok(()) | ||
} | ||
|
||
// NOTE: the CPI call makes that the token2022 program makes (naturally) does not | ||
// follow the anchor calling convention, so we need to implement a fallback | ||
// instruction to handle the custom instruction | ||
pub fn fallback<'info>( | ||
program_id: &Pubkey, | ||
accounts: &'info [AccountInfo<'info>], | ||
data: &[u8], | ||
) -> Result<()> { | ||
let instruction = TransferHookInstruction::unpack(data)?; | ||
|
||
// match instruction discriminator to transfer hook interface execute instruction | ||
// token2022 program CPIs this instruction on token transfer | ||
match instruction { | ||
TransferHookInstruction::Execute { amount } => { | ||
let amount_bytes = amount.to_le_bytes(); | ||
|
||
// invoke custom transfer hook instruction on our program | ||
__private::__global::transfer_hook(program_id, accounts, &amount_bytes) | ||
} | ||
_ => Err(ProgramError::InvalidInstructionData.into()), | ||
} | ||
} | ||
} | ||
|
||
#[account] | ||
#[derive(InitSpace)] | ||
pub struct Counter { | ||
pub count: u64, | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct InitializeExtraAccountMetaList<'info> { | ||
#[account(mut)] | ||
payer: Signer<'info>, | ||
|
||
/// CHECK: ExtraAccountMetaList Account, must use these seeds | ||
#[account( | ||
init, | ||
payer = payer, | ||
space = ExtraAccountMetaList::size_of(EXTRA_ACCOUNTS_LEN as usize)?, | ||
seeds = [b"extra-account-metas", mint.key().as_ref()], | ||
bump | ||
)] | ||
pub extra_account_meta_list: AccountInfo<'info>, | ||
pub mint: InterfaceAccount<'info, Mint>, | ||
pub token_program: Interface<'info, TokenInterface>, | ||
pub associated_token_program: Program<'info, AssociatedToken>, | ||
|
||
#[account( | ||
init, | ||
payer = payer, | ||
space = 8 + Counter::INIT_SPACE, | ||
seeds = [b"counter"], | ||
bump | ||
)] | ||
pub counter: Account<'info, Counter>, | ||
|
||
pub system_program: Program<'info, System>, | ||
} | ||
|
||
#[derive(Accounts)] | ||
/// NOTE: this is just a dummy transfer hook to test that the accounts are | ||
/// passed in correctly. Do NOT use this as a starting point in a real | ||
/// application, as it's not secure. | ||
pub struct TransferHook<'info> { | ||
#[account( | ||
token::mint = mint, | ||
)] | ||
pub source_token: InterfaceAccount<'info, TokenAccount>, | ||
pub mint: InterfaceAccount<'info, Mint>, | ||
#[account( | ||
token::mint = mint, | ||
)] | ||
pub destination_token: InterfaceAccount<'info, TokenAccount>, | ||
/// CHECK: source token account authority, can be SystemAccount or PDA owned by another program | ||
pub authority: UncheckedAccount<'info>, | ||
/// CHECK: ExtraAccountMetaList Account, | ||
#[account( | ||
seeds = [b"extra-account-metas", mint.key().as_ref()], | ||
bump | ||
)] | ||
pub extra_account_meta_list: UncheckedAccount<'info>, | ||
#[account( | ||
seeds = [b"dummy_account", source_token.owner.as_ref()], | ||
bump | ||
)] | ||
/// CHECK: dummy account. It just tests that the off-chain code correctly | ||
/// computes and the on-chain code correctly passes on the PDA. | ||
pub dummy_account: AccountInfo<'info>, | ||
|
||
#[account( | ||
mut, | ||
seeds = [b"counter"], | ||
bump | ||
)] | ||
pub counter: Account<'info, Counter>, | ||
} |
This file contains 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 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 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.
should remove this by default (mainnet too)