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.mjs CHANGED
@@ -1457,6 +1457,9 @@ function hashAbi(sierra) {
1457
1457
  return BigInt(addHexPrefix(starkCurve.keccak(utf8ToUint8Array(indentString)).toString(16)));
1458
1458
  }
1459
1459
  function computeSierraContractClassHash(sierra) {
1460
+ if (sierra.abi === void 0 || sierra.abi === null) {
1461
+ throw Error('Invalid Sierra contract: the "abi" field is missing');
1462
+ }
1460
1463
  const CONTRACT_CLASS_VERSION = "CONTRACT_CLASS_V0.1.0";
1461
1464
  const compiledClassVersion = utf8ToBigInt(CONTRACT_CLASS_VERSION);
1462
1465
  const externalEntryPointsHash = hashEntryPointSierra(sierra.entry_points_by_type.EXTERNAL);
@@ -11538,7 +11541,7 @@ function getAbiVersion(abi) {
11538
11541
  return 0;
11539
11542
  }
11540
11543
  function isNoConstructorValid(method, argsCalldata, abiMethod) {
11541
- return method === "constructor" && !abiMethod && !argsCalldata.length;
11544
+ return method === "constructor" && !abiMethod && Object.keys(argsCalldata).length === 0;
11542
11545
  }
11543
11546
 
11544
11547
  // src/utils/calldata/propertyOrder.ts
@@ -11620,7 +11623,7 @@ function orderPropsByAbi(unorderedObject, abiOfObject, structs, enums) {
11620
11623
  enumerable: true,
11621
11624
  value: value ?? unorderedObject2[abiParam.name]
11622
11625
  });
11623
- if (unorderedObject2[abiParam.name] === "undefined") {
11626
+ if (unorderedObject2[abiParam.name] === void 0) {
11624
11627
  if (isCairo1Type(abiParam.type) || !isLen(abiParam.name)) {
11625
11628
  throw Error(`Your object needs a property with key : ${abiParam.name} .`);
11626
11629
  }
@@ -11721,6 +11724,9 @@ function orderPropsByAbi(unorderedObject, abiOfObject, structs, enums) {
11721
11724
  if (isLen(abiParam.name) && !isCairo1Type(abiParam.type)) {
11722
11725
  return orderedObject;
11723
11726
  }
11727
+ if (unorderedObject[abiParam.name] === void 0) {
11728
+ throw Error(`Your object needs a property with key : ${abiParam.name} .`);
11729
+ }
11724
11730
  setProperty(orderInput(unorderedObject[abiParam.name], abiParam.type));
11725
11731
  return orderedObject;
11726
11732
  }, {});
@@ -11728,6 +11734,11 @@ function orderPropsByAbi(unorderedObject, abiOfObject, structs, enums) {
11728
11734
  }
11729
11735
 
11730
11736
  // src/utils/calldata/index.ts
11737
+ function methodNotInAbiError(method) {
11738
+ return Error(
11739
+ method === "constructor" ? "Cannot pass calldata to a contract with no constructor" : `Method "${method}" not found in abi`
11740
+ );
11741
+ }
11731
11742
  var CallData = class _CallData {
11732
11743
  abi;
11733
11744
  parser;
@@ -11758,11 +11769,14 @@ var CallData = class _CallData {
11758
11769
  );
11759
11770
  }
11760
11771
  const abiMethod = this.abi.find(
11761
- (abi) => type === ValidateType.DEPLOY ? abi.name === method && abi.type === "constructor" : abi.name === method && abi.type === "function"
11772
+ (abi) => type === ValidateType.DEPLOY ? abi.name === method : abi.name === method && abi.type === "function"
11762
11773
  );
11763
11774
  if (isNoConstructorValid(method, args, abiMethod)) {
11764
11775
  return;
11765
11776
  }
11777
+ if (!abiMethod) {
11778
+ throw methodNotInAbiError(method);
11779
+ }
11766
11780
  const inputsLength = this.parser.methodInputsLength(abiMethod);
11767
11781
  if (args.length !== inputsLength) {
11768
11782
  throw Error(
@@ -11790,6 +11804,9 @@ var CallData = class _CallData {
11790
11804
  if (isNoConstructorValid(method, argsCalldata, abiMethod)) {
11791
11805
  return [];
11792
11806
  }
11807
+ if (!abiMethod) {
11808
+ throw methodNotInAbiError(method);
11809
+ }
11793
11810
  let args;
11794
11811
  if (Array.isArray(argsCalldata)) {
11795
11812
  args = argsCalldata;
@@ -12454,6 +12471,9 @@ function wait(delay) {
12454
12471
  });
12455
12472
  }
12456
12473
  async function createSierraContractClass(contract) {
12474
+ if (contract.abi === void 0 || contract.abi === null) {
12475
+ throw Error('Invalid Sierra contract: the "abi" field is missing');
12476
+ }
12457
12477
  const result = { ...contract };
12458
12478
  delete result.sierra_program_debug_info;
12459
12479
  result.abi = formatSpaces(stringify2(contract.abi));
@@ -15122,7 +15142,7 @@ var SubscriptionChannel4 = class extends SubscriptionChannel {
15122
15142
  var WebSocketChannel = class {
15123
15143
  /**
15124
15144
  * The URL of the WebSocket RPC Node.
15125
- * @example 'wss://starknet-sepolia.public.blastapi.io/rpc/v0_8'
15145
+ * @example 'wss://your-starknet-node/rpc/v0_10'
15126
15146
  */
15127
15147
  nodeUrl;
15128
15148
  /** Carries the frames and owns the socket lifecycle. */
@@ -19031,6 +19051,76 @@ var PaymasterRpc = class _PaymasterRpc {
19031
19051
  var PaymasterInterface = class {
19032
19052
  };
19033
19053
 
19054
+ // src/deployer/compileConstructorCalldata.ts
19055
+ function checkCompiledConstructorCalldata(calldata, abi) {
19056
+ const calldataClass = new CallData(abi);
19057
+ const constructorAbi = calldataClass.abi.find((entry) => entry.name === "constructor");
19058
+ if (!constructorAbi) {
19059
+ if (calldata.length > 0) {
19060
+ throw Error("Cannot pass calldata to a contract with no constructor");
19061
+ }
19062
+ return;
19063
+ }
19064
+ let index = 0;
19065
+ const felts = {
19066
+ next: () => {
19067
+ if (index >= calldata.length) {
19068
+ throw Error("it has fewer felts than the constructor takes");
19069
+ }
19070
+ index += 1;
19071
+ return { value: String(calldata[index - 1]), done: false };
19072
+ }
19073
+ };
19074
+ const parsed = {};
19075
+ try {
19076
+ constructorAbi.inputs.forEach((input) => {
19077
+ parsed[input.name] = calldataClass.parser.parseResponse(felts, input, parsed);
19078
+ });
19079
+ } catch (error) {
19080
+ throw Error(
19081
+ `The compiled constructor calldata does not match the abi: ${error.message}`
19082
+ );
19083
+ }
19084
+ const leftover = calldata.length - index;
19085
+ if (leftover > 0) {
19086
+ throw Error(
19087
+ `The compiled constructor calldata does not match the abi: it has ${leftover} felt${leftover > 1 ? "s" : ""} too many`
19088
+ );
19089
+ }
19090
+ let written;
19091
+ try {
19092
+ written = calldataClass.compile("constructor", parsed);
19093
+ } catch (error) {
19094
+ throw Error(
19095
+ `The compiled constructor calldata does not match the abi: ${error.message}`
19096
+ );
19097
+ }
19098
+ const sameFelts = written.length === calldata.length && written.every((felt2, i) => BigInt(felt2) === BigInt(calldata[i]));
19099
+ if (!sameFelts) {
19100
+ throw Error(
19101
+ "The compiled constructor calldata does not match the abi: the abi writes the values it holds with other felts"
19102
+ );
19103
+ }
19104
+ }
19105
+ function compileConstructorCalldata(constructorCalldata = [], abi) {
19106
+ let compiledHere = false;
19107
+ const calldata = getCompiledCalldata(constructorCalldata, () => {
19108
+ compiledHere = true;
19109
+ if (abi) {
19110
+ const calldataClass = new CallData(abi);
19111
+ if (Array.isArray(constructorCalldata)) {
19112
+ calldataClass.validate(ValidateType.DEPLOY, "constructor", constructorCalldata);
19113
+ }
19114
+ return calldataClass.compile("constructor", constructorCalldata);
19115
+ }
19116
+ return CallData.compile(constructorCalldata);
19117
+ });
19118
+ if (abi && !compiledHere) {
19119
+ checkCompiledConstructorCalldata(calldata, abi);
19120
+ }
19121
+ return calldata;
19122
+ }
19123
+
19034
19124
  // src/deployer/default.ts
19035
19125
  var Deployer = class {
19036
19126
  address;
@@ -19049,16 +19139,7 @@ var Deployer = class {
19049
19139
  constructorCalldata = [],
19050
19140
  abi
19051
19141
  } = it;
19052
- const compiledConstructorCallData = getCompiledCalldata(constructorCalldata, () => {
19053
- if (abi) {
19054
- const calldataClass = new CallData(abi);
19055
- if (Array.isArray(constructorCalldata)) {
19056
- calldataClass.validate(ValidateType.DEPLOY, "constructor", constructorCalldata);
19057
- }
19058
- return calldataClass.compile("constructor", constructorCalldata);
19059
- }
19060
- return CallData.compile(constructorCalldata);
19061
- });
19142
+ const compiledConstructorCallData = compileConstructorCalldata(constructorCalldata, abi);
19062
19143
  const deploySalt = salt ?? randomFelt();
19063
19144
  return {
19064
19145
  call: {
@@ -19208,9 +19289,10 @@ var Account = class {
19208
19289
  classHash,
19209
19290
  addressSalt = 0,
19210
19291
  constructorCalldata = [],
19211
- contractAddress
19292
+ contractAddress,
19293
+ abi
19212
19294
  }, details = {}) {
19213
- const compiledCalldata = CallData.compile(constructorCalldata);
19295
+ const compiledCalldata = abi ? compileConstructorCalldata(constructorCalldata, abi) : CallData.compile(constructorCalldata);
19214
19296
  const contractAddressFinal = contractAddress ?? calculateContractAddressFromHash(addressSalt, classHash, compiledCalldata, 0);
19215
19297
  const invocations = [
19216
19298
  {
@@ -19387,6 +19469,9 @@ var Account = class {
19387
19469
  * First check if contract is already declared, if not declare it
19388
19470
  * If contract already declared returned transaction_hash is ''.
19389
19471
  * Method will pass even if contract is already declared
19472
+ *
19473
+ * A given `classHash` is used as is to look for the class on the node, without being checked
19474
+ * against `contract`, to save time.
19390
19475
  * @param transactionsDetail (optional)
19391
19476
  */
19392
19477
  async declareIfNot(payload, transactionsDetail = {}) {
@@ -19459,14 +19544,29 @@ var Account = class {
19459
19544
  );
19460
19545
  }
19461
19546
  async declareAndDeploy(payload, details = {}) {
19462
- let declare = await this.declareIfNot(payload, details);
19463
- if (declare.transaction_hash !== "") {
19547
+ if (payload.abi) {
19548
+ logger.warn(
19549
+ 'The "abi" field is ignored: the ABI is taken from "contract". This field will be removed in v12.'
19550
+ );
19551
+ }
19552
+ const abi = extractAbi(await parseContract(payload.contract));
19553
+ const constructorCalldata = compileConstructorCalldata(payload.constructorCalldata, abi);
19554
+ const classHash = computeContractClassHash(payload.contract);
19555
+ if (payload.classHash !== void 0 && payload.classHash !== null && toBigInt(payload.classHash) !== toBigInt(classHash)) {
19556
+ throw Error(
19557
+ `The given classHash ${payload.classHash} is not the class hash of "contract": ${classHash}`
19558
+ );
19559
+ }
19560
+ let declare = await this.declareIfNot({ ...payload, classHash }, details);
19561
+ const declarationSent = declare.transaction_hash !== "";
19562
+ if (declarationSent) {
19464
19563
  const tx = await this.provider.waitForTransaction(declare.transaction_hash, details);
19465
19564
  declare = { ...declare, ...tx };
19466
19565
  }
19566
+ const deployDetails = declarationSent && details.nonce !== void 0 && details.nonce !== null ? { ...details, nonce: toBigInt(details.nonce) + 1n } : details;
19467
19567
  const deploy = await this.deployContract(
19468
- { ...payload, classHash: declare.class_hash },
19469
- details
19568
+ { ...payload, classHash: declare.class_hash, constructorCalldata, abi: void 0 },
19569
+ deployDetails
19470
19570
  );
19471
19571
  return { declare: { ...declare }, deploy };
19472
19572
  }
@@ -19475,9 +19575,10 @@ var Account = class {
19475
19575
  classHash,
19476
19576
  constructorCalldata = [],
19477
19577
  addressSalt = 0,
19478
- contractAddress: providedContractAddress
19578
+ contractAddress: providedContractAddress,
19579
+ abi
19479
19580
  }, details = {}) {
19480
- const compiledCalldata = CallData.compile(constructorCalldata);
19581
+ const compiledCalldata = abi ? compileConstructorCalldata(constructorCalldata, abi) : CallData.compile(constructorCalldata);
19481
19582
  const contractAddress = providedContractAddress ?? calculateContractAddressFromHash(addressSalt, classHash, compiledCalldata, 0);
19482
19583
  const detailsWithTip = await this.resolveDetailsWithTip(details);
19483
19584
  const { resourceBounds: providedResourceBounds } = details;
@@ -19488,7 +19589,8 @@ var Account = class {
19488
19589
  classHash,
19489
19590
  constructorCalldata,
19490
19591
  addressSalt,
19491
- contractAddress
19592
+ contractAddress,
19593
+ abi
19492
19594
  },
19493
19595
  detailsWithTip
19494
19596
  );
@@ -19750,9 +19852,10 @@ var Account = class {
19750
19852
  classHash,
19751
19853
  addressSalt = 0,
19752
19854
  constructorCalldata = [],
19753
- contractAddress: providedContractAddress
19855
+ contractAddress: providedContractAddress,
19856
+ abi
19754
19857
  }, details) {
19755
- const compiledCalldata = CallData.compile(constructorCalldata);
19858
+ const compiledCalldata = abi ? compileConstructorCalldata(constructorCalldata, abi) : CallData.compile(constructorCalldata);
19756
19859
  const contractAddress = providedContractAddress ?? calculateContractAddressFromHash(addressSalt, classHash, compiledCalldata, 0);
19757
19860
  const signature = !details.skipValidate ? await this.signer.signDeployAccountTransaction({
19758
19861
  ...details,
@@ -21377,10 +21480,10 @@ var Contract = class _Contract {
21377
21480
  const deployResult = await account.deployContract(
21378
21481
  {
21379
21482
  classHash,
21380
- constructorCalldata: deployParams.constructorCalldata,
21483
+ constructorCalldata: parseRequest ? deployParams.constructorCalldata : compileConstructorCalldata(deployParams.constructorCalldata),
21381
21484
  salt: deployParams.salt,
21382
21485
  unique: deployParams.unique,
21383
- abi: parseRequest ? abi : void 0
21486
+ abi
21384
21487
  },
21385
21488
  details
21386
21489
  );
@@ -21388,17 +21491,12 @@ var Contract = class _Contract {
21388
21491
  } else {
21389
21492
  const declareParams = params;
21390
21493
  const contract = await parseContract(declareParams.contract);
21391
- abi = declareParams.abi ? declareParams.abi : extractAbi(contract);
21494
+ abi = extractAbi(contract);
21495
+ const constructorCalldata = parseRequest ? declareParams.constructorCalldata : compileConstructorCalldata(declareParams.constructorCalldata);
21392
21496
  const {
21393
21497
  declare: { class_hash },
21394
21498
  deploy: { contract_address: deployed_address }
21395
- } = await account.declareAndDeploy(
21396
- {
21397
- ...declareParams,
21398
- abi: parseRequest ? abi : void 0
21399
- },
21400
- details
21401
- );
21499
+ } = await account.declareAndDeploy({ ...declareParams, constructorCalldata }, details);
21402
21500
  classHash = class_hash.toString();
21403
21501
  contract_address = deployed_address;
21404
21502
  }