Add SPL Token 2022 to the list of known token ids (#23067)

* Add SPL Token 2022 to the list of known token ids

* Fix tests to accommodate #23729

* Test parsing of basic token-2022 instructsions

Co-authored-by: Tyera Eulberg <tyera@solana.com>
This commit is contained in:
Michael Vines 2022-04-19 18:23:43 -07:00 committed by GitHub
parent 255a6a729d
commit 413806684f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1009 additions and 834 deletions

506
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -23,6 +23,7 @@ solana-config-program = { path = "../programs/config", version = "=1.11.0" }
solana-sdk = { path = "../sdk", version = "=1.11.0" }
solana-vote-program = { path = "../programs/vote", version = "=1.11.0" }
spl-token = { version = "=3.2.0", features = ["no-entrypoint"] }
spl-token-2022 = { version = "=0.1.0", features = ["no-entrypoint"] }
thiserror = "1.0"
zstd = "0.11.1"

View File

@ -19,14 +19,20 @@ fn spl_token_id() -> Pubkey {
Pubkey::new_from_array(spl_token::id().to_bytes())
}
// A helper function to convert spl_token_2022::id() as spl_sdk::pubkey::Pubkey to
// solana_sdk::pubkey::Pubkey
fn spl_token_2022_id() -> Pubkey {
Pubkey::new_from_array(spl_token_2022::id().to_bytes())
}
// Returns all known SPL Token program ids
pub fn spl_token_ids() -> Vec<Pubkey> {
vec![spl_token_id()]
vec![spl_token_id(), spl_token_2022_id()]
}
// Check if the provided program id as a known SPL Token program id
pub fn is_known_spl_token_id(program_id: &Pubkey) -> bool {
*program_id == spl_token_id()
*program_id == spl_token_id() || *program_id == spl_token_2022_id()
}
// A helper function to convert spl_token::native_mint::id() as spl_sdk::pubkey::Pubkey to

540
programs/bpf/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -28,6 +28,7 @@ solana-vote-program = { path = "../programs/vote", version = "=1.11.0" }
spl-associated-token-account = { version = "=1.0.3", features = ["no-entrypoint"] }
spl-memo = { version = "=3.0.1", features = ["no-entrypoint"] }
spl-token = { version = "=3.2.0", features = ["no-entrypoint"] }
spl-token-2022 = { version = "=0.1.0", features = ["no-entrypoint"] }
thiserror = "1.0"
[package.metadata.docs.rs]

View File

@ -51,8 +51,9 @@ fn check_num_associated_token_accounts(
mod test {
use {
super::*,
solana_account_decoder::parse_token::pubkey_from_spl_token,
spl_associated_token_account::{
create_associated_token_account,
create_associated_token_account, get_associated_token_address,
solana_program::{
instruction::CompiledInstruction as SplAssociatedTokenCompiledInstruction,
message::Message, pubkey::Pubkey as SplAssociatedTokenPubkey,
@ -74,33 +75,46 @@ mod test {
}
}
fn convert_account_keys(message: &Message) -> Vec<Pubkey> {
message
.account_keys
.iter()
.map(pubkey_from_spl_token)
.collect()
}
#[test]
fn test_parse_associated_token() {
let mut keys: Vec<Pubkey> = vec![];
for _ in 0..7 {
keys.push(solana_sdk::pubkey::new_rand());
}
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));
let rent_sysvar = solana_sdk::sysvar::rent::id();
let create_ix = create_associated_token_account(
&convert_pubkey(keys[0]),
&convert_pubkey(keys[1]),
&convert_pubkey(keys[2]),
&convert_pubkey(funder),
&convert_pubkey(wallet_address),
&convert_pubkey(mint),
);
let message = Message::new(&[create_ix], None);
let compiled_instruction = convert_compiled_instruction(&message.instructions[0]);
let account_keys = AccountKeys::new(&keys, None);
assert_eq!(
parse_associated_token(&compiled_instruction, &account_keys).unwrap(),
parse_associated_token(
&compiled_instruction,
&AccountKeys::new(&convert_account_keys(&message), None)
)
.unwrap(),
ParsedInstructionEnum {
instruction_type: "create".to_string(),
info: json!({
"source": keys[0].to_string(),
"account": keys[1].to_string(),
"wallet": keys[2].to_string(),
"mint": keys[3].to_string(),
"systemProgram": keys[4].to_string(),
"tokenProgram": keys[5].to_string(),
"rentSysvar": keys[6].to_string(),
"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": rent_sysvar.to_string(),
})
}
);

File diff suppressed because it is too large Load Diff