starknet 5.15.1 → 5.16.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/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # [5.16.0](https://github.com/0xs34n/starknet.js/compare/v5.15.1...v5.16.0) (2023-07-03)
2
+
3
+ ### Features
4
+
5
+ - cairo1 version2 support ([e564033](https://github.com/0xs34n/starknet.js/commit/e564033273ac49e971bbf1db843fb3de236976c0))
6
+ - extract parser from CallData and Cairo ([b7eba2a](https://github.com/0xs34n/starknet.js/commit/b7eba2a1eada3dddb4dc0575c16ac76a42a78678))
7
+ - parsers ([cce9029](https://github.com/0xs34n/starknet.js/commit/cce90299e59e60090b568be14331378de3918924))
8
+
1
9
  ## [5.15.1](https://github.com/0xs34n/starknet.js/compare/v5.15.0...v5.15.1) (2023-06-29)
2
10
 
3
11
  ### Bug Fixes
package/dist/index.d.ts CHANGED
@@ -3193,7 +3193,7 @@ declare function getVersionsByType(versionType?: 'fee' | 'transaction'): {
3193
3193
  };
3194
3194
  declare function computeHashOnElements(data: BigNumberish[]): string;
3195
3195
  declare function calculateTransactionHashCommon(txHashPrefix: TransactionHashPrefix, version: BigNumberish, contractAddress: BigNumberish, entryPointSelector: BigNumberish, calldata: RawCalldata, maxFee: BigNumberish, chainId: StarknetChainId, additionalData?: BigNumberish[]): string;
3196
- declare function calculateDeployTransactionHash(contractAddress: BigNumberish, constructorCalldata: RawCalldata, version: BigNumberish, chainId: StarknetChainId): string;
3196
+ declare function calculateDeployTransactionHash(contractAddress: BigNumberish, constructorCalldata: RawCalldata, version: BigNumberish, chainId: StarknetChainId, constructorName?: string): string;
3197
3197
  declare function calculateDeclareTransactionHash(classHash: string, senderAddress: BigNumberish, version: BigNumberish, maxFee: BigNumberish, chainId: StarknetChainId, nonce: BigNumberish, compiledClassHash?: string): string;
3198
3198
  declare function calculateDeployAccountTransactionHash(contractAddress: BigNumberish, classHash: BigNumberish, constructorCalldata: RawCalldata, salt: BigNumberish, version: BigNumberish, maxFee: BigNumberish, chainId: StarknetChainId, nonce: BigNumberish): string;
3199
3199
  declare function calculateTransactionHash(contractAddress: BigNumberish, version: BigNumberish, calldata: RawCalldata, maxFee: BigNumberish, chainId: StarknetChainId, nonce: BigNumberish): string;
@@ -3709,6 +3709,26 @@ declare function validateChecksumAddress(address: string): boolean;
3709
3709
  declare function isUrl(s?: string): boolean;
3710
3710
  declare function buildUrl(baseUrl: string, defaultPath: string, urlOrPath?: string): string;
3711
3711
 
3712
+ declare abstract class AbiParserInterface {
3713
+ /**
3714
+ * Helper to calculate inputs length from abi
3715
+ * @param abiMethod FunctionAbi
3716
+ * @return number
3717
+ */
3718
+ abstract methodInputsLength(abiMethod: FunctionAbi): number;
3719
+ /**
3720
+ *
3721
+ * @param name string
3722
+ * @return FunctionAbi | undefined
3723
+ */
3724
+ abstract getMethod(name: string): FunctionAbi | undefined;
3725
+ /**
3726
+ * Return Abi in legacy format
3727
+ * @return Abi
3728
+ */
3729
+ abstract getLegacyFormat(): Abi;
3730
+ }
3731
+
3712
3732
  declare const isLen: (name: string) => boolean;
3713
3733
  declare const isTypeFelt: (type: string) => boolean;
3714
3734
  declare const isTypeArray: (type: string) => boolean;
@@ -3789,6 +3809,7 @@ declare namespace cairo {
3789
3809
 
3790
3810
  declare class CallData {
3791
3811
  abi: Abi;
3812
+ parser: AbiParserInterface;
3792
3813
  protected readonly structs: AbiStructs;
3793
3814
  constructor(abi: Abi);
3794
3815
  /**
@@ -3834,12 +3855,6 @@ declare class CallData {
3834
3855
  * @returns Result - parsed and formatted response object
3835
3856
  */
3836
3857
  format(method: string, response: string[], format: object): Result;
3837
- /**
3838
- * Helper to calculate inputs from abi
3839
- * @param inputs AbiEntry
3840
- * @returns number
3841
- */
3842
- static abiInputsLength(inputs: AbiEntry[]): number;
3843
3858
  /**
3844
3859
  * Helper to extract structs from abi
3845
3860
  * @param abi Abi
@@ -5727,6 +5727,9 @@ var starknet = (() => {
5727
5727
  function isCairo1Abi(abi) {
5728
5728
  const firstFunction = abi.find((entry) => entry.type === "function");
5729
5729
  if (!firstFunction) {
5730
+ if (abi.find((it) => it.type === "interface")) {
5731
+ return true;
5732
+ }
5730
5733
  throw new Error(`Error in ABI. No function in ABI.`);
5731
5734
  }
5732
5735
  if (firstFunction.inputs.length) {
@@ -5828,6 +5831,92 @@ var starknet = (() => {
5828
5831
  }, {});
5829
5832
  }
5830
5833
 
5834
+ // src/utils/calldata/parser/parser-0-1.1.0.ts
5835
+ var AbiParser1 = class {
5836
+ constructor(abi) {
5837
+ this.abi = abi;
5838
+ }
5839
+ /**
5840
+ * abi method inputs length without '_len' inputs
5841
+ * cairo 0 reducer
5842
+ * @param abiMethod FunctionAbi
5843
+ * @returns number
5844
+ */
5845
+ methodInputsLength(abiMethod) {
5846
+ return abiMethod.inputs.reduce((acc, input) => !isLen(input.name) ? acc + 1 : acc, 0);
5847
+ }
5848
+ /**
5849
+ * get method definition from abi
5850
+ * @param name string
5851
+ * @returns FunctionAbi | undefined
5852
+ */
5853
+ getMethod(name) {
5854
+ return this.abi.find((it) => it.name === name);
5855
+ }
5856
+ /**
5857
+ * Get Abi in legacy format
5858
+ * @returns Abi
5859
+ */
5860
+ getLegacyFormat() {
5861
+ return this.abi;
5862
+ }
5863
+ };
5864
+
5865
+ // src/utils/calldata/parser/parser-2.0.0.ts
5866
+ var AbiParser2 = class {
5867
+ constructor(abi) {
5868
+ this.abi = abi;
5869
+ }
5870
+ /**
5871
+ * abi method inputs length
5872
+ * @param abiMethod FunctionAbi
5873
+ * @returns number
5874
+ */
5875
+ methodInputsLength(abiMethod) {
5876
+ return abiMethod.inputs.length;
5877
+ }
5878
+ /**
5879
+ * get method definition from abi
5880
+ * @param name string
5881
+ * @returns FunctionAbi | undefined
5882
+ */
5883
+ getMethod(name) {
5884
+ const intf = this.abi.find((it) => it.type === "interface");
5885
+ return intf.items.find((it) => it.name === name);
5886
+ }
5887
+ /**
5888
+ * Get Abi in legacy format
5889
+ * @returns Abi
5890
+ */
5891
+ getLegacyFormat() {
5892
+ return this.abi.flatMap((e) => {
5893
+ if (e.type === "interface") {
5894
+ return e.items;
5895
+ }
5896
+ return e;
5897
+ });
5898
+ }
5899
+ };
5900
+
5901
+ // src/utils/calldata/parser/index.ts
5902
+ function createAbiParser(abi) {
5903
+ const version = getAbiVersion(abi);
5904
+ if (version === 0 || version === 1) {
5905
+ return new AbiParser1(abi);
5906
+ }
5907
+ if (version === 2) {
5908
+ return new AbiParser2(abi);
5909
+ }
5910
+ throw Error(`Unsupported ABI version ${version}`);
5911
+ }
5912
+ function getAbiVersion(abi) {
5913
+ if (abi.find((it) => it.type === "interface"))
5914
+ return 2;
5915
+ if (isCairo1Abi(abi))
5916
+ return 1;
5917
+ return 0;
5918
+ }
5919
+
5831
5920
  // src/utils/calldata/tuple.ts
5832
5921
  function parseNamedTuple(namedTuple) {
5833
5922
  const name = namedTuple.substring(0, namedTuple.indexOf(":"));
@@ -6056,6 +6145,14 @@ var starknet = (() => {
6056
6145
  };
6057
6146
  });
6058
6147
  }
6148
+ function parseUint256(element) {
6149
+ if (typeof element === "object") {
6150
+ const { low, high } = element;
6151
+ return [felt(low), felt(high)];
6152
+ }
6153
+ const el_uint256 = uint256(element);
6154
+ return [felt(el_uint256.low), felt(el_uint256.high)];
6155
+ }
6059
6156
  function parseCalldataValue(element, type, structs) {
6060
6157
  if (element === void 0) {
6061
6158
  throw Error(`Missing parameter for type ${type}`);
@@ -6069,6 +6166,9 @@ var starknet = (() => {
6069
6166
  }, result);
6070
6167
  }
6071
6168
  if (structs[type] && structs[type].members.length) {
6169
+ if (isTypeUint256(type)) {
6170
+ return parseUint256(element);
6171
+ }
6072
6172
  const { members } = structs[type];
6073
6173
  const subElement = element;
6074
6174
  return members.reduce((acc, it) => {
@@ -6083,12 +6183,7 @@ var starknet = (() => {
6083
6183
  }, []);
6084
6184
  }
6085
6185
  if (isTypeUint256(type)) {
6086
- if (typeof element === "object") {
6087
- const { low, high } = element;
6088
- return [felt(low), felt(high)];
6089
- }
6090
- const el_uint256 = uint256(element);
6091
- return [felt(el_uint256.low), felt(el_uint256.high)];
6186
+ return parseUint256(element);
6092
6187
  }
6093
6188
  if (typeof element === "object") {
6094
6189
  throw Error(`Parameter ${element} do not align with abi parameter ${type}`);
@@ -6131,6 +6226,11 @@ var starknet = (() => {
6131
6226
  }
6132
6227
  }
6133
6228
  function parseResponseValue(responseIterator, element, structs) {
6229
+ if (isTypeUint256(element.type)) {
6230
+ const low = responseIterator.next().value;
6231
+ const high = responseIterator.next().value;
6232
+ return uint256ToBN({ low, high });
6233
+ }
6134
6234
  if (element.type in structs && structs[element.type]) {
6135
6235
  return structs[element.type].members.reduce((acc, el) => {
6136
6236
  acc[el.name] = parseResponseValue(responseIterator, el, structs);
@@ -6257,6 +6357,10 @@ var starknet = (() => {
6257
6357
  );
6258
6358
  };
6259
6359
  var validateStruct = (parameter, input, structs) => {
6360
+ if (input.type === "core::integer::u256" /* u256 */) {
6361
+ validateUint(parameter, input);
6362
+ return;
6363
+ }
6260
6364
  assert(
6261
6365
  typeof parameter === "object" && !Array.isArray(parameter),
6262
6366
  `Validate: arg ${input.name} is cairo type struct (${input.type}), and should be defined as js object (not array)`
@@ -6346,8 +6450,9 @@ var starknet = (() => {
6346
6450
  // src/utils/calldata/index.ts
6347
6451
  var CallData = class {
6348
6452
  constructor(abi) {
6349
- this.abi = abi;
6350
6453
  this.structs = CallData.getAbiStruct(abi);
6454
+ this.parser = createAbiParser(abi);
6455
+ this.abi = this.parser.getLegacyFormat();
6351
6456
  }
6352
6457
  /**
6353
6458
  * Validate arguments passed to the method as corresponding to the ones in the abi
@@ -6369,9 +6474,9 @@ var starknet = (() => {
6369
6474
  );
6370
6475
  }
6371
6476
  const abiMethod = this.abi.find(
6372
- (abi) => type === "DEPLOY" /* DEPLOY */ ? abi.name === method && abi.type === method : abi.name === method && abi.type === "function"
6477
+ (abi) => type === "DEPLOY" /* DEPLOY */ ? abi.name === method && abi.type === "constructor" : abi.name === method && abi.type === "function"
6373
6478
  );
6374
- const inputsLength = CallData.abiInputsLength(abiMethod.inputs);
6479
+ const inputsLength = this.parser.methodInputsLength(abiMethod);
6375
6480
  if (args.length !== inputsLength) {
6376
6481
  throw Error(
6377
6482
  `Invalid number of arguments, expected ${inputsLength} arguments, but got ${args.length}`
@@ -6478,14 +6583,6 @@ var starknet = (() => {
6478
6583
  const parsed = this.parse(method, response);
6479
6584
  return formatter(parsed, format);
6480
6585
  }
6481
- /**
6482
- * Helper to calculate inputs from abi
6483
- * @param inputs AbiEntry
6484
- * @returns number
6485
- */
6486
- static abiInputsLength(inputs) {
6487
- return inputs.reduce((acc, input) => !isLen(input.name) ? acc + 1 : acc, 0);
6488
- }
6489
6586
  /**
6490
6587
  * Helper to extract structs from abi
6491
6588
  * @param abi Abi
@@ -7258,12 +7355,12 @@ var starknet = (() => {
7258
7355
  ];
7259
7356
  return computeHashOnElements2(dataToHash);
7260
7357
  }
7261
- function calculateDeployTransactionHash(contractAddress, constructorCalldata, version, chainId) {
7358
+ function calculateDeployTransactionHash(contractAddress, constructorCalldata, version, chainId, constructorName = "constructor") {
7262
7359
  return calculateTransactionHashCommon(
7263
7360
  "0x6465706c6f79" /* DEPLOY */,
7264
7361
  version,
7265
7362
  contractAddress,
7266
- getSelectorFromName("constructor"),
7363
+ getSelectorFromName(constructorName),
7267
7364
  constructorCalldata,
7268
7365
  0,
7269
7366
  chainId
@@ -14132,7 +14229,8 @@ ${res.tx_failure_reason.error_message}` : res.tx_status;
14132
14229
  this.providerOrAccount = providerOrAccount;
14133
14230
  this.callData = new CallData(abi);
14134
14231
  this.structs = CallData.getAbiStruct(abi);
14135
- this.abi = abi;
14232
+ const parser = createAbiParser(abi);
14233
+ this.abi = parser.getLegacyFormat();
14136
14234
  const options = { enumerable: true, value: {}, writable: false };
14137
14235
  Object.defineProperties(this, {
14138
14236
  functions: { enumerable: true, value: {}, writable: false },