starknet 3.3.0 → 3.5.1

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 (67) hide show
  1. package/.eslintrc +2 -1
  2. package/CHANGELOG.md +33 -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/account/default.d.ts +6 -0
  8. package/account/default.js +129 -0
  9. package/contract/contractFactory.d.ts +36 -0
  10. package/contract/contractFactory.js +218 -0
  11. package/contract/default.d.ts +143 -0
  12. package/{contract.js → contract/default.js} +356 -86
  13. package/contract/index.d.ts +3 -0
  14. package/contract/index.js +28 -0
  15. package/contract/interface.d.ts +79 -0
  16. package/contract/interface.js +8 -0
  17. package/dist/account/default.d.ts +6 -1
  18. package/dist/account/default.js +99 -0
  19. package/dist/contract/contractFactory.d.ts +32 -0
  20. package/dist/contract/contractFactory.js +102 -0
  21. package/dist/contract/default.d.ts +121 -0
  22. package/dist/{contract.js → contract/default.js} +320 -73
  23. package/dist/contract/index.d.ts +3 -0
  24. package/dist/contract/index.js +15 -0
  25. package/dist/contract/interface.d.ts +72 -0
  26. package/dist/contract/interface.js +9 -0
  27. package/dist/index.d.ts +1 -1
  28. package/dist/index.js +1 -1
  29. package/dist/provider/default.d.ts +5 -13
  30. package/dist/provider/default.js +12 -22
  31. package/dist/provider/interface.d.ts +3 -1
  32. package/dist/types/api.d.ts +6 -0
  33. package/dist/types/contract.d.ts +5 -0
  34. package/dist/types/contract.js +2 -0
  35. package/dist/types/index.d.ts +1 -0
  36. package/dist/types/index.js +1 -0
  37. package/dist/types/lib.d.ts +11 -1
  38. package/dist/utils/transaction.d.ts +1 -2
  39. package/index.d.ts +1 -1
  40. package/index.js +1 -1
  41. package/package.json +1 -1
  42. package/provider/default.d.ts +4 -18
  43. package/provider/default.js +18 -37
  44. package/provider/interface.d.ts +3 -1
  45. package/src/account/default.ts +109 -1
  46. package/src/contract/contractFactory.ts +78 -0
  47. package/src/contract/default.ts +626 -0
  48. package/src/contract/index.ts +3 -0
  49. package/src/contract/interface.ts +87 -0
  50. package/src/index.ts +1 -1
  51. package/src/provider/default.ts +15 -36
  52. package/src/provider/interface.ts +3 -1
  53. package/src/types/api.ts +7 -0
  54. package/src/types/contract.ts +5 -0
  55. package/src/types/index.ts +1 -0
  56. package/src/types/lib.ts +12 -1
  57. package/src/utils/transaction.ts +1 -2
  58. package/types/api.d.ts +6 -0
  59. package/types/contract.d.ts +5 -0
  60. package/types/contract.js +2 -0
  61. package/types/index.d.ts +1 -0
  62. package/types/index.js +1 -0
  63. package/types/lib.d.ts +11 -1
  64. package/utils/transaction.d.ts +1 -2
  65. package/contract.d.ts +0 -98
  66. package/dist/contract.d.ts +0 -94
  67. package/src/contract.ts +0 -357
@@ -187,8 +187,9 @@ Object.defineProperty(exports, '__esModule', { value: true });
187
187
  exports.Contract = void 0;
188
188
  var bn_js_1 = __importDefault(require('bn.js'));
189
189
  var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
190
- var provider_1 = require('./provider');
191
- var number_1 = require('./utils/number');
190
+ var provider_1 = require('../provider');
191
+ var hash_1 = require('../utils/hash');
192
+ var number_1 = require('../utils/number');
192
193
  function parseFelt(candidate) {
193
194
  try {
194
195
  return (0, number_1.toBN)(candidate);
@@ -196,23 +197,91 @@ function parseFelt(candidate) {
196
197
  throw Error('Couldnt parse felt');
197
198
  }
198
199
  }
200
+ /**
201
+ * Adds call methods to the contract
202
+ *
203
+ */
204
+ function buildCall(contract, functionAbi) {
205
+ return function () {
206
+ var args = [];
207
+ for (var _i = 0; _i < arguments.length; _i++) {
208
+ args[_i] = arguments[_i];
209
+ }
210
+ return __awaiter(this, void 0, void 0, function () {
211
+ return __generator(this, function (_a) {
212
+ return [2 /*return*/, contract.call(functionAbi.name, args)];
213
+ });
214
+ });
215
+ };
216
+ }
217
+ /**
218
+ * Adds invoke methods to the contract
219
+ *
220
+ */
221
+ function buildInvoke(contract, functionAbi) {
222
+ return function () {
223
+ var args = [];
224
+ for (var _i = 0; _i < arguments.length; _i++) {
225
+ args[_i] = arguments[_i];
226
+ }
227
+ return __awaiter(this, void 0, void 0, function () {
228
+ return __generator(this, function (_a) {
229
+ return [2 /*return*/, contract.invoke(functionAbi.name, args)];
230
+ });
231
+ });
232
+ };
233
+ }
234
+ /**
235
+ * Adds call/invoke methods to the contract
236
+ *
237
+ */
238
+ function buildDefault(contract, functionAbi) {
239
+ if (functionAbi.stateMutability === 'view') {
240
+ return buildCall(contract, functionAbi);
241
+ }
242
+ return buildInvoke(contract, functionAbi);
243
+ }
244
+ /**
245
+ * Adds populate for methods to the contract
246
+ *
247
+ */
248
+ function buildPopulate(contract, functionAbi) {
249
+ return function () {
250
+ var args = [];
251
+ for (var _i = 0; _i < arguments.length; _i++) {
252
+ args[_i] = arguments[_i];
253
+ }
254
+ return contract.populate(functionAbi.name, args);
255
+ };
256
+ }
257
+ /**
258
+ * Adds estimateFee for methods to the contract
259
+ *
260
+ */
261
+ function buildEstimate(contract, functionAbi) {
262
+ return function () {
263
+ var args = [];
264
+ for (var _i = 0; _i < arguments.length; _i++) {
265
+ args[_i] = arguments[_i];
266
+ }
267
+ return contract.estimate(functionAbi.name, args);
268
+ };
269
+ }
199
270
  var Contract = /** @class */ (function () {
200
271
  /**
201
272
  * Contract class to handle contract methods
202
273
  *
203
274
  * @param abi - Abi of the contract object
204
275
  * @param address (optional) - address to connect to
276
+ * @param providerOrAccount (optional) - Provider or Account to attach to
205
277
  */
206
- function Contract(abi, address, provider) {
207
- if (address === void 0) {
208
- address = null;
209
- }
210
- if (provider === void 0) {
211
- provider = provider_1.defaultProvider;
278
+ function Contract(abi, address, providerOrAccount) {
279
+ var _this = this;
280
+ if (providerOrAccount === void 0) {
281
+ providerOrAccount = provider_1.defaultProvider;
212
282
  }
213
- this.connectedTo = null;
214
- this.connectedTo = address;
215
- this.provider = provider;
283
+ this.address = address;
284
+ this.providerOrAccount = providerOrAccount;
216
285
  this.abi = abi;
217
286
  this.structs = abi
218
287
  .filter(function (abiEntry) {
@@ -222,16 +291,109 @@ var Contract = /** @class */ (function () {
222
291
  var _a;
223
292
  return __assign(__assign({}, acc), ((_a = {}), (_a[abiEntry.name] = abiEntry), _a));
224
293
  }, {});
294
+ Object.defineProperty(this, 'functions', {
295
+ enumerable: true,
296
+ value: {},
297
+ writable: false,
298
+ });
299
+ Object.defineProperty(this, 'callStatic', {
300
+ enumerable: true,
301
+ value: {},
302
+ writable: false,
303
+ });
304
+ Object.defineProperty(this, 'populateTransaction', {
305
+ enumerable: true,
306
+ value: {},
307
+ writable: false,
308
+ });
309
+ Object.defineProperty(this, 'estimateFee', {
310
+ enumerable: true,
311
+ value: {},
312
+ writable: false,
313
+ });
314
+ this.abi.forEach(function (abiElement) {
315
+ if (abiElement.type !== 'function') {
316
+ return;
317
+ }
318
+ var signature = abiElement.name;
319
+ if (!_this[signature]) {
320
+ Object.defineProperty(_this, signature, {
321
+ enumerable: true,
322
+ value: buildDefault(_this, abiElement),
323
+ writable: false,
324
+ });
325
+ }
326
+ if (!_this.functions[signature]) {
327
+ Object.defineProperty(_this.functions, signature, {
328
+ enumerable: true,
329
+ value: buildDefault(_this, abiElement),
330
+ writable: false,
331
+ });
332
+ }
333
+ if (!_this.callStatic[signature]) {
334
+ Object.defineProperty(_this.callStatic, signature, {
335
+ enumerable: true,
336
+ value: buildCall(_this, abiElement),
337
+ writable: false,
338
+ });
339
+ }
340
+ if (!_this.populateTransaction[signature]) {
341
+ Object.defineProperty(_this.populateTransaction, signature, {
342
+ enumerable: true,
343
+ value: buildPopulate(_this, abiElement),
344
+ writable: false,
345
+ });
346
+ }
347
+ if (!_this.estimateFee[signature]) {
348
+ Object.defineProperty(_this.estimateFee, signature, {
349
+ enumerable: true,
350
+ value: buildEstimate(_this, abiElement),
351
+ writable: false,
352
+ });
353
+ }
354
+ });
225
355
  }
226
356
  /**
227
357
  * Saves the address of the contract deployed on network that will be used for interaction
228
358
  *
229
359
  * @param address - address of the contract
230
- * @returns Contract
231
360
  */
232
- Contract.prototype.connect = function (address) {
233
- this.connectedTo = address;
234
- return this;
361
+ Contract.prototype.attach = function (address) {
362
+ this.address = address;
363
+ };
364
+ /**
365
+ * Attaches to new Provider or Account
366
+ *
367
+ * @param providerOrAccount - new Provider or Account to attach to
368
+ */
369
+ Contract.prototype.connect = function (providerOrAccount) {
370
+ this.providerOrAccount = providerOrAccount;
371
+ };
372
+ /**
373
+ * Resolves when contract is deployed on the network or when no deployment transaction is found
374
+ *
375
+ * @returns Promise that resolves when contract is deployed on the network or when no deployment transaction is found
376
+ * @throws When deployment fails
377
+ */
378
+ Contract.prototype.deployed = function () {
379
+ return __awaiter(this, void 0, void 0, function () {
380
+ return __generator(this, function (_a) {
381
+ switch (_a.label) {
382
+ case 0:
383
+ if (!this.deployTransactionHash) return [3 /*break*/, 2];
384
+ return [
385
+ 4 /*yield*/,
386
+ this.providerOrAccount.waitForTransaction(this.deployTransactionHash),
387
+ ];
388
+ case 1:
389
+ _a.sent();
390
+ this.deployTransactionHash = undefined;
391
+ _a.label = 2;
392
+ case 2:
393
+ return [2 /*return*/, this];
394
+ }
395
+ });
396
+ });
235
397
  };
236
398
  /**
237
399
  * Validates if all arguments that are passed to the method are corresponding to the ones in the abi
@@ -243,7 +405,7 @@ var Contract = /** @class */ (function () {
243
405
  Contract.prototype.validateMethodAndArgs = function (type, method, args) {
244
406
  var _this = this;
245
407
  if (args === void 0) {
246
- args = {};
408
+ args = [];
247
409
  }
248
410
  // ensure provided method exists
249
411
  var invokeableFunctionNames = this.abi
@@ -263,54 +425,95 @@ var Contract = /** @class */ (function () {
263
425
  var methodAbi = this.abi.find(function (abi) {
264
426
  return abi.name === method && abi.type === 'function';
265
427
  });
428
+ var argPosition = 0;
266
429
  methodAbi.inputs.forEach(function (input) {
267
- var arg = args[input.name];
268
- if (arg !== undefined) {
269
- if (input.type === 'felt') {
430
+ if (/_len$/.test(input.name)) {
431
+ return;
432
+ }
433
+ if (input.type === 'felt') {
434
+ (0, minimalistic_assert_1.default)(
435
+ typeof args[argPosition] === 'string' ||
436
+ typeof args[argPosition] === 'number' ||
437
+ args[argPosition] instanceof bn_js_1.default,
438
+ 'arg ' + input.name + ' should be a felt (string, number, BigNumber)'
439
+ );
440
+ argPosition += 1;
441
+ } else if (input.type in _this.structs && typeof args[argPosition] === 'object') {
442
+ if (Array.isArray(args[argPosition])) {
443
+ var structMembersLength = _this.calculateStructMembers(input.type);
270
444
  (0, minimalistic_assert_1.default)(
271
- typeof arg === 'string' || typeof arg === 'number' || arg instanceof bn_js_1.default,
272
- 'arg ' + input.name + ' should be a felt (string, number, BigNumber)'
445
+ args[argPosition].length === structMembersLength,
446
+ 'arg should be of length ' + structMembersLength
273
447
  );
274
- } else if (typeof arg === 'object' && input.type in _this.structs) {
448
+ } else {
275
449
  _this.structs[input.type].members.forEach(function (_a) {
276
450
  var name = _a.name;
277
451
  (0,
278
- minimalistic_assert_1.default)(Object.keys(arg).includes(name), 'arg should have a property ' + name);
452
+ minimalistic_assert_1.default)(Object.keys(args[argPosition]).includes(name), 'arg should have a property ' + name);
279
453
  });
280
- } else {
454
+ }
455
+ argPosition += 1;
456
+ } else {
457
+ (0, minimalistic_assert_1.default)(
458
+ Array.isArray(args[argPosition]),
459
+ 'arg ' + input.name + ' should be an Array'
460
+ );
461
+ if (input.type === 'felt*') {
462
+ args[argPosition].forEach(function (felt) {
463
+ (0,
464
+ 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');
465
+ });
466
+ argPosition += 1;
467
+ } else if (/\(felt/.test(input.type)) {
468
+ var tupleLength = input.type.split(',').length;
281
469
  (0, minimalistic_assert_1.default)(
282
- Array.isArray(arg),
283
- 'arg ' + input.name + ' should be an Array'
470
+ args[argPosition].length === tupleLength,
471
+ 'arg ' + input.name + ' should have ' + tupleLength + ' elements in tuple'
284
472
  );
285
- if (input.type === 'felt*') {
286
- arg.forEach(function (felt) {
287
- (0,
288
- 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');
289
- });
290
- } else if (/\(felt/.test(input.type)) {
291
- var tupleLength = input.type.split(',').length;
292
- (0, minimalistic_assert_1.default)(
293
- arg.length === tupleLength,
294
- 'arg ' + input.name + ' should have ' + tupleLength + ' elements in tuple'
295
- );
296
- arg.forEach(function (felt) {
297
- (0,
298
- 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');
299
- });
300
- } else {
301
- var arrayType_1 = input.type.replace('*', '');
302
- arg.forEach(function (struct) {
303
- _this.structs[arrayType_1].members.forEach(function (_a) {
304
- var name = _a.name;
305
- (0,
306
- minimalistic_assert_1.default)(Object.keys(struct).includes(name), 'arg ' + input.name + ' should be an array of ' + arrayType_1);
307
- });
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 ' + input.name + ' should be an array of string, number or BigNumber');
476
+ });
477
+ argPosition += 1;
478
+ } else {
479
+ var arrayType_1 = input.type.replace('*', '');
480
+ args[argPosition].forEach(function (struct) {
481
+ _this.structs[arrayType_1].members.forEach(function (_a) {
482
+ var name = _a.name;
483
+ if (Array.isArray(struct)) {
484
+ var structMembersLength = _this.calculateStructMembers(arrayType_1);
485
+ (0, minimalistic_assert_1.default)(
486
+ struct.length === structMembersLength,
487
+ 'arg should be of length ' + structMembersLength
488
+ );
489
+ } else {
490
+ (0, minimalistic_assert_1.default)(
491
+ Object.keys(struct).includes(name),
492
+ 'arg ' + input.name + ' should be an array of ' + arrayType_1
493
+ );
494
+ }
308
495
  });
309
- }
496
+ });
497
+ argPosition += 1;
310
498
  }
311
499
  }
312
500
  });
313
501
  };
502
+ /**
503
+ * Deep parse of the object that has been passed to the method
504
+ *
505
+ * @param struct - struct that needs to be calculated
506
+ * @return {number} - number of members for the given struct
507
+ */
508
+ Contract.prototype.calculateStructMembers = function (struct) {
509
+ var _this = this;
510
+ return this.structs[struct].members.reduce(function (acc, member) {
511
+ if (member.type === 'felt') {
512
+ return acc + 1;
513
+ }
514
+ return acc + _this.structMemberNum(member.type);
515
+ }, 0);
516
+ };
314
517
  /**
315
518
  * Deep parse of the object that has been passed to the method
316
519
  *
@@ -318,10 +521,19 @@ var Contract = /** @class */ (function () {
318
521
  * @param type - name of the method
319
522
  * @return {string | string[]} - parsed arguments in format that contract is expecting
320
523
  */
321
- Contract.prototype.parseCalldataObject = function (element, type) {
524
+ Contract.prototype.parseCalldataValue = function (element, type) {
322
525
  var _this = this;
323
526
  if (element === undefined) {
324
- throw Error('Missing element in calldata object');
527
+ throw Error('Missing element in calldata');
528
+ }
529
+ if (Array.isArray(element)) {
530
+ var structMemberNum = this.calculateStructMembers(type);
531
+ if (element.length !== structMemberNum) {
532
+ throw Error('Missing element in calldata');
533
+ }
534
+ return element.map(function (el) {
535
+ return (0, number_1.toFelt)(el);
536
+ });
325
537
  }
326
538
  // checking if the passed element is struct or element in struct
327
539
  if (this.structs[type] && this.structs[type].members.length) {
@@ -330,7 +542,7 @@ var Contract = /** @class */ (function () {
330
542
  // if the member of the struct is another struct this will return array of the felts if not it will be single felt
331
543
  // TODO: refactor types so member name can be used as keyof ParsedStruct
332
544
  /* @ts-ignore */
333
- var parsedData = _this.parseCalldataObject(element[member.name], member.type);
545
+ var parsedData = _this.parseCalldataValue(element[member.name], member.type);
334
546
  if (typeof parsedData === 'string') {
335
547
  acc.push(parsedData);
336
548
  } else {
@@ -367,21 +579,16 @@ var Contract = /** @class */ (function () {
367
579
  * @param input - input(field) information from the abi that will be used to parse the data
368
580
  * @return {string | string[]} - parsed arguments in format that contract is expecting
369
581
  */
370
- Contract.prototype.parsCalldataField = function (args, input) {
582
+ Contract.prototype.parseCalldataField = function (argsIterator, input) {
371
583
  var _this = this;
372
584
  var name = input.name,
373
585
  type = input.type;
374
- var value = args[name];
375
- var propName = name.replace(/_len$/, '');
586
+ var value = argsIterator.next().value;
587
+ var parsedCalldata = [];
376
588
  switch (true) {
377
- case /_len$/.test(name):
378
- if (Array.isArray(args[propName])) {
379
- var arr = args[propName];
380
- return (0, number_1.toFelt)(arr.length);
381
- }
382
- throw Error('Expected ' + propName + ' to be array');
383
589
  case /\*/.test(type):
384
590
  if (Array.isArray(value)) {
591
+ parsedCalldata.push((0, number_1.toFelt)(value.length));
385
592
  return value.reduce(function (acc, el) {
386
593
  if (/felt/.test(type)) {
387
594
  acc.push((0, number_1.toFelt)(el));
@@ -390,17 +597,17 @@ var Contract = /** @class */ (function () {
390
597
  acc,
391
598
  __spreadArray(
392
599
  [],
393
- __read(_this.parseCalldataObject(el, type.replace('*', ''))),
600
+ __read(_this.parseCalldataValue(el, type.replace('*', ''))),
394
601
  false
395
602
  )
396
603
  );
397
604
  }
398
605
  return acc;
399
- }, []);
606
+ }, parsedCalldata);
400
607
  }
401
608
  throw Error('Expected ' + name + ' to be array');
402
609
  case type in this.structs:
403
- return this.parseCalldataObject(value, type);
610
+ return this.parseCalldataValue(value, type);
404
611
  case /\(felt/.test(type):
405
612
  if (Array.isArray(value)) {
406
613
  return value.map(function (el) {
@@ -421,8 +628,12 @@ var Contract = /** @class */ (function () {
421
628
  */
422
629
  Contract.prototype.compileCalldata = function (args, inputs) {
423
630
  var _this = this;
631
+ var argsIterator = args[Symbol.iterator]();
424
632
  return inputs.reduce(function (acc, input) {
425
- var parsedData = _this.parsCalldataField(args, input);
633
+ if (/_len$/.test(input.name)) {
634
+ return acc;
635
+ }
636
+ var parsedData = _this.parseCalldataField(argsIterator, input);
426
637
  if (Array.isArray(parsedData)) {
427
638
  acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
428
639
  } else {
@@ -441,7 +652,6 @@ var Contract = /** @class */ (function () {
441
652
  Contract.prototype.parseResponseField = function (responseIterator, output, parsedResult) {
442
653
  var name = output.name,
443
654
  type = output.type;
444
- var arrLen;
445
655
  var parsedDataArr = [];
446
656
  switch (true) {
447
657
  case /_len$/.test(name):
@@ -453,7 +663,7 @@ var Contract = /** @class */ (function () {
453
663
  }, []);
454
664
  case /\*/.test(type):
455
665
  if (parsedResult && parsedResult[name + '_len']) {
456
- arrLen = parsedResult[name + '_len'];
666
+ var arrLen = parsedResult[name + '_len'];
457
667
  while (parsedDataArr.length < arrLen) {
458
668
  parsedDataArr.push(
459
669
  this.parseResponseStruct(responseIterator, output.type.replace('*', ''))
@@ -480,21 +690,29 @@ var Contract = /** @class */ (function () {
480
690
  return abi.name === method;
481
691
  }).outputs;
482
692
  var responseIterator = response.flat()[Symbol.iterator]();
483
- return outputs.flat().reduce(function (acc, output) {
693
+ var resultObject = outputs.flat().reduce(function (acc, output) {
484
694
  acc[output.name] = _this.parseResponseField(responseIterator, output, acc);
485
695
  if (acc[output.name] && acc[output.name + '_len']) {
486
696
  delete acc[output.name + '_len'];
487
697
  }
488
698
  return acc;
489
699
  }, {});
700
+ return Object.entries(resultObject).reduce(function (acc, _a) {
701
+ var _b = __read(_a, 2),
702
+ key = _b[0],
703
+ value = _b[1];
704
+ acc.push(value);
705
+ acc[key] = value;
706
+ return acc;
707
+ }, []);
490
708
  };
491
- Contract.prototype.invoke = function (method, args, signature) {
709
+ Contract.prototype.invoke = function (method, args) {
492
710
  if (args === void 0) {
493
- args = {};
711
+ args = [];
494
712
  }
495
713
  // ensure contract is connected
496
714
  (0, minimalistic_assert_1.default)(
497
- this.connectedTo !== null,
715
+ this.address !== null,
498
716
  'contract isnt connected to an address'
499
717
  );
500
718
  // validate method and args
@@ -502,45 +720,72 @@ var Contract = /** @class */ (function () {
502
720
  var inputs = this.abi.find(function (abi) {
503
721
  return abi.name === method;
504
722
  }).inputs;
723
+ var inputsLength = inputs.reduce(function (acc, input) {
724
+ if (!/_len$/.test(input.name)) {
725
+ return acc + 1;
726
+ }
727
+ return acc;
728
+ }, 0);
729
+ var signature = [];
730
+ if (args.length === inputsLength + 1 && Array.isArray(args[args.length - 1])) {
731
+ signature.push.apply(signature, __spreadArray([], __read(args.pop()), false));
732
+ }
733
+ if (args.length !== inputsLength) {
734
+ throw Error(
735
+ 'Invalid number of arguments, expected ' +
736
+ inputsLength +
737
+ ' arguments, but got ' +
738
+ args.length
739
+ );
740
+ }
505
741
  // compile calldata
506
742
  var calldata = this.compileCalldata(args, inputs);
507
- return this.provider.invokeFunction({
508
- contractAddress: this.connectedTo,
509
- signature: signature,
743
+ var invocation = {
744
+ contractAddress: this.address,
510
745
  calldata: calldata,
511
746
  entrypoint: method,
512
- });
747
+ };
748
+ if ('execute' in this.providerOrAccount) {
749
+ return this.providerOrAccount.execute(invocation);
750
+ }
751
+ return this.providerOrAccount.invokeFunction(
752
+ __assign(__assign({}, invocation), { signature: signature })
753
+ );
513
754
  };
514
- Contract.prototype.call = function (method, args, blockIdentifier) {
755
+ Contract.prototype.call = function (method, args) {
515
756
  if (args === void 0) {
516
- args = {};
517
- }
518
- if (blockIdentifier === void 0) {
519
- blockIdentifier = null;
757
+ args = [];
520
758
  }
521
759
  return __awaiter(this, void 0, void 0, function () {
522
- var inputs, calldata;
760
+ var inputs, inputsLength, options, calldata;
523
761
  var _this = this;
524
762
  return __generator(this, function (_a) {
525
763
  // ensure contract is connected
526
764
  (0,
527
- minimalistic_assert_1.default)(this.connectedTo !== null, 'contract isnt connected to an address');
765
+ minimalistic_assert_1.default)(this.address !== null, 'contract isnt connected to an address');
528
766
  // validate method and args
529
767
  this.validateMethodAndArgs('CALL', method, args);
530
768
  inputs = this.abi.find(function (abi) {
531
769
  return abi.name === method;
532
770
  }).inputs;
771
+ inputsLength = inputs.length;
772
+ options = {
773
+ blockIdentifier: null,
774
+ };
775
+ if (args.length === inputsLength + 1 && typeof args[args.length - 1] === 'object') {
776
+ Object.assign(options, args.pop());
777
+ }
533
778
  calldata = this.compileCalldata(args, inputs);
534
779
  return [
535
780
  2 /*return*/,
536
- this.provider
781
+ this.providerOrAccount
537
782
  .callContract(
538
783
  {
539
- contractAddress: this.connectedTo,
784
+ contractAddress: this.address,
540
785
  calldata: calldata,
541
786
  entrypoint: method,
542
787
  },
543
- blockIdentifier
788
+ options
544
789
  )
545
790
  .then(function (x) {
546
791
  return _this.parseResponse(method, x.result);
@@ -549,6 +794,31 @@ var Contract = /** @class */ (function () {
549
794
  });
550
795
  });
551
796
  };
797
+ Contract.prototype.estimate = function (_method, _args) {
798
+ if (_args === void 0) {
799
+ _args = [];
800
+ }
801
+ return __awaiter(this, void 0, void 0, function () {
802
+ return __generator(this, function (_a) {
803
+ // TODO; remove error as soon as estimate fees are supported
804
+ throw Error('Estimation of the fees are not yet supported');
805
+ });
806
+ });
807
+ };
808
+ Contract.prototype.populate = function (method, args) {
809
+ if (args === void 0) {
810
+ args = [];
811
+ }
812
+ var inputs = this.abi.find(function (abi) {
813
+ return abi.name === method;
814
+ }).inputs;
815
+ return {
816
+ contractAddress: this.address,
817
+ entrypoint: (0, hash_1.getSelectorFromName)(method),
818
+ calldata: this.compileCalldata(args, inputs),
819
+ signature: [],
820
+ };
821
+ };
552
822
  return Contract;
553
823
  })();
554
824
  exports.Contract = Contract;
@@ -0,0 +1,3 @@
1
+ export * from './default';
2
+ export * from './interface';
3
+ export * from './contractFactory';
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+ var __createBinding =
3
+ (this && this.__createBinding) ||
4
+ (Object.create
5
+ ? function (o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ Object.defineProperty(o, k2, {
8
+ enumerable: true,
9
+ get: function () {
10
+ return m[k];
11
+ },
12
+ });
13
+ }
14
+ : function (o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ o[k2] = m[k];
17
+ });
18
+ var __exportStar =
19
+ (this && this.__exportStar) ||
20
+ function (m, exports) {
21
+ for (var p in m)
22
+ if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p))
23
+ __createBinding(exports, m, p);
24
+ };
25
+ Object.defineProperty(exports, '__esModule', { value: true });
26
+ __exportStar(require('./default'), exports);
27
+ __exportStar(require('./interface'), exports);
28
+ __exportStar(require('./contractFactory'), exports);