starknet 11.7.0 → 11.7.2

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
@@ -22010,29 +22010,36 @@ var ContractInterface = class {
22010
22010
  };
22011
22011
 
22012
22012
  // src/utils/units.ts
22013
- function units(amount, simbol = "fri") {
22014
- if (simbol === "strk") {
22015
- let numStr = "";
22016
- if (isBigInt(amount)) numStr = amount.toString();
22017
- else if (isString(amount)) {
22018
- if (isHex(amount)) {
22019
- numStr = BigInt(amount).toString();
22020
- } else {
22021
- numStr = amount;
22022
- }
22013
+ function units(amount, symbol = "fri") {
22014
+ if (symbol === "strk") {
22015
+ if (!isBigInt(amount) && !isString(amount)) {
22016
+ throw new Error(`units: a strk amount must be a string or a bigint, not ${typeof amount}`);
22017
+ }
22018
+ const numStr = isBigInt(amount) || isHex(amount) ? toBigInt(amount).toString() : amount;
22019
+ const match = /^(-?)(\d*)(?:\.(\d*))?$/.exec(numStr);
22020
+ if (!match || `${match[2]}${match[3] ?? ""}` === "") {
22021
+ throw new Error(`units: '${numStr}' is not a strk amount`);
22023
22022
  }
22024
- const [integer, decimal = "0"] = numStr.split(".");
22025
- const pdec = decimal.padEnd(18, "0");
22026
- return `${integer}${pdec}`.replace(/\b0+/g, "");
22023
+ const [, sign2, integer, decimal = ""] = match;
22024
+ if (/[1-9]/.test(decimal.slice(18))) {
22025
+ throw new Error(`units: '${numStr}' strk has more than 18 decimals`);
22026
+ }
22027
+ const fri2 = BigInt(`${integer}${decimal.slice(0, 18).padEnd(18, "0")}`);
22028
+ return fri2 === 0n ? "0" : `${sign2}${fri2.toString()}`;
22029
+ }
22030
+ if (isString(amount) && amount.trim() === "") {
22031
+ throw new Error("units: the fri amount is an empty string");
22027
22032
  }
22028
- const bis = BigInt(amount).toString();
22033
+ const fri = toBigInt(amount);
22034
+ const sign = fri < 0n ? "-" : "";
22035
+ const bis = (fri < 0n ? -fri : fri).toString();
22029
22036
  let strk;
22030
22037
  if (bis.length <= 18) {
22031
22038
  strk = `0.${bis.padStart(18, "0")}`;
22032
22039
  } else {
22033
22040
  strk = `${bis.slice(0, bis.length - 18)}.${bis.slice(bis.length - 18)}`;
22034
22041
  }
22035
- return strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
22042
+ return sign + strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
22036
22043
  }
22037
22044
  // Annotate the CommonJS export names for ESM import in node:
22038
22045
  0 && (module.exports = {