structs.spec.ts 4.6 KB

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