assert_instruction_count.rs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #![cfg(feature = "test-sbf")]
  2. mod action;
  3. use {
  4. solana_program_test::{processor, tokio, ProgramTest},
  5. solana_sdk::{
  6. program_pack::Pack,
  7. pubkey::Pubkey,
  8. signature::{Keypair, Signer},
  9. system_instruction,
  10. transaction::Transaction,
  11. },
  12. spl_token::{
  13. id, instruction,
  14. processor::Processor,
  15. state::{Account, Mint},
  16. },
  17. };
  18. const TRANSFER_AMOUNT: u64 = 1_000_000_000_000_000;
  19. #[tokio::test]
  20. async fn initialize_mint() {
  21. let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
  22. pt.set_compute_max_units(5_000); // last known 2252
  23. let (mut banks_client, payer, recent_blockhash) = pt.start().await;
  24. let owner_key = Pubkey::new_unique();
  25. let mint = Keypair::new();
  26. let decimals = 9;
  27. let rent = banks_client.get_rent().await.unwrap();
  28. let mint_rent = rent.minimum_balance(Mint::LEN);
  29. let transaction = Transaction::new_signed_with_payer(
  30. &[system_instruction::create_account(
  31. &payer.pubkey(),
  32. &mint.pubkey(),
  33. mint_rent,
  34. Mint::LEN as u64,
  35. &id(),
  36. )],
  37. Some(&payer.pubkey()),
  38. &[&payer, &mint],
  39. recent_blockhash,
  40. );
  41. banks_client.process_transaction(transaction).await.unwrap();
  42. let transaction = Transaction::new_signed_with_payer(
  43. &[
  44. instruction::initialize_mint(&id(), &mint.pubkey(), &owner_key, None, decimals)
  45. .unwrap(),
  46. ],
  47. Some(&payer.pubkey()),
  48. &[&payer],
  49. recent_blockhash,
  50. );
  51. banks_client.process_transaction(transaction).await.unwrap();
  52. }
  53. #[tokio::test]
  54. async fn initialize_account() {
  55. let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
  56. pt.set_compute_max_units(6_000); // last known 3284
  57. let (mut banks_client, payer, recent_blockhash) = pt.start().await;
  58. let owner = Keypair::new();
  59. let mint = Keypair::new();
  60. let account = Keypair::new();
  61. let decimals = 9;
  62. action::create_mint(
  63. &mut banks_client,
  64. &payer,
  65. recent_blockhash,
  66. &mint,
  67. &owner.pubkey(),
  68. decimals,
  69. )
  70. .await
  71. .unwrap();
  72. let rent = banks_client.get_rent().await.unwrap();
  73. let account_rent = rent.minimum_balance(Account::LEN);
  74. let transaction = Transaction::new_signed_with_payer(
  75. &[system_instruction::create_account(
  76. &payer.pubkey(),
  77. &account.pubkey(),
  78. account_rent,
  79. Account::LEN as u64,
  80. &id(),
  81. )],
  82. Some(&payer.pubkey()),
  83. &[&payer, &account],
  84. recent_blockhash,
  85. );
  86. banks_client.process_transaction(transaction).await.unwrap();
  87. let transaction = Transaction::new_signed_with_payer(
  88. &[instruction::initialize_account(
  89. &id(),
  90. &account.pubkey(),
  91. &mint.pubkey(),
  92. &owner.pubkey(),
  93. )
  94. .unwrap()],
  95. Some(&payer.pubkey()),
  96. &[&payer],
  97. recent_blockhash,
  98. );
  99. banks_client.process_transaction(transaction).await.unwrap();
  100. }
  101. #[tokio::test]
  102. async fn mint_to() {
  103. let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
  104. pt.set_compute_max_units(6_000); // last known 2668
  105. let (mut banks_client, payer, recent_blockhash) = pt.start().await;
  106. let owner = Keypair::new();
  107. let mint = Keypair::new();
  108. let account = Keypair::new();
  109. let decimals = 9;
  110. action::create_mint(
  111. &mut banks_client,
  112. &payer,
  113. recent_blockhash,
  114. &mint,
  115. &owner.pubkey(),
  116. decimals,
  117. )
  118. .await
  119. .unwrap();
  120. action::create_account(
  121. &mut banks_client,
  122. &payer,
  123. recent_blockhash,
  124. &account,
  125. &mint.pubkey(),
  126. &owner.pubkey(),
  127. )
  128. .await
  129. .unwrap();
  130. let transaction = Transaction::new_signed_with_payer(
  131. &[instruction::mint_to(
  132. &id(),
  133. &mint.pubkey(),
  134. &account.pubkey(),
  135. &owner.pubkey(),
  136. &[],
  137. TRANSFER_AMOUNT,
  138. )
  139. .unwrap()],
  140. Some(&payer.pubkey()),
  141. &[&payer, &owner],
  142. recent_blockhash,
  143. );
  144. banks_client.process_transaction(transaction).await.unwrap();
  145. }
  146. #[tokio::test]
  147. async fn transfer() {
  148. let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
  149. pt.set_compute_max_units(7_000); // last known 2972
  150. let (mut banks_client, payer, recent_blockhash) = pt.start().await;
  151. let owner = Keypair::new();
  152. let mint = Keypair::new();
  153. let source = Keypair::new();
  154. let destination = Keypair::new();
  155. let decimals = 9;
  156. action::create_mint(
  157. &mut banks_client,
  158. &payer,
  159. recent_blockhash,
  160. &mint,
  161. &owner.pubkey(),
  162. decimals,
  163. )
  164. .await
  165. .unwrap();
  166. action::create_account(
  167. &mut banks_client,
  168. &payer,
  169. recent_blockhash,
  170. &source,
  171. &mint.pubkey(),
  172. &owner.pubkey(),
  173. )
  174. .await
  175. .unwrap();
  176. action::create_account(
  177. &mut banks_client,
  178. &payer,
  179. recent_blockhash,
  180. &destination,
  181. &mint.pubkey(),
  182. &owner.pubkey(),
  183. )
  184. .await
  185. .unwrap();
  186. action::mint_to(
  187. &mut banks_client,
  188. &payer,
  189. recent_blockhash,
  190. &mint.pubkey(),
  191. &source.pubkey(),
  192. &owner,
  193. TRANSFER_AMOUNT,
  194. )
  195. .await
  196. .unwrap();
  197. action::transfer(
  198. &mut banks_client,
  199. &payer,
  200. recent_blockhash,
  201. &source.pubkey(),
  202. &destination.pubkey(),
  203. &owner,
  204. TRANSFER_AMOUNT,
  205. )
  206. .await
  207. .unwrap();
  208. }
  209. #[tokio::test]
  210. async fn burn() {
  211. let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
  212. pt.set_compute_max_units(6_000); // last known 2655
  213. let (mut banks_client, payer, recent_blockhash) = pt.start().await;
  214. let owner = Keypair::new();
  215. let mint = Keypair::new();
  216. let account = Keypair::new();
  217. let decimals = 9;
  218. action::create_mint(
  219. &mut banks_client,
  220. &payer,
  221. recent_blockhash,
  222. &mint,
  223. &owner.pubkey(),
  224. decimals,
  225. )
  226. .await
  227. .unwrap();
  228. action::create_account(
  229. &mut banks_client,
  230. &payer,
  231. recent_blockhash,
  232. &account,
  233. &mint.pubkey(),
  234. &owner.pubkey(),
  235. )
  236. .await
  237. .unwrap();
  238. action::mint_to(
  239. &mut banks_client,
  240. &payer,
  241. recent_blockhash,
  242. &mint.pubkey(),
  243. &account.pubkey(),
  244. &owner,
  245. TRANSFER_AMOUNT,
  246. )
  247. .await
  248. .unwrap();
  249. action::burn(
  250. &mut banks_client,
  251. &payer,
  252. recent_blockhash,
  253. &mint.pubkey(),
  254. &account.pubkey(),
  255. &owner,
  256. TRANSFER_AMOUNT,
  257. )
  258. .await
  259. .unwrap();
  260. }
  261. #[tokio::test]
  262. async fn close_account() {
  263. let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
  264. pt.set_compute_max_units(6_000); // last known 1783
  265. let (mut banks_client, payer, recent_blockhash) = pt.start().await;
  266. let owner = Keypair::new();
  267. let mint = Keypair::new();
  268. let account = Keypair::new();
  269. let decimals = 9;
  270. action::create_mint(
  271. &mut banks_client,
  272. &payer,
  273. recent_blockhash,
  274. &mint,
  275. &owner.pubkey(),
  276. decimals,
  277. )
  278. .await
  279. .unwrap();
  280. action::create_account(
  281. &mut banks_client,
  282. &payer,
  283. recent_blockhash,
  284. &account,
  285. &mint.pubkey(),
  286. &owner.pubkey(),
  287. )
  288. .await
  289. .unwrap();
  290. let transaction = Transaction::new_signed_with_payer(
  291. &[instruction::close_account(
  292. &id(),
  293. &account.pubkey(),
  294. &owner.pubkey(),
  295. &owner.pubkey(),
  296. &[],
  297. )
  298. .unwrap()],
  299. Some(&payer.pubkey()),
  300. &[&payer, &owner],
  301. recent_blockhash,
  302. );
  303. banks_client.process_transaction(transaction).await.unwrap();
  304. }