2021-10-13 20:46:52 -07:00
|
|
|
use {
|
|
|
|
crate::parse_instruction::{
|
|
|
|
check_num_accounts, ParsableProgram, ParseInstructionError, ParsedInstructionEnum,
|
|
|
|
},
|
2022-04-20 10:13:42 -07:00
|
|
|
borsh::BorshDeserialize,
|
2021-10-13 20:46:52 -07:00
|
|
|
serde_json::json,
|
2022-02-05 04:00:31 -08:00
|
|
|
solana_sdk::{instruction::CompiledInstruction, message::AccountKeys, pubkey::Pubkey},
|
2022-04-20 10:13:42 -07:00
|
|
|
spl_associated_token_account::instruction::AssociatedTokenAccountInstruction,
|
2021-04-01 15:48:05 -07:00
|
|
|
};
|
|
|
|
|
2021-11-21 05:27:03 -08:00
|
|
|
// A helper function to convert spl_associated_token_account::id() as spl_sdk::pubkey::Pubkey
|
2021-04-01 15:48:05 -07:00
|
|
|
// to solana_sdk::pubkey::Pubkey
|
2021-11-21 05:27:03 -08:00
|
|
|
pub fn spl_associated_token_id() -> Pubkey {
|
|
|
|
Pubkey::new_from_array(spl_associated_token_account::id().to_bytes())
|
2021-04-01 15:48:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_associated_token(
|
|
|
|
instruction: &CompiledInstruction,
|
2022-02-05 04:00:31 -08:00
|
|
|
account_keys: &AccountKeys,
|
2021-04-01 15:48:05 -07:00
|
|
|
) -> Result<ParsedInstructionEnum, ParseInstructionError> {
|
|
|
|
match instruction.accounts.iter().max() {
|
|
|
|
Some(index) if (*index as usize) < account_keys.len() => {}
|
|
|
|
_ => {
|
|
|
|
// Runtime should prevent this from ever happening
|
|
|
|
return Err(ParseInstructionError::InstructionKeyMismatch(
|
|
|
|
ParsableProgram::SplAssociatedTokenAccount,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 10:13:42 -07:00
|
|
|
if instruction.data.is_empty() {
|
|
|
|
check_num_associated_token_accounts(&instruction.accounts, 7)?;
|
|
|
|
Ok(ParsedInstructionEnum {
|
|
|
|
instruction_type: "create".to_string(),
|
|
|
|
info: json!({
|
|
|
|
"source": account_keys[instruction.accounts[0] as usize].to_string(),
|
|
|
|
"account": account_keys[instruction.accounts[1] as usize].to_string(),
|
|
|
|
"wallet": account_keys[instruction.accounts[2] as usize].to_string(),
|
|
|
|
"mint": account_keys[instruction.accounts[3] as usize].to_string(),
|
|
|
|
"systemProgram": account_keys[instruction.accounts[4] as usize].to_string(),
|
|
|
|
"tokenProgram": account_keys[instruction.accounts[5] as usize].to_string(),
|
|
|
|
"rentSysvar": account_keys[instruction.accounts[6] as usize].to_string(),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let ata_instruction = AssociatedTokenAccountInstruction::try_from_slice(&instruction.data)
|
|
|
|
.map_err(|_| {
|
|
|
|
ParseInstructionError::InstructionNotParsable(ParsableProgram::SplToken)
|
|
|
|
})?;
|
|
|
|
match ata_instruction {
|
|
|
|
AssociatedTokenAccountInstruction::Create => {
|
|
|
|
check_num_associated_token_accounts(&instruction.accounts, 6)?;
|
|
|
|
Ok(ParsedInstructionEnum {
|
|
|
|
instruction_type: "create".to_string(),
|
|
|
|
info: json!({
|
|
|
|
"source": account_keys[instruction.accounts[0] as usize].to_string(),
|
|
|
|
"account": account_keys[instruction.accounts[1] as usize].to_string(),
|
|
|
|
"wallet": account_keys[instruction.accounts[2] as usize].to_string(),
|
|
|
|
"mint": account_keys[instruction.accounts[3] as usize].to_string(),
|
|
|
|
"systemProgram": account_keys[instruction.accounts[4] as usize].to_string(),
|
|
|
|
"tokenProgram": account_keys[instruction.accounts[5] as usize].to_string(),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 15:48:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_num_associated_token_accounts(
|
|
|
|
accounts: &[u8],
|
|
|
|
num: usize,
|
|
|
|
) -> Result<(), ParseInstructionError> {
|
|
|
|
check_num_accounts(accounts, num, ParsableProgram::SplAssociatedTokenAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2022-04-20 10:13:42 -07:00
|
|
|
#[allow(deprecated)]
|
|
|
|
use spl_associated_token_account::create_associated_token_account as create_associated_token_account_deprecated;
|
2021-10-13 20:46:52 -07:00
|
|
|
use {
|
|
|
|
super::*,
|
2022-04-19 18:23:43 -07:00
|
|
|
solana_account_decoder::parse_token::pubkey_from_spl_token,
|
2021-11-21 05:27:03 -08:00
|
|
|
spl_associated_token_account::{
|
2022-04-20 10:13:42 -07:00
|
|
|
get_associated_token_address,
|
|
|
|
instruction::create_associated_token_account,
|
2021-10-13 20:46:52 -07:00
|
|
|
solana_program::{
|
|
|
|
instruction::CompiledInstruction as SplAssociatedTokenCompiledInstruction,
|
2022-04-20 10:13:42 -07:00
|
|
|
message::Message, pubkey::Pubkey as SplAssociatedTokenPubkey, sysvar,
|
2021-10-13 20:46:52 -07:00
|
|
|
},
|
2021-04-01 15:48:05 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
fn convert_pubkey(pubkey: Pubkey) -> SplAssociatedTokenPubkey {
|
2021-05-21 15:53:53 -07:00
|
|
|
SplAssociatedTokenPubkey::new_from_array(pubkey.to_bytes())
|
2021-04-01 15:48:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn convert_compiled_instruction(
|
|
|
|
instruction: &SplAssociatedTokenCompiledInstruction,
|
|
|
|
) -> CompiledInstruction {
|
|
|
|
CompiledInstruction {
|
|
|
|
program_id_index: instruction.program_id_index,
|
|
|
|
accounts: instruction.accounts.clone(),
|
|
|
|
data: instruction.data.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 18:23:43 -07:00
|
|
|
fn convert_account_keys(message: &Message) -> Vec<Pubkey> {
|
|
|
|
message
|
|
|
|
.account_keys
|
|
|
|
.iter()
|
|
|
|
.map(pubkey_from_spl_token)
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2021-04-01 15:48:05 -07:00
|
|
|
#[test]
|
2022-04-20 10:13:42 -07:00
|
|
|
fn test_parse_associated_token_deprecated() {
|
2022-04-19 18:23:43 -07:00
|
|
|
let funder = Pubkey::new_unique();
|
|
|
|
let wallet_address = Pubkey::new_unique();
|
|
|
|
let mint = Pubkey::new_unique();
|
|
|
|
let associated_account_address =
|
|
|
|
get_associated_token_address(&convert_pubkey(wallet_address), &convert_pubkey(mint));
|
2022-04-20 10:13:42 -07:00
|
|
|
#[allow(deprecated)]
|
|
|
|
let create_ix = create_associated_token_account_deprecated(
|
|
|
|
&convert_pubkey(funder),
|
|
|
|
&convert_pubkey(wallet_address),
|
|
|
|
&convert_pubkey(mint),
|
|
|
|
);
|
|
|
|
let message = Message::new(&[create_ix], None);
|
2022-06-13 17:32:40 -07:00
|
|
|
let mut compiled_instruction = convert_compiled_instruction(&message.instructions[0]);
|
2022-04-20 10:13:42 -07:00
|
|
|
assert_eq!(
|
|
|
|
parse_associated_token(
|
|
|
|
&compiled_instruction,
|
|
|
|
&AccountKeys::new(&convert_account_keys(&message), None)
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
ParsedInstructionEnum {
|
|
|
|
instruction_type: "create".to_string(),
|
|
|
|
info: json!({
|
|
|
|
"source": funder.to_string(),
|
|
|
|
"account": associated_account_address.to_string(),
|
|
|
|
"wallet": wallet_address.to_string(),
|
|
|
|
"mint": mint.to_string(),
|
|
|
|
"systemProgram": solana_sdk::system_program::id().to_string(),
|
|
|
|
"tokenProgram": spl_token::id().to_string(),
|
|
|
|
"rentSysvar": sysvar::rent::id().to_string(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
);
|
2022-06-13 17:32:40 -07:00
|
|
|
compiled_instruction.accounts.pop();
|
|
|
|
assert!(parse_associated_token(
|
|
|
|
&compiled_instruction,
|
|
|
|
&AccountKeys::new(&convert_account_keys(&message), None)
|
|
|
|
)
|
|
|
|
.is_err());
|
2022-04-20 10:13:42 -07:00
|
|
|
}
|
2021-04-01 15:48:05 -07:00
|
|
|
|
2022-04-20 10:13:42 -07:00
|
|
|
#[test]
|
|
|
|
fn test_parse_associated_token() {
|
|
|
|
let funder = Pubkey::new_unique();
|
|
|
|
let wallet_address = Pubkey::new_unique();
|
|
|
|
let mint = Pubkey::new_unique();
|
|
|
|
let associated_account_address =
|
|
|
|
get_associated_token_address(&convert_pubkey(wallet_address), &convert_pubkey(mint));
|
2021-04-01 15:48:05 -07:00
|
|
|
let create_ix = create_associated_token_account(
|
2022-04-19 18:23:43 -07:00
|
|
|
&convert_pubkey(funder),
|
|
|
|
&convert_pubkey(wallet_address),
|
|
|
|
&convert_pubkey(mint),
|
2021-04-01 15:48:05 -07:00
|
|
|
);
|
|
|
|
let message = Message::new(&[create_ix], None);
|
2022-06-13 17:32:40 -07:00
|
|
|
let mut compiled_instruction = convert_compiled_instruction(&message.instructions[0]);
|
2021-04-01 15:48:05 -07:00
|
|
|
assert_eq!(
|
2022-04-19 18:23:43 -07:00
|
|
|
parse_associated_token(
|
|
|
|
&compiled_instruction,
|
|
|
|
&AccountKeys::new(&convert_account_keys(&message), None)
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2021-04-01 15:48:05 -07:00
|
|
|
ParsedInstructionEnum {
|
|
|
|
instruction_type: "create".to_string(),
|
|
|
|
info: json!({
|
2022-04-19 18:23:43 -07:00
|
|
|
"source": funder.to_string(),
|
|
|
|
"account": associated_account_address.to_string(),
|
|
|
|
"wallet": wallet_address.to_string(),
|
|
|
|
"mint": mint.to_string(),
|
|
|
|
"systemProgram": solana_sdk::system_program::id().to_string(),
|
|
|
|
"tokenProgram": spl_token::id().to_string(),
|
2021-04-01 15:48:05 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
);
|
2022-06-13 17:32:40 -07:00
|
|
|
compiled_instruction.accounts.pop();
|
|
|
|
assert!(parse_associated_token(
|
|
|
|
&compiled_instruction,
|
|
|
|
&AccountKeys::new(&convert_account_keys(&message), None)
|
|
|
|
)
|
|
|
|
.is_err());
|
2021-04-01 15:48:05 -07:00
|
|
|
}
|
|
|
|
}
|