transfer-fee.ts 4.1 KB

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