action.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. use {
  2. solana_program_test::BanksClient,
  3. solana_sdk::{
  4. hash::Hash,
  5. program_pack::Pack,
  6. pubkey::Pubkey,
  7. signature::{Keypair, Signer},
  8. system_instruction,
  9. transaction::Transaction,
  10. transport::TransportError,
  11. },
  12. spl_token::{
  13. id, instruction,
  14. state::{Account, Mint},
  15. },
  16. };
  17. pub async fn create_mint(
  18. banks_client: &mut BanksClient,
  19. payer: &Keypair,
  20. recent_blockhash: Hash,
  21. pool_mint: &Keypair,
  22. manager: &Pubkey,
  23. decimals: u8,
  24. ) -> Result<(), TransportError> {
  25. let rent = banks_client.get_rent().await.unwrap();
  26. let mint_rent = rent.minimum_balance(Mint::LEN);
  27. let transaction = Transaction::new_signed_with_payer(
  28. &[
  29. system_instruction::create_account(
  30. &payer.pubkey(),
  31. &pool_mint.pubkey(),
  32. mint_rent,
  33. Mint::LEN as u64,
  34. &id(),
  35. ),
  36. instruction::initialize_mint(&id(), &pool_mint.pubkey(), manager, None, decimals)
  37. .unwrap(),
  38. ],
  39. Some(&payer.pubkey()),
  40. &[payer, pool_mint],
  41. recent_blockhash,
  42. );
  43. banks_client.process_transaction(transaction).await?;
  44. Ok(())
  45. }
  46. pub async fn create_account(
  47. banks_client: &mut BanksClient,
  48. payer: &Keypair,
  49. recent_blockhash: Hash,
  50. account: &Keypair,
  51. pool_mint: &Pubkey,
  52. owner: &Pubkey,
  53. ) -> Result<(), TransportError> {
  54. let rent = banks_client.get_rent().await.unwrap();
  55. let account_rent = rent.minimum_balance(Account::LEN);
  56. let transaction = Transaction::new_signed_with_payer(
  57. &[
  58. system_instruction::create_account(
  59. &payer.pubkey(),
  60. &account.pubkey(),
  61. account_rent,
  62. Account::LEN as u64,
  63. &id(),
  64. ),
  65. instruction::initialize_account(&id(), &account.pubkey(), pool_mint, owner).unwrap(),
  66. ],
  67. Some(&payer.pubkey()),
  68. &[payer, account],
  69. recent_blockhash,
  70. );
  71. banks_client.process_transaction(transaction).await?;
  72. Ok(())
  73. }
  74. pub async fn mint_to(
  75. banks_client: &mut BanksClient,
  76. payer: &Keypair,
  77. recent_blockhash: Hash,
  78. mint: &Pubkey,
  79. account: &Pubkey,
  80. mint_authority: &Keypair,
  81. amount: u64,
  82. ) -> Result<(), TransportError> {
  83. let transaction = Transaction::new_signed_with_payer(
  84. &[
  85. instruction::mint_to(&id(), mint, account, &mint_authority.pubkey(), &[], amount)
  86. .unwrap(),
  87. ],
  88. Some(&payer.pubkey()),
  89. &[payer, mint_authority],
  90. recent_blockhash,
  91. );
  92. banks_client.process_transaction(transaction).await?;
  93. Ok(())
  94. }
  95. pub async fn transfer(
  96. banks_client: &mut BanksClient,
  97. payer: &Keypair,
  98. recent_blockhash: Hash,
  99. source: &Pubkey,
  100. destination: &Pubkey,
  101. authority: &Keypair,
  102. amount: u64,
  103. ) -> Result<(), TransportError> {
  104. let transaction = Transaction::new_signed_with_payer(
  105. &[
  106. instruction::transfer(&id(), source, destination, &authority.pubkey(), &[], amount)
  107. .unwrap(),
  108. ],
  109. Some(&payer.pubkey()),
  110. &[payer, authority],
  111. recent_blockhash,
  112. );
  113. banks_client.process_transaction(transaction).await?;
  114. Ok(())
  115. }
  116. pub async fn burn(
  117. banks_client: &mut BanksClient,
  118. payer: &Keypair,
  119. recent_blockhash: Hash,
  120. mint: &Pubkey,
  121. account: &Pubkey,
  122. authority: &Keypair,
  123. amount: u64,
  124. ) -> Result<(), TransportError> {
  125. let transaction = Transaction::new_signed_with_payer(
  126. &[instruction::burn(&id(), account, mint, &authority.pubkey(), &[], amount).unwrap()],
  127. Some(&payer.pubkey()),
  128. &[payer, authority],
  129. recent_blockhash,
  130. );
  131. banks_client.process_transaction(transaction).await?;
  132. Ok(())
  133. }