starknet 5.5.0 → 5.6.1

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
@@ -2229,6 +2229,27 @@ var CONSTANT_POINTS = [
2229
2229
  ]
2230
2230
  ];
2231
2231
 
2232
+ // src/utils/calldata/cairo.ts
2233
+ var cairo_exports = {};
2234
+ __export(cairo_exports, {
2235
+ Uint: () => Uint,
2236
+ felt: () => felt,
2237
+ getArrayType: () => getArrayType,
2238
+ isCairo1Type: () => isCairo1Type,
2239
+ isLen: () => isLen,
2240
+ isTypeArray: () => isTypeArray,
2241
+ isTypeBool: () => isTypeBool,
2242
+ isTypeContractAddress: () => isTypeContractAddress,
2243
+ isTypeFelt: () => isTypeFelt,
2244
+ isTypeNamedTuple: () => isTypeNamedTuple,
2245
+ isTypeStruct: () => isTypeStruct,
2246
+ isTypeTuple: () => isTypeTuple,
2247
+ isTypeUint: () => isTypeUint,
2248
+ isTypeUint256: () => isTypeUint256,
2249
+ tuple: () => tuple,
2250
+ uint256: () => uint256
2251
+ });
2252
+
2232
2253
  // src/utils/num.ts
2233
2254
  var num_exports = {};
2234
2255
  __export(num_exports, {
@@ -2385,8 +2406,8 @@ __export(uint256_exports, {
2385
2406
  isUint256: () => isUint256,
2386
2407
  uint256ToBN: () => uint256ToBN
2387
2408
  });
2388
- function uint256ToBN(uint256) {
2389
- return (toBigInt(uint256.high) << 128n) + toBigInt(uint256.low);
2409
+ function uint256ToBN(uint2562) {
2410
+ return (toBigInt(uint2562.high) << 128n) + toBigInt(uint2562.low);
2390
2411
  }
2391
2412
  var UINT_128_MAX = (1n << 128n) - 1n;
2392
2413
  var UINT_256_MAX = (1n << 256n) - 1n;
@@ -2404,13 +2425,42 @@ function bnToUint256(bignumber) {
2404
2425
  }
2405
2426
 
2406
2427
  // src/utils/calldata/cairo.ts
2428
+ var Uint = /* @__PURE__ */ ((Uint2) => {
2429
+ Uint2["u8"] = "core::integer::u8";
2430
+ Uint2["u16"] = "core::integer::u16";
2431
+ Uint2["u32"] = "core::integer::u32";
2432
+ Uint2["u64"] = "core::integer::u64";
2433
+ Uint2["u128"] = "core::integer::u128";
2434
+ Uint2["u256"] = "core::integer::u256";
2435
+ return Uint2;
2436
+ })(Uint || {});
2407
2437
  var isLen = (name) => /_len$/.test(name);
2408
- var isTypeFelt = (type) => type === "felt";
2409
- var isTypeFeltArray = (type) => type === "felt*";
2410
- var isTypeArray = (type) => /\*/.test(type);
2438
+ var isTypeFelt = (type) => type === "felt" || type === "core::felt252";
2439
+ var isTypeArray = (type) => /\*/.test(type) || type.includes("core::array::Array::");
2411
2440
  var isTypeTuple = (type) => /^\(.*\)$/i.test(type);
2412
2441
  var isTypeNamedTuple = (type) => /\(.*\)/i.test(type) && type.includes(":");
2413
2442
  var isTypeStruct = (type, structs) => type in structs;
2443
+ var isTypeUint = (type) => Object.values(Uint).includes(type);
2444
+ var isTypeUint256 = (type) => type === "core::integer::u256";
2445
+ var isTypeBool = (type) => type === "core::bool";
2446
+ var isTypeContractAddress = (type) => type === "core::starknet::contract_address::ContractAddress";
2447
+ var isCairo1Type = (type) => type.includes("core::");
2448
+ var getArrayType = (type) => {
2449
+ if (isCairo1Type(type)) {
2450
+ return type.substring(type.indexOf("<") + 1, type.indexOf(">"));
2451
+ }
2452
+ return type.replace("*", "");
2453
+ };
2454
+ var uint256 = (it) => {
2455
+ const bn = BigInt(it);
2456
+ if (!isUint256(bn))
2457
+ throw new Error("Number is too large");
2458
+ return {
2459
+ low: (bn & UINT_128_MAX).toString(10),
2460
+ high: (bn >> 128n).toString(10)
2461
+ };
2462
+ };
2463
+ var tuple = (...args) => ({ ...args });
2414
2464
  function felt(it) {
2415
2465
  if (isBigInt(it) || typeof it === "number" && Number.isInteger(it)) {
2416
2466
  return it.toString();
@@ -3681,10 +3731,11 @@ var SequencerProvider = class {
3681
3731
  const url = buildUrl(this.baseUrl, "", endpoint);
3682
3732
  const method = (options == null ? void 0 : options.method) ?? "GET";
3683
3733
  const headers = this.getHeaders(method);
3734
+ const body = stringify2(options == null ? void 0 : options.body);
3684
3735
  try {
3685
3736
  const response = await fetchPonyfill_default(url, {
3686
3737
  method,
3687
- body: stringify2(options == null ? void 0 : options.body),
3738
+ body,
3688
3739
  headers
3689
3740
  });
3690
3741
  const textResponse = await response.text();
@@ -4201,7 +4252,7 @@ function parseSubTuple(s) {
4201
4252
  result
4202
4253
  };
4203
4254
  }
4204
- function extractTupleMemberTypes(type) {
4255
+ function extractCairo0Tuple(type) {
4205
4256
  const cleanType = type.replace(/\s/g, "").slice(1, -1);
4206
4257
  const { subTuple, result } = parseSubTuple(cleanType);
4207
4258
  let recomposed = result.split(",").map((it) => {
@@ -4214,6 +4265,16 @@ function extractTupleMemberTypes(type) {
4214
4265
  }
4215
4266
  return recomposed;
4216
4267
  }
4268
+ function extractCairo1Tuple(type) {
4269
+ const cleanType = type.replace(/\s/g, "").slice(1, -1);
4270
+ return cleanType.split(",");
4271
+ }
4272
+ function extractTupleMemberTypes(type) {
4273
+ if (isCairo1Type(type)) {
4274
+ return extractCairo1Tuple(type);
4275
+ }
4276
+ return extractCairo0Tuple(type);
4277
+ }
4217
4278
 
4218
4279
  // src/utils/calldata/requestParser.ts
4219
4280
  function parseTuple(element, typeStr) {
@@ -4240,6 +4301,10 @@ function parseCalldataValue(element, type, structs) {
4240
4301
  if (Array.isArray(element)) {
4241
4302
  throw Error(`Array inside array (nD) are not supported by cairo. Element: ${element} ${type}`);
4242
4303
  }
4304
+ if (isTypeUint256(type)) {
4305
+ const el_uint256 = uint256(element);
4306
+ return [felt(el_uint256.low), felt(el_uint256.high)];
4307
+ }
4243
4308
  if (structs[type] && structs[type].members.length) {
4244
4309
  const { members } = structs[type];
4245
4310
  const subElement = element;
@@ -4272,16 +4337,21 @@ function parseCalldataField(argsIterator, input, structs) {
4272
4337
  }
4273
4338
  const result = [];
4274
4339
  result.push(felt(value.length));
4340
+ const arrayType = getArrayType(input.type);
4275
4341
  return value.reduce((acc, el) => {
4276
- if (isTypeFeltArray(type)) {
4342
+ if (isTypeFelt(arrayType) || isTypeUint(arrayType) || isTypeContractAddress(arrayType)) {
4277
4343
  acc.push(felt(el));
4344
+ } else if (isTypeBool(arrayType) && typeof el === "boolean") {
4345
+ acc.push(el.toString());
4278
4346
  } else {
4279
- acc.push(...parseCalldataValue(el, type.replace("*", ""), structs));
4347
+ acc.push(...parseCalldataValue(el, arrayType, structs));
4280
4348
  }
4281
4349
  return acc;
4282
4350
  }, result);
4283
- case (isTypeStruct(type, structs) || isTypeTuple(type)):
4351
+ case (isTypeStruct(type, structs) || isTypeTuple(type) || isTypeUint256(type)):
4284
4352
  return parseCalldataValue(value, type, structs);
4353
+ case isTypeBool(type):
4354
+ return `${+value}`;
4285
4355
  default:
4286
4356
  return felt(value);
4287
4357
  }
@@ -4314,8 +4384,24 @@ function responseParser(responseIterator, output, structs, parsedResult) {
4314
4384
  case isLen(name):
4315
4385
  temp = responseIterator.next().value;
4316
4386
  return BigInt(temp);
4387
+ case isTypeBool(type):
4388
+ temp = responseIterator.next().value;
4389
+ return Boolean(BigInt(temp));
4390
+ case isTypeUint256(type):
4391
+ const low = responseIterator.next().value;
4392
+ const high = responseIterator.next().value;
4393
+ return uint256ToBN({ low, high });
4317
4394
  case isTypeArray(type):
4318
4395
  const parsedDataArr = [];
4396
+ if (isCairo1Type(type)) {
4397
+ responseIterator.next();
4398
+ let it = responseIterator.next();
4399
+ while (!it.done) {
4400
+ parsedDataArr.push(BigInt(it.value));
4401
+ it = responseIterator.next();
4402
+ }
4403
+ return parsedDataArr;
4404
+ }
4319
4405
  if (parsedResult && parsedResult[`${name}_len`]) {
4320
4406
  const arrLen = parsedResult[`${name}_len`];
4321
4407
  while (parsedDataArr.length < arrLen) {
@@ -4337,7 +4423,66 @@ function responseParser(responseIterator, output, structs, parsedResult) {
4337
4423
  var validateFelt = (parameter, input) => {
4338
4424
  assert(
4339
4425
  typeof parameter === "string" || typeof parameter === "number" || typeof parameter === "bigint",
4340
- `Validate: arg ${input.name} should be a felt (string, number, BigNumber)`
4426
+ `Validate: arg ${input.name} should be a felt typed as (String, Number or BigInt)`
4427
+ );
4428
+ };
4429
+ var validateUint = (parameter, input) => {
4430
+ if (typeof parameter === "number") {
4431
+ assert(
4432
+ parameter <= Number.MAX_SAFE_INTEGER,
4433
+ `Validation: Parameter is to large to be typed as Number use (BigInt or String)`
4434
+ );
4435
+ }
4436
+ assert(
4437
+ typeof parameter === "string" || typeof parameter === "number" || typeof parameter === "bigint",
4438
+ `Validate: arg ${input.name} of cairo type ${input.type} should be type (String, Number or BigInt)`
4439
+ );
4440
+ const param = toBigInt(parameter);
4441
+ switch (input.type) {
4442
+ case "core::integer::u8" /* u8 */:
4443
+ assert(
4444
+ param >= 0n && param <= 255n,
4445
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0 - 255]`
4446
+ );
4447
+ break;
4448
+ case "core::integer::u16" /* u16 */:
4449
+ assert(
4450
+ param >= 0n && param <= 65535n,
4451
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 65535]`
4452
+ );
4453
+ break;
4454
+ case "core::integer::u32" /* u32 */:
4455
+ assert(
4456
+ param >= 0n && param <= 4294967295n,
4457
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 4294967295]`
4458
+ );
4459
+ break;
4460
+ case "core::integer::u64" /* u64 */:
4461
+ assert(
4462
+ param >= 0n && param <= 2n ** 64n - 1n,
4463
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^64-1]`
4464
+ );
4465
+ break;
4466
+ case "core::integer::u128" /* u128 */:
4467
+ assert(
4468
+ param >= 0n && param <= 2n ** 128n - 1n,
4469
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^128-1]`
4470
+ );
4471
+ break;
4472
+ case "core::integer::u256" /* u256 */:
4473
+ assert(
4474
+ param >= 0n && param <= 2n ** 256n - 1n,
4475
+ `Validate: arg ${input.name} is ${input.type} 0 - 2^256-1`
4476
+ );
4477
+ break;
4478
+ default:
4479
+ break;
4480
+ }
4481
+ };
4482
+ var validateBool = (parameter, input) => {
4483
+ assert(
4484
+ typeof parameter === "boolean",
4485
+ `Validate: arg ${input.name} of cairo type ${input.type} should be type (Boolean)`
4341
4486
  );
4342
4487
  };
4343
4488
  var validateStruct = (parameter, input, structs) => {
@@ -4359,7 +4504,7 @@ var validateTuple = (parameter, input) => {
4359
4504
  );
4360
4505
  };
4361
4506
  var validateArray = (parameter, input, structs) => {
4362
- const baseType = input.type.replace("*", "");
4507
+ const baseType = getArrayType(input.type);
4363
4508
  if (isTypeFelt(baseType) && isLongText(parameter))
4364
4509
  return;
4365
4510
  assert(Array.isArray(parameter), `Validate: arg ${input.name} should be an Array`);
@@ -4375,6 +4520,12 @@ var validateArray = (parameter, input, structs) => {
4375
4520
  (it) => validateStruct(it, { name: input.name, type: baseType }, structs)
4376
4521
  );
4377
4522
  break;
4523
+ case isTypeUint(baseType):
4524
+ parameter.forEach((param) => validateUint(param, input));
4525
+ break;
4526
+ case isTypeBool(baseType):
4527
+ parameter.forEach((param) => validateBool(param, input));
4528
+ break;
4378
4529
  default:
4379
4530
  throw new Error(
4380
4531
  `Validate Unhandled: argument ${input.name}, type ${input.type}, value ${parameter}`
@@ -4390,6 +4541,14 @@ function validateFields(abiMethod, args, structs) {
4390
4541
  case isTypeFelt(input.type):
4391
4542
  validateFelt(parameter, input);
4392
4543
  break;
4544
+ case isTypeUint(input.type):
4545
+ validateUint(parameter, input);
4546
+ break;
4547
+ case isTypeBool(input.type):
4548
+ validateBool(parameter, input);
4549
+ break;
4550
+ case isTypeContractAddress(input.type):
4551
+ break;
4393
4552
  case isTypeStruct(input.type, structs):
4394
4553
  validateStruct(parameter, input, structs);
4395
4554
  break;
@@ -4478,13 +4637,15 @@ var CallData = class {
4478
4637
  parse(method, response) {
4479
4638
  const { outputs } = this.abi.find((abi) => abi.name === method);
4480
4639
  const responseIterator = response.flat()[Symbol.iterator]();
4481
- return outputs.flat().reduce((acc, output) => {
4482
- acc[output.name] = responseParser(responseIterator, output, this.structs, acc);
4483
- if (acc[output.name] && acc[`${output.name}_len`]) {
4484
- delete acc[`${output.name}_len`];
4640
+ const parsed = outputs.flat().reduce((acc, output, idx) => {
4641
+ const propName = output.name ?? idx;
4642
+ acc[propName] = responseParser(responseIterator, output, this.structs, acc);
4643
+ if (acc[propName] && acc[`${propName}_len`]) {
4644
+ delete acc[`${propName}_len`];
4485
4645
  }
4486
4646
  return acc;
4487
4647
  }, {});
4648
+ return Object.keys(parsed).length === 1 && 0 in parsed ? parsed[0] : parsed;
4488
4649
  }
4489
4650
  format(method, response, format) {
4490
4651
  const parsed = this.parse(method, response);
@@ -4556,13 +4717,20 @@ function buildEstimate(contract, functionAbi) {
4556
4717
  return contract.estimate(functionAbi.name, args);
4557
4718
  };
4558
4719
  }
4720
+ var detectCairoVersion = (abi) => {
4721
+ if (!abi)
4722
+ return "0";
4723
+ return abi.find((it) => "state_mutability" in it) ? "1" : "0";
4724
+ };
4559
4725
  var Contract = class {
4560
- constructor(abi, address, providerOrAccount = defaultProvider) {
4726
+ constructor(abi, address, providerOrAccount = defaultProvider, cairoVersion = detectCairoVersion(abi)) {
4727
+ this.version = "0";
4561
4728
  this.address = address && address.toLowerCase();
4562
4729
  this.providerOrAccount = providerOrAccount;
4563
4730
  this.callData = new CallData(abi);
4564
4731
  this.structs = CallData.getAbiStruct(abi);
4565
4732
  this.abi = abi;
4733
+ this.version = cairoVersion;
4566
4734
  const options = { enumerable: true, value: {}, writable: false };
4567
4735
  Object.defineProperties(this, {
4568
4736
  functions: { enumerable: true, value: {}, writable: false },
@@ -4620,11 +4788,10 @@ var Contract = class {
4620
4788
  return this;
4621
4789
  }
4622
4790
  async call(method, args = [], options = { parseRequest: true, parseResponse: true, formatResponse: void 0 }) {
4623
- var _a;
4624
4791
  assert(this.address !== null, "contract is not connected to an address");
4625
4792
  const blockIdentifier = (options == null ? void 0 : options.blockIdentifier) || void 0;
4626
- let calldata = args[0];
4627
- if (options.parseRequest && !((_a = args[0]) == null ? void 0 : _a.compiled)) {
4793
+ let calldata = "compiled" in args ? args : args[0];
4794
+ if (options.parseRequest && !(calldata == null ? void 0 : calldata.compiled)) {
4628
4795
  const { inputs } = this.abi.find((abi) => abi.name === method);
4629
4796
  this.callData.validate("CALL", method, args);
4630
4797
  calldata = this.callData.compile(args, inputs);
@@ -4650,7 +4817,7 @@ var Contract = class {
4650
4817
  parseRequest: true
4651
4818
  }) {
4652
4819
  assert(this.address !== null, "contract is not connected to an address");
4653
- let calldata = args == null ? void 0 : args[0];
4820
+ let calldata = "compiled" in args ? args : args[0];
4654
4821
  if (options.parseRequest && !(calldata == null ? void 0 : calldata.compiled)) {
4655
4822
  const { inputs } = this.abi.find((abi) => abi.name === method);
4656
4823
  this.callData.validate("INVOKE", method, args);
@@ -5664,6 +5831,7 @@ export {
5664
5831
  TransactionType,
5665
5832
  addAddressPadding,
5666
5833
  buildUrl,
5834
+ cairo_exports as cairo,
5667
5835
  constants_exports as constants,
5668
5836
  defaultProvider,
5669
5837
  ec_exports as ec,