starknet 11.2.0 → 11.3.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/dist/index.js CHANGED
@@ -1664,6 +1664,9 @@ function hashAbi(sierra) {
1664
1664
  return BigInt(addHexPrefix(starkCurve.keccak(utf8ToUint8Array(indentString)).toString(16)));
1665
1665
  }
1666
1666
  function computeSierraContractClassHash(sierra) {
1667
+ if (sierra.abi === void 0 || sierra.abi === null) {
1668
+ throw Error('Invalid Sierra contract: the "abi" field is missing');
1669
+ }
1667
1670
  const CONTRACT_CLASS_VERSION = "CONTRACT_CLASS_V0.1.0";
1668
1671
  const compiledClassVersion = utf8ToBigInt(CONTRACT_CLASS_VERSION);
1669
1672
  const externalEntryPointsHash = hashEntryPointSierra(sierra.entry_points_by_type.EXTERNAL);
@@ -11745,7 +11748,7 @@ function getAbiVersion(abi) {
11745
11748
  return 0;
11746
11749
  }
11747
11750
  function isNoConstructorValid(method, argsCalldata, abiMethod) {
11748
- return method === "constructor" && !abiMethod && !argsCalldata.length;
11751
+ return method === "constructor" && !abiMethod && Object.keys(argsCalldata).length === 0;
11749
11752
  }
11750
11753
 
11751
11754
  // src/utils/calldata/propertyOrder.ts
@@ -11827,7 +11830,7 @@ function orderPropsByAbi(unorderedObject, abiOfObject, structs, enums) {
11827
11830
  enumerable: true,
11828
11831
  value: value ?? unorderedObject2[abiParam.name]
11829
11832
  });
11830
- if (unorderedObject2[abiParam.name] === "undefined") {
11833
+ if (unorderedObject2[abiParam.name] === void 0) {
11831
11834
  if (isCairo1Type(abiParam.type) || !isLen(abiParam.name)) {
11832
11835
  throw Error(`Your object needs a property with key : ${abiParam.name} .`);
11833
11836
  }
@@ -11928,6 +11931,9 @@ function orderPropsByAbi(unorderedObject, abiOfObject, structs, enums) {
11928
11931
  if (isLen(abiParam.name) && !isCairo1Type(abiParam.type)) {
11929
11932
  return orderedObject;
11930
11933
  }
11934
+ if (unorderedObject[abiParam.name] === void 0) {
11935
+ throw Error(`Your object needs a property with key : ${abiParam.name} .`);
11936
+ }
11931
11937
  setProperty(orderInput(unorderedObject[abiParam.name], abiParam.type));
11932
11938
  return orderedObject;
11933
11939
  }, {});
@@ -11935,6 +11941,11 @@ function orderPropsByAbi(unorderedObject, abiOfObject, structs, enums) {
11935
11941
  }
11936
11942
 
11937
11943
  // src/utils/calldata/index.ts
11944
+ function methodNotInAbiError(method) {
11945
+ return Error(
11946
+ method === "constructor" ? "Cannot pass calldata to a contract with no constructor" : `Method "${method}" not found in abi`
11947
+ );
11948
+ }
11938
11949
  var CallData = class _CallData {
11939
11950
  abi;
11940
11951
  parser;
@@ -11965,11 +11976,14 @@ var CallData = class _CallData {
11965
11976
  );
11966
11977
  }
11967
11978
  const abiMethod = this.abi.find(
11968
- (abi) => type === ValidateType.DEPLOY ? abi.name === method && abi.type === "constructor" : abi.name === method && abi.type === "function"
11979
+ (abi) => type === ValidateType.DEPLOY ? abi.name === method : abi.name === method && abi.type === "function"
11969
11980
  );
11970
11981
  if (isNoConstructorValid(method, args, abiMethod)) {
11971
11982
  return;
11972
11983
  }
11984
+ if (!abiMethod) {
11985
+ throw methodNotInAbiError(method);
11986
+ }
11973
11987
  const inputsLength = this.parser.methodInputsLength(abiMethod);
11974
11988
  if (args.length !== inputsLength) {
11975
11989
  throw Error(
@@ -11997,6 +12011,9 @@ var CallData = class _CallData {
11997
12011
  if (isNoConstructorValid(method, argsCalldata, abiMethod)) {
11998
12012
  return [];
11999
12013
  }
12014
+ if (!abiMethod) {
12015
+ throw methodNotInAbiError(method);
12016
+ }
12000
12017
  let args;
12001
12018
  if (Array.isArray(argsCalldata)) {
12002
12019
  args = argsCalldata;
@@ -12661,6 +12678,9 @@ function wait(delay) {
12661
12678
  });
12662
12679
  }
12663
12680
  async function createSierraContractClass(contract) {
12681
+ if (contract.abi === void 0 || contract.abi === null) {
12682
+ throw Error('Invalid Sierra contract: the "abi" field is missing');
12683
+ }
12664
12684
  const result = { ...contract };
12665
12685
  delete result.sierra_program_debug_info;
12666
12686
  result.abi = formatSpaces(stringify2(contract.abi));
@@ -15329,7 +15349,7 @@ var SubscriptionChannel4 = class extends SubscriptionChannel {
15329
15349
  var WebSocketChannel = class {
15330
15350
  /**
15331
15351
  * The URL of the WebSocket RPC Node.
15332
- * @example 'wss://starknet-sepolia.public.blastapi.io/rpc/v0_8'
15352
+ * @example 'wss://your-starknet-node/rpc/v0_10'
15333
15353
  */
15334
15354
  nodeUrl;
15335
15355
  /** Carries the frames and owns the socket lifecycle. */
@@ -19238,6 +19258,76 @@ var PaymasterRpc = class _PaymasterRpc {
19238
19258
  var PaymasterInterface = class {
19239
19259
  };
19240
19260
 
19261
+ // src/deployer/compileConstructorCalldata.ts
19262
+ function checkCompiledConstructorCalldata(calldata, abi) {
19263
+ const calldataClass = new CallData(abi);
19264
+ const constructorAbi = calldataClass.abi.find((entry) => entry.name === "constructor");
19265
+ if (!constructorAbi) {
19266
+ if (calldata.length > 0) {
19267
+ throw Error("Cannot pass calldata to a contract with no constructor");
19268
+ }
19269
+ return;
19270
+ }
19271
+ let index = 0;
19272
+ const felts = {
19273
+ next: () => {
19274
+ if (index >= calldata.length) {
19275
+ throw Error("it has fewer felts than the constructor takes");
19276
+ }
19277
+ index += 1;
19278
+ return { value: String(calldata[index - 1]), done: false };
19279
+ }
19280
+ };
19281
+ const parsed = {};
19282
+ try {
19283
+ constructorAbi.inputs.forEach((input) => {
19284
+ parsed[input.name] = calldataClass.parser.parseResponse(felts, input, parsed);
19285
+ });
19286
+ } catch (error) {
19287
+ throw Error(
19288
+ `The compiled constructor calldata does not match the abi: ${error.message}`
19289
+ );
19290
+ }
19291
+ const leftover = calldata.length - index;
19292
+ if (leftover > 0) {
19293
+ throw Error(
19294
+ `The compiled constructor calldata does not match the abi: it has ${leftover} felt${leftover > 1 ? "s" : ""} too many`
19295
+ );
19296
+ }
19297
+ let written;
19298
+ try {
19299
+ written = calldataClass.compile("constructor", parsed);
19300
+ } catch (error) {
19301
+ throw Error(
19302
+ `The compiled constructor calldata does not match the abi: ${error.message}`
19303
+ );
19304
+ }
19305
+ const sameFelts = written.length === calldata.length && written.every((felt2, i) => BigInt(felt2) === BigInt(calldata[i]));
19306
+ if (!sameFelts) {
19307
+ throw Error(
19308
+ "The compiled constructor calldata does not match the abi: the abi writes the values it holds with other felts"
19309
+ );
19310
+ }
19311
+ }
19312
+ function compileConstructorCalldata(constructorCalldata = [], abi) {
19313
+ let compiledHere = false;
19314
+ const calldata = getCompiledCalldata(constructorCalldata, () => {
19315
+ compiledHere = true;
19316
+ if (abi) {
19317
+ const calldataClass = new CallData(abi);
19318
+ if (Array.isArray(constructorCalldata)) {
19319
+ calldataClass.validate(ValidateType.DEPLOY, "constructor", constructorCalldata);
19320
+ }
19321
+ return calldataClass.compile("constructor", constructorCalldata);
19322
+ }
19323
+ return CallData.compile(constructorCalldata);
19324
+ });
19325
+ if (abi && !compiledHere) {
19326
+ checkCompiledConstructorCalldata(calldata, abi);
19327
+ }
19328
+ return calldata;
19329
+ }
19330
+
19241
19331
  // src/deployer/default.ts
19242
19332
  var Deployer = class {
19243
19333
  address;
@@ -19256,16 +19346,7 @@ var Deployer = class {
19256
19346
  constructorCalldata = [],
19257
19347
  abi
19258
19348
  } = it;
19259
- const compiledConstructorCallData = getCompiledCalldata(constructorCalldata, () => {
19260
- if (abi) {
19261
- const calldataClass = new CallData(abi);
19262
- if (Array.isArray(constructorCalldata)) {
19263
- calldataClass.validate(ValidateType.DEPLOY, "constructor", constructorCalldata);
19264
- }
19265
- return calldataClass.compile("constructor", constructorCalldata);
19266
- }
19267
- return CallData.compile(constructorCalldata);
19268
- });
19349
+ const compiledConstructorCallData = compileConstructorCalldata(constructorCalldata, abi);
19269
19350
  const deploySalt = salt ?? randomFelt();
19270
19351
  return {
19271
19352
  call: {
@@ -19415,9 +19496,10 @@ var Account = class {
19415
19496
  classHash,
19416
19497
  addressSalt = 0,
19417
19498
  constructorCalldata = [],
19418
- contractAddress
19499
+ contractAddress,
19500
+ abi
19419
19501
  }, details = {}) {
19420
- const compiledCalldata = CallData.compile(constructorCalldata);
19502
+ const compiledCalldata = abi ? compileConstructorCalldata(constructorCalldata, abi) : CallData.compile(constructorCalldata);
19421
19503
  const contractAddressFinal = contractAddress ?? calculateContractAddressFromHash(addressSalt, classHash, compiledCalldata, 0);
19422
19504
  const invocations = [
19423
19505
  {
@@ -19594,6 +19676,9 @@ var Account = class {
19594
19676
  * First check if contract is already declared, if not declare it
19595
19677
  * If contract already declared returned transaction_hash is ''.
19596
19678
  * Method will pass even if contract is already declared
19679
+ *
19680
+ * A given `classHash` is used as is to look for the class on the node, without being checked
19681
+ * against `contract`, to save time.
19597
19682
  * @param transactionsDetail (optional)
19598
19683
  */
19599
19684
  async declareIfNot(payload, transactionsDetail = {}) {
@@ -19666,14 +19751,29 @@ var Account = class {
19666
19751
  );
19667
19752
  }
19668
19753
  async declareAndDeploy(payload, details = {}) {
19669
- let declare = await this.declareIfNot(payload, details);
19670
- if (declare.transaction_hash !== "") {
19754
+ if (payload.abi) {
19755
+ logger.warn(
19756
+ 'The "abi" field is ignored: the ABI is taken from "contract". This field will be removed in v12.'
19757
+ );
19758
+ }
19759
+ const abi = extractAbi(await parseContract(payload.contract));
19760
+ const constructorCalldata = compileConstructorCalldata(payload.constructorCalldata, abi);
19761
+ const classHash = computeContractClassHash(payload.contract);
19762
+ if (payload.classHash !== void 0 && payload.classHash !== null && toBigInt(payload.classHash) !== toBigInt(classHash)) {
19763
+ throw Error(
19764
+ `The given classHash ${payload.classHash} is not the class hash of "contract": ${classHash}`
19765
+ );
19766
+ }
19767
+ let declare = await this.declareIfNot({ ...payload, classHash }, details);
19768
+ const declarationSent = declare.transaction_hash !== "";
19769
+ if (declarationSent) {
19671
19770
  const tx = await this.provider.waitForTransaction(declare.transaction_hash, details);
19672
19771
  declare = { ...declare, ...tx };
19673
19772
  }
19773
+ const deployDetails = declarationSent && details.nonce !== void 0 && details.nonce !== null ? { ...details, nonce: toBigInt(details.nonce) + 1n } : details;
19674
19774
  const deploy = await this.deployContract(
19675
- { ...payload, classHash: declare.class_hash },
19676
- details
19775
+ { ...payload, classHash: declare.class_hash, constructorCalldata, abi: void 0 },
19776
+ deployDetails
19677
19777
  );
19678
19778
  return { declare: { ...declare }, deploy };
19679
19779
  }
@@ -19682,9 +19782,10 @@ var Account = class {
19682
19782
  classHash,
19683
19783
  constructorCalldata = [],
19684
19784
  addressSalt = 0,
19685
- contractAddress: providedContractAddress
19785
+ contractAddress: providedContractAddress,
19786
+ abi
19686
19787
  }, details = {}) {
19687
- const compiledCalldata = CallData.compile(constructorCalldata);
19788
+ const compiledCalldata = abi ? compileConstructorCalldata(constructorCalldata, abi) : CallData.compile(constructorCalldata);
19688
19789
  const contractAddress = providedContractAddress ?? calculateContractAddressFromHash(addressSalt, classHash, compiledCalldata, 0);
19689
19790
  const detailsWithTip = await this.resolveDetailsWithTip(details);
19690
19791
  const { resourceBounds: providedResourceBounds } = details;
@@ -19695,7 +19796,8 @@ var Account = class {
19695
19796
  classHash,
19696
19797
  constructorCalldata,
19697
19798
  addressSalt,
19698
- contractAddress
19799
+ contractAddress,
19800
+ abi
19699
19801
  },
19700
19802
  detailsWithTip
19701
19803
  );
@@ -19957,9 +20059,10 @@ var Account = class {
19957
20059
  classHash,
19958
20060
  addressSalt = 0,
19959
20061
  constructorCalldata = [],
19960
- contractAddress: providedContractAddress
20062
+ contractAddress: providedContractAddress,
20063
+ abi
19961
20064
  }, details) {
19962
- const compiledCalldata = CallData.compile(constructorCalldata);
20065
+ const compiledCalldata = abi ? compileConstructorCalldata(constructorCalldata, abi) : CallData.compile(constructorCalldata);
19963
20066
  const contractAddress = providedContractAddress ?? calculateContractAddressFromHash(addressSalt, classHash, compiledCalldata, 0);
19964
20067
  const signature = !details.skipValidate ? await this.signer.signDeployAccountTransaction({
19965
20068
  ...details,
@@ -21584,10 +21687,10 @@ var Contract = class _Contract {
21584
21687
  const deployResult = await account.deployContract(
21585
21688
  {
21586
21689
  classHash,
21587
- constructorCalldata: deployParams.constructorCalldata,
21690
+ constructorCalldata: parseRequest ? deployParams.constructorCalldata : compileConstructorCalldata(deployParams.constructorCalldata),
21588
21691
  salt: deployParams.salt,
21589
21692
  unique: deployParams.unique,
21590
- abi: parseRequest ? abi : void 0
21693
+ abi
21591
21694
  },
21592
21695
  details
21593
21696
  );
@@ -21595,17 +21698,12 @@ var Contract = class _Contract {
21595
21698
  } else {
21596
21699
  const declareParams = params;
21597
21700
  const contract = await parseContract(declareParams.contract);
21598
- abi = declareParams.abi ? declareParams.abi : extractAbi(contract);
21701
+ abi = extractAbi(contract);
21702
+ const constructorCalldata = parseRequest ? declareParams.constructorCalldata : compileConstructorCalldata(declareParams.constructorCalldata);
21599
21703
  const {
21600
21704
  declare: { class_hash },
21601
21705
  deploy: { contract_address: deployed_address }
21602
- } = await account.declareAndDeploy(
21603
- {
21604
- ...declareParams,
21605
- abi: parseRequest ? abi : void 0
21606
- },
21607
- details
21608
- );
21706
+ } = await account.declareAndDeploy({ ...declareParams, constructorCalldata }, details);
21609
21707
  classHash = class_hash.toString();
21610
21708
  contract_address = deployed_address;
21611
21709
  }