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.mjs CHANGED
@@ -2229,6 +2229,26 @@ 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
+ tuple: () => tuple,
2249
+ uint256: () => uint256
2250
+ });
2251
+
2232
2252
  // src/utils/num.ts
2233
2253
  var num_exports = {};
2234
2254
  __export(num_exports, {
@@ -2385,8 +2405,8 @@ __export(uint256_exports, {
2385
2405
  isUint256: () => isUint256,
2386
2406
  uint256ToBN: () => uint256ToBN
2387
2407
  });
2388
- function uint256ToBN(uint256) {
2389
- return (toBigInt(uint256.high) << 128n) + toBigInt(uint256.low);
2408
+ function uint256ToBN(uint2562) {
2409
+ return (toBigInt(uint2562.high) << 128n) + toBigInt(uint2562.low);
2390
2410
  }
2391
2411
  var UINT_128_MAX = (1n << 128n) - 1n;
2392
2412
  var UINT_256_MAX = (1n << 256n) - 1n;
@@ -2404,13 +2424,41 @@ function bnToUint256(bignumber) {
2404
2424
  }
2405
2425
 
2406
2426
  // src/utils/calldata/cairo.ts
2427
+ var Uint = /* @__PURE__ */ ((Uint2) => {
2428
+ Uint2["u8"] = "core::integer::u8";
2429
+ Uint2["u16"] = "core::integer::u16";
2430
+ Uint2["u32"] = "core::integer::u32";
2431
+ Uint2["u64"] = "core::integer::u64";
2432
+ Uint2["u128"] = "core::integer::u128";
2433
+ Uint2["u256"] = "core::integer::u256";
2434
+ return Uint2;
2435
+ })(Uint || {});
2407
2436
  var isLen = (name) => /_len$/.test(name);
2408
- var isTypeFelt = (type) => type === "felt";
2409
- var isTypeFeltArray = (type) => type === "felt*";
2410
- var isTypeArray = (type) => /\*/.test(type);
2437
+ var isTypeFelt = (type) => type === "felt" || type === "core::felt252";
2438
+ var isTypeArray = (type) => /\*/.test(type) || type.includes("core::array::Array::");
2411
2439
  var isTypeTuple = (type) => /^\(.*\)$/i.test(type);
2412
2440
  var isTypeNamedTuple = (type) => /\(.*\)/i.test(type) && type.includes(":");
2413
2441
  var isTypeStruct = (type, structs) => type in structs;
2442
+ var isTypeUint = (type) => Object.values(Uint).includes(type);
2443
+ var isTypeBool = (type) => type === "core::bool";
2444
+ var isTypeContractAddress = (type) => type === "core::starknet::contract_address::ContractAddress";
2445
+ var isCairo1Type = (type) => type.includes("core::");
2446
+ var getArrayType = (type) => {
2447
+ if (isCairo1Type(type)) {
2448
+ return type.substring(type.indexOf("<") + 1, type.indexOf(">"));
2449
+ }
2450
+ return type.replace("*", "");
2451
+ };
2452
+ var uint256 = (it) => {
2453
+ const bn = BigInt(it);
2454
+ if (!isUint256(bn))
2455
+ throw new Error("Number is too large");
2456
+ return {
2457
+ low: (bn & UINT_128_MAX).toString(10),
2458
+ high: (bn >> 128n).toString(10)
2459
+ };
2460
+ };
2461
+ var tuple = (...args) => ({ ...args });
2414
2462
  function felt(it) {
2415
2463
  if (isBigInt(it) || typeof it === "number" && Number.isInteger(it)) {
2416
2464
  return it.toString();
@@ -4201,7 +4249,7 @@ function parseSubTuple(s) {
4201
4249
  result
4202
4250
  };
4203
4251
  }
4204
- function extractTupleMemberTypes(type) {
4252
+ function extractCairo0Tuple(type) {
4205
4253
  const cleanType = type.replace(/\s/g, "").slice(1, -1);
4206
4254
  const { subTuple, result } = parseSubTuple(cleanType);
4207
4255
  let recomposed = result.split(",").map((it) => {
@@ -4214,6 +4262,16 @@ function extractTupleMemberTypes(type) {
4214
4262
  }
4215
4263
  return recomposed;
4216
4264
  }
4265
+ function extractCairo1Tuple(type) {
4266
+ const cleanType = type.replace(/\s/g, "").slice(1, -1);
4267
+ return cleanType.split(",");
4268
+ }
4269
+ function extractTupleMemberTypes(type) {
4270
+ if (isCairo1Type(type)) {
4271
+ return extractCairo1Tuple(type);
4272
+ }
4273
+ return extractCairo0Tuple(type);
4274
+ }
4217
4275
 
4218
4276
  // src/utils/calldata/requestParser.ts
4219
4277
  function parseTuple(element, typeStr) {
@@ -4272,16 +4330,21 @@ function parseCalldataField(argsIterator, input, structs) {
4272
4330
  }
4273
4331
  const result = [];
4274
4332
  result.push(felt(value.length));
4333
+ const arrayType = getArrayType(input.type);
4275
4334
  return value.reduce((acc, el) => {
4276
- if (isTypeFeltArray(type)) {
4335
+ if (isTypeFelt(arrayType) || isTypeUint(arrayType) || isTypeContractAddress(arrayType)) {
4277
4336
  acc.push(felt(el));
4337
+ } else if (isTypeBool(arrayType) && typeof el === "boolean") {
4338
+ acc.push(el.toString());
4278
4339
  } else {
4279
- acc.push(...parseCalldataValue(el, type.replace("*", ""), structs));
4340
+ acc.push(...parseCalldataValue(el, arrayType, structs));
4280
4341
  }
4281
4342
  return acc;
4282
4343
  }, result);
4283
4344
  case (isTypeStruct(type, structs) || isTypeTuple(type)):
4284
4345
  return parseCalldataValue(value, type, structs);
4346
+ case isTypeBool(type):
4347
+ return value;
4285
4348
  default:
4286
4349
  return felt(value);
4287
4350
  }
@@ -4314,8 +4377,20 @@ function responseParser(responseIterator, output, structs, parsedResult) {
4314
4377
  case isLen(name):
4315
4378
  temp = responseIterator.next().value;
4316
4379
  return BigInt(temp);
4380
+ case isTypeBool(type):
4381
+ temp = responseIterator.next().value;
4382
+ return Boolean(BigInt(temp));
4317
4383
  case isTypeArray(type):
4318
4384
  const parsedDataArr = [];
4385
+ if (isCairo1Type(type)) {
4386
+ responseIterator.next();
4387
+ let it = responseIterator.next();
4388
+ while (!it.done) {
4389
+ parsedDataArr.push(BigInt(it.value));
4390
+ it = responseIterator.next();
4391
+ }
4392
+ return parsedDataArr;
4393
+ }
4319
4394
  if (parsedResult && parsedResult[`${name}_len`]) {
4320
4395
  const arrLen = parsedResult[`${name}_len`];
4321
4396
  while (parsedDataArr.length < arrLen) {
@@ -4337,7 +4412,66 @@ function responseParser(responseIterator, output, structs, parsedResult) {
4337
4412
  var validateFelt = (parameter, input) => {
4338
4413
  assert(
4339
4414
  typeof parameter === "string" || typeof parameter === "number" || typeof parameter === "bigint",
4340
- `Validate: arg ${input.name} should be a felt (string, number, BigNumber)`
4415
+ `Validate: arg ${input.name} should be a felt typed as (String, Number or BigInt)`
4416
+ );
4417
+ };
4418
+ var validateUint = (parameter, input) => {
4419
+ if (typeof parameter === "number") {
4420
+ assert(
4421
+ parameter <= Number.MAX_SAFE_INTEGER,
4422
+ `Validation: Parameter is to large to be typed as Number use (BigInt or String)`
4423
+ );
4424
+ }
4425
+ assert(
4426
+ typeof parameter === "string" || typeof parameter === "number" || typeof parameter === "bigint",
4427
+ `Validate: arg ${input.name} of cairo type ${input.type} should be type (String, Number or BigInt)`
4428
+ );
4429
+ const param = toBigInt(parameter);
4430
+ switch (input.type) {
4431
+ case "core::integer::u8" /* u8 */:
4432
+ assert(
4433
+ param >= 0n && param <= 255n,
4434
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0 - 255]`
4435
+ );
4436
+ break;
4437
+ case "core::integer::u16" /* u16 */:
4438
+ assert(
4439
+ param >= 0n && param <= 65535n,
4440
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 65535]`
4441
+ );
4442
+ break;
4443
+ case "core::integer::u32" /* u32 */:
4444
+ assert(
4445
+ param >= 0n && param <= 4294967295n,
4446
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 4294967295]`
4447
+ );
4448
+ break;
4449
+ case "core::integer::u64" /* u64 */:
4450
+ assert(
4451
+ param >= 0n && param <= 2n ** 64n - 1n,
4452
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^64-1]`
4453
+ );
4454
+ break;
4455
+ case "core::integer::u128" /* u128 */:
4456
+ assert(
4457
+ param >= 0n && param <= 2n ** 128n - 1n,
4458
+ `Validate: arg ${input.name} cairo typed ${input.type} should be in range [0, 2^128-1]`
4459
+ );
4460
+ break;
4461
+ case "core::integer::u256" /* u256 */:
4462
+ assert(
4463
+ param >= 0n && param <= 2n ** 256n - 1n,
4464
+ `Validate: arg ${input.name} is ${input.type} 0 - 2^256-1`
4465
+ );
4466
+ break;
4467
+ default:
4468
+ break;
4469
+ }
4470
+ };
4471
+ var validateBool = (parameter, input) => {
4472
+ assert(
4473
+ typeof parameter === "boolean",
4474
+ `Validate: arg ${input.name} of cairo type ${input.type} should be type (Boolean)`
4341
4475
  );
4342
4476
  };
4343
4477
  var validateStruct = (parameter, input, structs) => {
@@ -4359,7 +4493,7 @@ var validateTuple = (parameter, input) => {
4359
4493
  );
4360
4494
  };
4361
4495
  var validateArray = (parameter, input, structs) => {
4362
- const baseType = input.type.replace("*", "");
4496
+ const baseType = getArrayType(input.type);
4363
4497
  if (isTypeFelt(baseType) && isLongText(parameter))
4364
4498
  return;
4365
4499
  assert(Array.isArray(parameter), `Validate: arg ${input.name} should be an Array`);
@@ -4375,6 +4509,12 @@ var validateArray = (parameter, input, structs) => {
4375
4509
  (it) => validateStruct(it, { name: input.name, type: baseType }, structs)
4376
4510
  );
4377
4511
  break;
4512
+ case isTypeUint(baseType):
4513
+ parameter.forEach((param) => validateUint(param, input));
4514
+ break;
4515
+ case isTypeBool(baseType):
4516
+ parameter.forEach((param) => validateBool(param, input));
4517
+ break;
4378
4518
  default:
4379
4519
  throw new Error(
4380
4520
  `Validate Unhandled: argument ${input.name}, type ${input.type}, value ${parameter}`
@@ -4390,6 +4530,14 @@ function validateFields(abiMethod, args, structs) {
4390
4530
  case isTypeFelt(input.type):
4391
4531
  validateFelt(parameter, input);
4392
4532
  break;
4533
+ case isTypeUint(input.type):
4534
+ validateUint(parameter, input);
4535
+ break;
4536
+ case isTypeBool(input.type):
4537
+ validateBool(parameter, input);
4538
+ break;
4539
+ case isTypeContractAddress(input.type):
4540
+ break;
4393
4541
  case isTypeStruct(input.type, structs):
4394
4542
  validateStruct(parameter, input, structs);
4395
4543
  break;
@@ -4478,13 +4626,15 @@ var CallData = class {
4478
4626
  parse(method, response) {
4479
4627
  const { outputs } = this.abi.find((abi) => abi.name === method);
4480
4628
  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`];
4629
+ const parsed = outputs.flat().reduce((acc, output, idx) => {
4630
+ const propName = output.name ?? idx;
4631
+ acc[propName] = responseParser(responseIterator, output, this.structs, acc);
4632
+ if (acc[propName] && acc[`${propName}_len`]) {
4633
+ delete acc[`${propName}_len`];
4485
4634
  }
4486
4635
  return acc;
4487
4636
  }, {});
4637
+ return Object.keys(parsed).length === 1 && 0 in parsed ? parsed[0] : parsed;
4488
4638
  }
4489
4639
  format(method, response, format) {
4490
4640
  const parsed = this.parse(method, response);
@@ -4556,13 +4706,20 @@ function buildEstimate(contract, functionAbi) {
4556
4706
  return contract.estimate(functionAbi.name, args);
4557
4707
  };
4558
4708
  }
4709
+ var detectCairoVersion = (abi) => {
4710
+ if (!abi)
4711
+ return "0";
4712
+ return abi.find((it) => "state_mutability" in it) ? "1" : "0";
4713
+ };
4559
4714
  var Contract = class {
4560
- constructor(abi, address, providerOrAccount = defaultProvider) {
4715
+ constructor(abi, address, providerOrAccount = defaultProvider, cairoVersion = detectCairoVersion(abi)) {
4716
+ this.version = "0";
4561
4717
  this.address = address && address.toLowerCase();
4562
4718
  this.providerOrAccount = providerOrAccount;
4563
4719
  this.callData = new CallData(abi);
4564
4720
  this.structs = CallData.getAbiStruct(abi);
4565
4721
  this.abi = abi;
4722
+ this.version = cairoVersion;
4566
4723
  const options = { enumerable: true, value: {}, writable: false };
4567
4724
  Object.defineProperties(this, {
4568
4725
  functions: { enumerable: true, value: {}, writable: false },
@@ -5664,6 +5821,7 @@ export {
5664
5821
  TransactionType,
5665
5822
  addAddressPadding,
5666
5823
  buildUrl,
5824
+ cairo_exports as cairo,
5667
5825
  constants_exports as constants,
5668
5826
  defaultProvider,
5669
5827
  ec_exports as ec,