test.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. use pda_mint_authority_api::prelude::*;
  2. use solana_program::hash::Hash;
  3. use solana_program_test::{processor, BanksClient, ProgramTest};
  4. use solana_sdk::{signature::Keypair, signer::Signer, transaction::Transaction};
  5. async fn setup() -> (BanksClient, Keypair, Hash) {
  6. let mut program_test = ProgramTest::new(
  7. "pda_mint_authority_program",
  8. pda_mint_authority_api::ID,
  9. processor!(pda_mint_authority_program::process_instruction),
  10. );
  11. program_test.add_program("token_metadata", mpl_token_metadata::ID, None);
  12. program_test.prefer_bpf(true);
  13. program_test.start().await
  14. }
  15. #[tokio::test]
  16. async fn run_test() {
  17. // Setup test
  18. let (mut banks, payer, blockhash) = setup().await;
  19. let token_mint_keypair = Keypair::new();
  20. let name = str_to_bytes::<32>("Solana Gold");
  21. let symbol = str_to_bytes::<8>("GOLDSOL");
  22. let uri = str_to_bytes::<64>("https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json");
  23. // Submit init transaction.
  24. let ix = init(payer.pubkey());
  25. let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
  26. let res = banks.process_transaction(tx).await;
  27. assert!(res.is_ok());
  28. // Submit create transaction for spl token.
  29. let ix = create(
  30. payer.pubkey(),
  31. token_mint_keypair.pubkey(),
  32. name,
  33. symbol,
  34. uri,
  35. );
  36. let tx = Transaction::new_signed_with_payer(
  37. &[ix],
  38. Some(&payer.pubkey()),
  39. &[&payer, &token_mint_keypair],
  40. blockhash,
  41. );
  42. let res = banks.process_transaction(tx).await;
  43. assert!(res.is_ok());
  44. let to_ata = spl_associated_token_account::get_associated_token_address(
  45. &payer.pubkey(),
  46. &token_mint_keypair.pubkey(),
  47. );
  48. // Submit mint transaction.
  49. let ix = mint(payer.pubkey(), token_mint_keypair.pubkey(), to_ata, 100);
  50. let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
  51. let res = banks.process_transaction(tx).await;
  52. assert!(res.is_ok());
  53. }
  54. pub fn str_to_bytes<const N: usize>(str: &str) -> [u8; N] {
  55. let mut str_bytes = [0u8; N];
  56. let copy_len = str.len().min(N);
  57. str_bytes[..copy_len].copy_from_slice(&str.as_bytes()[..copy_len]);
  58. str_bytes
  59. }