1
0

withdraw_excess_lamports.rs 30 KB

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