bankrun.test.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { describe, it } from 'node:test';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import { Keypair, PublicKey, SystemProgram } from '@solana/web3.js';
  4. import { BankrunProvider } from 'anchor-bankrun';
  5. import { assert } from 'chai';
  6. import { startAnchor } from 'solana-bankrun';
  7. import { EscrowProgram } from '../target/types/escrow_program';
  8. const IDL = require('../target/idl/escrow_program.json');
  9. const PROGRAM_ID = new PublicKey(IDL.address);
  10. describe('escrow_program (Bankrun)', async () => {
  11. const context = await startAnchor('', [{ name: 'escrow_program', programId: PROGRAM_ID }], []);
  12. const provider = new BankrunProvider(context);
  13. const payer = provider.wallet as anchor.Wallet;
  14. const program = new anchor.Program<EscrowProgram>(IDL, provider);
  15. // Generate keypairs for Maker, Taker, and the escrow account
  16. const maker = Keypair.generate();
  17. const taker = Keypair.generate();
  18. const escrowKeypair = new Keypair();
  19. const mint = new Keypair(); // Mint account for SPL tokens
  20. let makerATA: PublicKey;
  21. let takerATA: PublicKey;
  22. let escrowVault: PublicKey;
  23. let escrowState: anchor.IdlAccounts<EscrowProgram>['escrowState'] | null;
  24. it('Make Escrow', async () => {
  25. await program.methods
  26. .make(new anchor.BN(100), new anchor.BN(50), new anchor.BN(12345)) // deposit_amount, offer_amount, seed
  27. .accounts({
  28. escrow: escrowKeypair.publicKey,
  29. maker: maker.publicKey,
  30. makerMint: mint.publicKey,
  31. takerMint: mint.publicKey,
  32. makerAta: makerATA,
  33. vault: escrowVault,
  34. auth: provider.wallet.publicKey,
  35. systemProgram: SystemProgram.programId,
  36. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  37. associatedTokenProgram: anchor.utils.token.ASSOCIATED_PROGRAM_ID,
  38. })
  39. .signers([maker, escrowKeypair])
  40. .rpc();
  41. // Fetch and verify the state of the escrow
  42. escrowState = await program.account.escrowState.fetch(escrowKeypair.publicKey);
  43. assert.equal(escrowState.maker.toString(), maker.publicKey.toString());
  44. assert.equal(escrowState.amount.toNumber(), 50);
  45. });
  46. it('Refund Escrow', async () => {
  47. await program.methods
  48. .refund()
  49. .accounts({
  50. escrow: escrowKeypair.publicKey,
  51. maker: maker.publicKey,
  52. vault: escrowVault,
  53. auth: provider.wallet.publicKey,
  54. makerAta: makerATA,
  55. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  56. })
  57. .signers([maker])
  58. .rpc();
  59. // Assert that escrow is closed or funds refunded
  60. escrowState = await program.account.escrowState.fetch(escrowKeypair.publicKey).catch(() => null);
  61. assert.isNull(escrowState, 'Escrow state should be closed after refund');
  62. });
  63. it('Take Escrow', async () => {
  64. await program.methods
  65. .take()
  66. .accounts({
  67. escrow: escrowKeypair.publicKey,
  68. taker: taker.publicKey,
  69. maker: maker.publicKey,
  70. makerAta: makerATA,
  71. takerAta: takerATA,
  72. vault: escrowVault,
  73. auth: provider.wallet.publicKey,
  74. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  75. })
  76. .signers([taker])
  77. .rpc();
  78. // Check token balances and transfers
  79. // Maker and Taker balances should reflect the correct amounts.
  80. });
  81. });