create-amm.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import * as anchor from '@coral-xyz/anchor';
  2. import type { Program } from '@coral-xyz/anchor';
  3. import { PublicKey } from '@solana/web3.js';
  4. import { BankrunProvider } from 'anchor-bankrun';
  5. import { expect } from 'chai';
  6. import { startAnchor } from 'solana-bankrun';
  7. import type { TokenSwap } from '../target/types/token_swap';
  8. import { type TestValues, createValues, expectRevert } from './utils';
  9. const IDL = require('../target/idl/token_swap.json');
  10. const PROGRAM_ID = new PublicKey(IDL.address);
  11. describe('Create AMM', async () => {
  12. // Configure the client to use the anchor-bankrun
  13. const context = await startAnchor('', [{ name: 'token_swap', programId: PROGRAM_ID }], []);
  14. const provider = new BankrunProvider(context);
  15. const connection = provider.connection;
  16. const payer = provider.wallet as anchor.Wallet;
  17. const program = new anchor.Program<TokenSwap>(IDL, provider);
  18. let values: TestValues;
  19. beforeEach(() => {
  20. values = createValues();
  21. });
  22. it('Creation', async () => {
  23. const id = new anchor.BN(values.id);
  24. const fee = values.fee;
  25. await program.methods
  26. .createAmm(id, fee)
  27. .accounts({
  28. payer: payer.publicKey,
  29. })
  30. .rpc();
  31. const ammAccount = await program.account.amm.fetch(values.ammKey);
  32. expect(ammAccount.id.toString()).to.equal(values.id.toString());
  33. expect(ammAccount.admin.toString()).to.equal(values.admin.publicKey.toString());
  34. expect(ammAccount.fee.toString()).to.equal(values.fee.toString());
  35. });
  36. it('Invalid fee', async () => {
  37. const id = new anchor.BN(values.id);
  38. values.fee = 10000;
  39. await expectRevert(
  40. program.methods
  41. .createAmm(id, values.fee)
  42. .accounts({
  43. payer: payer.publicKey,
  44. })
  45. .rpc(),
  46. );
  47. });
  48. });