UniswapV2ERC20.spec.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 { keccakAsU8a, keccakAsHex } from '@polkadot/util-crypto';
  8. const TOTAL_SUPPLY = BigInt(10000e18);
  9. const TEST_AMOUNT = BigInt(10e18);
  10. const MAX_UINT256 = BigInt(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
  11. describe('Deploy UniswapV2ERC20 contract and test', () => {
  12. let conn: ApiPromise;
  13. let token: ContractPromise;
  14. let alice: KeyringPair;
  15. let dave: KeyringPair;
  16. beforeEach(async function () {
  17. conn = await createConnection();
  18. alice = aliceKeypair();
  19. dave = daveKeypair();
  20. let deploy_contract = await deploy(conn, alice, 'ERC20.contract', BigInt(0), TOTAL_SUPPLY);
  21. token = new ContractPromise(conn, deploy_contract.abi, deploy_contract.address);
  22. });
  23. afterEach(async function () {
  24. await conn.disconnect();
  25. });
  26. it('name, symbol, decimals, totalSupply, balanceOf, DOMAIN_SEPARATOR, PERMIT_TYPEHASH', async () => {
  27. const { output: name } = await query(conn, alice, token, "name");
  28. expect(name?.toJSON()).toEqual('Uniswap V2')
  29. const { output: symbol } = await query(conn, alice, token, "symbol");
  30. expect(symbol?.toJSON()).toEqual('UNI-V2')
  31. const { output: decimals } = await query(conn, alice, token, "decimals");
  32. expect(decimals?.toJSON()).toEqual(18)
  33. const { output: totalSupply } = await query(conn, alice, token, "totalSupply");
  34. //console.log(`total supply: ${totalSupply?.toHuman()}`);
  35. expect(totalSupply?.eq(TOTAL_SUPPLY)).toBeTruthy();
  36. const { output: bal } = await query(conn, alice, token, "balanceOf", [alice.address]);
  37. expect(bal?.eq(TOTAL_SUPPLY)).toBeTruthy();
  38. const { output: domain_seperator } = await query(conn, alice, token, "domainSeparator");
  39. let expected = keccakAsHex(Buffer.concat([
  40. keccakAsU8a('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'),
  41. keccakAsU8a(name!.toString()),
  42. keccakAsU8a('1'),
  43. Buffer.from('0100000000000000000000000000000000000000000000000000000000000000', 'hex'),
  44. Buffer.from(token.address.buffer),
  45. ]));
  46. //console.log(`domain_separator: ${domain_seperator} ${expected}`);
  47. expect(domain_seperator?.eq(expected)).toBeTruthy();
  48. const { output: permit_typehash } = await query(conn, alice, token, "permitTypehash");
  49. expect(permit_typehash?.eq('0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9')).toBeTruthy();
  50. })
  51. it('approve', async () => {
  52. let gasLimit = await weight(conn, token, "approve", [dave.address, TEST_AMOUNT]);
  53. let tx = token.tx.approve({ gasLimit }, dave.address, TEST_AMOUNT);
  54. await transaction(tx, alice);
  55. let { output } = await query(conn, alice, token, "allowance", [alice.address, dave.address]);
  56. expect(output?.eq(TEST_AMOUNT)).toBeTruthy();
  57. })
  58. it('transfer', async () => {
  59. let gasLimit = await weight(conn, token, "transfer", [dave.address, TEST_AMOUNT]);
  60. let tx = token.tx.transfer({ gasLimit }, dave.address, TEST_AMOUNT);
  61. await transaction(tx, alice);
  62. const { output: aliceBal } = await query(conn, alice, token, "balanceOf", [alice.address]);
  63. expect(aliceBal?.eq(TOTAL_SUPPLY - TEST_AMOUNT)).toBeTruthy();
  64. const { output: daveBal } = await query(conn, alice, token, "balanceOf", [dave.address]);
  65. expect(daveBal?.eq(TEST_AMOUNT)).toBeTruthy();
  66. })
  67. // it('transfer:fail', async () => {
  68. // await expect(token.transfer(other.address, TOTAL_SUPPLY.add(1))).to.be.reverted // ds-math-sub-underflow
  69. // await expect(token.connect(other).transfer(wallet.address, 1)).to.be.reverted // ds-math-sub-underflow
  70. // })
  71. it('transferFrom', async () => {
  72. let gasLimit = await weight(conn, token, "approve", [dave.address, TEST_AMOUNT]);
  73. let tx = token.tx.approve({ gasLimit }, dave.address, TEST_AMOUNT);
  74. await transaction(tx, alice);
  75. const dryRun = await conn.call.contractsApi.call(
  76. dave.address,
  77. token.address,
  78. 0,
  79. null,
  80. null,
  81. token.abi.findMessage("transferFrom").toU8a([alice.address, dave.address, TEST_AMOUNT])
  82. );
  83. tx = token.tx.transferFrom({ gasLimit: dryRun.gasRequired }, alice.address, dave.address, TEST_AMOUNT);
  84. await transaction(tx, dave);
  85. const { output: allowance } = await query(conn, alice, token, "allowance", [alice.address, dave.address]);
  86. expect(allowance?.eq(0)).toBeTruthy();
  87. const { output: aliceBal } = await query(conn, alice, token, "balanceOf", [alice.address]);
  88. expect(aliceBal?.eq(TOTAL_SUPPLY - TEST_AMOUNT)).toBeTruthy();
  89. const { output: daveBal } = await query(conn, alice, token, "balanceOf", [dave.address]);
  90. expect(daveBal?.eq(TEST_AMOUNT)).toBeTruthy();
  91. })
  92. it('transferFrom:max', async () => {
  93. let gasLimit = await weight(conn, token, "approve", [dave.address, MAX_UINT256]);
  94. let tx = token.tx.approve({ gasLimit }, dave.address, MAX_UINT256);
  95. await transaction(tx, alice);
  96. const dryRun = await conn.call.contractsApi.call(
  97. dave.address,
  98. token.address,
  99. 0,
  100. null,
  101. null,
  102. token.abi.findMessage("transferFrom").toU8a([alice.address, dave.address, TEST_AMOUNT])
  103. );
  104. tx = token.tx.transferFrom({ gasLimit: dryRun.gasRequired }, alice.address, dave.address, TEST_AMOUNT);
  105. await transaction(tx, dave);
  106. const { output: allowance } = await query(conn, alice, token, "allowance", [alice.address, dave.address]);
  107. expect(allowance?.eq(MAX_UINT256 - TEST_AMOUNT)).toBeTruthy();
  108. const { output: aliceBal } = await query(conn, alice, token, "balanceOf", [alice.address]);
  109. expect(aliceBal?.eq(TOTAL_SUPPLY - TEST_AMOUNT)).toBeTruthy();
  110. const { output: daveBal } = await query(conn, alice, token, "balanceOf", [dave.address]);
  111. expect(daveBal?.eq(TEST_AMOUNT)).toBeTruthy();
  112. })
  113. });