anchor.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. anchor.setProvider(anchor.AnchorProvider.env());
  7. const program = anchor.workspace.Anchor as Program<Anchor>;
  8. const connection = program.provider.connection;
  9. const TOKEN_2022_PROGRAM_ID = new anchor.web3.PublicKey(
  10. "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"
  11. );
  12. const payer = anchor.web3.Keypair.generate();
  13. const ATA_PROGRAM_ID = new anchor.web3.PublicKey(
  14. "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
  15. );
  16. const tokenName = "TestToken";
  17. const [mint] = anchor.web3.PublicKey.findProgramAddressSync(
  18. [
  19. Buffer.from("token-2022-token"),
  20. payer.publicKey.toBytes(),
  21. Buffer.from(tokenName),
  22. ],
  23. program.programId
  24. );
  25. const [payerATA] = anchor.web3.PublicKey.findProgramAddressSync(
  26. [
  27. payer.publicKey.toBytes(),
  28. TOKEN_2022_PROGRAM_ID.toBytes(),
  29. mint.toBytes(),
  30. ],
  31. ATA_PROGRAM_ID
  32. );
  33. const receiver = anchor.web3.Keypair.generate();
  34. const [receiverATA] = anchor.web3.PublicKey.findProgramAddressSync(
  35. [
  36. receiver.publicKey.toBytes(),
  37. TOKEN_2022_PROGRAM_ID.toBytes(),
  38. mint.toBytes(),
  39. ],
  40. ATA_PROGRAM_ID
  41. );
  42. it("Create Token-2022 Token", async () => {
  43. await connection.requestAirdrop(receiver.publicKey, 1000000000);
  44. await connection.requestAirdrop(payer.publicKey, 1000000000);
  45. const tx = new anchor.web3.Transaction();
  46. const ix = await program.methods
  47. .createToken(tokenName)
  48. .accounts({
  49. mint: mint,
  50. signer: payer.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. [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: payer.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. [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: payer.publicKey,
  112. receiver: payerATA,
  113. tokenProgram: TOKEN_2022_PROGRAM_ID,
  114. })
  115. .signers([payer])
  116. .instruction();
  117. tx.add(ix);
  118. const sig = await anchor.web3.sendAndConfirmTransaction(
  119. program.provider.connection,
  120. tx,
  121. [payer]
  122. );
  123. console.log("Your transaction signature", sig);
  124. });
  125. // Using init in the transfer instruction, as init if needed is bot working with Token 2022 yet.
  126. it("Transfer Token", async () => {
  127. const tx = new anchor.web3.Transaction();
  128. const ix = await program.methods
  129. .transferToken(new anchor.BN(100))
  130. .accounts({
  131. mint: mint,
  132. signer: payer.publicKey,
  133. from: payerATA,
  134. to: receiver.publicKey,
  135. tokenProgram: TOKEN_2022_PROGRAM_ID,
  136. associatedTokenProgram: ATA_PROGRAM_ID,
  137. toAta: receiverATA,
  138. })
  139. .instruction();
  140. tx.add(ix);
  141. const sig = await anchor.web3.sendAndConfirmTransaction(
  142. program.provider.connection,
  143. tx,
  144. [payer]
  145. );
  146. console.log("Your transaction signature", sig);
  147. });
  148. });