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