starknet 4.16.0 → 4.17.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.
package/index.js CHANGED
@@ -62,8 +62,7 @@ __export(src_exports, {
62
62
  module.exports = __toCommonJS(src_exports);
63
63
 
64
64
  // src/contract/default.ts
65
- var import_bn3 = __toESM(require("bn.js"));
66
- var import_minimalistic_assert4 = __toESM(require("minimalistic-assert"));
65
+ var import_minimalistic_assert5 = __toESM(require("minimalistic-assert"));
67
66
 
68
67
  // src/utils/fetchPonyfill.ts
69
68
  var import_isomorphic_fetch = __toESM(require("isomorphic-fetch"));
@@ -3524,6 +3523,187 @@ var ProviderInterface = class {
3524
3523
  // src/provider/index.ts
3525
3524
  var defaultProvider = new Provider();
3526
3525
 
3526
+ // src/utils/calldata.ts
3527
+ var import_bn3 = __toESM(require("bn.js"));
3528
+ var import_minimalistic_assert4 = __toESM(require("minimalistic-assert"));
3529
+ var CheckCallData = class {
3530
+ constructor(abi) {
3531
+ this.abi = abi;
3532
+ this.structs = abi.filter((abiEntry) => abiEntry.type === "struct").reduce(
3533
+ (acc, abiEntry) => ({
3534
+ ...acc,
3535
+ [abiEntry.name]: abiEntry
3536
+ }),
3537
+ {}
3538
+ );
3539
+ }
3540
+ compileCalldata(args, inputs) {
3541
+ const argsIterator = args[Symbol.iterator]();
3542
+ return inputs.reduce((acc, input) => {
3543
+ if (/_len$/.test(input.name)) {
3544
+ return acc;
3545
+ }
3546
+ const parsedData = this.parseCalldataField(argsIterator, input);
3547
+ if (Array.isArray(parsedData)) {
3548
+ acc.push(...parsedData);
3549
+ } else {
3550
+ acc.push(parsedData);
3551
+ }
3552
+ return acc;
3553
+ }, []);
3554
+ }
3555
+ validateMethodAndArgs(type, method, args = []) {
3556
+ if (type !== "DEPLOY") {
3557
+ const invocableFunctionNames = this.abi.filter((abi) => {
3558
+ if (abi.type !== "function")
3559
+ return false;
3560
+ const isView = abi.stateMutability === "view";
3561
+ return type === "INVOKE" ? !isView : isView;
3562
+ }).map((abi) => abi.name);
3563
+ (0, import_minimalistic_assert4.default)(
3564
+ invocableFunctionNames.includes(method),
3565
+ `${type === "INVOKE" ? "invocable" : "viewable"} method not found in abi`
3566
+ );
3567
+ }
3568
+ const methodAbi = this.abi.find(
3569
+ (abi) => type === "DEPLOY" ? abi.name === method && abi.type === method : abi.name === method && abi.type === "function"
3570
+ );
3571
+ let argPosition = 0;
3572
+ methodAbi.inputs.forEach((input) => {
3573
+ if (/_len$/.test(input.name)) {
3574
+ return;
3575
+ }
3576
+ if (input.type === "felt") {
3577
+ (0, import_minimalistic_assert4.default)(
3578
+ typeof args[argPosition] === "string" || typeof args[argPosition] === "number" || args[argPosition] instanceof import_bn3.default,
3579
+ `arg ${input.name} should be a felt (string, number, BigNumber)`
3580
+ );
3581
+ argPosition += 1;
3582
+ } else if (input.type in this.structs && typeof args[argPosition] === "object") {
3583
+ if (Array.isArray(args[argPosition])) {
3584
+ const structMembersLength = this.calculateStructMembers(input.type);
3585
+ (0, import_minimalistic_assert4.default)(
3586
+ args[argPosition].length === structMembersLength,
3587
+ `arg should be of length ${structMembersLength}`
3588
+ );
3589
+ } else {
3590
+ this.structs[input.type].members.forEach(({ name }) => {
3591
+ (0, import_minimalistic_assert4.default)(
3592
+ Object.keys(args[argPosition]).includes(name),
3593
+ `arg should have a property ${name}`
3594
+ );
3595
+ });
3596
+ }
3597
+ argPosition += 1;
3598
+ } else {
3599
+ (0, import_minimalistic_assert4.default)(Array.isArray(args[argPosition]), `arg ${input.name} should be an Array`);
3600
+ if (input.type === "felt*") {
3601
+ args[argPosition].forEach((felt) => {
3602
+ (0, import_minimalistic_assert4.default)(
3603
+ typeof felt === "string" || typeof felt === "number" || felt instanceof import_bn3.default,
3604
+ `arg ${input.name} should be an array of string, number or BigNumber`
3605
+ );
3606
+ });
3607
+ argPosition += 1;
3608
+ } else if (/\(felt/.test(input.type)) {
3609
+ const tupleLength = input.type.split(",").length;
3610
+ (0, import_minimalistic_assert4.default)(
3611
+ args[argPosition].length === tupleLength,
3612
+ `arg ${input.name} should have ${tupleLength} elements in tuple`
3613
+ );
3614
+ args[argPosition].forEach((felt) => {
3615
+ (0, import_minimalistic_assert4.default)(
3616
+ typeof felt === "string" || typeof felt === "number" || felt instanceof import_bn3.default,
3617
+ `arg ${input.name} should be an array of string, number or BigNumber`
3618
+ );
3619
+ });
3620
+ argPosition += 1;
3621
+ } else {
3622
+ const arrayType = input.type.replace("*", "");
3623
+ args[argPosition].forEach((struct) => {
3624
+ this.structs[arrayType].members.forEach(({ name }) => {
3625
+ if (Array.isArray(struct)) {
3626
+ const structMembersLength = this.calculateStructMembers(arrayType);
3627
+ (0, import_minimalistic_assert4.default)(
3628
+ struct.length === structMembersLength,
3629
+ `arg should be of length ${structMembersLength}`
3630
+ );
3631
+ } else {
3632
+ (0, import_minimalistic_assert4.default)(
3633
+ Object.keys(struct).includes(name),
3634
+ `arg ${input.name} should be an array of ${arrayType}`
3635
+ );
3636
+ }
3637
+ });
3638
+ });
3639
+ argPosition += 1;
3640
+ }
3641
+ }
3642
+ });
3643
+ }
3644
+ parseCalldataField(argsIterator, input) {
3645
+ const { name, type } = input;
3646
+ const { value } = argsIterator.next();
3647
+ const parsedCalldata = [];
3648
+ switch (true) {
3649
+ case /\*/.test(type):
3650
+ if (Array.isArray(value)) {
3651
+ parsedCalldata.push(toFelt(value.length));
3652
+ return value.reduce((acc, el) => {
3653
+ if (/felt/.test(type)) {
3654
+ acc.push(toFelt(el));
3655
+ } else {
3656
+ acc.push(...this.parseCalldataValue(el, type.replace("*", "")));
3657
+ }
3658
+ return acc;
3659
+ }, parsedCalldata);
3660
+ }
3661
+ throw Error(`Expected ${name} to be array`);
3662
+ case type in this.structs:
3663
+ return this.parseCalldataValue(value, type);
3664
+ case /\(felt/.test(type):
3665
+ if (Array.isArray(value)) {
3666
+ return value.map((el) => toFelt(el));
3667
+ }
3668
+ throw Error(`Expected ${name} to be array`);
3669
+ default:
3670
+ return toFelt(value);
3671
+ }
3672
+ }
3673
+ parseCalldataValue(element, type) {
3674
+ if (element === void 0) {
3675
+ throw Error("Missing element in calldata");
3676
+ }
3677
+ if (Array.isArray(element)) {
3678
+ const structMemberNum = this.calculateStructMembers(type);
3679
+ if (element.length !== structMemberNum) {
3680
+ throw Error("Missing element in calldata");
3681
+ }
3682
+ return element.map((el) => toFelt(el));
3683
+ }
3684
+ if (this.structs[type] && this.structs[type].members.length) {
3685
+ return this.structs[type].members.reduce((acc, member) => {
3686
+ const parsedData = this.parseCalldataValue(element[member.name], member.type);
3687
+ if (typeof parsedData === "string") {
3688
+ acc.push(parsedData);
3689
+ } else {
3690
+ acc.push(...parsedData);
3691
+ }
3692
+ return acc;
3693
+ }, []);
3694
+ }
3695
+ return toFelt(element);
3696
+ }
3697
+ calculateStructMembers(struct) {
3698
+ return this.structs[struct].members.reduce((acc, member) => {
3699
+ if (member.type === "felt") {
3700
+ return acc + 1;
3701
+ }
3702
+ return acc + this.calculateStructMembers(member.type);
3703
+ }, 0);
3704
+ }
3705
+ };
3706
+
3527
3707
  // src/contract/default.ts
3528
3708
  function parseFelt(candidate) {
3529
3709
  try {
@@ -3587,6 +3767,7 @@ var Contract = class {
3587
3767
  }),
3588
3768
  {}
3589
3769
  );
3770
+ this.checkCalldata = new CheckCallData(abi);
3590
3771
  Object.defineProperty(this, "functions", {
3591
3772
  enumerable: true,
3592
3773
  value: {},
@@ -3664,10 +3845,10 @@ var Contract = class {
3664
3845
  }
3665
3846
  async call(method, args = [], options = {}) {
3666
3847
  const blockIdentifier = (options == null ? void 0 : options.blockIdentifier) || void 0;
3667
- (0, import_minimalistic_assert4.default)(this.address !== null, "contract is not connected to an address");
3668
- this.validateMethodAndArgs("CALL", method, args);
3848
+ (0, import_minimalistic_assert5.default)(this.address !== null, "contract is not connected to an address");
3849
+ this.checkCalldata.validateMethodAndArgs("CALL", method, args);
3669
3850
  const { inputs } = this.abi.find((abi) => abi.name === method);
3670
- const calldata = this.compileCalldata(args, inputs);
3851
+ const calldata = this.checkCalldata.compileCalldata(args, inputs);
3671
3852
  return this.providerOrAccount.callContract(
3672
3853
  {
3673
3854
  contractAddress: this.address,
@@ -3678,8 +3859,8 @@ var Contract = class {
3678
3859
  ).then((x) => this.parseResponse(method, x.result));
3679
3860
  }
3680
3861
  invoke(method, args = [], options = {}) {
3681
- (0, import_minimalistic_assert4.default)(this.address !== null, "contract is not connected to an address");
3682
- this.validateMethodAndArgs("INVOKE", method, args);
3862
+ (0, import_minimalistic_assert5.default)(this.address !== null, "contract is not connected to an address");
3863
+ this.checkCalldata.validateMethodAndArgs("INVOKE", method, args);
3683
3864
  const { inputs } = this.abi.find((abi) => abi.name === method);
3684
3865
  const inputsLength = inputs.reduce((acc, input) => {
3685
3866
  if (!/_len$/.test(input.name)) {
@@ -3692,7 +3873,7 @@ var Contract = class {
3692
3873
  `Invalid number of arguments, expected ${inputsLength} arguments, but got ${args.length}`
3693
3874
  );
3694
3875
  }
3695
- const calldata = this.compileCalldata(args, inputs);
3876
+ const calldata = this.checkCalldata.compileCalldata(args, inputs);
3696
3877
  const invocation = {
3697
3878
  contractAddress: this.address,
3698
3879
  calldata,
@@ -3719,8 +3900,8 @@ var Contract = class {
3719
3900
  );
3720
3901
  }
3721
3902
  async estimate(method, args = []) {
3722
- (0, import_minimalistic_assert4.default)(this.address !== null, "contract is not connected to an address");
3723
- this.validateMethodAndArgs("INVOKE", method, args);
3903
+ (0, import_minimalistic_assert5.default)(this.address !== null, "contract is not connected to an address");
3904
+ this.checkCalldata.validateMethodAndArgs("INVOKE", method, args);
3724
3905
  const invocation = this.populateTransaction[method](...args);
3725
3906
  if ("estimateInvokeFee" in this.providerOrAccount) {
3726
3907
  return this.providerOrAccount.estimateInvokeFee(invocation);
@@ -3732,128 +3913,9 @@ var Contract = class {
3732
3913
  return {
3733
3914
  contractAddress: this.address,
3734
3915
  entrypoint: method,
3735
- calldata: this.compileCalldata(args, inputs)
3916
+ calldata: this.checkCalldata.compileCalldata(args, inputs)
3736
3917
  };
3737
3918
  }
3738
- calculateStructMembers(struct) {
3739
- return this.structs[struct].members.reduce((acc, member) => {
3740
- if (member.type === "felt") {
3741
- return acc + 1;
3742
- }
3743
- return acc + this.calculateStructMembers(member.type);
3744
- }, 0);
3745
- }
3746
- validateMethodAndArgs(type, method, args = []) {
3747
- const invocableFunctionNames = this.abi.filter((abi) => {
3748
- if (abi.type !== "function")
3749
- return false;
3750
- const isView = abi.stateMutability === "view";
3751
- return type === "INVOKE" ? !isView : isView;
3752
- }).map((abi) => abi.name);
3753
- (0, import_minimalistic_assert4.default)(
3754
- invocableFunctionNames.includes(method),
3755
- `${type === "INVOKE" ? "invocable" : "viewable"} method not found in abi`
3756
- );
3757
- const methodAbi = this.abi.find(
3758
- (abi) => abi.name === method && abi.type === "function"
3759
- );
3760
- let argPosition = 0;
3761
- methodAbi.inputs.forEach((input) => {
3762
- if (/_len$/.test(input.name)) {
3763
- return;
3764
- }
3765
- if (input.type === "felt") {
3766
- (0, import_minimalistic_assert4.default)(
3767
- typeof args[argPosition] === "string" || typeof args[argPosition] === "number" || args[argPosition] instanceof import_bn3.default,
3768
- `arg ${input.name} should be a felt (string, number, BigNumber)`
3769
- );
3770
- argPosition += 1;
3771
- } else if (input.type in this.structs && typeof args[argPosition] === "object") {
3772
- if (Array.isArray(args[argPosition])) {
3773
- const structMembersLength = this.calculateStructMembers(input.type);
3774
- (0, import_minimalistic_assert4.default)(
3775
- args[argPosition].length === structMembersLength,
3776
- `arg should be of length ${structMembersLength}`
3777
- );
3778
- } else {
3779
- this.structs[input.type].members.forEach(({ name }) => {
3780
- (0, import_minimalistic_assert4.default)(
3781
- Object.keys(args[argPosition]).includes(name),
3782
- `arg should have a property ${name}`
3783
- );
3784
- });
3785
- }
3786
- argPosition += 1;
3787
- } else {
3788
- (0, import_minimalistic_assert4.default)(Array.isArray(args[argPosition]), `arg ${input.name} should be an Array`);
3789
- if (input.type === "felt*") {
3790
- args[argPosition].forEach((felt) => {
3791
- (0, import_minimalistic_assert4.default)(
3792
- typeof felt === "string" || typeof felt === "number" || felt instanceof import_bn3.default,
3793
- `arg ${input.name} should be an array of string, number or BigNumber`
3794
- );
3795
- });
3796
- argPosition += 1;
3797
- } else if (/\(felt/.test(input.type)) {
3798
- const tupleLength = input.type.split(",").length;
3799
- (0, import_minimalistic_assert4.default)(
3800
- args[argPosition].length === tupleLength,
3801
- `arg ${input.name} should have ${tupleLength} elements in tuple`
3802
- );
3803
- args[argPosition].forEach((felt) => {
3804
- (0, import_minimalistic_assert4.default)(
3805
- typeof felt === "string" || typeof felt === "number" || felt instanceof import_bn3.default,
3806
- `arg ${input.name} should be an array of string, number or BigNumber`
3807
- );
3808
- });
3809
- argPosition += 1;
3810
- } else {
3811
- const arrayType = input.type.replace("*", "");
3812
- args[argPosition].forEach((struct) => {
3813
- this.structs[arrayType].members.forEach(({ name }) => {
3814
- if (Array.isArray(struct)) {
3815
- const structMembersLength = this.calculateStructMembers(arrayType);
3816
- (0, import_minimalistic_assert4.default)(
3817
- struct.length === structMembersLength,
3818
- `arg should be of length ${structMembersLength}`
3819
- );
3820
- } else {
3821
- (0, import_minimalistic_assert4.default)(
3822
- Object.keys(struct).includes(name),
3823
- `arg ${input.name} should be an array of ${arrayType}`
3824
- );
3825
- }
3826
- });
3827
- });
3828
- argPosition += 1;
3829
- }
3830
- }
3831
- });
3832
- }
3833
- parseCalldataValue(element, type) {
3834
- if (element === void 0) {
3835
- throw Error("Missing element in calldata");
3836
- }
3837
- if (Array.isArray(element)) {
3838
- const structMemberNum = this.calculateStructMembers(type);
3839
- if (element.length !== structMemberNum) {
3840
- throw Error("Missing element in calldata");
3841
- }
3842
- return element.map((el) => toFelt(el));
3843
- }
3844
- if (this.structs[type] && this.structs[type].members.length) {
3845
- return this.structs[type].members.reduce((acc, member) => {
3846
- const parsedData = this.parseCalldataValue(element[member.name], member.type);
3847
- if (typeof parsedData === "string") {
3848
- acc.push(parsedData);
3849
- } else {
3850
- acc.push(...parsedData);
3851
- }
3852
- return acc;
3853
- }, []);
3854
- }
3855
- return toFelt(element);
3856
- }
3857
3919
  parseResponseStruct(responseIterator, type) {
3858
3920
  if (type in this.structs && this.structs[type]) {
3859
3921
  return this.structs[type].members.reduce((acc, el) => {
@@ -3863,50 +3925,6 @@ var Contract = class {
3863
3925
  }
3864
3926
  return parseFelt(responseIterator.next().value);
3865
3927
  }
3866
- parseCalldataField(argsIterator, input) {
3867
- const { name, type } = input;
3868
- const { value } = argsIterator.next();
3869
- const parsedCalldata = [];
3870
- switch (true) {
3871
- case /\*/.test(type):
3872
- if (Array.isArray(value)) {
3873
- parsedCalldata.push(toFelt(value.length));
3874
- return value.reduce((acc, el) => {
3875
- if (/felt/.test(type)) {
3876
- acc.push(toFelt(el));
3877
- } else {
3878
- acc.push(...this.parseCalldataValue(el, type.replace("*", "")));
3879
- }
3880
- return acc;
3881
- }, parsedCalldata);
3882
- }
3883
- throw Error(`Expected ${name} to be array`);
3884
- case type in this.structs:
3885
- return this.parseCalldataValue(value, type);
3886
- case /\(felt/.test(type):
3887
- if (Array.isArray(value)) {
3888
- return value.map((el) => toFelt(el));
3889
- }
3890
- throw Error(`Expected ${name} to be array`);
3891
- default:
3892
- return toFelt(value);
3893
- }
3894
- }
3895
- compileCalldata(args, inputs) {
3896
- const argsIterator = args[Symbol.iterator]();
3897
- return inputs.reduce((acc, input) => {
3898
- if (/_len$/.test(input.name)) {
3899
- return acc;
3900
- }
3901
- const parsedData = this.parseCalldataField(argsIterator, input);
3902
- if (Array.isArray(parsedData)) {
3903
- acc.push(...parsedData);
3904
- } else {
3905
- acc.push(parsedData);
3906
- }
3907
- return acc;
3908
- }, []);
3909
- }
3910
3928
  parseResponseField(responseIterator, output, parsedResult) {
3911
3929
  const { name, type } = output;
3912
3930
  const parsedDataArr = [];
@@ -3957,15 +3975,19 @@ var ContractInterface = class {
3957
3975
  };
3958
3976
 
3959
3977
  // src/contract/contractFactory.ts
3960
- var import_minimalistic_assert5 = __toESM(require("minimalistic-assert"));
3978
+ var import_minimalistic_assert6 = __toESM(require("minimalistic-assert"));
3961
3979
  var ContractFactory = class {
3962
3980
  constructor(compiledContract, classHash, account, abi = compiledContract.abi) {
3963
3981
  this.abi = abi;
3964
3982
  this.compiledContract = compiledContract;
3965
3983
  this.account = account;
3966
3984
  this.classHash = classHash;
3985
+ this.checkCalldata = new CheckCallData(abi);
3967
3986
  }
3968
- async deploy(constructorCalldata, addressSalt) {
3987
+ async deploy(args = [], addressSalt) {
3988
+ this.checkCalldata.validateMethodAndArgs("DEPLOY", "constructor", args);
3989
+ const { inputs } = this.abi.find((abi) => abi.type === "constructor");
3990
+ const constructorCalldata = this.checkCalldata.compileCalldata(args, inputs);
3969
3991
  const {
3970
3992
  deploy: { contract_address, transaction_hash }
3971
3993
  } = await this.account.declareDeploy({
@@ -3974,7 +3996,7 @@ var ContractFactory = class {
3974
3996
  constructorCalldata,
3975
3997
  salt: addressSalt
3976
3998
  });
3977
- (0, import_minimalistic_assert5.default)(Boolean(contract_address), "Deployment of the contract failed");
3999
+ (0, import_minimalistic_assert6.default)(Boolean(contract_address), "Deployment of the contract failed");
3978
4000
  const contractInstance = new Contract(
3979
4001
  this.compiledContract.abi,
3980
4002
  contract_address,