structs.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: Apache-2.0
  2. import expect from 'expect';
  3. import { createConnection, deploy, transaction, aliceKeypair, weight, query, } from './index';
  4. import { ContractPromise } from '@polkadot/api-contract';
  5. import { ApiPromise } from '@polkadot/api';
  6. describe('Deploy struct contract and test', () => {
  7. let conn: ApiPromise;
  8. before(async function () {
  9. conn = await createConnection();
  10. });
  11. after(async function () {
  12. await conn.disconnect();
  13. });
  14. it('structs', async function () {
  15. this.timeout(50000);
  16. const alice = aliceKeypair();
  17. let deployed_contract = await deploy(conn, alice, 'structs.contract', BigInt(0));
  18. let contract = new ContractPromise(conn, deployed_contract.abi, deployed_contract.address);
  19. let gasLimit = await weight(conn, contract, "setFoo1");
  20. const tx1 = contract.tx.setFoo1({ gasLimit });
  21. await transaction(tx1, alice);
  22. let res1 = await query(conn, alice, contract, "getBothFoos");
  23. expect(res1.output?.toJSON()).toStrictEqual([
  24. {
  25. "f1": "bar2",
  26. "f2": "0x446f6e277420636f756e7420796f757220636869636b656e73206265666f72652074686579206861746368",
  27. "f3": -102,
  28. "f4": "0xedaeda",
  29. "f5": "You can't have your cake and eat it too",
  30. "f6": { "in1": true, "in2": "There are other fish in the sea" }
  31. },
  32. {
  33. "f1": "bar1",
  34. "f2": "0x",
  35. "f3": 0,
  36. "f4": "0x000000",
  37. "f5": "",
  38. "f6": { "in1": false, "in2": "" }
  39. }
  40. ]);
  41. const arg1 = {
  42. "f1": "bar2",
  43. "f2": "0xb52b073595ccb35eaebb87178227b779",
  44. "f3": -123112321,
  45. "f4": "0x123456",
  46. "f5": "Barking up the wrong tree",
  47. "f6": {
  48. "in1": true, "in2": "Drive someone up the wall"
  49. }
  50. };
  51. gasLimit = await weight(conn, contract, "setFoo2", [arg1, "nah"]);
  52. const tx2 = contract.tx.setFoo2({ gasLimit }, arg1, "nah");
  53. await transaction(tx2, alice);
  54. if (1) {
  55. let res3 = await query(conn, alice, contract, "getFoo", [false]);
  56. expect(res3.output?.toJSON()).toStrictEqual(
  57. {
  58. "f1": "bar2",
  59. "f2": "0xb52b073595ccb35eaebb87178227b779",
  60. "f3": -123112321,
  61. "f4": "0x123456",
  62. "f5": "Barking up the wrong tree",
  63. "f6": { "in1": true, "in2": "nah" }
  64. },
  65. );
  66. }
  67. let res2 = await query(conn, alice, contract, "getBothFoos");
  68. expect(res2.output?.toJSON()).toStrictEqual([
  69. {
  70. "f1": "bar2",
  71. "f2": "0x446f6e277420636f756e7420796f757220636869636b656e73206265666f72652074686579206861746368",
  72. "f3": -102,
  73. "f4": "0xedaeda",
  74. "f5": "You can't have your cake and eat it too",
  75. "f6": { "in1": true, "in2": "There are other fish in the sea" }
  76. },
  77. {
  78. "f1": "bar2",
  79. "f2": "0xb52b073595ccb35eaebb87178227b779",
  80. "f3": -123112321,
  81. "f4": "0x123456",
  82. "f5": "Barking up the wrong tree",
  83. "f6": { "in1": true, "in2": "nah" }
  84. }
  85. ]);
  86. gasLimit = await weight(conn, contract, "deleteFoo", [true]);
  87. const tx3 = contract.tx.deleteFoo({ gasLimit }, true);
  88. await transaction(tx3, alice);
  89. let res3 = await query(conn, alice, contract, "getFoo", [false]);
  90. expect(res3.output?.toJSON()).toStrictEqual(
  91. {
  92. "f1": "bar2",
  93. "f2": "0xb52b073595ccb35eaebb87178227b779",
  94. "f3": -123112321,
  95. "f4": "0x123456",
  96. "f5": "Barking up the wrong tree",
  97. "f6": { "in1": true, "in2": "nah" }
  98. },
  99. );
  100. gasLimit = await weight(conn, contract, "structLiteral");
  101. const tx4 = contract.tx.structLiteral({ gasLimit });
  102. await transaction(tx4, alice);
  103. let res4 = await query(conn, alice, contract, "getFoo", [true]);
  104. expect(res4.output?.toJSON()).toStrictEqual(
  105. {
  106. "f1": "bar4",
  107. "f2": "0x537570657263616c6966726167696c697374696365787069616c69646f63696f7573",
  108. "f3": 64927,
  109. "f4": "0xe282ac",
  110. "f5": "Antidisestablishmentarianism",
  111. "f6": { "in1": true, "in2": "Pseudopseudohypoparathyroidism" },
  112. },
  113. );
  114. });
  115. });