starknet 3.4.0 → 3.5.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 (62) hide show
  1. package/.eslintrc +2 -1
  2. package/CHANGELOG.md +20 -0
  3. package/__tests__/account.test.ts +7 -17
  4. package/__tests__/accountContract.test.ts +13 -25
  5. package/__tests__/contract.test.ts +152 -55
  6. package/__tests__/utils/utils.browser.test.ts +1 -3
  7. package/contract/contractFactory.d.ts +36 -0
  8. package/contract/contractFactory.js +218 -0
  9. package/contract/default.d.ts +143 -0
  10. package/{contract.js → contract/default.js} +357 -86
  11. package/contract/index.d.ts +3 -0
  12. package/contract/index.js +28 -0
  13. package/contract/interface.d.ts +79 -0
  14. package/contract/interface.js +8 -0
  15. package/dist/contract/contractFactory.d.ts +32 -0
  16. package/dist/contract/contractFactory.js +102 -0
  17. package/dist/contract/default.d.ts +121 -0
  18. package/dist/{contract.js → contract/default.js} +321 -73
  19. package/dist/contract/index.d.ts +3 -0
  20. package/dist/contract/index.js +15 -0
  21. package/dist/contract/interface.d.ts +72 -0
  22. package/dist/contract/interface.js +9 -0
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.js +1 -1
  25. package/dist/provider/default.d.ts +4 -1
  26. package/dist/provider/default.js +15 -6
  27. package/dist/provider/interface.d.ts +3 -1
  28. package/dist/types/api.d.ts +6 -0
  29. package/dist/types/contract.d.ts +5 -0
  30. package/dist/types/contract.js +2 -0
  31. package/dist/types/index.d.ts +1 -0
  32. package/dist/types/index.js +1 -0
  33. package/dist/types/lib.d.ts +11 -1
  34. package/dist/utils/transaction.d.ts +1 -2
  35. package/index.d.ts +1 -1
  36. package/index.js +1 -1
  37. package/package.json +1 -1
  38. package/provider/default.d.ts +4 -1
  39. package/provider/default.js +25 -16
  40. package/provider/interface.d.ts +3 -1
  41. package/src/contract/contractFactory.ts +78 -0
  42. package/src/contract/default.ts +622 -0
  43. package/src/contract/index.ts +3 -0
  44. package/src/contract/interface.ts +87 -0
  45. package/src/index.ts +1 -1
  46. package/src/provider/default.ts +19 -14
  47. package/src/provider/interface.ts +3 -1
  48. package/src/types/api.ts +7 -0
  49. package/src/types/contract.ts +5 -0
  50. package/src/types/index.ts +1 -0
  51. package/src/types/lib.ts +12 -1
  52. package/src/utils/transaction.ts +1 -2
  53. package/types/api.d.ts +6 -0
  54. package/types/contract.d.ts +5 -0
  55. package/types/contract.js +2 -0
  56. package/types/index.d.ts +1 -0
  57. package/types/index.js +1 -0
  58. package/types/lib.d.ts +11 -1
  59. package/utils/transaction.d.ts +1 -2
  60. package/contract.d.ts +0 -98
  61. package/dist/contract.d.ts +0 -94
  62. package/src/contract.ts +0 -357
@@ -78,8 +78,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
78
78
  exports.Contract = void 0;
79
79
  var bn_js_1 = __importDefault(require("bn.js"));
80
80
  var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
81
- var provider_1 = require("./provider");
82
- var number_1 = require("./utils/number");
81
+ var account_1 = require("../account");
82
+ var provider_1 = require("../provider");
83
+ var hash_1 = require("../utils/hash");
84
+ var number_1 = require("../utils/number");
83
85
  function parseFelt(candidate) {
84
86
  try {
85
87
  return (0, number_1.toBN)(candidate);
@@ -88,19 +90,89 @@ function parseFelt(candidate) {
88
90
  throw Error('Couldnt parse felt');
89
91
  }
90
92
  }
93
+ /**
94
+ * Adds call methods to the contract
95
+ *
96
+ */
97
+ function buildCall(contract, functionAbi) {
98
+ return function () {
99
+ var args = [];
100
+ for (var _i = 0; _i < arguments.length; _i++) {
101
+ args[_i] = arguments[_i];
102
+ }
103
+ return __awaiter(this, void 0, void 0, function () {
104
+ return __generator(this, function (_a) {
105
+ return [2 /*return*/, contract.call(functionAbi.name, args)];
106
+ });
107
+ });
108
+ };
109
+ }
110
+ /**
111
+ * Adds invoke methods to the contract
112
+ *
113
+ */
114
+ function buildInvoke(contract, functionAbi) {
115
+ return function () {
116
+ var args = [];
117
+ for (var _i = 0; _i < arguments.length; _i++) {
118
+ args[_i] = arguments[_i];
119
+ }
120
+ return __awaiter(this, void 0, void 0, function () {
121
+ return __generator(this, function (_a) {
122
+ return [2 /*return*/, contract.invoke(functionAbi.name, args)];
123
+ });
124
+ });
125
+ };
126
+ }
127
+ /**
128
+ * Adds call/invoke methods to the contract
129
+ *
130
+ */
131
+ function buildDefault(contract, functionAbi) {
132
+ if (functionAbi.stateMutability === 'view') {
133
+ return buildCall(contract, functionAbi);
134
+ }
135
+ return buildInvoke(contract, functionAbi);
136
+ }
137
+ /**
138
+ * Adds populate for methods to the contract
139
+ *
140
+ */
141
+ function buildPopulate(contract, functionAbi) {
142
+ return function () {
143
+ var args = [];
144
+ for (var _i = 0; _i < arguments.length; _i++) {
145
+ args[_i] = arguments[_i];
146
+ }
147
+ return contract.populate(functionAbi.name, args);
148
+ };
149
+ }
150
+ /**
151
+ * Adds estimateFee for methods to the contract
152
+ *
153
+ */
154
+ function buildEstimate(contract, functionAbi) {
155
+ return function () {
156
+ var args = [];
157
+ for (var _i = 0; _i < arguments.length; _i++) {
158
+ args[_i] = arguments[_i];
159
+ }
160
+ return contract.estimate(functionAbi.name, args);
161
+ };
162
+ }
91
163
  var Contract = /** @class */ (function () {
92
164
  /**
93
165
  * Contract class to handle contract methods
94
166
  *
95
167
  * @param abi - Abi of the contract object
96
168
  * @param address (optional) - address to connect to
169
+ * @param providerOrAccount (optional) - Provider or Account to attach to
97
170
  */
98
- function Contract(abi, address, provider) {
99
- if (address === void 0) { address = null; }
100
- if (provider === void 0) { provider = provider_1.defaultProvider; }
101
- this.connectedTo = null;
102
- this.connectedTo = address;
103
- this.provider = provider;
171
+ function Contract(abi, address, providerOrAccount) {
172
+ var _this = this;
173
+ if (providerOrAccount === void 0) { providerOrAccount = provider_1.defaultProvider; }
174
+ this.address = address;
175
+ this.providerOrAccount = providerOrAccount;
104
176
  this.abi = abi;
105
177
  this.structs = abi
106
178
  .filter(function (abiEntry) { return abiEntry.type === 'struct'; })
@@ -108,16 +180,105 @@ var Contract = /** @class */ (function () {
108
180
  var _a;
109
181
  return (__assign(__assign({}, acc), (_a = {}, _a[abiEntry.name] = abiEntry, _a)));
110
182
  }, {});
183
+ Object.defineProperty(this, 'functions', {
184
+ enumerable: true,
185
+ value: {},
186
+ writable: false,
187
+ });
188
+ Object.defineProperty(this, 'callStatic', {
189
+ enumerable: true,
190
+ value: {},
191
+ writable: false,
192
+ });
193
+ Object.defineProperty(this, 'populateTransaction', {
194
+ enumerable: true,
195
+ value: {},
196
+ writable: false,
197
+ });
198
+ Object.defineProperty(this, 'estimateFee', {
199
+ enumerable: true,
200
+ value: {},
201
+ writable: false,
202
+ });
203
+ this.abi.forEach(function (abiElement) {
204
+ if (abiElement.type !== 'function') {
205
+ return;
206
+ }
207
+ var signature = abiElement.name;
208
+ if (!_this[signature]) {
209
+ Object.defineProperty(_this, signature, {
210
+ enumerable: true,
211
+ value: buildDefault(_this, abiElement),
212
+ writable: false,
213
+ });
214
+ }
215
+ if (!_this.functions[signature]) {
216
+ Object.defineProperty(_this.functions, signature, {
217
+ enumerable: true,
218
+ value: buildDefault(_this, abiElement),
219
+ writable: false,
220
+ });
221
+ }
222
+ if (!_this.callStatic[signature]) {
223
+ Object.defineProperty(_this.callStatic, signature, {
224
+ enumerable: true,
225
+ value: buildCall(_this, abiElement),
226
+ writable: false,
227
+ });
228
+ }
229
+ if (!_this.populateTransaction[signature]) {
230
+ Object.defineProperty(_this.populateTransaction, signature, {
231
+ enumerable: true,
232
+ value: buildPopulate(_this, abiElement),
233
+ writable: false,
234
+ });
235
+ }
236
+ if (!_this.estimateFee[signature]) {
237
+ Object.defineProperty(_this.estimateFee, signature, {
238
+ enumerable: true,
239
+ value: buildEstimate(_this, abiElement),
240
+ writable: false,
241
+ });
242
+ }
243
+ });
111
244
  }
112
245
  /**
113
246
  * Saves the address of the contract deployed on network that will be used for interaction
114
247
  *
115
248
  * @param address - address of the contract
116
- * @returns Contract
117
249
  */
118
- Contract.prototype.connect = function (address) {
119
- this.connectedTo = address;
120
- return this;
250
+ Contract.prototype.attach = function (address) {
251
+ this.address = address;
252
+ };
253
+ /**
254
+ * Attaches to new Provider or Account
255
+ *
256
+ * @param providerOrAccount - new Provider or Account to attach to
257
+ */
258
+ Contract.prototype.connect = function (providerOrAccount) {
259
+ this.providerOrAccount = providerOrAccount;
260
+ };
261
+ /**
262
+ * Resolves when contract is deployed on the network or when no deployment transaction is found
263
+ *
264
+ * @returns Promise that resolves when contract is deployed on the network or when no deployment transaction is found
265
+ * @throws When deployment fails
266
+ */
267
+ Contract.prototype.deployed = function () {
268
+ return __awaiter(this, void 0, void 0, function () {
269
+ return __generator(this, function (_a) {
270
+ switch (_a.label) {
271
+ case 0:
272
+ if (!this.deployTransactionHash) return [3 /*break*/, 2];
273
+ return [4 /*yield*/, this.providerOrAccount.waitForTransaction(this.deployTransactionHash)];
274
+ case 1:
275
+ _a.sent();
276
+ this.deployTransactionHash = undefined;
277
+ _a.label = 2;
278
+ case 2: return [2 /*return*/, this];
279
+ }
280
+ });
281
+ });
121
282
  };
122
283
  /**
123
284
  * Validates if all arguments that are passed to the method are corresponding to the ones in the abi
@@ -128,7 +289,7 @@ var Contract = /** @class */ (function () {
128
289
  */
129
290
  Contract.prototype.validateMethodAndArgs = function (type, method, args) {
130
291
  var _this = this;
131
- if (args === void 0) { args = {}; }
292
+ if (args === void 0) { args = []; }
132
293
  // ensure provided method exists
133
294
  var invokeableFunctionNames = this.abi
134
295
  .filter(function (abi) {
@@ -141,45 +302,80 @@ var Contract = /** @class */ (function () {
141
302
  (0, minimalistic_assert_1.default)(invokeableFunctionNames.includes(method), (type === 'INVOKE' ? 'invokeable' : 'viewable') + " method not found in abi");
142
303
  // ensure args match abi type
143
304
  var methodAbi = this.abi.find(function (abi) { return abi.name === method && abi.type === 'function'; });
305
+ var argPosition = 0;
144
306
  methodAbi.inputs.forEach(function (input) {
145
- var arg = args[input.name];
146
- if (arg !== undefined) {
147
- if (input.type === 'felt') {
148
- (0, minimalistic_assert_1.default)(typeof arg === 'string' || typeof arg === 'number' || arg instanceof bn_js_1.default, "arg " + input.name + " should be a felt (string, number, BigNumber)");
307
+ if (/_len$/.test(input.name)) {
308
+ return;
309
+ }
310
+ if (input.type === 'felt') {
311
+ (0, minimalistic_assert_1.default)(typeof args[argPosition] === 'string' ||
312
+ typeof args[argPosition] === 'number' ||
313
+ args[argPosition] instanceof bn_js_1.default, "arg " + input.name + " should be a felt (string, number, BigNumber)");
314
+ argPosition += 1;
315
+ }
316
+ else if (input.type in _this.structs && typeof args[argPosition] === 'object') {
317
+ if (Array.isArray(args[argPosition])) {
318
+ var structMembersLength = _this.calculateStructMembers(input.type);
319
+ (0, minimalistic_assert_1.default)(args[argPosition].length === structMembersLength, "arg should be of length " + structMembersLength);
149
320
  }
150
- else if (typeof arg === 'object' && input.type in _this.structs) {
321
+ else {
151
322
  _this.structs[input.type].members.forEach(function (_a) {
152
323
  var name = _a.name;
153
- (0, minimalistic_assert_1.default)(Object.keys(arg).includes(name), "arg should have a property " + name);
324
+ (0, minimalistic_assert_1.default)(Object.keys(args[argPosition]).includes(name), "arg should have a property " + name);
325
+ });
326
+ }
327
+ argPosition += 1;
328
+ }
329
+ else {
330
+ (0, minimalistic_assert_1.default)(Array.isArray(args[argPosition]), "arg " + input.name + " should be an Array");
331
+ if (input.type === 'felt*') {
332
+ args[argPosition].forEach(function (felt) {
333
+ (0, minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, "arg " + input.name + " should be an array of string, number or BigNumber");
334
+ });
335
+ argPosition += 1;
336
+ }
337
+ else if (/\(felt/.test(input.type)) {
338
+ var tupleLength = input.type.split(',').length;
339
+ (0, minimalistic_assert_1.default)(args[argPosition].length === tupleLength, "arg " + input.name + " should have " + tupleLength + " elements in tuple");
340
+ args[argPosition].forEach(function (felt) {
341
+ (0, minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, "arg " + input.name + " should be an array of string, number or BigNumber");
154
342
  });
343
+ argPosition += 1;
155
344
  }
156
345
  else {
157
- (0, minimalistic_assert_1.default)(Array.isArray(arg), "arg " + input.name + " should be an Array");
158
- if (input.type === 'felt*') {
159
- arg.forEach(function (felt) {
160
- (0, minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, "arg " + input.name + " should be an array of string, number or BigNumber");
161
- });
162
- }
163
- else if (/\(felt/.test(input.type)) {
164
- var tupleLength = input.type.split(',').length;
165
- (0, minimalistic_assert_1.default)(arg.length === tupleLength, "arg " + input.name + " should have " + tupleLength + " elements in tuple");
166
- arg.forEach(function (felt) {
167
- (0, minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, "arg " + input.name + " should be an array of string, number or BigNumber");
168
- });
169
- }
170
- else {
171
- var arrayType_1 = input.type.replace('*', '');
172
- arg.forEach(function (struct) {
173
- _this.structs[arrayType_1].members.forEach(function (_a) {
174
- var name = _a.name;
346
+ var arrayType_1 = input.type.replace('*', '');
347
+ args[argPosition].forEach(function (struct) {
348
+ _this.structs[arrayType_1].members.forEach(function (_a) {
349
+ var name = _a.name;
350
+ if (Array.isArray(struct)) {
351
+ var structMembersLength = _this.calculateStructMembers(arrayType_1);
352
+ (0, minimalistic_assert_1.default)(struct.length === structMembersLength, "arg should be of length " + structMembersLength);
353
+ }
354
+ else {
175
355
  (0, minimalistic_assert_1.default)(Object.keys(struct).includes(name), "arg " + input.name + " should be an array of " + arrayType_1);
176
- });
356
+ }
177
357
  });
178
- }
358
+ });
359
+ argPosition += 1;
179
360
  }
180
361
  }
181
362
  });
182
363
  };
364
+ /**
365
+ * Deep parse of the object that has been passed to the method
366
+ *
367
+ * @param struct - struct that needs to be calculated
368
+ * @return {number} - number of members for the given struct
369
+ */
370
+ Contract.prototype.calculateStructMembers = function (struct) {
371
+ var _this = this;
372
+ return this.structs[struct].members.reduce(function (acc, member) {
373
+ if (member.type === 'felt') {
374
+ return acc + 1;
375
+ }
376
+ return acc + _this.structMemberNum(member.type);
377
+ }, 0);
378
+ };
183
379
  /**
184
380
  * Deep parse of the object that has been passed to the method
185
381
  *
@@ -187,10 +383,17 @@ var Contract = /** @class */ (function () {
187
383
  * @param type - name of the method
188
384
  * @return {string | string[]} - parsed arguments in format that contract is expecting
189
385
  */
190
- Contract.prototype.parseCalldataObject = function (element, type) {
386
+ Contract.prototype.parseCalldataValue = function (element, type) {
191
387
  var _this = this;
192
388
  if (element === undefined) {
193
- throw Error('Missing element in calldata object');
389
+ throw Error('Missing element in calldata');
390
+ }
391
+ if (Array.isArray(element)) {
392
+ var structMemberNum = this.calculateStructMembers(type);
393
+ if (element.length !== structMemberNum) {
394
+ throw Error('Missing element in calldata');
395
+ }
396
+ return element.map(function (el) { return (0, number_1.toFelt)(el); });
194
397
  }
195
398
  // checking if the passed element is struct or element in struct
196
399
  if (this.structs[type] && this.structs[type].members.length) {
@@ -199,7 +402,7 @@ var Contract = /** @class */ (function () {
199
402
  // if the member of the struct is another struct this will return array of the felts if not it will be single felt
200
403
  // TODO: refactor types so member name can be used as keyof ParsedStruct
201
404
  /* @ts-ignore */
202
- var parsedData = _this.parseCalldataObject(element[member.name], member.type);
405
+ var parsedData = _this.parseCalldataValue(element[member.name], member.type);
203
406
  if (typeof parsedData === 'string') {
204
407
  acc.push(parsedData);
205
408
  }
@@ -237,33 +440,28 @@ var Contract = /** @class */ (function () {
237
440
  * @param input - input(field) information from the abi that will be used to parse the data
238
441
  * @return {string | string[]} - parsed arguments in format that contract is expecting
239
442
  */
240
- Contract.prototype.parsCalldataField = function (args, input) {
443
+ Contract.prototype.parseCalldataField = function (argsIterator, input) {
241
444
  var _this = this;
242
445
  var name = input.name, type = input.type;
243
- var value = args[name];
244
- var propName = name.replace(/_len$/, '');
446
+ var value = argsIterator.next().value;
447
+ var parsedCalldata = [];
245
448
  switch (true) {
246
- case /_len$/.test(name):
247
- if (Array.isArray(args[propName])) {
248
- var arr = args[propName];
249
- return (0, number_1.toFelt)(arr.length);
250
- }
251
- throw Error("Expected " + propName + " to be array");
252
449
  case /\*/.test(type):
253
450
  if (Array.isArray(value)) {
451
+ parsedCalldata.push((0, number_1.toFelt)(value.length));
254
452
  return value.reduce(function (acc, el) {
255
453
  if (/felt/.test(type)) {
256
454
  acc.push((0, number_1.toFelt)(el));
257
455
  }
258
456
  else {
259
- acc.push.apply(acc, __spreadArray([], __read(_this.parseCalldataObject(el, type.replace('*', ''))), false));
457
+ acc.push.apply(acc, __spreadArray([], __read(_this.parseCalldataValue(el, type.replace('*', ''))), false));
260
458
  }
261
459
  return acc;
262
- }, []);
460
+ }, parsedCalldata);
263
461
  }
264
462
  throw Error("Expected " + name + " to be array");
265
463
  case type in this.structs:
266
- return this.parseCalldataObject(value, type);
464
+ return this.parseCalldataValue(value, type);
267
465
  case /\(felt/.test(type):
268
466
  if (Array.isArray(value)) {
269
467
  return value.map(function (el) { return (0, number_1.toFelt)(el); });
@@ -282,8 +480,12 @@ var Contract = /** @class */ (function () {
282
480
  */
283
481
  Contract.prototype.compileCalldata = function (args, inputs) {
284
482
  var _this = this;
483
+ var argsIterator = args[Symbol.iterator]();
285
484
  return inputs.reduce(function (acc, input) {
286
- var parsedData = _this.parsCalldataField(args, input);
485
+ if (/_len$/.test(input.name)) {
486
+ return acc;
487
+ }
488
+ var parsedData = _this.parseCalldataField(argsIterator, input);
287
489
  if (Array.isArray(parsedData)) {
288
490
  acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
289
491
  }
@@ -302,7 +504,6 @@ var Contract = /** @class */ (function () {
302
504
  */
303
505
  Contract.prototype.parseResponseField = function (responseIterator, output, parsedResult) {
304
506
  var name = output.name, type = output.type;
305
- var arrLen;
306
507
  var parsedDataArr = [];
307
508
  switch (true) {
308
509
  case /_len$/.test(name):
@@ -314,7 +515,7 @@ var Contract = /** @class */ (function () {
314
515
  }, []);
315
516
  case /\*/.test(type):
316
517
  if (parsedResult && parsedResult[name + "_len"]) {
317
- arrLen = parsedResult[name + "_len"];
518
+ var arrLen = parsedResult[name + "_len"];
318
519
  while (parsedDataArr.length < arrLen) {
319
520
  parsedDataArr.push(this.parseResponseStruct(responseIterator, output.type.replace('*', '')));
320
521
  }
@@ -337,53 +538,100 @@ var Contract = /** @class */ (function () {
337
538
  var _this = this;
338
539
  var outputs = this.abi.find(function (abi) { return abi.name === method; }).outputs;
339
540
  var responseIterator = response.flat()[Symbol.iterator]();
340
- return outputs.flat().reduce(function (acc, output) {
541
+ var resultObject = outputs.flat().reduce(function (acc, output) {
341
542
  acc[output.name] = _this.parseResponseField(responseIterator, output, acc);
342
543
  if (acc[output.name] && acc[output.name + "_len"]) {
343
544
  delete acc[output.name + "_len"];
344
545
  }
345
546
  return acc;
346
547
  }, {});
548
+ return Object.entries(resultObject).reduce(function (acc, _a) {
549
+ var _b = __read(_a, 2), key = _b[0], value = _b[1];
550
+ acc.push(value);
551
+ acc[key] = value;
552
+ return acc;
553
+ }, []);
347
554
  };
348
- Contract.prototype.invoke = function (method, args, signature) {
349
- if (args === void 0) { args = {}; }
555
+ Contract.prototype.invoke = function (method, args) {
556
+ if (args === void 0) { args = []; }
350
557
  // ensure contract is connected
351
- (0, minimalistic_assert_1.default)(this.connectedTo !== null, 'contract isnt connected to an address');
558
+ (0, minimalistic_assert_1.default)(this.address !== null, 'contract isnt connected to an address');
352
559
  // validate method and args
353
560
  this.validateMethodAndArgs('INVOKE', method, args);
354
561
  var inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
562
+ var inputsLength = inputs.reduce(function (acc, input) {
563
+ if (!/_len$/.test(input.name)) {
564
+ return acc + 1;
565
+ }
566
+ return acc;
567
+ }, 0);
568
+ var signature = [];
569
+ if (args.length === inputsLength + 1 && Array.isArray(args[args.length - 1])) {
570
+ signature.push.apply(signature, __spreadArray([], __read(args.pop()), false));
571
+ }
572
+ if (args.length !== inputsLength) {
573
+ throw Error("Invalid number of arguments, expected " + inputsLength + " arguments, but got " + args.length);
574
+ }
355
575
  // compile calldata
356
576
  var calldata = this.compileCalldata(args, inputs);
357
- return this.provider.invokeFunction({
358
- contractAddress: this.connectedTo,
359
- signature: signature,
577
+ var invocation = {
578
+ contractAddress: this.address,
360
579
  calldata: calldata,
361
580
  entrypoint: method,
362
- });
581
+ };
582
+ if (this.providerOrAccount instanceof account_1.Account) {
583
+ return this.providerOrAccount.execute(invocation);
584
+ }
585
+ return this.providerOrAccount.invokeFunction(__assign(__assign({}, invocation), { signature: signature }));
363
586
  };
364
- Contract.prototype.call = function (method, args, blockIdentifier) {
365
- if (args === void 0) { args = {}; }
366
- if (blockIdentifier === void 0) { blockIdentifier = null; }
587
+ Contract.prototype.call = function (method, args) {
588
+ if (args === void 0) { args = []; }
367
589
  return __awaiter(this, void 0, void 0, function () {
368
- var inputs, calldata;
590
+ var inputs, inputsLength, options, calldata;
369
591
  var _this = this;
370
592
  return __generator(this, function (_a) {
371
593
  // ensure contract is connected
372
- (0, minimalistic_assert_1.default)(this.connectedTo !== null, 'contract isnt connected to an address');
594
+ (0, minimalistic_assert_1.default)(this.address !== null, 'contract isnt connected to an address');
373
595
  // validate method and args
374
596
  this.validateMethodAndArgs('CALL', method, args);
375
597
  inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
598
+ inputsLength = inputs.length;
599
+ options = {
600
+ blockIdentifier: null,
601
+ };
602
+ if (args.length === inputsLength + 1 && typeof args[args.length - 1] === 'object') {
603
+ Object.assign(options, args.pop());
604
+ }
376
605
  calldata = this.compileCalldata(args, inputs);
377
- return [2 /*return*/, this.provider
606
+ return [2 /*return*/, this.providerOrAccount
378
607
  .callContract({
379
- contractAddress: this.connectedTo,
608
+ contractAddress: this.address,
380
609
  calldata: calldata,
381
610
  entrypoint: method,
382
- }, blockIdentifier)
611
+ }, options)
383
612
  .then(function (x) { return _this.parseResponse(method, x.result); })];
384
613
  });
385
614
  });
386
615
  };
616
+ Contract.prototype.estimate = function (_method, _args) {
617
+ if (_args === void 0) { _args = []; }
618
+ return __awaiter(this, void 0, void 0, function () {
619
+ return __generator(this, function (_a) {
620
+ // TODO; remove error as soon as estimate fees are supported
621
+ throw Error('Estimation of the fees are not yet supported');
622
+ });
623
+ });
624
+ };
625
+ Contract.prototype.populate = function (method, args) {
626
+ if (args === void 0) { args = []; }
627
+ var inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
628
+ return {
629
+ contractAddress: this.address,
630
+ entrypoint: (0, hash_1.getSelectorFromName)(method),
631
+ calldata: this.compileCalldata(args, inputs),
632
+ signature: [],
633
+ };
634
+ };
387
635
  return Contract;
388
636
  }());
389
637
  exports.Contract = Contract;
@@ -0,0 +1,3 @@
1
+ export * from './default';
2
+ export * from './interface';
3
+ export * from './contractFactory';
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./default"), exports);
14
+ __exportStar(require("./interface"), exports);
15
+ __exportStar(require("./contractFactory"), exports);
@@ -0,0 +1,72 @@
1
+ import { Account } from '../account';
2
+ import { Provider } from '../provider';
3
+ import { Abi, AddTransactionResponse, AsyncContractFunction, ContractFunction, Invocation, Result } from '../types';
4
+ export declare abstract class ContractInterface {
5
+ abstract abi: Abi;
6
+ abstract address: string;
7
+ abstract providerOrAccount: Provider | Account;
8
+ abstract deployTransactionHash?: string;
9
+ readonly functions: {
10
+ [name: string]: AsyncContractFunction;
11
+ };
12
+ readonly callStatic: {
13
+ [name: string]: AsyncContractFunction;
14
+ };
15
+ readonly populateTransaction: {
16
+ [name: string]: ContractFunction;
17
+ };
18
+ readonly estimateFee: {
19
+ [name: string]: ContractFunction;
20
+ };
21
+ readonly [key: string]: AsyncContractFunction | any;
22
+ /**
23
+ * Saves the address of the contract deployed on network that will be used for interaction
24
+ *
25
+ * @param address - address of the contract
26
+ */
27
+ abstract attach(address: string): void;
28
+ /**
29
+ * Attaches to new Provider or Account
30
+ *
31
+ * @param providerOrAccount - new Provider or Account to attach to
32
+ */
33
+ abstract connect(providerOrAccount: Provider | Account): void;
34
+ /**
35
+ * Resolves when contract is deployed on the network or when no deployment transaction is found
36
+ *
37
+ * @returns Promise that resolves when contract is deployed on the network or when no deployment transaction is found
38
+ * @throws When deployment fails
39
+ */
40
+ abstract deployed(): Promise<ContractInterface>;
41
+ /**
42
+ * Calls a method on a contract
43
+ *
44
+ * @param method name of the method
45
+ * @param args Array of the arguments for the call
46
+ * @returns Result of the call as an array with key value pars
47
+ */
48
+ abstract call(method: string, args?: Array<any>): Promise<Result>;
49
+ /**
50
+ * Invokes a method on a contract
51
+ *
52
+ * @param method name of the method
53
+ * @param args Array of the arguments for the invoke
54
+ * @returns Add Transaction Response
55
+ */
56
+ abstract invoke(method: string, args?: Array<any>): Promise<AddTransactionResponse>;
57
+ /**
58
+ * Calls a method on a contract
59
+ *
60
+ * @param method name of the method
61
+ * @param args Array of the arguments for the call
62
+ */
63
+ abstract estimate(method: string, args?: Array<any>): Promise<any>;
64
+ /**
65
+ * Calls a method on a contract
66
+ *
67
+ * @param method name of the method
68
+ * @param args Array of the arguments for the call
69
+ * @returns Invocation objet
70
+ */
71
+ abstract populate(method: string, args?: Array<any>): Invocation;
72
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ContractInterface = void 0;
4
+ var ContractInterface = /** @class */ (function () {
5
+ function ContractInterface() {
6
+ }
7
+ return ContractInterface;
8
+ }());
9
+ exports.ContractInterface = ContractInterface;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Main
3
3
  */
4
- export * from './types';
5
4
  export * from './contract';
5
+ export * from './types';
6
6
  export * from './provider';
7
7
  export * from './account';
8
8
  export * from './signer';