UniswapV2Factory.spec.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import expect from 'expect';
  2. import { weight, createConnection, deploy, transaction, aliceKeypair, daveKeypair, query } from './index';
  3. import { ContractPromise } from '@polkadot/api-contract';
  4. import { ApiPromise } from '@polkadot/api';
  5. import { KeyringPair } from '@polkadot/keyring/types';
  6. import { DecodedEvent } from '@polkadot/api-contract/types';
  7. const TEST_ADDRESSES: [string, string] = [
  8. '5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUv7BA',
  9. '5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyV1W6M'
  10. ]
  11. describe('UniswapV2Factory', () => {
  12. let conn: ApiPromise;
  13. let factory: ContractPromise;
  14. let alice: KeyringPair;
  15. let dave: KeyringPair;
  16. let pairAbi: any;
  17. beforeEach(async function () {
  18. conn = await createConnection();
  19. alice = aliceKeypair();
  20. dave = daveKeypair();
  21. let deploy_contract = await deploy(conn, alice, 'UniswapV2Factory.contract', 10000000000000000n, alice.address);
  22. factory = new ContractPromise(conn, deploy_contract.abi, deploy_contract.address);
  23. // Upload UniswapV2Pair contract code so that it can instantiated from the factory
  24. // there probably is a better way of doing this than deploying a contract. Patches welcome.
  25. let pair = await deploy(conn, alice, 'UniswapV2Pair.contract', 0n);
  26. pairAbi = pair.abi;
  27. });
  28. afterEach(async function () {
  29. await conn.disconnect();
  30. });
  31. it('feeTo, feeToSetter, allPairsLength', async () => {
  32. const { output: feeTo } = await query(conn, alice, factory, "feeTo");
  33. // This is the 32-byte 0-address in ss58 format
  34. expect(feeTo?.eq('5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM')).toBeTruthy();
  35. const { output: feeToSetter } = await query(conn, alice, factory, "feeToSetter");
  36. expect(feeToSetter?.eq(alice.address)).toBeTruthy();
  37. const { output: allPairsLength } = await query(conn, alice, factory, "allPairsLength");
  38. expect(allPairsLength?.eq(0)).toBeTruthy();
  39. })
  40. async function createPair(tokens: [string, string]) {
  41. let gasLimit = await weight(conn, factory, "createPair", [tokens[0], tokens[1]]);
  42. let tx = factory.tx.createPair({ gasLimit }, ...tokens);
  43. let res0: any = await transaction(tx, alice);
  44. let events: DecodedEvent[] = res0.contractEvents;
  45. expect(events.length).toEqual(1)
  46. expect(events[0].event.identifier).toBe('UniswapV2Factory::PairCreated')
  47. expect(events[0].args[0].toString()).toBe(TEST_ADDRESSES[0])
  48. expect(events[0].args[1].toString()).toBe(TEST_ADDRESSES[1])
  49. expect(events[0].args[3].eq(1)).toBeTruthy();
  50. let pair_address = events[0].args[2].toString();
  51. const { output: get_pair } = await query(conn, alice, factory, "getPair", [tokens[0], tokens[1]]);
  52. expect(get_pair?.eq(pair_address)).toBeTruthy();
  53. const { output: pairRev } = await query(conn, alice, factory, "getPair", [tokens[1], tokens[0]]);
  54. expect(pairRev?.eq(pair_address)).toBeTruthy();
  55. const { output: pair0 } = await query(conn, alice, factory, "allPairs", [0]);
  56. expect(pair0?.eq(pair_address)).toBeTruthy();
  57. const { output: pairLength } = await query(conn, alice, factory, "allPairsLength");
  58. expect(pairLength?.eq(1)).toBeTruthy();
  59. const pair = new ContractPromise(conn, pairAbi, pair_address);
  60. const { output: pair_factory } = await query(conn, alice, pair, "factory");
  61. expect(pair_factory?.eq(factory.address)).toBeTruthy();
  62. const { output: token0 } = await query(conn, alice, pair, "token0");
  63. expect(token0?.eq(TEST_ADDRESSES[0])).toBeTruthy();
  64. const { output: token1 } = await query(conn, alice, pair, "token1");
  65. expect(token1?.eq(TEST_ADDRESSES[1])).toBeTruthy();
  66. }
  67. it('createPair', async () => {
  68. await createPair(TEST_ADDRESSES)
  69. })
  70. it('createPair:reverse', async () => {
  71. await createPair(TEST_ADDRESSES.slice().reverse() as [string, string])
  72. })
  73. it('setFeeTo', async () => {
  74. let gasLimit = await weight(conn, factory, "setFeeTo", [dave.address]);
  75. let tx = factory.tx.setFeeTo({ gasLimit }, dave.address);
  76. await transaction(tx, alice);
  77. const { output: feeTo } = await query(conn, alice, factory, "feeTo");
  78. expect(feeTo?.eq(dave.address)).toBeTruthy();
  79. })
  80. it('setFeeToSetter', async () => {
  81. let gasLimit = await weight(conn, factory, "setFeeToSetter", [dave.address]);
  82. let tx = factory.tx.setFeeToSetter({ gasLimit }, dave.address);
  83. await transaction(tx, alice);
  84. const { output: feeTo } = await query(conn, alice, factory, "feeToSetter");
  85. expect(feeTo?.eq(dave.address)).toBeTruthy();
  86. })
  87. })