token-2022: Add compatibility test for instructions (#3106)

This commit is contained in:
Jon Cinque 2022-04-22 23:55:18 +02:00 committed by GitHub
parent 049a89f351
commit c616db83c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 231 additions and 1 deletions

View File

@ -1990,4 +1990,234 @@ mod test {
let unpacked = TokenInstruction::unpack(&expect).unwrap();
assert_eq!(unpacked, check);
}
macro_rules! test_instruction {
($a:ident($($b:tt)*)) => {
let instruction_v3 = spl_token::instruction::$a($($b)*).unwrap();
let instruction_2022 = $a($($b)*).unwrap();
assert_eq!(instruction_v3, instruction_2022);
}
}
#[test]
fn test_v3_compatibility() {
let token_program_id = spl_token::id();
let mint_pubkey = Pubkey::new_unique();
let mint_authority_pubkey = Pubkey::new_unique();
let freeze_authority_pubkey = Pubkey::new_unique();
let decimals = 9u8;
let account_pubkey = Pubkey::new_unique();
let owner_pubkey = Pubkey::new_unique();
let multisig_pubkey = Pubkey::new_unique();
let signer_pubkeys_vec = vec![Pubkey::new_unique(); MAX_SIGNERS];
let signer_pubkeys = signer_pubkeys_vec.iter().collect::<Vec<_>>();
let m = 10u8;
let source_pubkey = Pubkey::new_unique();
let destination_pubkey = Pubkey::new_unique();
let authority_pubkey = Pubkey::new_unique();
let amount = 1_000_000_000_000;
let delegate_pubkey = Pubkey::new_unique();
let owned_pubkey = Pubkey::new_unique();
let new_authority_pubkey = Pubkey::new_unique();
let ui_amount = "100000.00";
test_instruction!(initialize_mint(
&token_program_id,
&mint_pubkey,
&mint_authority_pubkey,
None,
decimals,
));
test_instruction!(initialize_mint2(
&token_program_id,
&mint_pubkey,
&mint_authority_pubkey,
Some(&freeze_authority_pubkey),
decimals,
));
test_instruction!(initialize_account(
&token_program_id,
&account_pubkey,
&mint_pubkey,
&owner_pubkey,
));
test_instruction!(initialize_account2(
&token_program_id,
&account_pubkey,
&mint_pubkey,
&owner_pubkey,
));
test_instruction!(initialize_account3(
&token_program_id,
&account_pubkey,
&mint_pubkey,
&owner_pubkey,
));
test_instruction!(initialize_multisig(
&token_program_id,
&multisig_pubkey,
&signer_pubkeys,
m,
));
test_instruction!(initialize_multisig2(
&token_program_id,
&multisig_pubkey,
&signer_pubkeys,
m,
));
#[allow(deprecated)]
{
test_instruction!(transfer(
&token_program_id,
&source_pubkey,
&destination_pubkey,
&authority_pubkey,
&signer_pubkeys,
amount
));
}
test_instruction!(transfer_checked(
&token_program_id,
&source_pubkey,
&mint_pubkey,
&destination_pubkey,
&authority_pubkey,
&signer_pubkeys,
amount,
decimals,
));
test_instruction!(approve(
&token_program_id,
&source_pubkey,
&delegate_pubkey,
&owner_pubkey,
&signer_pubkeys,
amount
));
test_instruction!(approve_checked(
&token_program_id,
&source_pubkey,
&mint_pubkey,
&delegate_pubkey,
&owner_pubkey,
&signer_pubkeys,
amount,
decimals
));
test_instruction!(revoke(
&token_program_id,
&source_pubkey,
&owner_pubkey,
&signer_pubkeys,
));
// set_authority
{
let instruction_v3 = spl_token::instruction::set_authority(
&token_program_id,
&owned_pubkey,
Some(&new_authority_pubkey),
spl_token::instruction::AuthorityType::AccountOwner,
&owner_pubkey,
&signer_pubkeys,
)
.unwrap();
let instruction_2022 = set_authority(
&token_program_id,
&owned_pubkey,
Some(&new_authority_pubkey),
AuthorityType::AccountOwner,
&owner_pubkey,
&signer_pubkeys,
)
.unwrap();
assert_eq!(instruction_v3, instruction_2022);
}
test_instruction!(mint_to(
&token_program_id,
&mint_pubkey,
&account_pubkey,
&owner_pubkey,
&signer_pubkeys,
amount,
));
test_instruction!(mint_to_checked(
&token_program_id,
&mint_pubkey,
&account_pubkey,
&owner_pubkey,
&signer_pubkeys,
amount,
decimals,
));
test_instruction!(burn(
&token_program_id,
&account_pubkey,
&mint_pubkey,
&authority_pubkey,
&signer_pubkeys,
amount,
));
test_instruction!(burn_checked(
&token_program_id,
&account_pubkey,
&mint_pubkey,
&authority_pubkey,
&signer_pubkeys,
amount,
decimals,
));
test_instruction!(close_account(
&token_program_id,
&account_pubkey,
&destination_pubkey,
&owner_pubkey,
&signer_pubkeys,
));
test_instruction!(freeze_account(
&token_program_id,
&account_pubkey,
&mint_pubkey,
&owner_pubkey,
&signer_pubkeys,
));
test_instruction!(thaw_account(
&token_program_id,
&account_pubkey,
&mint_pubkey,
&owner_pubkey,
&signer_pubkeys,
));
test_instruction!(sync_native(&token_program_id, &account_pubkey,));
// get_account_data_size
{
let instruction_v3 =
spl_token::instruction::get_account_data_size(&token_program_id, &mint_pubkey)
.unwrap();
let instruction_2022 =
get_account_data_size(&token_program_id, &mint_pubkey, &[]).unwrap();
assert_eq!(instruction_v3, instruction_2022);
}
test_instruction!(initialize_immutable_owner(
&token_program_id,
&account_pubkey,
));
test_instruction!(amount_to_ui_amount(&token_program_id, &mint_pubkey, amount,));
test_instruction!(ui_amount_to_amount(
&token_program_id,
&mint_pubkey,
ui_amount,
));
}
}

View File

@ -1392,7 +1392,7 @@ pub fn get_account_data_size(
Ok(Instruction {
program_id: *token_program_id,
accounts: vec![AccountMeta::new(*mint_pubkey, false)],
accounts: vec![AccountMeta::new_readonly(*mint_pubkey, false)],
data: TokenInstruction::GetAccountDataSize.pack(),
})
}