12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- mod setup;
- use {
- setup::{account, mint, TOKEN_PROGRAM_ID},
- solana_program_test::{tokio, ProgramTest},
- solana_sdk::{
- program_pack::Pack,
- pubkey::Pubkey,
- signature::{Keypair, Signer},
- transaction::Transaction,
- },
- };
- #[tokio::test]
- async fn approve_checked() {
- let mut context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
- .start_with_context()
- .await;
- // Given a mint account.
- let mint_authority = Keypair::new();
- let freeze_authority = Pubkey::new_unique();
- let mint = mint::initialize(
- &mut context,
- mint_authority.pubkey(),
- Some(freeze_authority),
- &TOKEN_PROGRAM_ID,
- )
- .await
- .unwrap();
- // And a token account with 100 tokens.
- let owner = Keypair::new();
- let account =
- account::initialize(&mut context, &mint, &owner.pubkey(), &TOKEN_PROGRAM_ID).await;
- mint::mint(
- &mut context,
- &mint,
- &account,
- &mint_authority,
- 100,
- &TOKEN_PROGRAM_ID,
- )
- .await
- .unwrap();
- // When we approve a delegate.
- let delegate = Pubkey::new_unique();
- let approve_ix = spl_token::instruction::approve_checked(
- &TOKEN_PROGRAM_ID,
- &account,
- &mint,
- &delegate,
- &owner.pubkey(),
- &[],
- 50,
- 4,
- )
- .unwrap();
- let tx = Transaction::new_signed_with_payer(
- &[approve_ix],
- Some(&context.payer.pubkey()),
- &[&context.payer, &owner],
- context.last_blockhash,
- );
- context.banks_client.process_transaction(tx).await.unwrap();
- // Then the account should have the delegate and delegated amount.
- let account = context.banks_client.get_account(account).await.unwrap();
- assert!(account.is_some());
- let account = account.unwrap();
- let account = spl_token::state::Account::unpack(&account.data).unwrap();
- assert!(account.delegate.is_some());
- assert!(account.delegate.unwrap() == delegate);
- assert!(account.delegated_amount == 50);
- }
|