withdraw_excess_lamports.rs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. #![allow(clippy::arithmetic_side_effects)]
  2. mod setup;
  3. use {
  4. assert_matches::assert_matches,
  5. setup::{mint, TOKEN_PROGRAM_ID},
  6. solana_program_test::{tokio, BanksClientError, ProgramTest},
  7. solana_sdk::{
  8. instruction::InstructionError,
  9. program_pack::Pack,
  10. pubkey::Pubkey,
  11. signature::{Keypair, Signer},
  12. system_instruction,
  13. transaction::{Transaction, TransactionError},
  14. },
  15. spl_token_interface::state::{account::Account, mint::Mint, multisig::Multisig},
  16. std::mem::size_of,
  17. };
  18. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  19. #[tokio::test]
  20. async fn withdraw_excess_lamports_from_mint(token_program: Pubkey) {
  21. let context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  22. .start_with_context()
  23. .await;
  24. let excess_lamports = 4_000_000_000_000;
  25. // Given a mint authority, freeze authority and an account keypair.
  26. let mint_authority = Keypair::new();
  27. let freeze_authority = Pubkey::new_unique();
  28. let account = Keypair::new();
  29. let account_pubkey = account.pubkey();
  30. let account_size = size_of::<Mint>();
  31. let rent = context.banks_client.get_rent().await.unwrap();
  32. let mut initialize_ix = spl_token::instruction::initialize_mint(
  33. &spl_token::ID,
  34. &account.pubkey(),
  35. &mint_authority.pubkey(),
  36. Some(&freeze_authority),
  37. 0,
  38. )
  39. .unwrap();
  40. // Switches the program id to the token program.
  41. initialize_ix.program_id = token_program;
  42. // And we initialize a mint account with excess lamports.
  43. let instructions = vec![
  44. system_instruction::create_account(
  45. &context.payer.pubkey(),
  46. &account.pubkey(),
  47. rent.minimum_balance(account_size) + excess_lamports,
  48. account_size as u64,
  49. &token_program,
  50. ),
  51. initialize_ix,
  52. ];
  53. let tx = Transaction::new_signed_with_payer(
  54. &instructions,
  55. Some(&context.payer.pubkey()),
  56. &[&context.payer, &account],
  57. context.last_blockhash,
  58. );
  59. context.banks_client.process_transaction(tx).await.unwrap();
  60. let account = context
  61. .banks_client
  62. .get_account(account.pubkey())
  63. .await
  64. .unwrap();
  65. assert!(account.is_some());
  66. let account = account.unwrap();
  67. assert_eq!(
  68. account.lamports,
  69. rent.minimum_balance(account_size) + excess_lamports
  70. );
  71. // When we withdraw the excess lamports.
  72. let destination = Pubkey::new_unique();
  73. let mut withdraw_ix = spl_token_2022::instruction::withdraw_excess_lamports(
  74. &spl_token_2022::ID,
  75. &account_pubkey,
  76. &destination,
  77. &mint_authority.pubkey(),
  78. &[],
  79. )
  80. .unwrap();
  81. // Switches the program id to the token program.
  82. withdraw_ix.program_id = token_program;
  83. let tx = Transaction::new_signed_with_payer(
  84. &[withdraw_ix],
  85. Some(&context.payer.pubkey()),
  86. &[&context.payer, &mint_authority],
  87. context.last_blockhash,
  88. );
  89. context.banks_client.process_transaction(tx).await.unwrap();
  90. // Then the destination account has the excess lamports.
  91. let destination = context.banks_client.get_account(destination).await.unwrap();
  92. assert!(destination.is_some());
  93. let destination = destination.unwrap();
  94. assert_eq!(destination.lamports, excess_lamports);
  95. }
  96. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  97. #[tokio::test]
  98. async fn withdraw_excess_lamports_from_account(token_program: Pubkey) {
  99. let mut context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  100. .start_with_context()
  101. .await;
  102. let excess_lamports = 4_000_000_000_000;
  103. // Given a mint account.
  104. let mint_authority = Pubkey::new_unique();
  105. let freeze_authority = Pubkey::new_unique();
  106. let mint = mint::initialize(
  107. &mut context,
  108. mint_authority,
  109. Some(freeze_authority),
  110. &token_program,
  111. )
  112. .await
  113. .unwrap();
  114. // Given a mint authority, freeze authority and an account keypair.
  115. let owner = Keypair::new();
  116. let account = Keypair::new();
  117. let account_pubkey = account.pubkey();
  118. let account_size = size_of::<Account>();
  119. let rent = context.banks_client.get_rent().await.unwrap();
  120. let mut initialize_ix = spl_token::instruction::initialize_account(
  121. &spl_token::ID,
  122. &account.pubkey(),
  123. &mint,
  124. &owner.pubkey(),
  125. )
  126. .unwrap();
  127. // Switches the program id to the token program.
  128. initialize_ix.program_id = token_program;
  129. // When a new mint account is created and initialized.
  130. let instructions = vec![
  131. system_instruction::create_account(
  132. &context.payer.pubkey(),
  133. &account.pubkey(),
  134. rent.minimum_balance(account_size) + excess_lamports,
  135. account_size as u64,
  136. &token_program,
  137. ),
  138. initialize_ix,
  139. ];
  140. let tx = Transaction::new_signed_with_payer(
  141. &instructions,
  142. Some(&context.payer.pubkey()),
  143. &[&context.payer, &account],
  144. context.last_blockhash,
  145. );
  146. context.banks_client.process_transaction(tx).await.unwrap();
  147. let account = context
  148. .banks_client
  149. .get_account(account.pubkey())
  150. .await
  151. .unwrap();
  152. assert!(account.is_some());
  153. let account = account.unwrap();
  154. assert_eq!(
  155. account.lamports,
  156. rent.minimum_balance(account_size) + excess_lamports
  157. );
  158. // When we withdraw the excess lamports.
  159. let destination = Pubkey::new_unique();
  160. let mut withdraw_ix = spl_token_2022::instruction::withdraw_excess_lamports(
  161. &spl_token_2022::ID,
  162. &account_pubkey,
  163. &destination,
  164. &owner.pubkey(),
  165. &[],
  166. )
  167. .unwrap();
  168. // Switches the program id to the token program.
  169. withdraw_ix.program_id = token_program;
  170. let tx = Transaction::new_signed_with_payer(
  171. &[withdraw_ix],
  172. Some(&context.payer.pubkey()),
  173. &[&context.payer, &owner],
  174. context.last_blockhash,
  175. );
  176. context.banks_client.process_transaction(tx).await.unwrap();
  177. // Then the destination account has the excess lamports.
  178. let destination = context.banks_client.get_account(destination).await.unwrap();
  179. assert!(destination.is_some());
  180. let destination = destination.unwrap();
  181. assert_eq!(destination.lamports, excess_lamports);
  182. }
  183. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  184. #[tokio::test]
  185. async fn withdraw_excess_lamports_from_multisig(token_program: Pubkey) {
  186. let context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  187. .start_with_context()
  188. .await;
  189. let excess_lamports = 4_000_000_000_000;
  190. // Given an account
  191. let multisig = Keypair::new();
  192. let signer1 = Keypair::new();
  193. let signer1_pubkey = signer1.pubkey();
  194. let signer2 = Keypair::new();
  195. let signer2_pubkey = signer2.pubkey();
  196. let signer3 = Keypair::new();
  197. let signer3_pubkey = signer3.pubkey();
  198. let signers = vec![&signer1_pubkey, &signer2_pubkey, &signer3_pubkey];
  199. let rent = context.banks_client.get_rent().await.unwrap();
  200. let account_size = size_of::<Multisig>();
  201. let mut initialize_ix = spl_token::instruction::initialize_multisig(
  202. &spl_token::ID,
  203. &multisig.pubkey(),
  204. &signers,
  205. 3,
  206. )
  207. .unwrap();
  208. // Switches the program id to the token program.
  209. initialize_ix.program_id = token_program;
  210. // And we initialize the multisig account.
  211. let instructions = vec![
  212. system_instruction::create_account(
  213. &context.payer.pubkey(),
  214. &multisig.pubkey(),
  215. rent.minimum_balance(account_size) + excess_lamports,
  216. account_size as u64,
  217. &token_program,
  218. ),
  219. initialize_ix,
  220. ];
  221. let tx = Transaction::new_signed_with_payer(
  222. &instructions,
  223. Some(&context.payer.pubkey()),
  224. &[&context.payer, &multisig],
  225. context.last_blockhash,
  226. );
  227. context.banks_client.process_transaction(tx).await.unwrap();
  228. let account = context
  229. .banks_client
  230. .get_account(multisig.pubkey())
  231. .await
  232. .unwrap();
  233. assert!(account.is_some());
  234. let account = account.unwrap();
  235. assert_eq!(
  236. account.lamports,
  237. rent.minimum_balance(account_size) + excess_lamports
  238. );
  239. // When we withdraw the excess lamports.
  240. let destination = Pubkey::new_unique();
  241. let mut withdraw_ix = spl_token_2022::instruction::withdraw_excess_lamports(
  242. &spl_token_2022::ID,
  243. &multisig.pubkey(),
  244. &destination,
  245. &multisig.pubkey(),
  246. &signers,
  247. )
  248. .unwrap();
  249. // Switches the program id to the token program.
  250. withdraw_ix.program_id = token_program;
  251. let tx = Transaction::new_signed_with_payer(
  252. &[withdraw_ix],
  253. Some(&context.payer.pubkey()),
  254. &[&context.payer, &signer1, &signer2, &signer3],
  255. context.last_blockhash,
  256. );
  257. context.banks_client.process_transaction(tx).await.unwrap();
  258. // Then the destination account has the excess lamports.
  259. let destination = context.banks_client.get_account(destination).await.unwrap();
  260. assert!(destination.is_some());
  261. let destination = destination.unwrap();
  262. assert_eq!(destination.lamports, excess_lamports);
  263. }
  264. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  265. #[tokio::test]
  266. async fn fail_withdraw_excess_lamports_from_mint_wrong_authority(token_program: Pubkey) {
  267. let context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  268. .start_with_context()
  269. .await;
  270. let excess_lamports = 4_000_000_000_000;
  271. // Given a mint authority, freeze authority and an account keypair.
  272. let mint_authority = Keypair::new();
  273. let freeze_authority = Pubkey::new_unique();
  274. let account = Keypair::new();
  275. let account_pubkey = account.pubkey();
  276. let account_size = size_of::<Mint>();
  277. let rent = context.banks_client.get_rent().await.unwrap();
  278. let mut initialize_ix = spl_token::instruction::initialize_mint(
  279. &spl_token::ID,
  280. &account.pubkey(),
  281. &mint_authority.pubkey(),
  282. Some(&freeze_authority),
  283. 0,
  284. )
  285. .unwrap();
  286. // Switches the program id to the token program.
  287. initialize_ix.program_id = token_program;
  288. // And we initialize a mint account with excess lamports.
  289. let instructions = vec![
  290. system_instruction::create_account(
  291. &context.payer.pubkey(),
  292. &account.pubkey(),
  293. rent.minimum_balance(account_size) + excess_lamports,
  294. account_size as u64,
  295. &token_program,
  296. ),
  297. initialize_ix,
  298. ];
  299. let tx = Transaction::new_signed_with_payer(
  300. &instructions,
  301. Some(&context.payer.pubkey()),
  302. &[&context.payer, &account],
  303. context.last_blockhash,
  304. );
  305. context.banks_client.process_transaction(tx).await.unwrap();
  306. let account = context
  307. .banks_client
  308. .get_account(account.pubkey())
  309. .await
  310. .unwrap();
  311. assert!(account.is_some());
  312. let account = account.unwrap();
  313. assert_eq!(
  314. account.lamports,
  315. rent.minimum_balance(account_size) + excess_lamports
  316. );
  317. // When we try to withdraw the excess lamports with the wrong authority.
  318. let destination = Pubkey::new_unique();
  319. let wrong_authority = Keypair::new();
  320. let mut withdraw_ix = spl_token_2022::instruction::withdraw_excess_lamports(
  321. &spl_token_2022::ID,
  322. &account_pubkey,
  323. &destination,
  324. &wrong_authority.pubkey(),
  325. &[],
  326. )
  327. .unwrap();
  328. // Switches the program id to the token program.
  329. withdraw_ix.program_id = token_program;
  330. let tx = Transaction::new_signed_with_payer(
  331. &[withdraw_ix],
  332. Some(&context.payer.pubkey()),
  333. &[&context.payer, &wrong_authority],
  334. context.last_blockhash,
  335. );
  336. let error = context
  337. .banks_client
  338. .process_transaction(tx)
  339. .await
  340. .unwrap_err();
  341. // The we expect an error.
  342. assert_matches!(
  343. error,
  344. BanksClientError::TransactionError(TransactionError::InstructionError(
  345. _,
  346. InstructionError::Custom(4) // TokenError::OwnerMismatch
  347. ))
  348. );
  349. }
  350. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  351. #[tokio::test]
  352. async fn fail_withdraw_excess_lamports_from_account_wrong_authority(token_program: Pubkey) {
  353. let mut context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  354. .start_with_context()
  355. .await;
  356. let excess_lamports = 4_000_000_000_000;
  357. // Given a mint account.
  358. let mint_authority = Pubkey::new_unique();
  359. let freeze_authority = Pubkey::new_unique();
  360. let mint = mint::initialize(
  361. &mut context,
  362. mint_authority,
  363. Some(freeze_authority),
  364. &token_program,
  365. )
  366. .await
  367. .unwrap();
  368. // Given a mint authority, freeze authority and an account keypair.
  369. let owner = Keypair::new();
  370. let account = Keypair::new();
  371. let account_pubkey = account.pubkey();
  372. let account_size = size_of::<Account>();
  373. let rent = context.banks_client.get_rent().await.unwrap();
  374. let mut initialize_ix = spl_token::instruction::initialize_account(
  375. &spl_token::ID,
  376. &account.pubkey(),
  377. &mint,
  378. &owner.pubkey(),
  379. )
  380. .unwrap();
  381. // Switches the program id to the token program.
  382. initialize_ix.program_id = token_program;
  383. // When a new mint account is created and initialized.
  384. let instructions = vec![
  385. system_instruction::create_account(
  386. &context.payer.pubkey(),
  387. &account.pubkey(),
  388. rent.minimum_balance(account_size) + excess_lamports,
  389. account_size as u64,
  390. &token_program,
  391. ),
  392. initialize_ix,
  393. ];
  394. let tx = Transaction::new_signed_with_payer(
  395. &instructions,
  396. Some(&context.payer.pubkey()),
  397. &[&context.payer, &account],
  398. context.last_blockhash,
  399. );
  400. context.banks_client.process_transaction(tx).await.unwrap();
  401. let account = context
  402. .banks_client
  403. .get_account(account.pubkey())
  404. .await
  405. .unwrap();
  406. assert!(account.is_some());
  407. let account = account.unwrap();
  408. assert_eq!(
  409. account.lamports,
  410. rent.minimum_balance(account_size) + excess_lamports
  411. );
  412. // When we try to withdraw the excess lamports with the wrong owner.
  413. let destination = Pubkey::new_unique();
  414. let wrong_owner = Keypair::new();
  415. let mut withdraw_ix = spl_token_2022::instruction::withdraw_excess_lamports(
  416. &spl_token_2022::ID,
  417. &account_pubkey,
  418. &destination,
  419. &wrong_owner.pubkey(),
  420. &[],
  421. )
  422. .unwrap();
  423. // Switches the program id to the token program.
  424. withdraw_ix.program_id = token_program;
  425. let tx = Transaction::new_signed_with_payer(
  426. &[withdraw_ix],
  427. Some(&context.payer.pubkey()),
  428. &[&context.payer, &wrong_owner],
  429. context.last_blockhash,
  430. );
  431. let error = context
  432. .banks_client
  433. .process_transaction(tx)
  434. .await
  435. .unwrap_err();
  436. // The we expect an error.
  437. assert_matches!(
  438. error,
  439. BanksClientError::TransactionError(TransactionError::InstructionError(
  440. _,
  441. InstructionError::Custom(4) // TokenError::OwnerMismatch
  442. ))
  443. );
  444. }
  445. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  446. #[tokio::test]
  447. async fn fail_withdraw_excess_lamports_from_multisig_wrong_authority(token_program: Pubkey) {
  448. let context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  449. .start_with_context()
  450. .await;
  451. let excess_lamports = 4_000_000_000_000;
  452. // Given an account
  453. let multisig = Keypair::new();
  454. let signer1 = Keypair::new();
  455. let signer1_pubkey = signer1.pubkey();
  456. let signer2 = Keypair::new();
  457. let signer2_pubkey = signer2.pubkey();
  458. let signer3 = Keypair::new();
  459. let signer3_pubkey = signer3.pubkey();
  460. let signers = vec![&signer1_pubkey, &signer2_pubkey, &signer3_pubkey];
  461. let rent = context.banks_client.get_rent().await.unwrap();
  462. let account_size = size_of::<Multisig>();
  463. let mut initialize_ix = spl_token::instruction::initialize_multisig(
  464. &spl_token::ID,
  465. &multisig.pubkey(),
  466. &signers,
  467. 3,
  468. )
  469. .unwrap();
  470. // Switches the program id to the token program.
  471. initialize_ix.program_id = token_program;
  472. // And we initialize the multisig account.
  473. let instructions = vec![
  474. system_instruction::create_account(
  475. &context.payer.pubkey(),
  476. &multisig.pubkey(),
  477. rent.minimum_balance(account_size) + excess_lamports,
  478. account_size as u64,
  479. &token_program,
  480. ),
  481. initialize_ix,
  482. ];
  483. let tx = Transaction::new_signed_with_payer(
  484. &instructions,
  485. Some(&context.payer.pubkey()),
  486. &[&context.payer, &multisig],
  487. context.last_blockhash,
  488. );
  489. context.banks_client.process_transaction(tx).await.unwrap();
  490. let account = context
  491. .banks_client
  492. .get_account(multisig.pubkey())
  493. .await
  494. .unwrap();
  495. assert!(account.is_some());
  496. let account = account.unwrap();
  497. assert_eq!(
  498. account.lamports,
  499. rent.minimum_balance(account_size) + excess_lamports
  500. );
  501. // When we try to withdraw the excess lamports with the wrong authority.
  502. let destination = Pubkey::new_unique();
  503. let wrong_authority = Keypair::new();
  504. let mut withdraw_ix = spl_token_2022::instruction::withdraw_excess_lamports(
  505. &spl_token_2022::ID,
  506. &multisig.pubkey(),
  507. &destination,
  508. &wrong_authority.pubkey(),
  509. &signers,
  510. )
  511. .unwrap();
  512. // Switches the program id to the token program.
  513. withdraw_ix.program_id = token_program;
  514. let tx = Transaction::new_signed_with_payer(
  515. &[withdraw_ix],
  516. Some(&context.payer.pubkey()),
  517. &[&context.payer, &signer1, &signer2, &signer3],
  518. context.last_blockhash,
  519. );
  520. let error = context
  521. .banks_client
  522. .process_transaction(tx)
  523. .await
  524. .unwrap_err();
  525. // The we expect an error.
  526. assert_matches!(
  527. error,
  528. BanksClientError::TransactionError(TransactionError::InstructionError(
  529. _,
  530. InstructionError::Custom(4) // TokenError::OwnerMismatch
  531. ))
  532. );
  533. }
  534. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  535. #[tokio::test]
  536. async fn fail_withdraw_excess_lamports_from_multisig_missing_signer(token_program: Pubkey) {
  537. let context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  538. .start_with_context()
  539. .await;
  540. let excess_lamports = 4_000_000_000_000;
  541. // Given an account
  542. let multisig = Keypair::new();
  543. let signer1 = Keypair::new();
  544. let signer1_pubkey = signer1.pubkey();
  545. let signer2 = Keypair::new();
  546. let signer2_pubkey = signer2.pubkey();
  547. let signer3 = Keypair::new();
  548. let signer3_pubkey = signer3.pubkey();
  549. let signers = vec![&signer1_pubkey, &signer2_pubkey, &signer3_pubkey];
  550. let rent = context.banks_client.get_rent().await.unwrap();
  551. let account_size = size_of::<Multisig>();
  552. let mut initialize_ix = spl_token::instruction::initialize_multisig(
  553. &spl_token::ID,
  554. &multisig.pubkey(),
  555. &signers,
  556. 3,
  557. )
  558. .unwrap();
  559. // Switches the program id to the token program.
  560. initialize_ix.program_id = token_program;
  561. // And we initialize the multisig account.
  562. let instructions = vec![
  563. system_instruction::create_account(
  564. &context.payer.pubkey(),
  565. &multisig.pubkey(),
  566. rent.minimum_balance(account_size) + excess_lamports,
  567. account_size as u64,
  568. &token_program,
  569. ),
  570. initialize_ix,
  571. ];
  572. let tx = Transaction::new_signed_with_payer(
  573. &instructions,
  574. Some(&context.payer.pubkey()),
  575. &[&context.payer, &multisig],
  576. context.last_blockhash,
  577. );
  578. context.banks_client.process_transaction(tx).await.unwrap();
  579. let account = context
  580. .banks_client
  581. .get_account(multisig.pubkey())
  582. .await
  583. .unwrap();
  584. assert!(account.is_some());
  585. let account = account.unwrap();
  586. assert_eq!(
  587. account.lamports,
  588. rent.minimum_balance(account_size) + excess_lamports
  589. );
  590. // When we try to withdraw the excess lamports with the wrong authority.
  591. let destination = Pubkey::new_unique();
  592. let mut withdraw_ix = spl_token_2022::instruction::withdraw_excess_lamports(
  593. &spl_token_2022::ID,
  594. &multisig.pubkey(),
  595. &destination,
  596. &multisig.pubkey(),
  597. &[&signer1_pubkey, &signer2_pubkey],
  598. )
  599. .unwrap();
  600. // Switches the program id to the token program.
  601. withdraw_ix.program_id = token_program;
  602. let tx = Transaction::new_signed_with_payer(
  603. &[withdraw_ix],
  604. Some(&context.payer.pubkey()),
  605. &[&context.payer, &signer1, &signer2],
  606. context.last_blockhash,
  607. );
  608. let error = context
  609. .banks_client
  610. .process_transaction(tx)
  611. .await
  612. .unwrap_err();
  613. // The we expect an error.
  614. assert_matches!(
  615. error,
  616. BanksClientError::TransactionError(TransactionError::InstructionError(
  617. _,
  618. InstructionError::MissingRequiredSignature
  619. ))
  620. );
  621. }
  622. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  623. #[tokio::test]
  624. async fn withdraw_excess_lamports_from_mint_with_no_authority(token_program: Pubkey) {
  625. let context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  626. .start_with_context()
  627. .await;
  628. let excess_lamports = 4_000_000_000_000;
  629. // Given a mint authority, freeze authority and an account keypair.
  630. let mint_authority = Keypair::new();
  631. let freeze_authority = Pubkey::new_unique();
  632. let mint_account = Keypair::new();
  633. let account_pubkey = mint_account.pubkey();
  634. let account_size = size_of::<Mint>();
  635. let rent = context.banks_client.get_rent().await.unwrap();
  636. let mut initialize_ix = spl_token::instruction::initialize_mint(
  637. &spl_token::ID,
  638. &mint_account.pubkey(),
  639. &mint_authority.pubkey(),
  640. Some(&freeze_authority),
  641. 0,
  642. )
  643. .unwrap();
  644. // Switches the program id to the token program.
  645. initialize_ix.program_id = token_program;
  646. // And we initialize a mint account with excess lamports.
  647. let instructions = vec![
  648. system_instruction::create_account(
  649. &context.payer.pubkey(),
  650. &mint_account.pubkey(),
  651. rent.minimum_balance(account_size) + excess_lamports,
  652. account_size as u64,
  653. &token_program,
  654. ),
  655. initialize_ix,
  656. ];
  657. let tx = Transaction::new_signed_with_payer(
  658. &instructions,
  659. Some(&context.payer.pubkey()),
  660. &[&context.payer, &mint_account],
  661. context.last_blockhash,
  662. );
  663. context.banks_client.process_transaction(tx).await.unwrap();
  664. let account = context
  665. .banks_client
  666. .get_account(mint_account.pubkey())
  667. .await
  668. .unwrap();
  669. assert!(account.is_some());
  670. let account = account.unwrap();
  671. assert_eq!(
  672. account.lamports,
  673. rent.minimum_balance(account_size) + excess_lamports
  674. );
  675. // And we remove the mint authority.
  676. let mut set_authority_ix = spl_token::instruction::set_authority(
  677. &spl_token::ID,
  678. &mint_account.pubkey(),
  679. None,
  680. spl_token::instruction::AuthorityType::MintTokens,
  681. &mint_authority.pubkey(),
  682. &[&mint_authority.pubkey()],
  683. )
  684. .unwrap();
  685. // Switches the program id to the token program.
  686. set_authority_ix.program_id = token_program;
  687. let tx = Transaction::new_signed_with_payer(
  688. &[set_authority_ix],
  689. Some(&context.payer.pubkey()),
  690. &[&context.payer, &mint_authority],
  691. context.last_blockhash,
  692. );
  693. context.banks_client.process_transaction(tx).await.unwrap();
  694. let account = context
  695. .banks_client
  696. .get_account(mint_account.pubkey())
  697. .await
  698. .unwrap();
  699. assert!(account.is_some());
  700. let account = account.unwrap();
  701. let account = spl_token::state::Mint::unpack(&account.data).unwrap();
  702. assert!(account.mint_authority.is_none());
  703. // When we withdraw the excess lamports with no authority.
  704. let destination = Pubkey::new_unique();
  705. let mut withdraw_ix = spl_token_2022::instruction::withdraw_excess_lamports(
  706. &spl_token_2022::ID,
  707. &account_pubkey,
  708. &destination,
  709. &mint_account.pubkey(),
  710. &[],
  711. )
  712. .unwrap();
  713. // Switches the program id to the token program.
  714. withdraw_ix.program_id = token_program;
  715. let tx = Transaction::new_signed_with_payer(
  716. &[withdraw_ix],
  717. Some(&context.payer.pubkey()),
  718. &[&context.payer, &mint_account],
  719. context.last_blockhash,
  720. );
  721. context.banks_client.process_transaction(tx).await.unwrap();
  722. // Then the destination account has the excess lamports.
  723. let destination = context.banks_client.get_account(destination).await.unwrap();
  724. assert!(destination.is_some());
  725. let destination = destination.unwrap();
  726. assert_eq!(destination.lamports, excess_lamports);
  727. }
  728. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  729. #[tokio::test]
  730. async fn fail_withdraw_excess_lamports_from_mint_with_authority_and_mint_as_signer(
  731. token_program: Pubkey,
  732. ) {
  733. let context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  734. .start_with_context()
  735. .await;
  736. let excess_lamports = 4_000_000_000_000;
  737. // Given a mint authority, freeze authority and an account keypair.
  738. let mint_authority = Keypair::new();
  739. let freeze_authority = Pubkey::new_unique();
  740. let mint_account = Keypair::new();
  741. let account_pubkey = mint_account.pubkey();
  742. let account_size = size_of::<Mint>();
  743. let rent = context.banks_client.get_rent().await.unwrap();
  744. let mut initialize_ix = spl_token::instruction::initialize_mint(
  745. &spl_token::ID,
  746. &mint_account.pubkey(),
  747. &mint_authority.pubkey(),
  748. Some(&freeze_authority),
  749. 0,
  750. )
  751. .unwrap();
  752. // Switches the program id to the token program.
  753. initialize_ix.program_id = token_program;
  754. // And we initialize a mint account with excess lamports.
  755. let instructions = vec![
  756. system_instruction::create_account(
  757. &context.payer.pubkey(),
  758. &mint_account.pubkey(),
  759. rent.minimum_balance(account_size) + excess_lamports,
  760. account_size as u64,
  761. &token_program,
  762. ),
  763. initialize_ix,
  764. ];
  765. let tx = Transaction::new_signed_with_payer(
  766. &instructions,
  767. Some(&context.payer.pubkey()),
  768. &[&context.payer, &mint_account],
  769. context.last_blockhash,
  770. );
  771. context.banks_client.process_transaction(tx).await.unwrap();
  772. let account = context
  773. .banks_client
  774. .get_account(mint_account.pubkey())
  775. .await
  776. .unwrap();
  777. assert!(account.is_some());
  778. let account = account.unwrap();
  779. assert_eq!(
  780. account.lamports,
  781. rent.minimum_balance(account_size) + excess_lamports
  782. );
  783. // When we try to withdraw the excess lamports with the mint as authority.
  784. let destination = Pubkey::new_unique();
  785. let mut withdraw_ix = spl_token_2022::instruction::withdraw_excess_lamports(
  786. &spl_token_2022::ID,
  787. &account_pubkey,
  788. &destination,
  789. &mint_account.pubkey(),
  790. &[],
  791. )
  792. .unwrap();
  793. // Switches the program id to the token program.
  794. withdraw_ix.program_id = token_program;
  795. let tx = Transaction::new_signed_with_payer(
  796. &[withdraw_ix],
  797. Some(&context.payer.pubkey()),
  798. &[&context.payer, &mint_account],
  799. context.last_blockhash,
  800. );
  801. let error = context
  802. .banks_client
  803. .process_transaction(tx)
  804. .await
  805. .unwrap_err();
  806. // Then we expect an error.
  807. assert_matches!(
  808. error,
  809. BanksClientError::TransactionError(TransactionError::InstructionError(
  810. _,
  811. InstructionError::Custom(4) // TokenError::OwnerMismatch
  812. ))
  813. );
  814. }
  815. #[test_case::test_case(TOKEN_PROGRAM_ID ; "p-token")]
  816. #[tokio::test]
  817. async fn fail_withdraw_excess_lamports_from_mint_with_no_authority_and_authority_signer(
  818. token_program: Pubkey,
  819. ) {
  820. let context = ProgramTest::new("pinocchio_token_program", TOKEN_PROGRAM_ID, None)
  821. .start_with_context()
  822. .await;
  823. let excess_lamports = 4_000_000_000_000;
  824. // Given a mint authority, freeze authority and an account keypair.
  825. let mint_authority = Keypair::new();
  826. let freeze_authority = Pubkey::new_unique();
  827. let mint_account = Keypair::new();
  828. let account_pubkey = mint_account.pubkey();
  829. let account_size = size_of::<Mint>();
  830. let rent = context.banks_client.get_rent().await.unwrap();
  831. let mut initialize_ix = spl_token::instruction::initialize_mint(
  832. &spl_token::ID,
  833. &mint_account.pubkey(),
  834. &mint_authority.pubkey(),
  835. Some(&freeze_authority),
  836. 0,
  837. )
  838. .unwrap();
  839. // Switches the program id to the token program.
  840. initialize_ix.program_id = token_program;
  841. // And we initialize a mint account with excess lamports.
  842. let instructions = vec![
  843. system_instruction::create_account(
  844. &context.payer.pubkey(),
  845. &mint_account.pubkey(),
  846. rent.minimum_balance(account_size) + excess_lamports,
  847. account_size as u64,
  848. &token_program,
  849. ),
  850. initialize_ix,
  851. ];
  852. let tx = Transaction::new_signed_with_payer(
  853. &instructions,
  854. Some(&context.payer.pubkey()),
  855. &[&context.payer, &mint_account],
  856. context.last_blockhash,
  857. );
  858. context.banks_client.process_transaction(tx).await.unwrap();
  859. let account = context
  860. .banks_client
  861. .get_account(mint_account.pubkey())
  862. .await
  863. .unwrap();
  864. assert!(account.is_some());
  865. let account = account.unwrap();
  866. assert_eq!(
  867. account.lamports,
  868. rent.minimum_balance(account_size) + excess_lamports
  869. );
  870. // And we remove the mint authority.
  871. let mut set_authority_ix = spl_token::instruction::set_authority(
  872. &spl_token::ID,
  873. &mint_account.pubkey(),
  874. None,
  875. spl_token::instruction::AuthorityType::MintTokens,
  876. &mint_authority.pubkey(),
  877. &[&mint_authority.pubkey()],
  878. )
  879. .unwrap();
  880. // Switches the program id to the token program.
  881. set_authority_ix.program_id = token_program;
  882. let tx = Transaction::new_signed_with_payer(
  883. &[set_authority_ix],
  884. Some(&context.payer.pubkey()),
  885. &[&context.payer, &mint_authority],
  886. context.last_blockhash,
  887. );
  888. context.banks_client.process_transaction(tx).await.unwrap();
  889. let account = context
  890. .banks_client
  891. .get_account(mint_account.pubkey())
  892. .await
  893. .unwrap();
  894. assert!(account.is_some());
  895. let account = account.unwrap();
  896. let account = spl_token::state::Mint::unpack(&account.data).unwrap();
  897. assert!(account.mint_authority.is_none());
  898. // When we try to withdraw the excess lamports with the "old" mint authority.
  899. let destination = Pubkey::new_unique();
  900. let mut withdraw_ix = spl_token_2022::instruction::withdraw_excess_lamports(
  901. &spl_token_2022::ID,
  902. &account_pubkey,
  903. &destination,
  904. &mint_authority.pubkey(),
  905. &[],
  906. )
  907. .unwrap();
  908. // Switches the program id to the token program.
  909. withdraw_ix.program_id = token_program;
  910. let tx = Transaction::new_signed_with_payer(
  911. &[withdraw_ix],
  912. Some(&context.payer.pubkey()),
  913. &[&context.payer, &mint_authority],
  914. context.last_blockhash,
  915. );
  916. let error = context
  917. .banks_client
  918. .process_transaction(tx)
  919. .await
  920. .unwrap_err();
  921. // Then we expect an error.
  922. assert_matches!(
  923. error,
  924. BanksClientError::TransactionError(TransactionError::InstructionError(
  925. _,
  926. InstructionError::Custom(15) // TokenError::AuthorityTypeNotSupported
  927. ))
  928. );
  929. }