escrow.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import * as anchor from '@coral-xyz/anchor';
  2. import { Program } from '@coral-xyz/anchor';
  3. import { PublicKey, SystemProgram } from '@solana/web3.js';
  4. import { assert } from 'chai';
  5. import { EscrowProgram } from '../target/types/escrow_program';
  6. describe('escrow_program', () => {
  7. // Configure the client to use the local cluster.
  8. const provider = anchor.AnchorProvider.env();
  9. anchor.setProvider(provider);
  10. const program = anchor.workspace.EscrowProgram as Program<EscrowProgram>;
  11. const maker = provider.wallet;
  12. let vaultAccount: PublicKey;
  13. let escrowAccount: PublicKey;
  14. it('Creates a new escrow', async () => {
  15. // Generate a keypair for escrow
  16. escrowAccount = anchor.web3.Keypair.generate().publicKey;
  17. const vault = anchor.web3.Keypair.generate();
  18. const depositAmount = new anchor.BN(1000);
  19. const offerAmount = new anchor.BN(2000);
  20. const seed = new anchor.BN(123);
  21. await program.methods
  22. .make(depositAmount, offerAmount, seed)
  23. .accounts({
  24. vault: vaultAccount,
  25. auth: maker.publicKey,
  26. escrow: escrowAccount,
  27. maker: maker.publicKey,
  28. makerAta: maker.publicKey, // assuming the ATA
  29. makerMint: maker.publicKey, // assuming mint for simplicity
  30. takerMint: maker.publicKey, // assuming mint for simplicity
  31. tokenProgram: SystemProgram.programId,
  32. associatedTokenProgram: SystemProgram.programId,
  33. systemProgram: SystemProgram.programId,
  34. })
  35. .signers([])
  36. .rpc();
  37. // Assert escrow account was created
  38. const escrowState = await program.account.escrowState.fetch(escrowAccount);
  39. assert.equal(escrowState.amount.toString(), offerAmount.toString(), 'Escrow amount is incorrect');
  40. assert.equal(escrowState.seed.toString(), seed.toString(), 'Escrow seed is incorrect');
  41. });
  42. it('Refunds from the vault', async () => {
  43. await program.methods
  44. .refund()
  45. .accounts({
  46. vault: vaultAccount,
  47. auth: maker.publicKey,
  48. escrow: escrowAccount,
  49. maker: maker.publicKey,
  50. makerAta: maker.publicKey, // assuming the ATA
  51. makerMint: maker.publicKey, // assuming mint for simplicity
  52. tokenProgram: SystemProgram.programId,
  53. })
  54. .signers([])
  55. .rpc();
  56. // Assert that the escrow account was closed
  57. try {
  58. await program.account.escrowState.fetch(escrowAccount);
  59. assert.fail('Escrow account should be closed');
  60. } catch (err) {
  61. assert.ok('Escrow account was closed');
  62. }
  63. });
  64. it('Transfers tokens to taker', async () => {
  65. const taker = anchor.web3.Keypair.generate();
  66. const takerAta = anchor.web3.Keypair.generate();
  67. const takerReceiveAta = anchor.web3.Keypair.generate();
  68. await program.methods
  69. .take()
  70. .accounts({
  71. taker: taker.publicKey,
  72. maker: maker.publicKey,
  73. makerAta: maker.publicKey,
  74. takerAta: takerAta.publicKey,
  75. takerReceiveAta: takerReceiveAta.publicKey,
  76. escrow: escrowAccount,
  77. vault: vaultAccount,
  78. auth: maker.publicKey,
  79. tokenProgram: SystemProgram.programId,
  80. })
  81. .signers([taker])
  82. .rpc();
  83. // Assert the transfer occurred
  84. const escrowState = await program.account.escrowState.fetch(escrowAccount);
  85. assert.isNotNull(escrowState, 'Escrow state should be updated');
  86. });
  87. });