transfer-fee.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. .rpc({ skipPreflight: true });
  103. console.log("Your transaction signature", transactionSignature);
  104. });
  105. it("Withdraw Transfer Fees from Mint Account", async () => {
  106. const transactionSignature = await program.methods
  107. .withdraw()
  108. .accounts({
  109. mintAccount: mintKeypair.publicKey,
  110. tokenAccount: senderTokenAccountAddress,
  111. })
  112. .rpc({ skipPreflight: true });
  113. console.log("Your transaction signature", transactionSignature);
  114. });
  115. it("Update Transfer Fee", async () => {
  116. const transferFeeBasisPoints = 0;
  117. const maximumFee = 0;
  118. const transactionSignature = await program.methods
  119. .updateFee(transferFeeBasisPoints, new anchor.BN(maximumFee))
  120. .accounts({ mintAccount: mintKeypair.publicKey })
  121. .rpc({ skipPreflight: true });
  122. console.log("Your transaction signature", transactionSignature);
  123. });
  124. });