anchor.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import * as anchor from '@coral-xyz/anchor';
  2. import type { Program } from '@coral-xyz/anchor';
  3. import { sendAndConfirmTransaction } from '@solana/web3.js';
  4. import type { Anchor } from '../target/types/anchor';
  5. describe('anchor', () => {
  6. // Configure the client to use the local cluster.
  7. const provider = anchor.AnchorProvider.env();
  8. anchor.setProvider(provider);
  9. const program = anchor.workspace.Anchor as Program<Anchor>;
  10. const connection = program.provider.connection;
  11. const TOKEN_2022_PROGRAM_ID = new anchor.web3.PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
  12. const wallet = provider.wallet as anchor.Wallet;
  13. const ATA_PROGRAM_ID = new anchor.web3.PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');
  14. const tokenName = 'TestToken';
  15. const [mint] = anchor.web3.PublicKey.findProgramAddressSync(
  16. [Buffer.from('token-2022-token'), wallet.publicKey.toBytes(), Buffer.from(tokenName)],
  17. program.programId,
  18. );
  19. const [payerATA] = anchor.web3.PublicKey.findProgramAddressSync(
  20. [wallet.publicKey.toBytes(), TOKEN_2022_PROGRAM_ID.toBytes(), mint.toBytes()],
  21. ATA_PROGRAM_ID,
  22. );
  23. const receiver = anchor.web3.Keypair.generate();
  24. const [receiverATA] = anchor.web3.PublicKey.findProgramAddressSync(
  25. [receiver.publicKey.toBytes(), TOKEN_2022_PROGRAM_ID.toBytes(), mint.toBytes()],
  26. ATA_PROGRAM_ID,
  27. );
  28. it('Create Token-2022 Token', async () => {
  29. await connection.requestAirdrop(receiver.publicKey, 1000000000);
  30. await connection.requestAirdrop(wallet.publicKey, 1000000000);
  31. const tx = new anchor.web3.Transaction();
  32. const ix = await program.methods
  33. .createToken(tokenName)
  34. .accounts({
  35. signer: wallet.publicKey,
  36. tokenProgram: TOKEN_2022_PROGRAM_ID,
  37. })
  38. .instruction();
  39. tx.add(ix);
  40. const sig = await sendAndConfirmTransaction(program.provider.connection, tx, [wallet.payer]);
  41. console.log('Your transaction signature', sig);
  42. });
  43. it('Initialize payer ATA', async () => {
  44. const tx = new anchor.web3.Transaction();
  45. const ix = await program.methods
  46. .createAssociatedTokenAccount()
  47. .accounts({
  48. tokenAccount: payerATA,
  49. mint: mint,
  50. signer: wallet.publicKey,
  51. tokenProgram: TOKEN_2022_PROGRAM_ID,
  52. })
  53. .instruction();
  54. tx.add(ix);
  55. const sig = await sendAndConfirmTransaction(program.provider.connection, tx, [wallet.payer]);
  56. console.log('Your transaction signature', sig);
  57. });
  58. /*
  59. // 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.
  60. it("Initialize receiver ATA", async () => {
  61. const tx = new anchor.web3.Transaction();
  62. const ix = await program.methods
  63. .createAssociatedTokenAccount()
  64. .accounts({
  65. tokenAccount: receiverATA,
  66. mint: mint,
  67. signer: receiver.publicKey,
  68. tokenProgram: TOKEN_2022_PROGRAM_ID,
  69. associatedTokenProgram: ATA_PROGRAM_ID,
  70. })
  71. .signers([receiver])
  72. .instruction();
  73. tx.add(ix);
  74. const sig = await anchor.web3.sendAndConfirmTransaction(
  75. program.provider.connection,
  76. tx,
  77. [receiver]
  78. );
  79. console.log("Your transaction signature", sig);
  80. });
  81. */
  82. it('Mint Token to payer', async () => {
  83. const tx = new anchor.web3.Transaction();
  84. const ix = await program.methods
  85. .mintToken(new anchor.BN(200000000))
  86. .accounts({
  87. mint: mint,
  88. signer: wallet.publicKey,
  89. receiver: payerATA,
  90. tokenProgram: TOKEN_2022_PROGRAM_ID,
  91. })
  92. .instruction();
  93. tx.add(ix);
  94. const sig = await sendAndConfirmTransaction(program.provider.connection, tx, [wallet.payer]);
  95. console.log('Your transaction signature', sig);
  96. });
  97. // Using init in the transfer instruction, as init if needed is bot working with Token 2022 yet.
  98. it('Transfer Token', async () => {
  99. const tx = new anchor.web3.Transaction();
  100. const ix = await program.methods
  101. .transferToken(new anchor.BN(100))
  102. .accounts({
  103. mint: mint,
  104. signer: wallet.publicKey,
  105. from: payerATA,
  106. to: receiver.publicKey,
  107. tokenProgram: TOKEN_2022_PROGRAM_ID,
  108. toAta: receiverATA,
  109. })
  110. .instruction();
  111. tx.add(ix);
  112. const sig = await sendAndConfirmTransaction(program.provider.connection, tx, [wallet.payer]);
  113. console.log('Your transaction signature', sig);
  114. });
  115. });