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.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,27 @@ 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
+ isTypeUint256: () => isTypeUint256,
2317
+ tuple: () => tuple,
2318
+ uint256: () => uint256
2319
+ });
2320
+
2299
2321
  // src/utils/num.ts
2300
2322
  var num_exports = {};
2301
2323
  __export(num_exports, {
@@ -2452,8 +2474,8 @@ __export(uint256_exports, {
2452
2474
  isUint256: () => isUint256,
2453
2475
  uint256ToBN: () => uint256ToBN
2454
2476
  });
2455
- function uint256ToBN(uint256) {
2456
- return (toBigInt(uint256.high) << 128n) + toBigInt(uint256.low);
2477
+ function uint256ToBN(uint2562) {
2478
+ return (toBigInt(uint2562.high) << 128n) + toBigInt(uint2562.low);
2457
2479
  }
2458
2480
  var UINT_128_MAX = (1n << 128n) - 1n;
2459
2481
  var UINT_256_MAX = (1n << 256n) - 1n;
@@ -2471,13 +2493,42 @@ function bnToUint256(bignumber) {
2471
2493
  }
2472
2494
 
2473
2495
  // src/utils/calldata/cairo.ts
2496
+ var Uint = /* @__PURE__ */ ((Uint2) => {
2497
+ Uint2["u8"] = "core::integer::u8";
2498
+ Uint2["u16"] = "core::integer::u16";
2499
+ Uint2["u32"] = "core::integer::u32";
2500
+ Uint2["u64"] = "core::integer::u64";
2501
+ Uint2["u128"] = "core::integer::u128";
2502
+ Uint2["u256"] = "core::integer::u256";
2503
+ return Uint2;
2504
+ })(Uint || {});
2474
2505
  var isLen = (name) => /_len$/.test(name);
2475
- var isTypeFelt = (type) => type === "felt";
2476
- var isTypeFeltArray = (type) => type === "felt*";
2477
- var isTypeArray = (type) => /\*/.test(type);
2506
+ var isTypeFelt = (type) => type === "felt" || type === "core::felt252";
2507
+ var isTypeArray = (type) => /\*/.test(type) || type.includes("core::array::Array::");
2478
2508
  var isTypeTuple = (type) => /^\(.*\)$/i.test(type);
2479
2509
  var isTypeNamedTuple = (type) => /\(.*\)/i.test(type) && type.includes(":");
2480
2510
  var isTypeStruct = (type, structs) => type in structs;
2511
+ var isTypeUint = (type) => Object.values(Uint).includes(type);
2512
+ var isTypeUint256 = (type) => type === "core::integer::u256";
2513
+ var isTypeBool = (type) => type === "core::bool";
2514
+ var isTypeContractAddress = (type) => type === "core::starknet::contract_address::ContractAddress";
2515
+ var isCairo1Type = (type) => type.includes("core::");
2516
+ var getArrayType = (type) => {
2517
+ if (isCairo1Type(type)) {
2518
+ return type.substring(type.indexOf("<") + 1, type.indexOf(">"));
2519
+ }
2520
+ return type.replace("*", "");
2521
+ };
2522
+ var uint256 = (it) => {
2523
+ const bn = BigInt(it);
2524
+ if (!isUint256(bn))
2525
+ throw new Error("Number is too large");
2526
+ return {
2527
+ low: (bn & UINT_128_MAX).toString(10),
2528
+ high: (bn >> 128n).toString(10)
2529
+ };
2530
+ };
2531
+ var tuple = (...args) => ({ ...args });
2481
2532
  function felt(it) {
2482
2533
  if (isBigInt(it) || typeof it === "number" && Number.isInteger(it)) {
2483
2534
  return it.toString();
@@ -3748,10 +3799,11 @@ var SequencerProvider = class {
3748
3799
  const url = buildUrl(this.baseUrl, "", endpoint);
3749
3800
  const method = (options == null ? void 0 : options.method) ?? "GET";
3750
3801
  const headers = this.getHeaders(method);
3802
+ const body = stringify2(options == null ? void 0 : options.body);
3751
3803
  try {
3752
3804
  const response = await fetchPonyfill_default(url, {
3753
3805
  method,
3754
- body: stringify2(options == null ? void 0 : options.body),
3806
+ body,
3755
3807
  headers
3756
3808
  });
3757
3809
  const textResponse = await response.text();
@@ -4268,7 +4320,7 @@ function parseSubTuple(s) {
4268
4320
  result
4269
4321
  };
4270
4322
  }
4271
- function extractTupleMemberTypes(type) {
4323
+ function extractCairo0Tuple(type) {
4272
4324
  const cleanType = type.replace(/\s/g, "").slice(1, -1);
4273
4325
  const { subTuple, result } = parseSubTuple(cleanType);
4274
4326
  let recomposed = result.split(",").map((it) => {
@@ -4281,6 +4333,16 @@ function extractTupleMemberTypes(type) {
4281
4333
  }
4282
4334
  return recomposed;
4283
4335
  }
4336
+ function extractCairo1Tuple(type) {
4337
+ const cleanType = type.replace(/\s/g, "").slice(1, -1);
4338
+ return cleanType.split(",");
4339
+ }
4340
+ function extractTupleMemberTypes(type) {
4341
+ if (isCairo1Type(type)) {
4342
+ return extractCairo1Tuple(type);
4343
+ }
4344
+ return extractCairo0Tuple(type);
4345
+ }
4284
4346
 
4285
4347
  // src/utils/calldata/requestParser.ts
4286
4348
  function parseTuple(element, typeStr) {
@@ -4307,6 +4369,10 @@ function parseCalldataValue(element, type, structs) {
4307
4369
  if (Array.isArray(element)) {
4308
4370
  throw Error(`Array inside array (nD) are not supported by cairo. Element: ${element} ${type}`);
4309
4371
  }
4372
+ if (isTypeUint256(type)) {
4373
+ const el_uint256 = uint256(element);
4374
+ return [felt(el_uint256.low), felt(el_uint256.high)];
4375
+ }
4310
4376
  if (structs[type] && structs[type].members.length) {
4311
4377
  const { members } = structs[type];
4312
4378
  const subElement = element;
@@ -4339,16 +4405,21 @@ function parseCalldataField(argsIterator, input, structs) {
4339
4405
  }
4340
4406
  const result = [];
4341
4407
  result.push(felt(value.length));
4408
+ const arrayType = getArrayType(input.type);
4342
4409
  return value.reduce((acc, el) => {
4343
- if (isTypeFeltArray(type)) {
4410
+ if (isTypeFelt(arrayType) || isTypeUint(arrayType) || isTypeContractAddress(arrayType)) {
4344
4411
  acc.push(felt(el));
4412
+ } else if (isTypeBool(arrayType) && typeof el === "boolean") {
4413
+ acc.push(el.toString());
4345
4414
  } else {
4346
- acc.push(...parseCalldataValue(el, type.replace("*", ""), structs));
4415
+ acc.push(...parseCalldataValue(el, arrayType, structs));
4347
4416
  }
4348
4417
  return acc;
4349
4418
  }, result);
4350
- case (isTypeStruct(type, structs) || isTypeTuple(type)):
4419
+ case (isTypeStruct(type, structs) || isTypeTuple(type) || isTypeUint256(type)):
4351
4420
  return parseCalldataValue(value, type, structs);
4421
+ case isTypeBool(type):
4422
+ return `${+value}`;
4352
4423
  default:
4353
4424
  return felt(value);
4354
4425
  }
@@ -4381,8 +4452,24 @@ function responseParser(responseIterator, output, structs, parsedResult) {
4381
4452
  case isLen(name):
4382
4453
  temp = responseIterator.next().value;
4383
4454
  return BigInt(temp);
4455
+ case isTypeBool(type):
4456
+ temp = responseIterator.next().value;
4457
+ return Boolean(BigInt(temp));
4458
+ case isTypeUint256(type):
4459
+ const low = responseIterator.next().value;
4460
+ const high = responseIterator.next().value;
4461
+ return uint256ToBN({ low, high });
4384
4462
  case isTypeArray(type):
4385
4463
  const parsedDataArr = [];
4464
+ if (isCairo1Type(type)) {
4465
+ responseIterator.next();
4466
+ let it = responseIterator.next();
4467
+ while (!it.done) {
4468
+ parsedDataArr.push(BigInt(it.value));
4469
+ it = responseIterator.next();
4470
+ }
4471
+ return parsedDataArr;
4472
+ }
4386
4473
  if (parsedResult && parsedResult[`${name}_len`]) {
4387
4474
  const arrLen = parsedResult[`${name}_len`];
4388
4475
  while (parsedDataArr.length < arrLen) {
@@ -4404,7 +4491,66 @@ function responseParser(responseIterator, output, structs, parsedResult) {
4404
4491
  var validateFelt = (parameter, input) => {
4405
4492
  assert(
4406
4493
  typeof parameter === "string" || typeof parameter === "number" || typeof parameter === "bigint",
4407
- `Validate: arg ${input.name} should be a felt (string, number, BigNumber)`
4494
+ `Validate: arg ${input.name} should be a felt typed as (String, Number or BigInt)`
4495
+ );
4496
+ };
4497
+ var validateUint = (parameter, input) => {
4498
+ if (typeof parameter === "number") {
4499
+ assert(
4500
+ parameter <= Number.MAX_SAFE_INTEGER,
4501
+ `Validation: Parameter is to large to be typed as Number use (BigInt or String)`
4502
+ );
4503
+ }
4504
+ assert(
4505
+ typeof parameter === "string" || typeof parameter === "number" || typeof parameter === "bigint",
4506
+ `Validate: arg ${input.name} of cairo type ${input.type} should be type (String, Number or BigInt)`
4507
+ );
4508
+ const param = toBigInt(parameter);
4509
+ switch (input.type) {
4510
+ case "core::integer::u8" /* u8 */:
4511
+ assert(
4512
+ param >= 0n && param <= 255n,
4513
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0 - 255]`
4514
+ );
4515
+ break;
4516
+ case "core::integer::u16" /* u16 */:
4517
+ assert(
4518
+ param >= 0n && param <= 65535n,
4519
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 65535]`
4520
+ );
4521
+ break;
4522
+ case "core::integer::u32" /* u32 */:
4523
+ assert(
4524
+ param >= 0n && param <= 4294967295n,
4525
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 4294967295]`
4526
+ );
4527
+ break;
4528
+ case "core::integer::u64" /* u64 */:
4529
+ assert(
4530
+ param >= 0n && param <= 2n ** 64n - 1n,
4531
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^64-1]`
4532
+ );
4533
+ break;
4534
+ case "core::integer::u128" /* u128 */:
4535
+ assert(
4536
+ param >= 0n && param <= 2n ** 128n - 1n,
4537
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^128-1]`
4538
+ );
4539
+ break;
4540
+ case "core::integer::u256" /* u256 */:
4541
+ assert(
4542
+ param >= 0n && param <= 2n ** 256n - 1n,
4543
+ `Validate: arg ${input.name} is ${input.type} 0 - 2^256-1`
4544
+ );
4545
+ break;
4546
+ default:
4547
+ break;
4548
+ }
4549
+ };
4550
+ var validateBool = (parameter, input) => {
4551
+ assert(
4552
+ typeof parameter === "boolean",
4553
+ `Validate: arg ${input.name} of cairo type ${input.type} should be type (Boolean)`
4408
4554
  );
4409
4555
  };
4410
4556
  var validateStruct = (parameter, input, structs) => {
@@ -4426,7 +4572,7 @@ var validateTuple = (parameter, input) => {
4426
4572
  );
4427
4573
  };
4428
4574
  var validateArray = (parameter, input, structs) => {
4429
- const baseType = input.type.replace("*", "");
4575
+ const baseType = getArrayType(input.type);
4430
4576
  if (isTypeFelt(baseType) && isLongText(parameter))
4431
4577
  return;
4432
4578
  assert(Array.isArray(parameter), `Validate: arg ${input.name} should be an Array`);
@@ -4442,6 +4588,12 @@ var validateArray = (parameter, input, structs) => {
4442
4588
  (it) => validateStruct(it, { name: input.name, type: baseType }, structs)
4443
4589
  );
4444
4590
  break;
4591
+ case isTypeUint(baseType):
4592
+ parameter.forEach((param) => validateUint(param, input));
4593
+ break;
4594
+ case isTypeBool(baseType):
4595
+ parameter.forEach((param) => validateBool(param, input));
4596
+ break;
4445
4597
  default:
4446
4598
  throw new Error(
4447
4599
  `Validate Unhandled: argument ${input.name}, type ${input.type}, value ${parameter}`
@@ -4457,6 +4609,14 @@ function validateFields(abiMethod, args, structs) {
4457
4609
  case isTypeFelt(input.type):
4458
4610
  validateFelt(parameter, input);
4459
4611
  break;
4612
+ case isTypeUint(input.type):
4613
+ validateUint(parameter, input);
4614
+ break;
4615
+ case isTypeBool(input.type):
4616
+ validateBool(parameter, input);
4617
+ break;
4618
+ case isTypeContractAddress(input.type):
4619
+ break;
4460
4620
  case isTypeStruct(input.type, structs):
4461
4621
  validateStruct(parameter, input, structs);
4462
4622
  break;
@@ -4545,13 +4705,15 @@ var CallData = class {
4545
4705
  parse(method, response) {
4546
4706
  const { outputs } = this.abi.find((abi) => abi.name === method);
4547
4707
  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`];
4708
+ const parsed = outputs.flat().reduce((acc, output, idx) => {
4709
+ const propName = output.name ?? idx;
4710
+ acc[propName] = responseParser(responseIterator, output, this.structs, acc);
4711
+ if (acc[propName] && acc[`${propName}_len`]) {
4712
+ delete acc[`${propName}_len`];
4552
4713
  }
4553
4714
  return acc;
4554
4715
  }, {});
4716
+ return Object.keys(parsed).length === 1 && 0 in parsed ? parsed[0] : parsed;
4555
4717
  }
4556
4718
  format(method, response, format) {
4557
4719
  const parsed = this.parse(method, response);
@@ -4623,13 +4785,20 @@ function buildEstimate(contract, functionAbi) {
4623
4785
  return contract.estimate(functionAbi.name, args);
4624
4786
  };
4625
4787
  }
4788
+ var detectCairoVersion = (abi) => {
4789
+ if (!abi)
4790
+ return "0";
4791
+ return abi.find((it) => "state_mutability" in it) ? "1" : "0";
4792
+ };
4626
4793
  var Contract = class {
4627
- constructor(abi, address, providerOrAccount = defaultProvider) {
4794
+ constructor(abi, address, providerOrAccount = defaultProvider, cairoVersion = detectCairoVersion(abi)) {
4795
+ this.version = "0";
4628
4796
  this.address = address && address.toLowerCase();
4629
4797
  this.providerOrAccount = providerOrAccount;
4630
4798
  this.callData = new CallData(abi);
4631
4799
  this.structs = CallData.getAbiStruct(abi);
4632
4800
  this.abi = abi;
4801
+ this.version = cairoVersion;
4633
4802
  const options = { enumerable: true, value: {}, writable: false };
4634
4803
  Object.defineProperties(this, {
4635
4804
  functions: { enumerable: true, value: {}, writable: false },
@@ -4687,11 +4856,10 @@ var Contract = class {
4687
4856
  return this;
4688
4857
  }
4689
4858
  async call(method, args = [], options = { parseRequest: true, parseResponse: true, formatResponse: void 0 }) {
4690
- var _a;
4691
4859
  assert(this.address !== null, "contract is not connected to an address");
4692
4860
  const blockIdentifier = (options == null ? void 0 : options.blockIdentifier) || void 0;
4693
- let calldata = args[0];
4694
- if (options.parseRequest && !((_a = args[0]) == null ? void 0 : _a.compiled)) {
4861
+ let calldata = "compiled" in args ? args : args[0];
4862
+ if (options.parseRequest && !(calldata == null ? void 0 : calldata.compiled)) {
4695
4863
  const { inputs } = this.abi.find((abi) => abi.name === method);
4696
4864
  this.callData.validate("CALL", method, args);
4697
4865
  calldata = this.callData.compile(args, inputs);
@@ -4717,7 +4885,7 @@ var Contract = class {
4717
4885
  parseRequest: true
4718
4886
  }) {
4719
4887
  assert(this.address !== null, "contract is not connected to an address");
4720
- let calldata = args == null ? void 0 : args[0];
4888
+ let calldata = "compiled" in args ? args : args[0];
4721
4889
  if (options.parseRequest && !(calldata == null ? void 0 : calldata.compiled)) {
4722
4890
  const { inputs } = this.abi.find((abi) => abi.name === method);
4723
4891
  this.callData.validate("INVOKE", method, args);
@@ -5732,6 +5900,7 @@ var number = num_exports;
5732
5900
  TransactionType,
5733
5901
  addAddressPadding,
5734
5902
  buildUrl,
5903
+ cairo,
5735
5904
  constants,
5736
5905
  defaultProvider,
5737
5906
  ec,