anchor.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 = loadKeypairFromFile(
  18. require("os").homedir() + "/.config/solana/id.json"
  19. );
  20. const ATA_PROGRAM_ID = new anchor.web3.PublicKey(
  21. "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"
  22. );
  23. const tokenName = "TestToken";
  24. const [mint] = anchor.web3.PublicKey.findProgramAddressSync(
  25. [
  26. Buffer.from("token-2022-token"),
  27. payer.publicKey.toBytes(),
  28. Buffer.from(tokenName),
  29. ],
  30. program.programId
  31. );
  32. const [payerATA] = anchor.web3.PublicKey.findProgramAddressSync(
  33. [
  34. payer.publicKey.toBytes(),
  35. TOKEN_2022_PROGRAM_ID.toBytes(),
  36. mint.toBytes(),
  37. ],
  38. ATA_PROGRAM_ID
  39. );
  40. const receiver = loadKeypairFromFile(
  41. require("os").homedir() + "/.config/solana/testkeypair1.json"
  42. );
  43. connection.requestAirdrop(receiver.publicKey, 1000000000);
  44. const [receiverATA] = anchor.web3.PublicKey.findProgramAddressSync(
  45. [
  46. receiver.publicKey.toBytes(),
  47. TOKEN_2022_PROGRAM_ID.toBytes(),
  48. mint.toBytes(),
  49. ],
  50. ATA_PROGRAM_ID
  51. );
  52. it("Create Token-2022 Token", async () => {
  53. const tx = new anchor.web3.Transaction();
  54. const ix = await program.methods
  55. .createToken(tokenName)
  56. .accounts({
  57. mint: mint,
  58. signer: payer.publicKey,
  59. tokenProgram: TOKEN_2022_PROGRAM_ID,
  60. })
  61. .instruction();
  62. tx.add(ix);
  63. const sig = await anchor.web3.sendAndConfirmTransaction(
  64. program.provider.connection,
  65. tx,
  66. [payer]
  67. );
  68. console.log("Your transaction signature", sig);
  69. });
  70. it("Initialize payer ATA", async () => {
  71. const tx = new anchor.web3.Transaction();
  72. const ix = await program.methods
  73. .createAssociatedTokenAccount()
  74. .accounts({
  75. tokenAccount: payerATA,
  76. mint: mint,
  77. signer: payer.publicKey,
  78. tokenProgram: TOKEN_2022_PROGRAM_ID,
  79. })
  80. .instruction();
  81. tx.add(ix);
  82. const sig = await anchor.web3.sendAndConfirmTransaction(
  83. program.provider.connection,
  84. tx,
  85. [payer]
  86. );
  87. console.log("Your transaction signature", sig);
  88. });
  89. /*
  90. it("Initialize receiver ATA", async () => {
  91. const tx = new anchor.web3.Transaction();
  92. const ix = await program.methods
  93. .createAssociatedTokenAccount()
  94. .accounts({
  95. tokenAccount: receiverATA,
  96. mint: mint,
  97. signer: receiver.publicKey,
  98. tokenProgram: TOKEN_2022_PROGRAM_ID,
  99. associatedTokenProgram: ATA_PROGRAM_ID,
  100. })
  101. .signers([receiver])
  102. .instruction();
  103. tx.add(ix);
  104. const sig = await anchor.web3.sendAndConfirmTransaction(
  105. program.provider.connection,
  106. tx,
  107. [receiver]
  108. );
  109. console.log("Your transaction signature", sig);
  110. });
  111. */
  112. it("Mint Token to payer", async () => {
  113. const tx = new anchor.web3.Transaction();
  114. const ix = await program.methods
  115. .mintToken(new anchor.BN(200000000))
  116. .accounts({
  117. mint: mint,
  118. signer: payer.publicKey,
  119. receiver: payerATA,
  120. tokenProgram: TOKEN_2022_PROGRAM_ID,
  121. })
  122. .signers([payer])
  123. .instruction();
  124. tx.add(ix);
  125. const sig = await anchor.web3.sendAndConfirmTransaction(
  126. program.provider.connection,
  127. tx,
  128. [payer]
  129. );
  130. console.log("Your transaction signature", sig);
  131. });
  132. // init_if_needed not working with Token 22 ATA
  133. it("Transfer Token", async () => {
  134. const tx = new anchor.web3.Transaction();
  135. const ix = await program.methods
  136. .transferToken(new anchor.BN(100))
  137. .accounts({
  138. mint: mint,
  139. signer: payer.publicKey,
  140. from: payerATA,
  141. to: receiver.publicKey,
  142. tokenProgram: TOKEN_2022_PROGRAM_ID,
  143. associatedTokenProgram: ATA_PROGRAM_ID,
  144. toAta: receiverATA,
  145. })
  146. .instruction();
  147. tx.add(ix);
  148. const sig = await anchor.web3.sendAndConfirmTransaction(
  149. program.provider.connection,
  150. tx,
  151. [payer]
  152. );
  153. console.log("Your transaction signature", sig);
  154. });
  155. });