bankrun.test.ts 3.4 KB

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