starknet 3.18.2 → 3.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/__tests__/account.test.ts +11 -56
  3. package/__tests__/contract.test.ts +11 -49
  4. package/__tests__/defaultProvider.test.ts +321 -0
  5. package/__tests__/fixtures.ts +32 -11
  6. package/__tests__/rpcProvider.test.ts +17 -0
  7. package/__tests__/sequencerProvider.test.ts +45 -0
  8. package/account/default.d.ts +54 -77
  9. package/account/default.js +271 -596
  10. package/account/index.js +18 -31
  11. package/account/interface.d.ts +66 -95
  12. package/account/interface.js +20 -30
  13. package/constants.d.ts +17 -19
  14. package/constants.js +2038 -2059
  15. package/contract/contractFactory.d.ts +25 -29
  16. package/contract/contractFactory.js +94 -210
  17. package/contract/default.d.ts +117 -146
  18. package/contract/default.js +582 -776
  19. package/contract/index.js +19 -32
  20. package/contract/interface.d.ts +72 -92
  21. package/contract/interface.js +6 -5
  22. package/dist/account/default.d.ts +5 -9
  23. package/dist/account/default.js +35 -169
  24. package/dist/account/interface.d.ts +3 -15
  25. package/dist/contract/contractFactory.js +4 -4
  26. package/dist/contract/default.d.ts +3 -3
  27. package/dist/contract/default.js +3 -2
  28. package/dist/contract/interface.d.ts +2 -2
  29. package/dist/provider/default.d.ts +18 -134
  30. package/dist/provider/default.js +47 -411
  31. package/dist/provider/index.d.ts +2 -0
  32. package/dist/provider/index.js +2 -0
  33. package/dist/provider/interface.d.ts +45 -50
  34. package/dist/provider/rpc.d.ts +57 -0
  35. package/dist/provider/rpc.js +364 -0
  36. package/dist/provider/sequencer.d.ts +66 -0
  37. package/dist/provider/sequencer.js +443 -0
  38. package/dist/types/account.d.ts +2 -3
  39. package/dist/types/api/index.d.ts +16 -0
  40. package/dist/types/api/index.js +18 -0
  41. package/dist/types/api/rpc.d.ts +221 -0
  42. package/dist/types/{api.js → api/rpc.js} +0 -0
  43. package/dist/types/api/sequencer.d.ts +289 -0
  44. package/dist/types/api/sequencer.js +2 -0
  45. package/dist/types/index.d.ts +3 -1
  46. package/dist/types/index.js +15 -1
  47. package/dist/types/lib.d.ts +3 -1
  48. package/dist/types/provider.d.ts +86 -0
  49. package/dist/types/provider.js +2 -0
  50. package/dist/utils/provider.d.ts +4 -0
  51. package/dist/utils/provider.js +38 -0
  52. package/dist/utils/responseParser/index.d.ts +11 -0
  53. package/dist/utils/responseParser/index.js +9 -0
  54. package/dist/utils/responseParser/rpc.d.ts +13 -0
  55. package/dist/utils/responseParser/rpc.js +96 -0
  56. package/dist/utils/responseParser/sequencer.d.ts +13 -0
  57. package/dist/utils/responseParser/sequencer.js +124 -0
  58. package/index.js +42 -75
  59. package/package.json +1 -1
  60. package/provider/default.d.ts +21 -175
  61. package/provider/default.js +139 -704
  62. package/provider/errors.d.ts +4 -4
  63. package/provider/errors.js +30 -40
  64. package/provider/index.d.ts +2 -0
  65. package/provider/index.js +22 -33
  66. package/provider/interface.d.ts +104 -131
  67. package/provider/interface.js +6 -5
  68. package/provider/rpc.d.ts +57 -0
  69. package/provider/rpc.js +364 -0
  70. package/provider/sequencer.d.ts +66 -0
  71. package/provider/sequencer.js +443 -0
  72. package/provider/utils.d.ts +7 -9
  73. package/provider/utils.js +39 -44
  74. package/signer/default.d.ts +5 -9
  75. package/signer/default.js +72 -177
  76. package/signer/index.js +18 -31
  77. package/signer/interface.d.ts +29 -33
  78. package/signer/interface.js +6 -5
  79. package/src/account/default.ts +26 -146
  80. package/src/account/interface.ts +5 -20
  81. package/src/contract/contractFactory.ts +3 -6
  82. package/src/contract/default.ts +6 -4
  83. package/src/contract/interface.ts +2 -2
  84. package/src/provider/default.ts +63 -395
  85. package/src/provider/index.ts +2 -0
  86. package/src/provider/interface.ts +68 -63
  87. package/src/provider/rpc.ts +300 -0
  88. package/src/provider/sequencer.ts +384 -0
  89. package/src/types/account.ts +2 -3
  90. package/src/types/api/index.ts +17 -0
  91. package/src/types/api/rpc.ts +247 -0
  92. package/src/types/api/sequencer.ts +331 -0
  93. package/src/types/index.ts +3 -1
  94. package/src/types/lib.ts +3 -1
  95. package/src/types/provider.ts +108 -0
  96. package/src/utils/provider.ts +28 -0
  97. package/src/utils/responseParser/index.ts +28 -0
  98. package/src/utils/responseParser/rpc.ts +93 -0
  99. package/src/utils/responseParser/sequencer.ts +127 -0
  100. package/types/account.d.ts +5 -7
  101. package/types/account.js +2 -2
  102. package/types/api/index.d.ts +16 -0
  103. package/types/api/index.js +18 -0
  104. package/types/api/rpc.d.ts +221 -0
  105. package/types/api/rpc.js +2 -0
  106. package/types/api/sequencer.d.ts +289 -0
  107. package/types/api/sequencer.js +2 -0
  108. package/types/contract.d.ts +1 -1
  109. package/types/contract.js +2 -2
  110. package/types/index.d.ts +3 -1
  111. package/types/index.js +35 -34
  112. package/types/lib.d.ts +36 -41
  113. package/types/lib.js +2 -2
  114. package/types/provider.d.ts +86 -0
  115. package/types/provider.js +2 -0
  116. package/types/signer.d.ts +2 -2
  117. package/types/signer.js +2 -2
  118. package/utils/address.js +26 -37
  119. package/utils/ellipticCurve.d.ts +1 -6
  120. package/utils/ellipticCurve.js +73 -137
  121. package/utils/encode.js +49 -85
  122. package/utils/hash.d.ts +4 -31
  123. package/utils/hash.js +76 -141
  124. package/utils/json.d.ts +13 -45
  125. package/utils/json.js +15 -22
  126. package/utils/number.d.ts +2 -9
  127. package/utils/number.js +47 -81
  128. package/utils/provider.d.ts +4 -0
  129. package/utils/provider.js +38 -0
  130. package/utils/responseParser/index.d.ts +11 -0
  131. package/utils/responseParser/index.js +9 -0
  132. package/utils/responseParser/rpc.d.ts +13 -0
  133. package/utils/responseParser/rpc.js +96 -0
  134. package/utils/responseParser/sequencer.d.ts +13 -0
  135. package/utils/responseParser/sequencer.js +124 -0
  136. package/utils/shortString.js +13 -21
  137. package/utils/stark.d.ts +0 -1
  138. package/utils/stark.js +59 -93
  139. package/utils/transaction.d.ts +3 -6
  140. package/utils/transaction.js +50 -81
  141. package/utils/typedData/index.d.ts +3 -15
  142. package/utils/typedData/index.js +109 -175
  143. package/utils/typedData/types.d.ts +9 -9
  144. package/utils/typedData/types.js +2 -2
  145. package/utils/typedData/utils.js +6 -6
  146. package/utils/uint256.d.ts +5 -5
  147. package/utils/uint256.js +16 -26
  148. package/www/docs/API/account.md +3 -4
  149. package/www/docs/API/contract.md +2 -2
  150. package/www/docs/API/contractFactory.md +2 -2
  151. package/www/docs/API/provider.md +185 -74
  152. package/www/guides/account.md +1 -8
  153. package/www/guides/erc20.md +3 -0
  154. package/__tests__/provider.test.ts +0 -168
  155. package/dist/types/api.d.ts +0 -261
  156. package/src/types/api.ts +0 -303
  157. package/types/api.d.ts +0 -287
  158. package/types/api.js +0 -2
@@ -1,846 +1,652 @@
1
- 'use strict';
2
- var __assign =
3
- (this && this.__assign) ||
4
- function () {
5
- __assign =
6
- Object.assign ||
7
- function (t) {
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
8
4
  for (var s, i = 1, n = arguments.length; i < n; i++) {
9
- s = arguments[i];
10
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
11
8
  }
12
9
  return t;
13
- };
10
+ };
14
11
  return __assign.apply(this, arguments);
15
- };
16
- var __awaiter =
17
- (this && this.__awaiter) ||
18
- function (thisArg, _arguments, P, generator) {
19
- function adopt(value) {
20
- return value instanceof P
21
- ? value
22
- : new P(function (resolve) {
23
- resolve(value);
24
- });
25
- }
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
26
15
  return new (P || (P = Promise))(function (resolve, reject) {
27
- function fulfilled(value) {
28
- try {
29
- step(generator.next(value));
30
- } catch (e) {
31
- reject(e);
32
- }
33
- }
34
- function rejected(value) {
35
- try {
36
- step(generator['throw'](value));
37
- } catch (e) {
38
- reject(e);
39
- }
40
- }
41
- function step(result) {
42
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
43
- }
44
- step((generator = generator.apply(thisArg, _arguments || [])).next());
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
45
20
  });
46
- };
47
- var __generator =
48
- (this && this.__generator) ||
49
- function (thisArg, body) {
50
- var _ = {
51
- label: 0,
52
- sent: function () {
53
- if (t[0] & 1) throw t[1];
54
- return t[1];
55
- },
56
- trys: [],
57
- ops: [],
58
- },
59
- f,
60
- y,
61
- t,
62
- g;
63
- return (
64
- (g = { next: verb(0), throw: verb(1), return: verb(2) }),
65
- typeof Symbol === 'function' &&
66
- (g[Symbol.iterator] = function () {
67
- return this;
68
- }),
69
- g
70
- );
71
- function verb(n) {
72
- return function (v) {
73
- return step([n, v]);
74
- };
75
- }
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
76
26
  function step(op) {
77
- if (f) throw new TypeError('Generator is already executing.');
78
- while (_)
79
- try {
80
- if (
81
- ((f = 1),
82
- y &&
83
- (t =
84
- op[0] & 2
85
- ? y['return']
86
- : op[0]
87
- ? y['throw'] || ((t = y['return']) && t.call(y), 0)
88
- : y.next) &&
89
- !(t = t.call(y, op[1])).done)
90
- )
91
- return t;
92
- if (((y = 0), t)) op = [op[0] & 2, t.value];
93
- switch (op[0]) {
94
- case 0:
95
- case 1:
96
- t = op;
97
- break;
98
- case 4:
99
- _.label++;
100
- return { value: op[1], done: false };
101
- case 5:
102
- _.label++;
103
- y = op[1];
104
- op = [0];
105
- continue;
106
- case 7:
107
- op = _.ops.pop();
108
- _.trys.pop();
109
- continue;
110
- default:
111
- if (
112
- !((t = _.trys), (t = t.length > 0 && t[t.length - 1])) &&
113
- (op[0] === 6 || op[0] === 2)
114
- ) {
115
- _ = 0;
116
- continue;
117
- }
118
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
119
- _.label = op[1];
120
- break;
121
- }
122
- if (op[0] === 6 && _.label < t[1]) {
123
- _.label = t[1];
124
- t = op;
125
- break;
126
- }
127
- if (t && _.label < t[2]) {
128
- _.label = t[2];
129
- _.ops.push(op);
130
- break;
131
- }
132
- if (t[2]) _.ops.pop();
133
- _.trys.pop();
134
- continue;
135
- }
136
- op = body.call(thisArg, _);
137
- } catch (e) {
138
- op = [6, e];
139
- y = 0;
140
- } finally {
141
- f = t = 0;
142
- }
143
- if (op[0] & 5) throw op[1];
144
- return { value: op[0] ? op[1] : void 0, done: true };
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
145
47
  }
146
- };
147
- var __read =
148
- (this && this.__read) ||
149
- function (o, n) {
150
- var m = typeof Symbol === 'function' && o[Symbol.iterator];
48
+ };
49
+ var __read = (this && this.__read) || function (o, n) {
50
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
151
51
  if (!m) return o;
152
- var i = m.call(o),
153
- r,
154
- ar = [],
155
- e;
52
+ var i = m.call(o), r, ar = [], e;
156
53
  try {
157
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
158
- } catch (error) {
159
- e = { error: error };
160
- } finally {
161
- try {
162
- if (r && !r.done && (m = i['return'])) m.call(i);
163
- } finally {
164
- if (e) throw e.error;
165
- }
54
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
+ }
56
+ catch (error) { e = { error: error }; }
57
+ finally {
58
+ try {
59
+ if (r && !r.done && (m = i["return"])) m.call(i);
60
+ }
61
+ finally { if (e) throw e.error; }
166
62
  }
167
63
  return ar;
168
- };
169
- var __spreadArray =
170
- (this && this.__spreadArray) ||
171
- function (to, from, pack) {
172
- if (pack || arguments.length === 2)
173
- for (var i = 0, l = from.length, ar; i < l; i++) {
64
+ };
65
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
66
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
174
67
  if (ar || !(i in from)) {
175
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
176
- ar[i] = from[i];
68
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
69
+ ar[i] = from[i];
177
70
  }
178
- }
71
+ }
179
72
  return to.concat(ar || Array.prototype.slice.call(from));
180
- };
181
- var __importDefault =
182
- (this && this.__importDefault) ||
183
- function (mod) {
184
- return mod && mod.__esModule ? mod : { default: mod };
185
- };
186
- Object.defineProperty(exports, '__esModule', { value: true });
73
+ };
74
+ var __importDefault = (this && this.__importDefault) || function (mod) {
75
+ return (mod && mod.__esModule) ? mod : { "default": mod };
76
+ };
77
+ Object.defineProperty(exports, "__esModule", { value: true });
187
78
  exports.Contract = void 0;
188
- var bn_js_1 = __importDefault(require('bn.js'));
189
- var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
190
- var provider_1 = require('../provider');
191
- var number_1 = require('../utils/number');
79
+ var bn_js_1 = __importDefault(require("bn.js"));
80
+ var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
81
+ var provider_1 = require("../provider");
82
+ var number_1 = require("../utils/number");
192
83
  function parseFelt(candidate) {
193
- try {
194
- return (0, number_1.toBN)(candidate);
195
- } catch (e) {
196
- throw Error('Couldnt parse felt');
197
- }
84
+ try {
85
+ return (0, number_1.toBN)(candidate);
86
+ }
87
+ catch (e) {
88
+ throw Error('Couldnt parse felt');
89
+ }
198
90
  }
199
91
  /**
200
92
  * Adds call methods to the contract
201
93
  *
202
94
  */
203
95
  function buildCall(contract, functionAbi) {
204
- return function () {
205
- var args = [];
206
- for (var _i = 0; _i < arguments.length; _i++) {
207
- args[_i] = arguments[_i];
208
- }
209
- return __awaiter(this, void 0, void 0, function () {
210
- return __generator(this, function (_a) {
211
- return [2 /*return*/, contract.call(functionAbi.name, args)];
212
- });
213
- });
214
- };
96
+ return function () {
97
+ var args = [];
98
+ for (var _i = 0; _i < arguments.length; _i++) {
99
+ args[_i] = arguments[_i];
100
+ }
101
+ return __awaiter(this, void 0, void 0, function () {
102
+ return __generator(this, function (_a) {
103
+ return [2 /*return*/, contract.call(functionAbi.name, args)];
104
+ });
105
+ });
106
+ };
215
107
  }
216
108
  /**
217
109
  * Adds invoke methods to the contract
218
110
  *
219
111
  */
220
112
  function buildInvoke(contract, functionAbi) {
221
- return function () {
222
- var args = [];
223
- for (var _i = 0; _i < arguments.length; _i++) {
224
- args[_i] = arguments[_i];
225
- }
226
- return __awaiter(this, void 0, void 0, function () {
227
- var inputs, inputsLength, options;
228
- return __generator(this, function (_a) {
229
- inputs = functionAbi.inputs;
230
- inputsLength = inputs.reduce(function (acc, input) {
231
- if (!/_len$/.test(input.name)) {
232
- return acc + 1;
233
- }
234
- return acc;
235
- }, 0);
236
- options = {};
237
- if (inputsLength + 1 === args.length && typeof args[args.length - 1] === 'object') {
238
- Object.assign(options, args.pop());
113
+ return function () {
114
+ var args = [];
115
+ for (var _i = 0; _i < arguments.length; _i++) {
116
+ args[_i] = arguments[_i];
239
117
  }
240
- return [2 /*return*/, contract.invoke(functionAbi.name, args, options)];
241
- });
242
- });
243
- };
118
+ return __awaiter(this, void 0, void 0, function () {
119
+ var inputs, inputsLength, options;
120
+ return __generator(this, function (_a) {
121
+ inputs = functionAbi.inputs;
122
+ inputsLength = inputs.reduce(function (acc, input) {
123
+ if (!/_len$/.test(input.name)) {
124
+ return acc + 1;
125
+ }
126
+ return acc;
127
+ }, 0);
128
+ options = {};
129
+ if (inputsLength + 1 === args.length && typeof args[args.length - 1] === 'object') {
130
+ Object.assign(options, args.pop());
131
+ }
132
+ return [2 /*return*/, contract.invoke(functionAbi.name, args, options)];
133
+ });
134
+ });
135
+ };
244
136
  }
245
137
  /**
246
138
  * Adds call/invoke methods to the contract
247
139
  *
248
140
  */
249
141
  function buildDefault(contract, functionAbi) {
250
- if (functionAbi.stateMutability === 'view') {
251
- return buildCall(contract, functionAbi);
252
- }
253
- return buildInvoke(contract, functionAbi);
142
+ if (functionAbi.stateMutability === 'view') {
143
+ return buildCall(contract, functionAbi);
144
+ }
145
+ return buildInvoke(contract, functionAbi);
254
146
  }
255
147
  /**
256
148
  * Adds populate for methods to the contract
257
149
  *
258
150
  */
259
151
  function buildPopulate(contract, functionAbi) {
260
- return function () {
261
- var args = [];
262
- for (var _i = 0; _i < arguments.length; _i++) {
263
- args[_i] = arguments[_i];
264
- }
265
- return contract.populate(functionAbi.name, args);
266
- };
152
+ return function () {
153
+ var args = [];
154
+ for (var _i = 0; _i < arguments.length; _i++) {
155
+ args[_i] = arguments[_i];
156
+ }
157
+ return contract.populate(functionAbi.name, args);
158
+ };
267
159
  }
268
160
  /**
269
161
  * Adds estimateFee for methods to the contract
270
162
  *
271
163
  */
272
164
  function buildEstimate(contract, functionAbi) {
273
- return function () {
274
- var args = [];
275
- for (var _i = 0; _i < arguments.length; _i++) {
276
- args[_i] = arguments[_i];
277
- }
278
- return contract.estimate(functionAbi.name, args);
279
- };
165
+ return function () {
166
+ var args = [];
167
+ for (var _i = 0; _i < arguments.length; _i++) {
168
+ args[_i] = arguments[_i];
169
+ }
170
+ return contract.estimate(functionAbi.name, args);
171
+ };
280
172
  }
281
173
  var Contract = /** @class */ (function () {
282
- /**
283
- * Contract class to handle contract methods
284
- *
285
- * @param abi - Abi of the contract object
286
- * @param address (optional) - address to connect to
287
- * @param providerOrAccount (optional) - Provider or Account to attach to
288
- */
289
- function Contract(abi, address, providerOrAccount) {
290
- if (providerOrAccount === void 0) {
291
- providerOrAccount = provider_1.defaultProvider;
292
- }
293
- var _this = this;
294
- this.address = address;
295
- this.providerOrAccount = providerOrAccount;
296
- this.abi = abi;
297
- this.structs = abi
298
- .filter(function (abiEntry) {
299
- return abiEntry.type === 'struct';
300
- })
301
- .reduce(function (acc, abiEntry) {
302
- var _a;
303
- return __assign(__assign({}, acc), ((_a = {}), (_a[abiEntry.name] = abiEntry), _a));
304
- }, {});
305
- Object.defineProperty(this, 'functions', {
306
- enumerable: true,
307
- value: {},
308
- writable: false,
309
- });
310
- Object.defineProperty(this, 'callStatic', {
311
- enumerable: true,
312
- value: {},
313
- writable: false,
314
- });
315
- Object.defineProperty(this, 'populateTransaction', {
316
- enumerable: true,
317
- value: {},
318
- writable: false,
319
- });
320
- Object.defineProperty(this, 'estimateFee', {
321
- enumerable: true,
322
- value: {},
323
- writable: false,
324
- });
325
- this.abi.forEach(function (abiElement) {
326
- if (abiElement.type !== 'function') {
327
- return;
328
- }
329
- var signature = abiElement.name;
330
- if (!_this[signature]) {
331
- Object.defineProperty(_this, signature, {
332
- enumerable: true,
333
- value: buildDefault(_this, abiElement),
334
- writable: false,
174
+ /**
175
+ * Contract class to handle contract methods
176
+ *
177
+ * @param abi - Abi of the contract object
178
+ * @param address (optional) - address to connect to
179
+ * @param providerOrAccount (optional) - Provider or Account to attach to
180
+ */
181
+ function Contract(abi, address, providerOrAccount) {
182
+ if (providerOrAccount === void 0) { providerOrAccount = provider_1.defaultProvider; }
183
+ var _this = this;
184
+ this.address = address;
185
+ this.providerOrAccount = providerOrAccount;
186
+ this.abi = abi;
187
+ this.structs = abi
188
+ .filter(function (abiEntry) { return abiEntry.type === 'struct'; })
189
+ .reduce(function (acc, abiEntry) {
190
+ var _a;
191
+ return (__assign(__assign({}, acc), (_a = {}, _a[abiEntry.name] = abiEntry, _a)));
192
+ }, {});
193
+ Object.defineProperty(this, 'functions', {
194
+ enumerable: true,
195
+ value: {},
196
+ writable: false,
335
197
  });
336
- }
337
- if (!_this.functions[signature]) {
338
- Object.defineProperty(_this.functions, signature, {
339
- enumerable: true,
340
- value: buildDefault(_this, abiElement),
341
- writable: false,
198
+ Object.defineProperty(this, 'callStatic', {
199
+ enumerable: true,
200
+ value: {},
201
+ writable: false,
342
202
  });
343
- }
344
- if (!_this.callStatic[signature]) {
345
- Object.defineProperty(_this.callStatic, signature, {
346
- enumerable: true,
347
- value: buildCall(_this, abiElement),
348
- writable: false,
203
+ Object.defineProperty(this, 'populateTransaction', {
204
+ enumerable: true,
205
+ value: {},
206
+ writable: false,
349
207
  });
350
- }
351
- if (!_this.populateTransaction[signature]) {
352
- Object.defineProperty(_this.populateTransaction, signature, {
353
- enumerable: true,
354
- value: buildPopulate(_this, abiElement),
355
- writable: false,
208
+ Object.defineProperty(this, 'estimateFee', {
209
+ enumerable: true,
210
+ value: {},
211
+ writable: false,
356
212
  });
357
- }
358
- if (!_this.estimateFee[signature]) {
359
- Object.defineProperty(_this.estimateFee, signature, {
360
- enumerable: true,
361
- value: buildEstimate(_this, abiElement),
362
- writable: false,
213
+ this.abi.forEach(function (abiElement) {
214
+ if (abiElement.type !== 'function') {
215
+ return;
216
+ }
217
+ var signature = abiElement.name;
218
+ if (!_this[signature]) {
219
+ Object.defineProperty(_this, signature, {
220
+ enumerable: true,
221
+ value: buildDefault(_this, abiElement),
222
+ writable: false,
223
+ });
224
+ }
225
+ if (!_this.functions[signature]) {
226
+ Object.defineProperty(_this.functions, signature, {
227
+ enumerable: true,
228
+ value: buildDefault(_this, abiElement),
229
+ writable: false,
230
+ });
231
+ }
232
+ if (!_this.callStatic[signature]) {
233
+ Object.defineProperty(_this.callStatic, signature, {
234
+ enumerable: true,
235
+ value: buildCall(_this, abiElement),
236
+ writable: false,
237
+ });
238
+ }
239
+ if (!_this.populateTransaction[signature]) {
240
+ Object.defineProperty(_this.populateTransaction, signature, {
241
+ enumerable: true,
242
+ value: buildPopulate(_this, abiElement),
243
+ writable: false,
244
+ });
245
+ }
246
+ if (!_this.estimateFee[signature]) {
247
+ Object.defineProperty(_this.estimateFee, signature, {
248
+ enumerable: true,
249
+ value: buildEstimate(_this, abiElement),
250
+ writable: false,
251
+ });
252
+ }
363
253
  });
364
- }
365
- });
366
- }
367
- /**
368
- * Saves the address of the contract deployed on network that will be used for interaction
369
- *
370
- * @param address - address of the contract
371
- */
372
- Contract.prototype.attach = function (address) {
373
- this.address = address;
374
- };
375
- /**
376
- * Attaches to new Provider or Account
377
- *
378
- * @param providerOrAccount - new Provider or Account to attach to
379
- */
380
- Contract.prototype.connect = function (providerOrAccount) {
381
- this.providerOrAccount = providerOrAccount;
382
- };
383
- /**
384
- * Resolves when contract is deployed on the network or when no deployment transaction is found
385
- *
386
- * @returns Promise that resolves when contract is deployed on the network or when no deployment transaction is found
387
- * @throws When deployment fails
388
- */
389
- Contract.prototype.deployed = function () {
390
- return __awaiter(this, void 0, void 0, function () {
391
- return __generator(this, function (_a) {
392
- switch (_a.label) {
393
- case 0:
394
- if (!this.deployTransactionHash) return [3 /*break*/, 2];
395
- return [
396
- 4 /*yield*/,
397
- this.providerOrAccount.waitForTransaction(this.deployTransactionHash),
398
- ];
399
- case 1:
400
- _a.sent();
401
- this.deployTransactionHash = undefined;
402
- _a.label = 2;
403
- case 2:
404
- return [2 /*return*/, this];
405
- }
406
- });
407
- });
408
- };
409
- /**
410
- * Validates if all arguments that are passed to the method are corresponding to the ones in the abi
411
- *
412
- * @param type - type of the method
413
- * @param method - name of the method
414
- * @param args - arguments that are passed to the method
415
- */
416
- Contract.prototype.validateMethodAndArgs = function (type, method, args) {
417
- var _this = this;
418
- if (args === void 0) {
419
- args = [];
420
254
  }
421
- // ensure provided method exists
422
- var invokeableFunctionNames = this.abi
423
- .filter(function (abi) {
424
- if (abi.type !== 'function') return false;
425
- var isView = abi.stateMutability === 'view';
426
- return type === 'INVOKE' ? !isView : isView;
427
- })
428
- .map(function (abi) {
429
- return abi.name;
430
- });
431
- (0, minimalistic_assert_1.default)(
432
- invokeableFunctionNames.includes(method),
433
- ''.concat(type === 'INVOKE' ? 'invokeable' : 'viewable', ' method not found in abi')
434
- );
435
- // ensure args match abi type
436
- var methodAbi = this.abi.find(function (abi) {
437
- return abi.name === method && abi.type === 'function';
438
- });
439
- var argPosition = 0;
440
- methodAbi.inputs.forEach(function (input) {
441
- if (/_len$/.test(input.name)) {
442
- return;
443
- }
444
- if (input.type === 'felt') {
445
- (0, minimalistic_assert_1.default)(
446
- typeof args[argPosition] === 'string' ||
447
- typeof args[argPosition] === 'number' ||
448
- args[argPosition] instanceof bn_js_1.default,
449
- 'arg '.concat(input.name, ' should be a felt (string, number, BigNumber)')
450
- );
451
- argPosition += 1;
452
- } else if (input.type in _this.structs && typeof args[argPosition] === 'object') {
453
- if (Array.isArray(args[argPosition])) {
454
- var structMembersLength = _this.calculateStructMembers(input.type);
455
- (0, minimalistic_assert_1.default)(
456
- args[argPosition].length === structMembersLength,
457
- 'arg should be of length '.concat(structMembersLength)
458
- );
459
- } else {
460
- _this.structs[input.type].members.forEach(function (_a) {
461
- var name = _a.name;
462
- (0,
463
- minimalistic_assert_1.default)(Object.keys(args[argPosition]).includes(name), 'arg should have a property '.concat(name));
464
- });
465
- }
466
- argPosition += 1;
467
- } else {
468
- (0, minimalistic_assert_1.default)(
469
- Array.isArray(args[argPosition]),
470
- 'arg '.concat(input.name, ' should be an Array')
471
- );
472
- if (input.type === 'felt*') {
473
- args[argPosition].forEach(function (felt) {
474
- (0,
475
- minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, 'arg '.concat(input.name, ' should be an array of string, number or BigNumber'));
476
- });
477
- argPosition += 1;
478
- } else if (/\(felt/.test(input.type)) {
479
- var tupleLength = input.type.split(',').length;
480
- (0, minimalistic_assert_1.default)(
481
- args[argPosition].length === tupleLength,
482
- 'arg '.concat(input.name, ' should have ').concat(tupleLength, ' elements in tuple')
483
- );
484
- args[argPosition].forEach(function (felt) {
485
- (0,
486
- minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, 'arg '.concat(input.name, ' should be an array of string, number or BigNumber'));
487
- });
488
- argPosition += 1;
489
- } else {
490
- var arrayType_1 = input.type.replace('*', '');
491
- args[argPosition].forEach(function (struct) {
492
- _this.structs[arrayType_1].members.forEach(function (_a) {
493
- var name = _a.name;
494
- if (Array.isArray(struct)) {
495
- var structMembersLength = _this.calculateStructMembers(arrayType_1);
496
- (0, minimalistic_assert_1.default)(
497
- struct.length === structMembersLength,
498
- 'arg should be of length '.concat(structMembersLength)
499
- );
500
- } else {
501
- (0, minimalistic_assert_1.default)(
502
- Object.keys(struct).includes(name),
503
- 'arg '.concat(input.name, ' should be an array of ').concat(arrayType_1)
504
- );
505
- }
255
+ /**
256
+ * Saves the address of the contract deployed on network that will be used for interaction
257
+ *
258
+ * @param address - address of the contract
259
+ */
260
+ Contract.prototype.attach = function (address) {
261
+ this.address = address;
262
+ };
263
+ /**
264
+ * Attaches to new Provider or Account
265
+ *
266
+ * @param providerOrAccount - new Provider or Account to attach to
267
+ */
268
+ Contract.prototype.connect = function (providerOrAccount) {
269
+ this.providerOrAccount = providerOrAccount;
270
+ };
271
+ /**
272
+ * Resolves when contract is deployed on the network or when no deployment transaction is found
273
+ *
274
+ * @returns Promise that resolves when contract is deployed on the network or when no deployment transaction is found
275
+ * @throws When deployment fails
276
+ */
277
+ Contract.prototype.deployed = function () {
278
+ return __awaiter(this, void 0, void 0, function () {
279
+ return __generator(this, function (_a) {
280
+ switch (_a.label) {
281
+ case 0:
282
+ if (!this.deployTransactionHash) return [3 /*break*/, 2];
283
+ return [4 /*yield*/, this.providerOrAccount.waitForTransaction(this.deployTransactionHash)];
284
+ case 1:
285
+ _a.sent();
286
+ this.deployTransactionHash = undefined;
287
+ _a.label = 2;
288
+ case 2: return [2 /*return*/, this];
289
+ }
506
290
  });
507
- });
508
- argPosition += 1;
509
- }
510
- }
511
- });
512
- };
513
- /**
514
- * Deep parse of the object that has been passed to the method
515
- *
516
- * @param struct - struct that needs to be calculated
517
- * @return {number} - number of members for the given struct
518
- */
519
- Contract.prototype.calculateStructMembers = function (struct) {
520
- var _this = this;
521
- return this.structs[struct].members.reduce(function (acc, member) {
522
- if (member.type === 'felt') {
523
- return acc + 1;
524
- }
525
- return acc + _this.calculateStructMembers(member.type);
526
- }, 0);
527
- };
528
- /**
529
- * Deep parse of the object that has been passed to the method
530
- *
531
- * @param element - element that needs to be parsed
532
- * @param type - name of the method
533
- * @return {string | string[]} - parsed arguments in format that contract is expecting
534
- */
535
- Contract.prototype.parseCalldataValue = function (element, type) {
536
- var _this = this;
537
- if (element === undefined) {
538
- throw Error('Missing element in calldata');
539
- }
540
- if (Array.isArray(element)) {
541
- var structMemberNum = this.calculateStructMembers(type);
542
- if (element.length !== structMemberNum) {
543
- throw Error('Missing element in calldata');
544
- }
545
- return element.map(function (el) {
546
- return (0, number_1.toFelt)(el);
547
- });
548
- }
549
- // checking if the passed element is struct or element in struct
550
- if (this.structs[type] && this.structs[type].members.length) {
551
- // going through all the members of the struct and parsing the value
552
- return this.structs[type].members.reduce(function (acc, member) {
553
- // if the member of the struct is another struct this will return array of the felts if not it will be single felt
554
- // TODO: refactor types so member name can be used as keyof ParsedStruct
555
- /* @ts-ignore */
556
- var parsedData = _this.parseCalldataValue(element[member.name], member.type);
557
- if (typeof parsedData === 'string') {
558
- acc.push(parsedData);
559
- } else {
560
- acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
291
+ });
292
+ };
293
+ /**
294
+ * Validates if all arguments that are passed to the method are corresponding to the ones in the abi
295
+ *
296
+ * @param type - type of the method
297
+ * @param method - name of the method
298
+ * @param args - arguments that are passed to the method
299
+ */
300
+ Contract.prototype.validateMethodAndArgs = function (type, method, args) {
301
+ var _this = this;
302
+ if (args === void 0) { args = []; }
303
+ // ensure provided method exists
304
+ var invokeableFunctionNames = this.abi
305
+ .filter(function (abi) {
306
+ if (abi.type !== 'function')
307
+ return false;
308
+ var isView = abi.stateMutability === 'view';
309
+ return type === 'INVOKE' ? !isView : isView;
310
+ })
311
+ .map(function (abi) { return abi.name; });
312
+ (0, minimalistic_assert_1.default)(invokeableFunctionNames.includes(method), "".concat(type === 'INVOKE' ? 'invokeable' : 'viewable', " method not found in abi"));
313
+ // ensure args match abi type
314
+ var methodAbi = this.abi.find(function (abi) { return abi.name === method && abi.type === 'function'; });
315
+ var argPosition = 0;
316
+ methodAbi.inputs.forEach(function (input) {
317
+ if (/_len$/.test(input.name)) {
318
+ return;
319
+ }
320
+ if (input.type === 'felt') {
321
+ (0, minimalistic_assert_1.default)(typeof args[argPosition] === 'string' ||
322
+ typeof args[argPosition] === 'number' ||
323
+ args[argPosition] instanceof bn_js_1.default, "arg ".concat(input.name, " should be a felt (string, number, BigNumber)"));
324
+ argPosition += 1;
325
+ }
326
+ else if (input.type in _this.structs && typeof args[argPosition] === 'object') {
327
+ if (Array.isArray(args[argPosition])) {
328
+ var structMembersLength = _this.calculateStructMembers(input.type);
329
+ (0, minimalistic_assert_1.default)(args[argPosition].length === structMembersLength, "arg should be of length ".concat(structMembersLength));
330
+ }
331
+ else {
332
+ _this.structs[input.type].members.forEach(function (_a) {
333
+ var name = _a.name;
334
+ (0, minimalistic_assert_1.default)(Object.keys(args[argPosition]).includes(name), "arg should have a property ".concat(name));
335
+ });
336
+ }
337
+ argPosition += 1;
338
+ }
339
+ else {
340
+ (0, minimalistic_assert_1.default)(Array.isArray(args[argPosition]), "arg ".concat(input.name, " should be an Array"));
341
+ if (input.type === 'felt*') {
342
+ args[argPosition].forEach(function (felt) {
343
+ (0, minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, "arg ".concat(input.name, " should be an array of string, number or BigNumber"));
344
+ });
345
+ argPosition += 1;
346
+ }
347
+ else if (/\(felt/.test(input.type)) {
348
+ var tupleLength = input.type.split(',').length;
349
+ (0, minimalistic_assert_1.default)(args[argPosition].length === tupleLength, "arg ".concat(input.name, " should have ").concat(tupleLength, " elements in tuple"));
350
+ args[argPosition].forEach(function (felt) {
351
+ (0, minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, "arg ".concat(input.name, " should be an array of string, number or BigNumber"));
352
+ });
353
+ argPosition += 1;
354
+ }
355
+ else {
356
+ var arrayType_1 = input.type.replace('*', '');
357
+ args[argPosition].forEach(function (struct) {
358
+ _this.structs[arrayType_1].members.forEach(function (_a) {
359
+ var name = _a.name;
360
+ if (Array.isArray(struct)) {
361
+ var structMembersLength = _this.calculateStructMembers(arrayType_1);
362
+ (0, minimalistic_assert_1.default)(struct.length === structMembersLength, "arg should be of length ".concat(structMembersLength));
363
+ }
364
+ else {
365
+ (0, minimalistic_assert_1.default)(Object.keys(struct).includes(name), "arg ".concat(input.name, " should be an array of ").concat(arrayType_1));
366
+ }
367
+ });
368
+ });
369
+ argPosition += 1;
370
+ }
371
+ }
372
+ });
373
+ };
374
+ /**
375
+ * Deep parse of the object that has been passed to the method
376
+ *
377
+ * @param struct - struct that needs to be calculated
378
+ * @return {number} - number of members for the given struct
379
+ */
380
+ Contract.prototype.calculateStructMembers = function (struct) {
381
+ var _this = this;
382
+ return this.structs[struct].members.reduce(function (acc, member) {
383
+ if (member.type === 'felt') {
384
+ return acc + 1;
385
+ }
386
+ return acc + _this.calculateStructMembers(member.type);
387
+ }, 0);
388
+ };
389
+ /**
390
+ * Deep parse of the object that has been passed to the method
391
+ *
392
+ * @param element - element that needs to be parsed
393
+ * @param type - name of the method
394
+ * @return {string | string[]} - parsed arguments in format that contract is expecting
395
+ */
396
+ Contract.prototype.parseCalldataValue = function (element, type) {
397
+ var _this = this;
398
+ if (element === undefined) {
399
+ throw Error('Missing element in calldata');
561
400
  }
562
- return acc;
563
- }, []);
564
- }
565
- return (0, number_1.toFelt)(element);
566
- };
567
- /**
568
- * Parse of the response elements that are converted to Object (Struct) by using the abi
569
- *
570
- * @param responseIterator - iterator of the response
571
- * @param type - type of the struct
572
- * @return {BigNumberish | ParsedStruct} - parsed arguments in format that contract is expecting
573
- */
574
- Contract.prototype.parseResponseStruct = function (responseIterator, type) {
575
- var _this = this;
576
- // check the type of current element
577
- if (type in this.structs && this.structs[type]) {
578
- return this.structs[type].members.reduce(function (acc, el) {
579
- // parse each member of the struct (member can felt or nested struct)
580
- acc[el.name] = _this.parseResponseStruct(responseIterator, el.type);
581
- return acc;
582
- }, {});
583
- }
584
- return parseFelt(responseIterator.next().value);
585
- };
586
- /**
587
- * Parse one field of the calldata by using input field from the abi for that method
588
- *
589
- * @param args - value of the field
590
- * @param input - input(field) information from the abi that will be used to parse the data
591
- * @return {string | string[]} - parsed arguments in format that contract is expecting
592
- */
593
- Contract.prototype.parseCalldataField = function (argsIterator, input) {
594
- var _this = this;
595
- var name = input.name,
596
- type = input.type;
597
- var value = argsIterator.next().value;
598
- var parsedCalldata = [];
599
- switch (true) {
600
- case /\*/.test(type):
601
- if (Array.isArray(value)) {
602
- parsedCalldata.push((0, number_1.toFelt)(value.length));
603
- return value.reduce(function (acc, el) {
604
- if (/felt/.test(type)) {
605
- acc.push((0, number_1.toFelt)(el));
606
- } else {
607
- acc.push.apply(
608
- acc,
609
- __spreadArray(
610
- [],
611
- __read(_this.parseCalldataValue(el, type.replace('*', ''))),
612
- false
613
- )
614
- );
401
+ if (Array.isArray(element)) {
402
+ var structMemberNum = this.calculateStructMembers(type);
403
+ if (element.length !== structMemberNum) {
404
+ throw Error('Missing element in calldata');
615
405
  }
616
- return acc;
617
- }, parsedCalldata);
406
+ return element.map(function (el) { return (0, number_1.toFelt)(el); });
618
407
  }
619
- throw Error('Expected '.concat(name, ' to be array'));
620
- case type in this.structs:
621
- return this.parseCalldataValue(value, type);
622
- case /\(felt/.test(type):
623
- if (Array.isArray(value)) {
624
- return value.map(function (el) {
625
- return (0, number_1.toFelt)(el);
626
- });
408
+ // checking if the passed element is struct or element in struct
409
+ if (this.structs[type] && this.structs[type].members.length) {
410
+ // going through all the members of the struct and parsing the value
411
+ return this.structs[type].members.reduce(function (acc, member) {
412
+ // if the member of the struct is another struct this will return array of the felts if not it will be single felt
413
+ // TODO: refactor types so member name can be used as keyof ParsedStruct
414
+ /* @ts-ignore */
415
+ var parsedData = _this.parseCalldataValue(element[member.name], member.type);
416
+ if (typeof parsedData === 'string') {
417
+ acc.push(parsedData);
418
+ }
419
+ else {
420
+ acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
421
+ }
422
+ return acc;
423
+ }, []);
627
424
  }
628
- throw Error('Expected '.concat(name, ' to be array'));
629
- default:
630
- return (0, number_1.toFelt)(value);
631
- }
632
- };
633
- /**
634
- * Parse the calldata by using input fields from the abi for that method
635
- *
636
- * @param args - arguments passed the the method
637
- * @param inputs - list of inputs(fields) that are in the abi
638
- * @return {Calldata} - parsed arguments in format that contract is expecting
639
- */
640
- Contract.prototype.compileCalldata = function (args, inputs) {
641
- var _this = this;
642
- var argsIterator = args[Symbol.iterator]();
643
- return inputs.reduce(function (acc, input) {
644
- if (/_len$/.test(input.name)) {
645
- return acc;
646
- }
647
- var parsedData = _this.parseCalldataField(argsIterator, input);
648
- if (Array.isArray(parsedData)) {
649
- acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
650
- } else {
651
- acc.push(parsedData);
652
- }
653
- return acc;
654
- }, []);
655
- };
656
- /**
657
- * Parse elements of the response and structuring them into one field by using output property from the abi for that method
658
- *
659
- * @param responseIterator - iterator of the response
660
- * @param output - output(field) information from the abi that will be used to parse the data
661
- * @return - parsed response corresponding to the abi structure of the field
662
- */
663
- Contract.prototype.parseResponseField = function (responseIterator, output, parsedResult) {
664
- var name = output.name,
665
- type = output.type;
666
- var parsedDataArr = [];
667
- switch (true) {
668
- case /_len$/.test(name):
669
- return parseFelt(responseIterator.next().value).toNumber();
670
- case /\(felt/.test(type):
671
- return type.split(',').reduce(function (acc) {
672
- acc.push(parseFelt(responseIterator.next().value));
673
- return acc;
674
- }, []);
675
- case /\*/.test(type):
676
- if (parsedResult && parsedResult[''.concat(name, '_len')]) {
677
- var arrLen = parsedResult[''.concat(name, '_len')];
678
- while (parsedDataArr.length < arrLen) {
679
- parsedDataArr.push(
680
- this.parseResponseStruct(responseIterator, output.type.replace('*', ''))
681
- );
682
- }
425
+ return (0, number_1.toFelt)(element);
426
+ };
427
+ /**
428
+ * Parse of the response elements that are converted to Object (Struct) by using the abi
429
+ *
430
+ * @param responseIterator - iterator of the response
431
+ * @param type - type of the struct
432
+ * @return {BigNumberish | ParsedStruct} - parsed arguments in format that contract is expecting
433
+ */
434
+ Contract.prototype.parseResponseStruct = function (responseIterator, type) {
435
+ var _this = this;
436
+ // check the type of current element
437
+ if (type in this.structs && this.structs[type]) {
438
+ return this.structs[type].members.reduce(function (acc, el) {
439
+ // parse each member of the struct (member can felt or nested struct)
440
+ acc[el.name] = _this.parseResponseStruct(responseIterator, el.type);
441
+ return acc;
442
+ }, {});
683
443
  }
684
- return parsedDataArr;
685
- case type in this.structs:
686
- return this.parseResponseStruct(responseIterator, type);
687
- default:
688
444
  return parseFelt(responseIterator.next().value);
689
- }
690
- };
691
- /**
692
- * Parse elements of the response array and structuring them into response object
693
- *
694
- * @param method - method name
695
- * @param response - response from the method
696
- * @return - parsed response corresponding to the abi
697
- */
698
- Contract.prototype.parseResponse = function (method, response) {
699
- var _this = this;
700
- var outputs = this.abi.find(function (abi) {
701
- return abi.name === method;
702
- }).outputs;
703
- var responseIterator = response.flat()[Symbol.iterator]();
704
- var resultObject = outputs.flat().reduce(function (acc, output) {
705
- acc[output.name] = _this.parseResponseField(responseIterator, output, acc);
706
- if (acc[output.name] && acc[''.concat(output.name, '_len')]) {
707
- delete acc[''.concat(output.name, '_len')];
708
- }
709
- return acc;
710
- }, {});
711
- return Object.entries(resultObject).reduce(function (acc, _a) {
712
- var _b = __read(_a, 2),
713
- key = _b[0],
714
- value = _b[1];
715
- acc.push(value);
716
- acc[key] = value;
717
- return acc;
718
- }, []);
719
- };
720
- Contract.prototype.invoke = function (method, args, options) {
721
- if (args === void 0) {
722
- args = [];
723
- }
724
- if (options === void 0) {
725
- options = {};
726
- }
727
- // ensure contract is connected
728
- (0, minimalistic_assert_1.default)(
729
- this.address !== null,
730
- 'contract isnt connected to an address'
731
- );
732
- // validate method and args
733
- this.validateMethodAndArgs('INVOKE', method, args);
734
- var inputs = this.abi.find(function (abi) {
735
- return abi.name === method;
736
- }).inputs;
737
- var inputsLength = inputs.reduce(function (acc, input) {
738
- if (!/_len$/.test(input.name)) {
739
- return acc + 1;
740
- }
741
- return acc;
742
- }, 0);
743
- if (args.length !== inputsLength) {
744
- throw Error(
745
- 'Invalid number of arguments, expected '
746
- .concat(inputsLength, ' arguments, but got ')
747
- .concat(args.length)
748
- );
749
- }
750
- // compile calldata
751
- var calldata = this.compileCalldata(args, inputs);
752
- var invocation = {
753
- contractAddress: this.address,
754
- calldata: calldata,
755
- entrypoint: method,
756
445
  };
757
- if ('execute' in this.providerOrAccount) {
758
- return this.providerOrAccount.execute(invocation, undefined, {
759
- maxFee: options.maxFee,
760
- nonce: options.nonce,
761
- });
762
- }
763
- return this.providerOrAccount.invokeFunction(
764
- __assign(__assign({}, invocation), { signature: options.signature || [] })
765
- );
766
- };
767
- Contract.prototype.call = function (method, args, _a) {
768
- if (args === void 0) {
769
- args = [];
770
- }
771
- var _b = _a === void 0 ? {} : _a,
772
- _c = _b.blockIdentifier,
773
- blockIdentifier = _c === void 0 ? 'pending' : _c;
774
- return __awaiter(this, void 0, void 0, function () {
775
- var inputs, calldata;
776
- var _this = this;
777
- return __generator(this, function (_d) {
778
- // ensure contract is connected
779
- (0,
780
- minimalistic_assert_1.default)(this.address !== null, 'contract isnt connected to an address');
781
- // validate method and args
782
- this.validateMethodAndArgs('CALL', method, args);
783
- inputs = this.abi.find(function (abi) {
784
- return abi.name === method;
785
- }).inputs;
786
- calldata = this.compileCalldata(args, inputs);
787
- return [
788
- 2 /*return*/,
789
- this.providerOrAccount
790
- .callContract(
791
- {
792
- contractAddress: this.address,
793
- calldata: calldata,
794
- entrypoint: method,
795
- },
796
- { blockIdentifier: blockIdentifier }
797
- )
798
- .then(function (x) {
799
- return _this.parseResponse(method, x.result);
800
- }),
801
- ];
802
- });
803
- });
804
- };
805
- Contract.prototype.estimate = function (method, args) {
806
- if (args === void 0) {
807
- args = [];
808
- }
809
- return __awaiter(this, void 0, void 0, function () {
810
- var invocation;
811
- var _a;
812
- return __generator(this, function (_b) {
813
- // TODO; remove error as soon as estimate fees are supported
446
+ /**
447
+ * Parse one field of the calldata by using input field from the abi for that method
448
+ *
449
+ * @param args - value of the field
450
+ * @param input - input(field) information from the abi that will be used to parse the data
451
+ * @return {string | string[]} - parsed arguments in format that contract is expecting
452
+ */
453
+ Contract.prototype.parseCalldataField = function (argsIterator, input) {
454
+ var _this = this;
455
+ var name = input.name, type = input.type;
456
+ var value = argsIterator.next().value;
457
+ var parsedCalldata = [];
458
+ switch (true) {
459
+ case /\*/.test(type):
460
+ if (Array.isArray(value)) {
461
+ parsedCalldata.push((0, number_1.toFelt)(value.length));
462
+ return value.reduce(function (acc, el) {
463
+ if (/felt/.test(type)) {
464
+ acc.push((0, number_1.toFelt)(el));
465
+ }
466
+ else {
467
+ acc.push.apply(acc, __spreadArray([], __read(_this.parseCalldataValue(el, type.replace('*', ''))), false));
468
+ }
469
+ return acc;
470
+ }, parsedCalldata);
471
+ }
472
+ throw Error("Expected ".concat(name, " to be array"));
473
+ case type in this.structs:
474
+ return this.parseCalldataValue(value, type);
475
+ case /\(felt/.test(type):
476
+ if (Array.isArray(value)) {
477
+ return value.map(function (el) { return (0, number_1.toFelt)(el); });
478
+ }
479
+ throw Error("Expected ".concat(name, " to be array"));
480
+ default:
481
+ return (0, number_1.toFelt)(value);
482
+ }
483
+ };
484
+ /**
485
+ * Parse the calldata by using input fields from the abi for that method
486
+ *
487
+ * @param args - arguments passed the the method
488
+ * @param inputs - list of inputs(fields) that are in the abi
489
+ * @return {Calldata} - parsed arguments in format that contract is expecting
490
+ */
491
+ Contract.prototype.compileCalldata = function (args, inputs) {
492
+ var _this = this;
493
+ var argsIterator = args[Symbol.iterator]();
494
+ return inputs.reduce(function (acc, input) {
495
+ if (/_len$/.test(input.name)) {
496
+ return acc;
497
+ }
498
+ var parsedData = _this.parseCalldataField(argsIterator, input);
499
+ if (Array.isArray(parsedData)) {
500
+ acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
501
+ }
502
+ else {
503
+ acc.push(parsedData);
504
+ }
505
+ return acc;
506
+ }, []);
507
+ };
508
+ /**
509
+ * Parse elements of the response and structuring them into one field by using output property from the abi for that method
510
+ *
511
+ * @param responseIterator - iterator of the response
512
+ * @param output - output(field) information from the abi that will be used to parse the data
513
+ * @return - parsed response corresponding to the abi structure of the field
514
+ */
515
+ Contract.prototype.parseResponseField = function (responseIterator, output, parsedResult) {
516
+ var name = output.name, type = output.type;
517
+ var parsedDataArr = [];
518
+ switch (true) {
519
+ case /_len$/.test(name):
520
+ return parseFelt(responseIterator.next().value).toNumber();
521
+ case /\(felt/.test(type):
522
+ return type.split(',').reduce(function (acc) {
523
+ acc.push(parseFelt(responseIterator.next().value));
524
+ return acc;
525
+ }, []);
526
+ case /\*/.test(type):
527
+ if (parsedResult && parsedResult["".concat(name, "_len")]) {
528
+ var arrLen = parsedResult["".concat(name, "_len")];
529
+ while (parsedDataArr.length < arrLen) {
530
+ parsedDataArr.push(this.parseResponseStruct(responseIterator, output.type.replace('*', '')));
531
+ }
532
+ }
533
+ return parsedDataArr;
534
+ case type in this.structs:
535
+ return this.parseResponseStruct(responseIterator, type);
536
+ default:
537
+ return parseFelt(responseIterator.next().value);
538
+ }
539
+ };
540
+ /**
541
+ * Parse elements of the response array and structuring them into response object
542
+ *
543
+ * @param method - method name
544
+ * @param response - response from the method
545
+ * @return - parsed response corresponding to the abi
546
+ */
547
+ Contract.prototype.parseResponse = function (method, response) {
548
+ var _this = this;
549
+ var outputs = this.abi.find(function (abi) { return abi.name === method; }).outputs;
550
+ var responseIterator = response.flat()[Symbol.iterator]();
551
+ var resultObject = outputs.flat().reduce(function (acc, output) {
552
+ acc[output.name] = _this.parseResponseField(responseIterator, output, acc);
553
+ if (acc[output.name] && acc["".concat(output.name, "_len")]) {
554
+ delete acc["".concat(output.name, "_len")];
555
+ }
556
+ return acc;
557
+ }, {});
558
+ return Object.entries(resultObject).reduce(function (acc, _a) {
559
+ var _b = __read(_a, 2), key = _b[0], value = _b[1];
560
+ acc.push(value);
561
+ acc[key] = value;
562
+ return acc;
563
+ }, []);
564
+ };
565
+ Contract.prototype.invoke = function (method, args, options) {
566
+ if (args === void 0) { args = []; }
567
+ if (options === void 0) { options = {}; }
814
568
  // ensure contract is connected
815
- (0,
816
- minimalistic_assert_1.default)(this.address !== null, 'contract isnt connected to an address');
569
+ (0, minimalistic_assert_1.default)(this.address !== null, 'contract isnt connected to an address');
817
570
  // validate method and args
818
571
  this.validateMethodAndArgs('INVOKE', method, args);
819
- invocation = (_a = this.populateTransaction)[method].apply(
820
- _a,
821
- __spreadArray([], __read(args), false)
822
- );
823
- if ('estimateFee' in this.providerOrAccount) {
824
- return [2 /*return*/, this.providerOrAccount.estimateFee(invocation)];
572
+ var inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
573
+ var inputsLength = inputs.reduce(function (acc, input) {
574
+ if (!/_len$/.test(input.name)) {
575
+ return acc + 1;
576
+ }
577
+ return acc;
578
+ }, 0);
579
+ if (args.length !== inputsLength) {
580
+ throw Error("Invalid number of arguments, expected ".concat(inputsLength, " arguments, but got ").concat(args.length));
825
581
  }
826
- throw Error('Contract must be connected to the account contract to estimate');
827
- });
828
- });
829
- };
830
- Contract.prototype.populate = function (method, args) {
831
- if (args === void 0) {
832
- args = [];
833
- }
834
- var inputs = this.abi.find(function (abi) {
835
- return abi.name === method;
836
- }).inputs;
837
- return {
838
- contractAddress: this.address,
839
- entrypoint: method,
840
- calldata: this.compileCalldata(args, inputs),
841
- signature: [],
582
+ // compile calldata
583
+ var calldata = this.compileCalldata(args, inputs);
584
+ var invocation = {
585
+ contractAddress: this.address,
586
+ calldata: calldata,
587
+ entrypoint: method,
588
+ };
589
+ if ('execute' in this.providerOrAccount) {
590
+ return this.providerOrAccount.execute(invocation, undefined, {
591
+ maxFee: options.maxFee,
592
+ nonce: options.nonce,
593
+ });
594
+ }
595
+ // eslint-disable-next-line no-console
596
+ console.warn("Invoking ".concat(method, " without an account. This will not work on a public node."));
597
+ return this.providerOrAccount.invokeFunction(__assign(__assign({}, invocation), { signature: options.signature || [] }));
598
+ };
599
+ Contract.prototype.call = function (method, args, _a) {
600
+ if (args === void 0) { args = []; }
601
+ var _b = _a === void 0 ? {} : _a, _c = _b.blockIdentifier, blockIdentifier = _c === void 0 ? 'pending' : _c;
602
+ return __awaiter(this, void 0, void 0, function () {
603
+ var inputs, calldata;
604
+ var _this = this;
605
+ return __generator(this, function (_d) {
606
+ // ensure contract is connected
607
+ (0, minimalistic_assert_1.default)(this.address !== null, 'contract isnt connected to an address');
608
+ // validate method and args
609
+ this.validateMethodAndArgs('CALL', method, args);
610
+ inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
611
+ calldata = this.compileCalldata(args, inputs);
612
+ return [2 /*return*/, this.providerOrAccount
613
+ .callContract({
614
+ contractAddress: this.address,
615
+ calldata: calldata,
616
+ entrypoint: method,
617
+ }, blockIdentifier)
618
+ .then(function (x) { return _this.parseResponse(method, x.result); })];
619
+ });
620
+ });
621
+ };
622
+ Contract.prototype.estimate = function (method, args) {
623
+ if (args === void 0) { args = []; }
624
+ return __awaiter(this, void 0, void 0, function () {
625
+ var invocation;
626
+ var _a;
627
+ return __generator(this, function (_b) {
628
+ // ensure contract is connected
629
+ (0, minimalistic_assert_1.default)(this.address !== null, 'contract isnt connected to an address');
630
+ // validate method and args
631
+ this.validateMethodAndArgs('INVOKE', method, args);
632
+ invocation = (_a = this.populateTransaction)[method].apply(_a, __spreadArray([], __read(args), false));
633
+ if ('estimateFee' in this.providerOrAccount) {
634
+ return [2 /*return*/, this.providerOrAccount.estimateFee(invocation)];
635
+ }
636
+ throw Error('Contract must be connected to the account contract to estimate');
637
+ });
638
+ });
639
+ };
640
+ Contract.prototype.populate = function (method, args) {
641
+ if (args === void 0) { args = []; }
642
+ var inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
643
+ return {
644
+ contractAddress: this.address,
645
+ entrypoint: method,
646
+ calldata: this.compileCalldata(args, inputs),
647
+ signature: [],
648
+ };
842
649
  };
843
- };
844
- return Contract;
845
- })();
650
+ return Contract;
651
+ }());
846
652
  exports.Contract = Contract;