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.js CHANGED
@@ -176,6 +176,7 @@ __export(index_exports, {
176
176
  fastCairoTypeStrategy: () => fastCairoTypeStrategy,
177
177
  fastExecute: () => fastExecute,
178
178
  fastParsingStrategy: () => fastParsingStrategy,
179
+ formatUnits: () => formatUnits,
179
180
  getAbiVersion: () => getAbiVersion,
180
181
  getChecksumAddress: () => getChecksumAddress,
181
182
  getGasPrices: () => getGasPrices,
@@ -205,6 +206,7 @@ __export(index_exports, {
205
206
  num: () => num_exports,
206
207
  outsideExecution: () => outsideExecution_exports,
207
208
  parseCalldataField: () => parseCalldataField,
209
+ parseUnits: () => parseUnits,
208
210
  paymaster: () => utils_exports,
209
211
  paymasterPlugin: () => paymasterPlugin,
210
212
  provider: () => provider_exports,
@@ -1049,17 +1051,21 @@ function tryToBigInt(value) {
1049
1051
  return value ? BigInt(value) : void 0;
1050
1052
  }
1051
1053
  function toHex(value) {
1052
- return addHexPrefix(toBigInt(value).toString(16));
1054
+ const bn = toBigInt(value);
1055
+ if (bn < 0n) {
1056
+ throw new Error(`Invalid input: ${bn} is negative, a hex-string cannot hold a sign`);
1057
+ }
1058
+ return addHexPrefix(bn.toString(16));
1053
1059
  }
1054
1060
  var toHexString = toHex;
1055
1061
  function cleanHex(hex) {
1056
1062
  return toHex(hex);
1057
1063
  }
1058
1064
  function toStorageKey(number) {
1059
- return addHexPrefix(toBigInt(number).toString(16).padStart(64, "0"));
1065
+ return addHexPrefix(removeHexPrefix(toHex(number)).padStart(64, "0"));
1060
1066
  }
1061
1067
  function toHex64(number) {
1062
- const res = addHexPrefix(toBigInt(number).toString(16).padStart(64, "0"));
1068
+ const res = addHexPrefix(removeHexPrefix(toHex(number)).padStart(64, "0"));
1063
1069
  if (res.length !== 66) throw TypeError("number is too big for hex 0x(64) representation");
1064
1070
  return res;
1065
1071
  }
@@ -16222,14 +16228,16 @@ function identifyRevision({ types, domain }) {
16222
16228
  return void 0;
16223
16229
  }
16224
16230
  function getHex(value) {
16231
+ let bn;
16225
16232
  try {
16226
- return toHex(value);
16233
+ bn = toBigInt(value);
16227
16234
  } catch (e) {
16228
16235
  if (isString(value)) {
16229
16236
  return toHex(CairoBytes31.fromText(value).toBigInt());
16230
16237
  }
16231
16238
  throw new Error(`Invalid BigNumberish: ${value}`);
16232
16239
  }
16240
+ return toHex(bn);
16233
16241
  }
16234
16242
  function validateTypedData(data) {
16235
16243
  const typedData = data;
@@ -22080,36 +22088,80 @@ var ContractInterface = class {
22080
22088
  };
22081
22089
 
22082
22090
  // src/utils/units.ts
22091
+ function amountName(tokenName) {
22092
+ return tokenName ? `${tokenName} amount` : "amount";
22093
+ }
22094
+ function checkTokenName(tokenName) {
22095
+ if (tokenName === void 0) return;
22096
+ assert(isString(tokenName), `the token name must be a string, not ${typeof tokenName}`);
22097
+ assert(tokenName.trim() !== "", "the token name is an empty string");
22098
+ }
22099
+ function toDecimals(decimals) {
22100
+ const shown = isString(decimals) ? `'${decimals}'` : String(decimals);
22101
+ assert(
22102
+ isBigNumberish(decimals) && (!isNumber(decimals) || Number.isInteger(decimals)),
22103
+ `decimals must be an integer, not ${shown}`
22104
+ );
22105
+ assertInRange(decimals, RANGE_U8.min, RANGE_U8.max, "decimals");
22106
+ return Number(decimals);
22107
+ }
22108
+ function decimalToInteger(text, decimals, tokenName) {
22109
+ const match = /^(-?)(\d*)(?:\.(\d*))?$/.exec(text);
22110
+ assert(
22111
+ match !== null && `${match[2]}${match[3] ?? ""}` !== "",
22112
+ `'${text}' is not a valid ${amountName(tokenName)}`
22113
+ );
22114
+ const [, sign, integer, fraction = ""] = match;
22115
+ const name = tokenName ? ` ${tokenName}` : "";
22116
+ const unit = decimals === 1 ? "decimal" : "decimals";
22117
+ assert(
22118
+ !/[1-9]/.test(fraction.slice(decimals)),
22119
+ `'${text}'${name} has more than ${decimals} ${unit}`
22120
+ );
22121
+ const result = BigInt(`0${integer}${fraction.slice(0, decimals).padEnd(decimals, "0")}`);
22122
+ return sign === "-" ? -result : result;
22123
+ }
22124
+ function integerToDecimal(value, decimals, tokenName) {
22125
+ assert(
22126
+ !isString(value) || value.trim() !== "",
22127
+ `the ${amountName(tokenName)} is an empty string`
22128
+ );
22129
+ const integer = toBigInt(value);
22130
+ const sign = integer < 0n ? "-" : "";
22131
+ const digits = (integer < 0n ? -integer : integer).toString();
22132
+ let text;
22133
+ if (digits.length <= decimals) {
22134
+ text = `0.${digits.padStart(decimals, "0")}`;
22135
+ } else {
22136
+ text = `${digits.slice(0, digits.length - decimals)}.${digits.slice(digits.length - decimals)}`;
22137
+ }
22138
+ return sign + text.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
22139
+ }
22083
22140
  function units(amount, symbol = "fri") {
22084
22141
  if (symbol === "strk") {
22085
- if (!isBigInt(amount) && !isString(amount)) {
22086
- throw new Error(`units: a strk amount must be a string or a bigint, not ${typeof amount}`);
22087
- }
22088
- const numStr = isBigInt(amount) || isHex(amount) ? toBigInt(amount).toString() : amount;
22089
- const match = /^(-?)(\d*)(?:\.(\d*))?$/.exec(numStr);
22090
- if (!match || `${match[2]}${match[3] ?? ""}` === "") {
22091
- throw new Error(`units: '${numStr}' is not a strk amount`);
22092
- }
22093
- const [, sign2, integer, decimal = ""] = match;
22094
- if (/[1-9]/.test(decimal.slice(18))) {
22095
- throw new Error(`units: '${numStr}' strk has more than 18 decimals`);
22096
- }
22097
- const fri2 = BigInt(`${integer}${decimal.slice(0, 18).padEnd(18, "0")}`);
22098
- return fri2 === 0n ? "0" : `${sign2}${fri2.toString()}`;
22099
- }
22100
- if (isString(amount) && amount.trim() === "") {
22101
- throw new Error("units: the fri amount is an empty string");
22102
- }
22103
- const fri = toBigInt(amount);
22104
- const sign = fri < 0n ? "-" : "";
22105
- const bis = (fri < 0n ? -fri : fri).toString();
22106
- let strk;
22107
- if (bis.length <= 18) {
22108
- strk = `0.${bis.padStart(18, "0")}`;
22109
- } else {
22110
- strk = `${bis.slice(0, bis.length - 18)}.${bis.slice(bis.length - 18)}`;
22142
+ assert(
22143
+ isBigInt(amount) || isString(amount),
22144
+ `the strk amount must be a string or a bigint, not ${typeof amount}`
22145
+ );
22146
+ const text = isBigInt(amount) || isHex(amount) ? toBigInt(amount).toString() : amount;
22147
+ return decimalToInteger(text, 18, "strk").toString();
22111
22148
  }
22112
- return sign + strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
22149
+ return integerToDecimal(amount, 18, "fri");
22150
+ }
22151
+ function parseUnits(value, decimals, tokenName) {
22152
+ checkTokenName(tokenName);
22153
+ const decimalCount = toDecimals(decimals);
22154
+ assert(isString(value), `the ${amountName(tokenName)} must be a string, not ${typeof value}`);
22155
+ return decimalToInteger(value, decimalCount, tokenName);
22156
+ }
22157
+ function formatUnits(value, decimals, tokenName) {
22158
+ checkTokenName(tokenName);
22159
+ const decimalCount = toDecimals(decimals);
22160
+ assert(
22161
+ isNumber(value) || isBigInt(value) || isString(value),
22162
+ `the ${amountName(tokenName)} must be a number, a bigint or a string, not ${typeof value}`
22163
+ );
22164
+ return integerToDecimal(value, decimalCount, tokenName);
22113
22165
  }
22114
22166
  // Annotate the CommonJS export names for ESM import in node:
22115
22167
  0 && (module.exports = {
@@ -22258,6 +22310,7 @@ function units(amount, symbol = "fri") {
22258
22310
  fastCairoTypeStrategy,
22259
22311
  fastExecute,
22260
22312
  fastParsingStrategy,
22313
+ formatUnits,
22261
22314
  getAbiVersion,
22262
22315
  getChecksumAddress,
22263
22316
  getGasPrices,
@@ -22287,6 +22340,7 @@ function units(amount, symbol = "fri") {
22287
22340
  num,
22288
22341
  outsideExecution,
22289
22342
  parseCalldataField,
22343
+ parseUnits,
22290
22344
  paymaster,
22291
22345
  paymasterPlugin,
22292
22346
  provider,