anchor.ts 4.5 KB

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