store.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import expect from 'expect';
  2. import { weight, createConnection, deploy, transaction, aliceKeypair, } from './index';
  3. import { ContractPromise } from '@polkadot/api-contract';
  4. import { ApiPromise } from '@polkadot/api';
  5. // TODO:
  6. // This apparently works with subxt.
  7. describe.skip('Deploy store contract and test', () => {
  8. let conn: ApiPromise;
  9. before(async function () {
  10. conn = await createConnection();
  11. });
  12. after(async function () {
  13. await conn.disconnect();
  14. });
  15. it('store', async function () {
  16. this.timeout(100000);
  17. const alice = aliceKeypair();
  18. let deployed_contract = await deploy(conn, alice, 'store.contract', BigInt(0));
  19. let contract = new ContractPromise(conn, deployed_contract.abi, deployed_contract.address);
  20. let res1 = await contract.query.getValues1(alice.address, {});
  21. expect(res1.output?.toJSON()).toStrictEqual([0, 0, 0, 0]);
  22. let res2 = await contract.query.getValues2(alice.address, {});
  23. expect(res2.output?.toJSON()).toStrictEqual([0, "", "0xb00b1e", "0x00000000", "bar1"]);
  24. let gasLimit = await weight(conn, contract, "setValues");
  25. const tx1 = contract.tx.setValues({ gasLimit });
  26. await transaction(tx1, alice);
  27. let res3 = await contract.query.getValues1(alice.address, {});
  28. expect(res3.output?.toJSON()).toStrictEqual(["0xffffffffffffffff",
  29. 3671129839,
  30. 32766,
  31. "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  32. ]);
  33. let res4 = await contract.query.getValues2(alice.address, {});
  34. expect(res4.output?.toJSON()).toStrictEqual([
  35. 102,
  36. "the course of true love never did run smooth",
  37. "0xb00b1e",
  38. "0x41424344",
  39. "bar2",
  40. ]);
  41. gasLimit = await weight(conn, contract, "doOps");
  42. const tx2 = contract.tx.doOps({ gasLimit });
  43. await transaction(tx2, alice);
  44. let res5 = await contract.query.getValues1(alice.address, {});
  45. expect(res5.output?.toJSON()).toStrictEqual([
  46. 1,
  47. 65263,
  48. 32767,
  49. "0x7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
  50. ]);
  51. let res6 = await contract.query.getValues2(alice.address, {});
  52. expect(res6.output?.toJSON()).toStrictEqual([
  53. 61200,
  54. "",
  55. "0xb0ff1e",
  56. "0x61626364",
  57. "bar4",
  58. ]);
  59. gasLimit = await weight(conn, contract, "pushZero");
  60. const tx3 = contract.tx.pushZero({ gasLimit });
  61. await transaction(tx3, alice);
  62. let bs = "0xb0ff1e00";
  63. for (let i = 0; i < 20; i++) {
  64. let res7 = await contract.query.getBs(alice.address, {});
  65. expect(res7.output?.toJSON()).toStrictEqual(bs);
  66. if (bs.length <= 4 || Math.random() >= 0.5) {
  67. let val = ((Math.random() * 256) | 0).toString(16);
  68. val = val.length == 1 ? "0" + val : val;
  69. gasLimit = await weight(conn, contract, "push", ["0x" + val]);
  70. const tx = contract.tx.push({ gasLimit }, ["0x" + val]);
  71. await transaction(tx, alice);
  72. bs += val;
  73. } else {
  74. const tx = contract.tx.pop({ gasLimit });
  75. await transaction(tx, alice);
  76. // note transactions do not give us access to the return values of a contract execution;
  77. // therefore, we can't check the return values of pop
  78. bs = bs.slice(0, -2);
  79. }
  80. }
  81. });
  82. });