starknet 11.9.3 → 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,
@@ -22086,36 +22088,80 @@ var ContractInterface = class {
22086
22088
  };
22087
22089
 
22088
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
+ }
22089
22140
  function units(amount, symbol = "fri") {
22090
22141
  if (symbol === "strk") {
22091
- if (!isBigInt(amount) && !isString(amount)) {
22092
- throw new Error(`units: a strk amount must be a string or a bigint, not ${typeof amount}`);
22093
- }
22094
- const numStr = isBigInt(amount) || isHex(amount) ? toBigInt(amount).toString() : amount;
22095
- const match = /^(-?)(\d*)(?:\.(\d*))?$/.exec(numStr);
22096
- if (!match || `${match[2]}${match[3] ?? ""}` === "") {
22097
- throw new Error(`units: '${numStr}' is not a strk amount`);
22098
- }
22099
- const [, sign2, integer, decimal = ""] = match;
22100
- if (/[1-9]/.test(decimal.slice(18))) {
22101
- throw new Error(`units: '${numStr}' strk has more than 18 decimals`);
22102
- }
22103
- const fri2 = BigInt(`${integer}${decimal.slice(0, 18).padEnd(18, "0")}`);
22104
- return fri2 === 0n ? "0" : `${sign2}${fri2.toString()}`;
22105
- }
22106
- if (isString(amount) && amount.trim() === "") {
22107
- throw new Error("units: the fri amount is an empty string");
22108
- }
22109
- const fri = toBigInt(amount);
22110
- const sign = fri < 0n ? "-" : "";
22111
- const bis = (fri < 0n ? -fri : fri).toString();
22112
- let strk;
22113
- if (bis.length <= 18) {
22114
- strk = `0.${bis.padStart(18, "0")}`;
22115
- } else {
22116
- 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();
22117
22148
  }
22118
- 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);
22119
22165
  }
22120
22166
  // Annotate the CommonJS export names for ESM import in node:
22121
22167
  0 && (module.exports = {
@@ -22264,6 +22310,7 @@ function units(amount, symbol = "fri") {
22264
22310
  fastCairoTypeStrategy,
22265
22311
  fastExecute,
22266
22312
  fastParsingStrategy,
22313
+ formatUnits,
22267
22314
  getAbiVersion,
22268
22315
  getChecksumAddress,
22269
22316
  getGasPrices,
@@ -22293,6 +22340,7 @@ function units(amount, symbol = "fri") {
22293
22340
  num,
22294
22341
  outsideExecution,
22295
22342
  parseCalldataField,
22343
+ parseUnits,
22296
22344
  paymaster,
22297
22345
  paymasterPlugin,
22298
22346
  provider,