starknet 11.9.2 → 11.10.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
@@ -840,17 +840,21 @@ function tryToBigInt(value) {
840
840
  return value ? BigInt(value) : void 0;
841
841
  }
842
842
  function toHex(value) {
843
- return addHexPrefix(toBigInt(value).toString(16));
843
+ const bn = toBigInt(value);
844
+ if (bn < 0n) {
845
+ throw new Error(`Invalid input: ${bn} is negative, a hex-string cannot hold a sign`);
846
+ }
847
+ return addHexPrefix(bn.toString(16));
844
848
  }
845
849
  var toHexString = toHex;
846
850
  function cleanHex(hex) {
847
851
  return toHex(hex);
848
852
  }
849
853
  function toStorageKey(number) {
850
- return addHexPrefix(toBigInt(number).toString(16).padStart(64, "0"));
854
+ return addHexPrefix(removeHexPrefix(toHex(number)).padStart(64, "0"));
851
855
  }
852
856
  function toHex64(number) {
853
- const res = addHexPrefix(toBigInt(number).toString(16).padStart(64, "0"));
857
+ const res = addHexPrefix(removeHexPrefix(toHex(number)).padStart(64, "0"));
854
858
  if (res.length !== 66) throw TypeError("number is too big for hex 0x(64) representation");
855
859
  return res;
856
860
  }
@@ -16013,14 +16017,16 @@ function identifyRevision({ types, domain }) {
16013
16017
  return void 0;
16014
16018
  }
16015
16019
  function getHex(value) {
16020
+ let bn;
16016
16021
  try {
16017
- return toHex(value);
16022
+ bn = toBigInt(value);
16018
16023
  } catch (e) {
16019
16024
  if (isString(value)) {
16020
16025
  return toHex(CairoBytes31.fromText(value).toBigInt());
16021
16026
  }
16022
16027
  throw new Error(`Invalid BigNumberish: ${value}`);
16023
16028
  }
16029
+ return toHex(bn);
16024
16030
  }
16025
16031
  function validateTypedData(data) {
16026
16032
  const typedData = data;
@@ -21871,36 +21877,80 @@ var ContractInterface = class {
21871
21877
  };
21872
21878
 
21873
21879
  // src/utils/units.ts
21880
+ function amountName(tokenName) {
21881
+ return tokenName ? `${tokenName} amount` : "amount";
21882
+ }
21883
+ function checkTokenName(tokenName) {
21884
+ if (tokenName === void 0) return;
21885
+ assert(isString(tokenName), `the token name must be a string, not ${typeof tokenName}`);
21886
+ assert(tokenName.trim() !== "", "the token name is an empty string");
21887
+ }
21888
+ function toDecimals(decimals) {
21889
+ const shown = isString(decimals) ? `'${decimals}'` : String(decimals);
21890
+ assert(
21891
+ isBigNumberish(decimals) && (!isNumber(decimals) || Number.isInteger(decimals)),
21892
+ `decimals must be an integer, not ${shown}`
21893
+ );
21894
+ assertInRange(decimals, RANGE_U8.min, RANGE_U8.max, "decimals");
21895
+ return Number(decimals);
21896
+ }
21897
+ function decimalToInteger(text, decimals, tokenName) {
21898
+ const match = /^(-?)(\d*)(?:\.(\d*))?$/.exec(text);
21899
+ assert(
21900
+ match !== null && `${match[2]}${match[3] ?? ""}` !== "",
21901
+ `'${text}' is not a valid ${amountName(tokenName)}`
21902
+ );
21903
+ const [, sign, integer, fraction = ""] = match;
21904
+ const name = tokenName ? ` ${tokenName}` : "";
21905
+ const unit = decimals === 1 ? "decimal" : "decimals";
21906
+ assert(
21907
+ !/[1-9]/.test(fraction.slice(decimals)),
21908
+ `'${text}'${name} has more than ${decimals} ${unit}`
21909
+ );
21910
+ const result = BigInt(`0${integer}${fraction.slice(0, decimals).padEnd(decimals, "0")}`);
21911
+ return sign === "-" ? -result : result;
21912
+ }
21913
+ function integerToDecimal(value, decimals, tokenName) {
21914
+ assert(
21915
+ !isString(value) || value.trim() !== "",
21916
+ `the ${amountName(tokenName)} is an empty string`
21917
+ );
21918
+ const integer = toBigInt(value);
21919
+ const sign = integer < 0n ? "-" : "";
21920
+ const digits = (integer < 0n ? -integer : integer).toString();
21921
+ let text;
21922
+ if (digits.length <= decimals) {
21923
+ text = `0.${digits.padStart(decimals, "0")}`;
21924
+ } else {
21925
+ text = `${digits.slice(0, digits.length - decimals)}.${digits.slice(digits.length - decimals)}`;
21926
+ }
21927
+ return sign + text.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
21928
+ }
21874
21929
  function units(amount, symbol = "fri") {
21875
21930
  if (symbol === "strk") {
21876
- if (!isBigInt(amount) && !isString(amount)) {
21877
- throw new Error(`units: a strk amount must be a string or a bigint, not ${typeof amount}`);
21878
- }
21879
- const numStr = isBigInt(amount) || isHex(amount) ? toBigInt(amount).toString() : amount;
21880
- const match = /^(-?)(\d*)(?:\.(\d*))?$/.exec(numStr);
21881
- if (!match || `${match[2]}${match[3] ?? ""}` === "") {
21882
- throw new Error(`units: '${numStr}' is not a strk amount`);
21883
- }
21884
- const [, sign2, integer, decimal = ""] = match;
21885
- if (/[1-9]/.test(decimal.slice(18))) {
21886
- throw new Error(`units: '${numStr}' strk has more than 18 decimals`);
21887
- }
21888
- const fri2 = BigInt(`${integer}${decimal.slice(0, 18).padEnd(18, "0")}`);
21889
- return fri2 === 0n ? "0" : `${sign2}${fri2.toString()}`;
21890
- }
21891
- if (isString(amount) && amount.trim() === "") {
21892
- throw new Error("units: the fri amount is an empty string");
21893
- }
21894
- const fri = toBigInt(amount);
21895
- const sign = fri < 0n ? "-" : "";
21896
- const bis = (fri < 0n ? -fri : fri).toString();
21897
- let strk;
21898
- if (bis.length <= 18) {
21899
- strk = `0.${bis.padStart(18, "0")}`;
21900
- } else {
21901
- strk = `${bis.slice(0, bis.length - 18)}.${bis.slice(bis.length - 18)}`;
21931
+ assert(
21932
+ isBigInt(amount) || isString(amount),
21933
+ `the strk amount must be a string or a bigint, not ${typeof amount}`
21934
+ );
21935
+ const text = isBigInt(amount) || isHex(amount) ? toBigInt(amount).toString() : amount;
21936
+ return decimalToInteger(text, 18, "strk").toString();
21902
21937
  }
21903
- return sign + strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
21938
+ return integerToDecimal(amount, 18, "fri");
21939
+ }
21940
+ function parseUnits(value, decimals, tokenName) {
21941
+ checkTokenName(tokenName);
21942
+ const decimalCount = toDecimals(decimals);
21943
+ assert(isString(value), `the ${amountName(tokenName)} must be a string, not ${typeof value}`);
21944
+ return decimalToInteger(value, decimalCount, tokenName);
21945
+ }
21946
+ function formatUnits(value, decimals, tokenName) {
21947
+ checkTokenName(tokenName);
21948
+ const decimalCount = toDecimals(decimals);
21949
+ assert(
21950
+ isNumber(value) || isBigInt(value) || isString(value),
21951
+ `the ${amountName(tokenName)} must be a number, a bigint or a string, not ${typeof value}`
21952
+ );
21953
+ return integerToDecimal(value, decimalCount, tokenName);
21904
21954
  }
21905
21955
  var export_TypedDataRevision = api_exports.TypedDataRevision;
21906
21956
  export {
@@ -22049,6 +22099,7 @@ export {
22049
22099
  fastCairoTypeStrategy,
22050
22100
  fastExecute,
22051
22101
  fastParsingStrategy,
22102
+ formatUnits,
22052
22103
  getAbiVersion,
22053
22104
  getChecksumAddress,
22054
22105
  getGasPrices,
@@ -22078,6 +22129,7 @@ export {
22078
22129
  num_exports as num,
22079
22130
  outsideExecution_exports as outsideExecution,
22080
22131
  parseCalldataField,
22132
+ parseUnits,
22081
22133
  utils_exports as paymaster,
22082
22134
  paymasterPlugin,
22083
22135
  provider_exports as provider,