Skip to content
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

token-2022: repair offchain extra metas helper #6088

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ where
if let Some(transfer_hook_accounts) = &self.transfer_hook_accounts {
instruction.accounts.extend(transfer_hook_accounts.clone());
} else {
#[allow(deprecated)]
offchain::resolve_extra_transfer_account_metas(
&mut instruction,
|address| {
Expand All @@ -946,6 +947,7 @@ where
.map_ok(|opt| opt.map(|acc| acc.data))
},
self.get_address(),
amount,
)
.await
.map_err(|_| TokenError::AccountNotFound)?;
Expand Down
50 changes: 37 additions & 13 deletions token/program-2022-test/tests/transfer_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ async fn success_downgrade_writable_and_signer_accounts() {
.unwrap();
}

#[allow(deprecated)]
#[tokio::test]
async fn success_transfers_using_onchain_helper() {
let authority = Pubkey::new_unique();
Expand Down Expand Up @@ -688,23 +689,20 @@ async fn success_transfers_using_onchain_helper() {
let (source_b_account, destination_b_account) =
setup_accounts(&token_b_context, Keypair::new(), Keypair::new(), amount).await;
let authority_b = token_b_context.alice;
let account_metas = vec![

// Since we need to add extra account metas for our swap, which is a
// combination of two transfers, we need to resolve the extra metas
// for each transfer instruction.
let transfer_1_metas = vec![
AccountMeta::new(source_a_account, false),
AccountMeta::new_readonly(mint_a, false),
AccountMeta::new(destination_a_account, false),
AccountMeta::new_readonly(authority_a.pubkey(), true),
AccountMeta::new_readonly(spl_token_2022::id(), false),
AccountMeta::new(source_b_account, false),
AccountMeta::new_readonly(mint_b, false),
AccountMeta::new(destination_b_account, false),
AccountMeta::new_readonly(authority_b.pubkey(), true),
AccountMeta::new_readonly(spl_token_2022::id(), false),
];

let mut instruction = Instruction::new_with_bytes(swap_program_id, &[], account_metas);

let mut transfer_1_instruction =
Instruction::new_with_bytes(swap_program_id, &[], transfer_1_metas.clone());
offchain::resolve_extra_transfer_account_metas(
&mut instruction,
&mut transfer_1_instruction,
|address| {
token_a.get_account(address).map_ok_or_else(
|e| match e {
Expand All @@ -715,11 +713,21 @@ async fn success_transfers_using_onchain_helper() {
)
},
&mint_a,
amount,
)
.await
.unwrap();

let transfer_2_metas = vec![
AccountMeta::new(source_b_account, false),
AccountMeta::new_readonly(mint_b, false),
AccountMeta::new(destination_b_account, false),
AccountMeta::new_readonly(authority_b.pubkey(), true),
];
let mut transfer_2_instruction =
Instruction::new_with_bytes(swap_program_id, &[], transfer_2_metas.clone());
offchain::resolve_extra_transfer_account_metas(
&mut instruction,
&mut transfer_2_instruction,
|address| {
token_a.get_account(address).map_ok_or_else(
|e| match e {
Expand All @@ -730,12 +738,28 @@ async fn success_transfers_using_onchain_helper() {
)
},
&mint_b,
amount,
)
.await
.unwrap();

let mut swap_metas = vec![
AccountMeta::new(source_a_account, false),
AccountMeta::new_readonly(mint_a, false),
AccountMeta::new(destination_a_account, false),
AccountMeta::new_readonly(authority_a.pubkey(), true),
AccountMeta::new_readonly(spl_token_2022::id(), false),
AccountMeta::new(source_b_account, false),
AccountMeta::new_readonly(mint_b, false),
AccountMeta::new(destination_b_account, false),
AccountMeta::new_readonly(authority_b.pubkey(), true),
AccountMeta::new_readonly(spl_token_2022::id(), false),
];
swap_metas.extend_from_slice(&transfer_1_instruction.accounts[4..]); // Remaining accounts from transfer 1
swap_metas.extend_from_slice(&transfer_2_instruction.accounts[4..]); // Remaining accounts from transfer 2
let swap_instruction = Instruction::new_with_bytes(swap_program_id, &[], swap_metas);
token_a
.process_ixs(&[instruction], &[&authority_a, &authority_b])
.process_ixs(&[swap_instruction], &[&authority_a, &authority_b])
.await
.unwrap();
}
1 change: 1 addition & 0 deletions token/program-2022/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ proptest = "1.4"
serial_test = "3.0.0"
solana-program-test = "1.17.6"
solana-sdk = "1.17.6"
spl-tlv-account-resolution = { version = "0.5.0", path = "../../libraries/tlv-account-resolution" }
serde_json = "1.0.111"

[lib]
Expand Down
Loading