anchor.ts 4.6 KB

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