bankrun.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { describe, it } from 'node:test';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import { PublicKey, sendAndConfirmTransaction } from '@solana/web3.js';
  4. import { BankrunProvider } from 'anchor-bankrun';
  5. import { startAnchor } from 'solana-bankrun';
  6. import type { Anchor } from '../target/types/anchor';
  7. const IDL = require('../target/idl/anchor.json');
  8. const PROGRAM_ID = new PublicKey(IDL.address);
  9. describe('anchor', async () => {
  10. const context = await startAnchor('', [{ name: 'anchor', programId: PROGRAM_ID }], []);
  11. const provider = new BankrunProvider(context);
  12. anchor.setProvider(provider);
  13. const program = new anchor.Program<Anchor>(IDL, provider);
  14. const client = context.banksClient;
  15. const TOKEN_2022_PROGRAM_ID = new anchor.web3.PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
  16. const wallet = provider.wallet as anchor.Wallet;
  17. const ATA_PROGRAM_ID = new anchor.web3.PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');
  18. const tokenName = 'TestToken';
  19. const [mint] = anchor.web3.PublicKey.findProgramAddressSync(
  20. [Buffer.from('token-2022-token'), wallet.publicKey.toBytes(), Buffer.from(tokenName)],
  21. program.programId,
  22. );
  23. const [payerATA] = anchor.web3.PublicKey.findProgramAddressSync(
  24. [wallet.publicKey.toBytes(), TOKEN_2022_PROGRAM_ID.toBytes(), mint.toBytes()],
  25. ATA_PROGRAM_ID,
  26. );
  27. const receiver = anchor.web3.Keypair.generate();
  28. const [receiverATA] = anchor.web3.PublicKey.findProgramAddressSync(
  29. [receiver.publicKey.toBytes(), TOKEN_2022_PROGRAM_ID.toBytes(), mint.toBytes()],
  30. ATA_PROGRAM_ID,
  31. );
  32. it('Create Token-2022 Token', async () => {
  33. // await connection.requestAirdrop(receiver.publicKey, 1000000000);
  34. // await connection.requestAirdrop(wallet.publicKey, 1000000000);
  35. const tx = new anchor.web3.Transaction();
  36. const [blockhash, _height] = await client.getLatestBlockhash();
  37. const ix = await program.methods
  38. .createToken(tokenName)
  39. .accounts({
  40. signer: wallet.publicKey,
  41. tokenProgram: TOKEN_2022_PROGRAM_ID,
  42. })
  43. .instruction();
  44. tx.recentBlockhash = blockhash;
  45. tx.add(ix);
  46. tx.sign(wallet.payer);
  47. const sig = await client.processTransaction(tx);
  48. console.log('Your transaction signature', sig);
  49. });
  50. it('Initialize payer ATA', async () => {
  51. const tx = new anchor.web3.Transaction();
  52. const [blockhash, _height] = await client.getLatestBlockhash();
  53. const ix = await program.methods
  54. .createAssociatedTokenAccount()
  55. .accounts({
  56. tokenAccount: payerATA,
  57. mint: mint,
  58. signer: wallet.publicKey,
  59. tokenProgram: TOKEN_2022_PROGRAM_ID,
  60. })
  61. .instruction();
  62. tx.recentBlockhash = blockhash;
  63. tx.add(ix);
  64. tx.sign(wallet.payer);
  65. const sig = await client.processTransaction(tx);
  66. console.log('Your transaction signature', sig);
  67. });
  68. /*
  69. // This instruction is included only as a reference, but is not required to run this test, because we are using "init" in the program's transfer instruction. The create_associated_token_account instruction on the program is provided as a reference as well.
  70. it("Initialize receiver ATA", async () => {
  71. const tx = new anchor.web3.Transaction();
  72. const ix = await program.methods
  73. .createAssociatedTokenAccount()
  74. .accounts({
  75. tokenAccount: receiverATA,
  76. mint: mint,
  77. signer: receiver.publicKey,
  78. tokenProgram: TOKEN_2022_PROGRAM_ID,
  79. associatedTokenProgram: ATA_PROGRAM_ID,
  80. })
  81. .signers([receiver])
  82. .instruction();
  83. tx.add(ix);
  84. const sig = await anchor.web3.sendAndConfirmTransaction(
  85. program.provider.connection,
  86. tx,
  87. [receiver]
  88. );
  89. console.log("Your transaction signature", sig);
  90. });
  91. */
  92. it('Mint Token to payer', async () => {
  93. const tx = new anchor.web3.Transaction();
  94. const [blockhash, _height] = await client.getLatestBlockhash();
  95. const ix = await program.methods
  96. .mintToken(new anchor.BN(200000000))
  97. .accounts({
  98. mint: mint,
  99. signer: wallet.publicKey,
  100. receiver: payerATA,
  101. tokenProgram: TOKEN_2022_PROGRAM_ID,
  102. })
  103. .instruction();
  104. tx.recentBlockhash = blockhash;
  105. tx.add(ix);
  106. tx.sign(wallet.payer);
  107. const sig = await client.processTransaction(tx);
  108. console.log('Your transaction signature', sig);
  109. });
  110. // Using init in the transfer instruction, as init if needed is bot working with Token 2022 yet.
  111. it('Transfer Token', async () => {
  112. const tx = new anchor.web3.Transaction();
  113. const [blockhash, _height] = await client.getLatestBlockhash();
  114. const ix = await program.methods
  115. .transferToken(new anchor.BN(100))
  116. .accounts({
  117. mint: mint,
  118. signer: wallet.publicKey,
  119. from: payerATA,
  120. to: receiver.publicKey,
  121. tokenProgram: TOKEN_2022_PROGRAM_ID,
  122. toAta: receiverATA,
  123. })
  124. .instruction();
  125. tx.recentBlockhash = blockhash;
  126. tx.add(ix);
  127. tx.sign(wallet.payer);
  128. const sig = await client.processTransaction(tx);
  129. console.log('Your transaction signature', sig);
  130. });
  131. });