starknet 5.5.0 → 5.6.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
@@ -48,6 +48,7 @@ __export(src_exports, {
48
48
  TransactionType: () => TransactionType,
49
49
  addAddressPadding: () => addAddressPadding,
50
50
  buildUrl: () => buildUrl,
51
+ cairo: () => cairo_exports,
51
52
  constants: () => constants_exports,
52
53
  defaultProvider: () => defaultProvider,
53
54
  ec: () => ec_exports,
@@ -2296,6 +2297,26 @@ var CONSTANT_POINTS = [
2296
2297
  ]
2297
2298
  ];
2298
2299
 
2300
+ // src/utils/calldata/cairo.ts
2301
+ var cairo_exports = {};
2302
+ __export(cairo_exports, {
2303
+ Uint: () => Uint,
2304
+ felt: () => felt,
2305
+ getArrayType: () => getArrayType,
2306
+ isCairo1Type: () => isCairo1Type,
2307
+ isLen: () => isLen,
2308
+ isTypeArray: () => isTypeArray,
2309
+ isTypeBool: () => isTypeBool,
2310
+ isTypeContractAddress: () => isTypeContractAddress,
2311
+ isTypeFelt: () => isTypeFelt,
2312
+ isTypeNamedTuple: () => isTypeNamedTuple,
2313
+ isTypeStruct: () => isTypeStruct,
2314
+ isTypeTuple: () => isTypeTuple,
2315
+ isTypeUint: () => isTypeUint,
2316
+ tuple: () => tuple,
2317
+ uint256: () => uint256
2318
+ });
2319
+
2299
2320
  // src/utils/num.ts
2300
2321
  var num_exports = {};
2301
2322
  __export(num_exports, {
@@ -2452,8 +2473,8 @@ __export(uint256_exports, {
2452
2473
  isUint256: () => isUint256,
2453
2474
  uint256ToBN: () => uint256ToBN
2454
2475
  });
2455
- function uint256ToBN(uint256) {
2456
- return (toBigInt(uint256.high) << 128n) + toBigInt(uint256.low);
2476
+ function uint256ToBN(uint2562) {
2477
+ return (toBigInt(uint2562.high) << 128n) + toBigInt(uint2562.low);
2457
2478
  }
2458
2479
  var UINT_128_MAX = (1n << 128n) - 1n;
2459
2480
  var UINT_256_MAX = (1n << 256n) - 1n;
@@ -2471,13 +2492,41 @@ function bnToUint256(bignumber) {
2471
2492
  }
2472
2493
 
2473
2494
  // src/utils/calldata/cairo.ts
2495
+ var Uint = /* @__PURE__ */ ((Uint2) => {
2496
+ Uint2["u8"] = "core::integer::u8";
2497
+ Uint2["u16"] = "core::integer::u16";
2498
+ Uint2["u32"] = "core::integer::u32";
2499
+ Uint2["u64"] = "core::integer::u64";
2500
+ Uint2["u128"] = "core::integer::u128";
2501
+ Uint2["u256"] = "core::integer::u256";
2502
+ return Uint2;
2503
+ })(Uint || {});
2474
2504
  var isLen = (name) => /_len$/.test(name);
2475
- var isTypeFelt = (type) => type === "felt";
2476
- var isTypeFeltArray = (type) => type === "felt*";
2477
- var isTypeArray = (type) => /\*/.test(type);
2505
+ var isTypeFelt = (type) => type === "felt" || type === "core::felt252";
2506
+ var isTypeArray = (type) => /\*/.test(type) || type.includes("core::array::Array::");
2478
2507
  var isTypeTuple = (type) => /^\(.*\)$/i.test(type);
2479
2508
  var isTypeNamedTuple = (type) => /\(.*\)/i.test(type) && type.includes(":");
2480
2509
  var isTypeStruct = (type, structs) => type in structs;
2510
+ var isTypeUint = (type) => Object.values(Uint).includes(type);
2511
+ var isTypeBool = (type) => type === "core::bool";
2512
+ var isTypeContractAddress = (type) => type === "core::starknet::contract_address::ContractAddress";
2513
+ var isCairo1Type = (type) => type.includes("core::");
2514
+ var getArrayType = (type) => {
2515
+ if (isCairo1Type(type)) {
2516
+ return type.substring(type.indexOf("<") + 1, type.indexOf(">"));
2517
+ }
2518
+ return type.replace("*", "");
2519
+ };
2520
+ var uint256 = (it) => {
2521
+ const bn = BigInt(it);
2522
+ if (!isUint256(bn))
2523
+ throw new Error("Number is too large");
2524
+ return {
2525
+ low: (bn & UINT_128_MAX).toString(10),
2526
+ high: (bn >> 128n).toString(10)
2527
+ };
2528
+ };
2529
+ var tuple = (...args) => ({ ...args });
2481
2530
  function felt(it) {
2482
2531
  if (isBigInt(it) || typeof it === "number" && Number.isInteger(it)) {
2483
2532
  return it.toString();
@@ -4268,7 +4317,7 @@ function parseSubTuple(s) {
4268
4317
  result
4269
4318
  };
4270
4319
  }
4271
- function extractTupleMemberTypes(type) {
4320
+ function extractCairo0Tuple(type) {
4272
4321
  const cleanType = type.replace(/\s/g, "").slice(1, -1);
4273
4322
  const { subTuple, result } = parseSubTuple(cleanType);
4274
4323
  let recomposed = result.split(",").map((it) => {
@@ -4281,6 +4330,16 @@ function extractTupleMemberTypes(type) {
4281
4330
  }
4282
4331
  return recomposed;
4283
4332
  }
4333
+ function extractCairo1Tuple(type) {
4334
+ const cleanType = type.replace(/\s/g, "").slice(1, -1);
4335
+ return cleanType.split(",");
4336
+ }
4337
+ function extractTupleMemberTypes(type) {
4338
+ if (isCairo1Type(type)) {
4339
+ return extractCairo1Tuple(type);
4340
+ }
4341
+ return extractCairo0Tuple(type);
4342
+ }
4284
4343
 
4285
4344
  // src/utils/calldata/requestParser.ts
4286
4345
  function parseTuple(element, typeStr) {
@@ -4339,16 +4398,21 @@ function parseCalldataField(argsIterator, input, structs) {
4339
4398
  }
4340
4399
  const result = [];
4341
4400
  result.push(felt(value.length));
4401
+ const arrayType = getArrayType(input.type);
4342
4402
  return value.reduce((acc, el) => {
4343
- if (isTypeFeltArray(type)) {
4403
+ if (isTypeFelt(arrayType) || isTypeUint(arrayType) || isTypeContractAddress(arrayType)) {
4344
4404
  acc.push(felt(el));
4405
+ } else if (isTypeBool(arrayType) && typeof el === "boolean") {
4406
+ acc.push(el.toString());
4345
4407
  } else {
4346
- acc.push(...parseCalldataValue(el, type.replace("*", ""), structs));
4408
+ acc.push(...parseCalldataValue(el, arrayType, structs));
4347
4409
  }
4348
4410
  return acc;
4349
4411
  }, result);
4350
4412
  case (isTypeStruct(type, structs) || isTypeTuple(type)):
4351
4413
  return parseCalldataValue(value, type, structs);
4414
+ case isTypeBool(type):
4415
+ return value;
4352
4416
  default:
4353
4417
  return felt(value);
4354
4418
  }
@@ -4381,8 +4445,20 @@ function responseParser(responseIterator, output, structs, parsedResult) {
4381
4445
  case isLen(name):
4382
4446
  temp = responseIterator.next().value;
4383
4447
  return BigInt(temp);
4448
+ case isTypeBool(type):
4449
+ temp = responseIterator.next().value;
4450
+ return Boolean(BigInt(temp));
4384
4451
  case isTypeArray(type):
4385
4452
  const parsedDataArr = [];
4453
+ if (isCairo1Type(type)) {
4454
+ responseIterator.next();
4455
+ let it = responseIterator.next();
4456
+ while (!it.done) {
4457
+ parsedDataArr.push(BigInt(it.value));
4458
+ it = responseIterator.next();
4459
+ }
4460
+ return parsedDataArr;
4461
+ }
4386
4462
  if (parsedResult && parsedResult[`${name}_len`]) {
4387
4463
  const arrLen = parsedResult[`${name}_len`];
4388
4464
  while (parsedDataArr.length < arrLen) {
@@ -4404,7 +4480,66 @@ function responseParser(responseIterator, output, structs, parsedResult) {
4404
4480
  var validateFelt = (parameter, input) => {
4405
4481
  assert(
4406
4482
  typeof parameter === "string" || typeof parameter === "number" || typeof parameter === "bigint",
4407
- `Validate: arg ${input.name} should be a felt (string, number, BigNumber)`
4483
+ `Validate: arg ${input.name} should be a felt typed as (String, Number or BigInt)`
4484
+ );
4485
+ };
4486
+ var validateUint = (parameter, input) => {
4487
+ if (typeof parameter === "number") {
4488
+ assert(
4489
+ parameter <= Number.MAX_SAFE_INTEGER,
4490
+ `Validation: Parameter is to large to be typed as Number use (BigInt or String)`
4491
+ );
4492
+ }
4493
+ assert(
4494
+ typeof parameter === "string" || typeof parameter === "number" || typeof parameter === "bigint",
4495
+ `Validate: arg ${input.name} of cairo type ${input.type} should be type (String, Number or BigInt)`
4496
+ );
4497
+ const param = toBigInt(parameter);
4498
+ switch (input.type) {
4499
+ case "core::integer::u8" /* u8 */:
4500
+ assert(
4501
+ param >= 0n && param <= 255n,
4502
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0 - 255]`
4503
+ );
4504
+ break;
4505
+ case "core::integer::u16" /* u16 */:
4506
+ assert(
4507
+ param >= 0n && param <= 65535n,
4508
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 65535]`
4509
+ );
4510
+ break;
4511
+ case "core::integer::u32" /* u32 */:
4512
+ assert(
4513
+ param >= 0n && param <= 4294967295n,
4514
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 4294967295]`
4515
+ );
4516
+ break;
4517
+ case "core::integer::u64" /* u64 */:
4518
+ assert(
4519
+ param >= 0n && param <= 2n ** 64n - 1n,
4520
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^64-1]`
4521
+ );
4522
+ break;
4523
+ case "core::integer::u128" /* u128 */:
4524
+ assert(
4525
+ param >= 0n && param <= 2n ** 128n - 1n,
4526
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^128-1]`
4527
+ );
4528
+ break;
4529
+ case "core::integer::u256" /* u256 */:
4530
+ assert(
4531
+ param >= 0n && param <= 2n ** 256n - 1n,
4532
+ `Validate: arg ${input.name} is ${input.type} 0 - 2^256-1`
4533
+ );
4534
+ break;
4535
+ default:
4536
+ break;
4537
+ }
4538
+ };
4539
+ var validateBool = (parameter, input) => {
4540
+ assert(
4541
+ typeof parameter === "boolean",
4542
+ `Validate: arg ${input.name} of cairo type ${input.type} should be type (Boolean)`
4408
4543
  );
4409
4544
  };
4410
4545
  var validateStruct = (parameter, input, structs) => {
@@ -4426,7 +4561,7 @@ var validateTuple = (parameter, input) => {
4426
4561
  );
4427
4562
  };
4428
4563
  var validateArray = (parameter, input, structs) => {
4429
- const baseType = input.type.replace("*", "");
4564
+ const baseType = getArrayType(input.type);
4430
4565
  if (isTypeFelt(baseType) && isLongText(parameter))
4431
4566
  return;
4432
4567
  assert(Array.isArray(parameter), `Validate: arg ${input.name} should be an Array`);
@@ -4442,6 +4577,12 @@ var validateArray = (parameter, input, structs) => {
4442
4577
  (it) => validateStruct(it, { name: input.name, type: baseType }, structs)
4443
4578
  );
4444
4579
  break;
4580
+ case isTypeUint(baseType):
4581
+ parameter.forEach((param) => validateUint(param, input));
4582
+ break;
4583
+ case isTypeBool(baseType):
4584
+ parameter.forEach((param) => validateBool(param, input));
4585
+ break;
4445
4586
  default:
4446
4587
  throw new Error(
4447
4588
  `Validate Unhandled: argument ${input.name}, type ${input.type}, value ${parameter}`
@@ -4457,6 +4598,14 @@ function validateFields(abiMethod, args, structs) {
4457
4598
  case isTypeFelt(input.type):
4458
4599
  validateFelt(parameter, input);
4459
4600
  break;
4601
+ case isTypeUint(input.type):
4602
+ validateUint(parameter, input);
4603
+ break;
4604
+ case isTypeBool(input.type):
4605
+ validateBool(parameter, input);
4606
+ break;
4607
+ case isTypeContractAddress(input.type):
4608
+ break;
4460
4609
  case isTypeStruct(input.type, structs):
4461
4610
  validateStruct(parameter, input, structs);
4462
4611
  break;
@@ -4545,13 +4694,15 @@ var CallData = class {
4545
4694
  parse(method, response) {
4546
4695
  const { outputs } = this.abi.find((abi) => abi.name === method);
4547
4696
  const responseIterator = response.flat()[Symbol.iterator]();
4548
- return outputs.flat().reduce((acc, output) => {
4549
- acc[output.name] = responseParser(responseIterator, output, this.structs, acc);
4550
- if (acc[output.name] && acc[`${output.name}_len`]) {
4551
- delete acc[`${output.name}_len`];
4697
+ const parsed = outputs.flat().reduce((acc, output, idx) => {
4698
+ const propName = output.name ?? idx;
4699
+ acc[propName] = responseParser(responseIterator, output, this.structs, acc);
4700
+ if (acc[propName] && acc[`${propName}_len`]) {
4701
+ delete acc[`${propName}_len`];
4552
4702
  }
4553
4703
  return acc;
4554
4704
  }, {});
4705
+ return Object.keys(parsed).length === 1 && 0 in parsed ? parsed[0] : parsed;
4555
4706
  }
4556
4707
  format(method, response, format) {
4557
4708
  const parsed = this.parse(method, response);
@@ -4623,13 +4774,20 @@ function buildEstimate(contract, functionAbi) {
4623
4774
  return contract.estimate(functionAbi.name, args);
4624
4775
  };
4625
4776
  }
4777
+ var detectCairoVersion = (abi) => {
4778
+ if (!abi)
4779
+ return "0";
4780
+ return abi.find((it) => "state_mutability" in it) ? "1" : "0";
4781
+ };
4626
4782
  var Contract = class {
4627
- constructor(abi, address, providerOrAccount = defaultProvider) {
4783
+ constructor(abi, address, providerOrAccount = defaultProvider, cairoVersion = detectCairoVersion(abi)) {
4784
+ this.version = "0";
4628
4785
  this.address = address && address.toLowerCase();
4629
4786
  this.providerOrAccount = providerOrAccount;
4630
4787
  this.callData = new CallData(abi);
4631
4788
  this.structs = CallData.getAbiStruct(abi);
4632
4789
  this.abi = abi;
4790
+ this.version = cairoVersion;
4633
4791
  const options = { enumerable: true, value: {}, writable: false };
4634
4792
  Object.defineProperties(this, {
4635
4793
  functions: { enumerable: true, value: {}, writable: false },
@@ -5732,6 +5890,7 @@ var number = num_exports;
5732
5890
  TransactionType,
5733
5891
  addAddressPadding,
5734
5892
  buildUrl,
5893
+ cairo,
5735
5894
  constants,
5736
5895
  defaultProvider,
5737
5896
  ec,