structs.spec.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import expect from 'expect';
  2. import { gasLimit, createConnection, deploy, transaction, aliceKeypair, } 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');
  17. let contract = new ContractPromise(conn, deployed_contract.abi, deployed_contract.address);
  18. const tx1 = contract.tx.setFoo1({ gasLimit });
  19. await transaction(tx1, alice);
  20. let res1 = await contract.query.getBothFoos(alice.address, {});
  21. expect(res1.output?.toJSON()).toStrictEqual([
  22. {
  23. "f1": "bar2",
  24. "f2": "0x446f6e277420636f756e7420796f757220636869636b656e73206265666f72652074686579206861746368",
  25. "f3": -102,
  26. "f4": "0xedaeda",
  27. "f5": "You can't have your cake and eat it too",
  28. "f6": { "in1": true, "in2": "There are other fish in the sea" }
  29. },
  30. {
  31. "f1": "bar1",
  32. "f2": "0x",
  33. "f3": 0,
  34. "f4": "0x000000",
  35. "f5": "",
  36. "f6": { "in1": false, "in2": "" }
  37. }
  38. ]);
  39. const tx2 = contract.tx.setFoo2({ gasLimit },
  40. {
  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. "nah"
  51. );
  52. await transaction(tx2, alice);
  53. if (1) {
  54. let res3 = await contract.query.getFoo(alice.address, {}, 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 contract.query.getBothFoos(alice.address, {});
  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. const tx3 = contract.tx.deleteFoo({ gasLimit }, true);
  86. await transaction(tx3, alice);
  87. let res3 = await contract.query.getFoo(alice.address, {}, false);
  88. expect(res3.output?.toJSON()).toStrictEqual(
  89. {
  90. "f1": "bar2",
  91. "f2": "0xb52b073595ccb35eaebb87178227b779",
  92. "f3": -123112321,
  93. "f4": "0x123456",
  94. "f5": "Barking up the wrong tree",
  95. "f6": { "in1": true, "in2": "nah" }
  96. },
  97. );
  98. const tx4 = contract.tx.structLiteral({ gasLimit });
  99. await transaction(tx4, alice);
  100. let res4 = await contract.query.getFoo(alice.address, {}, true);
  101. expect(res4.output?.toJSON()).toStrictEqual(
  102. {
  103. "f1": "bar4",
  104. "f2": "0x537570657263616c6966726167696c697374696365787069616c69646f63696f7573",
  105. "f3": 64927,
  106. "f4": "0xe282ac",
  107. "f5": "Antidisestablishmentarianism",
  108. "f6": { "in1": true, "in2": "Pseudopseudohypoparathyroidism" },
  109. },
  110. );
  111. });
  112. });