UniswapV2Factory.spec.ts 4.5 KB

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