anchor.ts 4.5 KB

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