transfer-tokens-program.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import * as anchor from '@coral-xyz/anchor';
  2. import { Program } from '@coral-xyz/anchor';
  3. import { TOKEN_PROGRAM_ID, createMint, getAssociatedTokenAddressSync, getMint } from '@solana/spl-token';
  4. import { Keypair, PublicKey } from '@solana/web3.js';
  5. import { assert } from 'chai';
  6. import { TransferTokensProgram } from '../target/types/transfer_tokens_program';
  7. describe('Transfer Tokens Program', () => {
  8. const provider = anchor.AnchorProvider.env();
  9. anchor.setProvider(provider);
  10. const program = anchor.workspace.TransferTokensProgram as Program<TransferTokensProgram>;
  11. const payer = provider.wallet as anchor.Wallet;
  12. const mintKeypair = Keypair.generate();
  13. const recipientKeypair = Keypair.generate();
  14. const DECIMALS = 9;
  15. let senderTokenAccount: PublicKey;
  16. let recipientTokenAccount: PublicKey;
  17. before(async () => {
  18. // Derive associated token account addresses
  19. senderTokenAccount = getAssociatedTokenAddressSync(mintKeypair.publicKey, payer.publicKey);
  20. recipientTokenAccount = getAssociatedTokenAddressSync(mintKeypair.publicKey, recipientKeypair.publicKey);
  21. });
  22. it('Creates a new SPL Token', async () => {
  23. const txSig = await program.methods
  24. .createToken(DECIMALS, payer.publicKey)
  25. .accounts({
  26. payer: payer.publicKey,
  27. mintAccount: mintKeypair.publicKey,
  28. })
  29. .signers([payer.payer, mintKeypair])
  30. .rpc();
  31. console.log(`Transaction Signature: ${txSig}`);
  32. const mintInfo = await getMint(provider.connection, mintKeypair.publicKey);
  33. assert.equal(mintInfo.decimals, DECIMALS, 'Mint decimals should match the specified value');
  34. assert.equal(mintInfo.mintAuthority?.toBase58(), payer.publicKey.toBase58(), 'Mint authority should be the payer');
  35. console.log('Mint created successfully:', mintKeypair.publicKey.toBase58());
  36. });
  37. it("Mints tokens to sender's account", async () => {
  38. const mintAmount = new anchor.BN(1_000_000_000); // Mint 1 token with 9 decimals
  39. const txSig = await program.methods
  40. .mint(mintAmount)
  41. .accounts({
  42. mintAccount: mintKeypair.publicKey,
  43. mintAuthority: payer.publicKey,
  44. recipient: payer.publicKey,
  45. })
  46. .signers([payer.payer])
  47. .rpc();
  48. console.log(`Minted ${mintAmount.toString()} tokens to ${senderTokenAccount}`);
  49. console.log(`Transaction Signature: ${txSig}`);
  50. });
  51. it('Transfers tokens from sender to recipient', async () => {
  52. const transferAmount = new anchor.BN(500_000_000); // Transfer 0.5 tokens
  53. const txSig = await program.methods
  54. .transferTokens(transferAmount)
  55. .accounts({
  56. sender: payer.publicKey,
  57. mintAccount: mintKeypair.publicKey,
  58. recipient: recipientKeypair.publicKey,
  59. })
  60. .signers([payer.payer])
  61. .rpc();
  62. console.log(`Transferred ${transferAmount.toString()} tokens to ${recipientTokenAccount}`);
  63. console.log(`Transaction Signature: ${txSig}`);
  64. });
  65. });