transfer-fee.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { TransferFee } from "../target/types/transfer_fee";
  4. import {
  5. TOKEN_2022_PROGRAM_ID,
  6. getAssociatedTokenAddressSync,
  7. getOrCreateAssociatedTokenAccount,
  8. mintTo,
  9. } from "@solana/spl-token";
  10. import { ASSOCIATED_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/utils/token";
  11. describe("transfer-fee", () => {
  12. const provider = anchor.AnchorProvider.env();
  13. const connection = provider.connection;
  14. const wallet = provider.wallet as anchor.Wallet;
  15. anchor.setProvider(provider);
  16. const program = anchor.workspace.TransferFee as Program<TransferFee>;
  17. const mintKeypair = new anchor.web3.Keypair();
  18. const recipient = new anchor.web3.Keypair();
  19. const senderTokenAccountAddress = getAssociatedTokenAddressSync(
  20. mintKeypair.publicKey,
  21. wallet.publicKey,
  22. false,
  23. TOKEN_2022_PROGRAM_ID
  24. );
  25. const recipientTokenAccountAddress = getAssociatedTokenAddressSync(
  26. mintKeypair.publicKey,
  27. recipient.publicKey,
  28. false,
  29. TOKEN_2022_PROGRAM_ID
  30. );
  31. it("Create Mint with Transfer Fee", async () => {
  32. const transferFeeBasisPoints = 100;
  33. const maximumFee = 1;
  34. const transactionSignature = await program.methods
  35. .initialize(transferFeeBasisPoints, new anchor.BN(maximumFee))
  36. .accounts({ mintAccount: mintKeypair.publicKey })
  37. .signers([mintKeypair])
  38. .rpc({ skipPreflight: true });
  39. console.log("Your transaction signature", transactionSignature);
  40. });
  41. it("Mint Tokens", async () => {
  42. await getOrCreateAssociatedTokenAccount(
  43. connection,
  44. wallet.payer,
  45. mintKeypair.publicKey,
  46. wallet.publicKey,
  47. false,
  48. null,
  49. null,
  50. TOKEN_2022_PROGRAM_ID,
  51. ASSOCIATED_PROGRAM_ID
  52. );
  53. await mintTo(
  54. connection,
  55. wallet.payer,
  56. mintKeypair.publicKey,
  57. senderTokenAccountAddress,
  58. wallet.payer,
  59. 300,
  60. [],
  61. null,
  62. TOKEN_2022_PROGRAM_ID
  63. );
  64. });
  65. it("Transfer", async () => {
  66. const transactionSignature = await program.methods
  67. .transfer(new anchor.BN(100))
  68. .accounts({
  69. sender: wallet.publicKey,
  70. recipient: recipient.publicKey,
  71. mintAccount: mintKeypair.publicKey,
  72. senderTokenAccount: senderTokenAccountAddress,
  73. recipientTokenAccount: recipientTokenAccountAddress,
  74. })
  75. .rpc({ skipPreflight: true });
  76. console.log("Your transaction signature", transactionSignature);
  77. });
  78. it("Transfer Again, fee limit by maximumFee", async () => {
  79. const transactionSignature = await program.methods
  80. .transfer(new anchor.BN(200))
  81. .accounts({
  82. sender: wallet.publicKey,
  83. recipient: recipient.publicKey,
  84. mintAccount: mintKeypair.publicKey,
  85. senderTokenAccount: senderTokenAccountAddress,
  86. recipientTokenAccount: recipientTokenAccountAddress,
  87. })
  88. .rpc({ skipPreflight: true });
  89. console.log("Your transaction signature", transactionSignature);
  90. });
  91. it("Harvest Transfer Fees to Mint Account", async () => {
  92. const transactionSignature = await program.methods
  93. .harvest()
  94. .accounts({ mintAccount: mintKeypair.publicKey })
  95. .remainingAccounts([
  96. {
  97. pubkey: recipientTokenAccountAddress,
  98. isSigner: false,
  99. isWritable: true,
  100. },
  101. {
  102. pubkey: new anchor.web3.Keypair().publicKey,
  103. isSigner: false,
  104. isWritable: true,
  105. },
  106. ])
  107. .rpc({ skipPreflight: true });
  108. console.log("Your transaction signature", transactionSignature);
  109. });
  110. it("Withdraw Transfer Fees from Mint Account", async () => {
  111. const transactionSignature = await program.methods
  112. .withdraw()
  113. .accounts({
  114. mintAccount: mintKeypair.publicKey,
  115. tokenAccount: senderTokenAccountAddress,
  116. })
  117. .rpc({ skipPreflight: true });
  118. console.log("Your transaction signature", transactionSignature);
  119. });
  120. it("Update Transfer Fee", async () => {
  121. const transferFeeBasisPoints = 0;
  122. const maximumFee = 0;
  123. const transactionSignature = await program.methods
  124. .updateFee(transferFeeBasisPoints, new anchor.BN(maximumFee))
  125. .accounts({ mintAccount: mintKeypair.publicKey })
  126. .rpc({ skipPreflight: true });
  127. console.log("Your transaction signature", transactionSignature);
  128. });
  129. });