starknet 11.7.1 → 11.8.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
@@ -21801,32 +21801,36 @@ var ContractInterface = class {
21801
21801
  };
21802
21802
 
21803
21803
  // src/utils/units.ts
21804
- function units(amount, simbol = "fri") {
21805
- if (simbol === "strk") {
21806
- let numStr = "";
21807
- if (isBigInt(amount)) numStr = amount.toString();
21808
- else if (isString(amount)) {
21809
- if (isHex(amount)) {
21810
- numStr = BigInt(amount).toString();
21811
- } else {
21812
- numStr = amount;
21813
- }
21814
- }
21815
- const [integer, decimal = "0"] = numStr.split(".");
21816
- if (decimal.length > 18) {
21804
+ function units(amount, symbol = "fri") {
21805
+ if (symbol === "strk") {
21806
+ if (!isBigInt(amount) && !isString(amount)) {
21807
+ throw new Error(`units: a strk amount must be a string or a bigint, not ${typeof amount}`);
21808
+ }
21809
+ const numStr = isBigInt(amount) || isHex(amount) ? toBigInt(amount).toString() : amount;
21810
+ const match = /^(-?)(\d*)(?:\.(\d*))?$/.exec(numStr);
21811
+ if (!match || `${match[2]}${match[3] ?? ""}` === "") {
21812
+ throw new Error(`units: '${numStr}' is not a strk amount`);
21813
+ }
21814
+ const [, sign2, integer, decimal = ""] = match;
21815
+ if (/[1-9]/.test(decimal.slice(18))) {
21817
21816
  throw new Error(`units: '${numStr}' strk has more than 18 decimals`);
21818
21817
  }
21819
- const pdec = decimal.padEnd(18, "0");
21820
- return `${integer}${pdec}`.replace(/\b0+(?=\d)/g, "");
21818
+ const fri2 = BigInt(`${integer}${decimal.slice(0, 18).padEnd(18, "0")}`);
21819
+ return fri2 === 0n ? "0" : `${sign2}${fri2.toString()}`;
21820
+ }
21821
+ if (isString(amount) && amount.trim() === "") {
21822
+ throw new Error("units: the fri amount is an empty string");
21821
21823
  }
21822
- const bis = BigInt(amount).toString();
21824
+ const fri = toBigInt(amount);
21825
+ const sign = fri < 0n ? "-" : "";
21826
+ const bis = (fri < 0n ? -fri : fri).toString();
21823
21827
  let strk;
21824
21828
  if (bis.length <= 18) {
21825
21829
  strk = `0.${bis.padStart(18, "0")}`;
21826
21830
  } else {
21827
21831
  strk = `${bis.slice(0, bis.length - 18)}.${bis.slice(bis.length - 18)}`;
21828
21832
  }
21829
- return strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
21833
+ return sign + strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
21830
21834
  }
21831
21835
  var export_TypedDataRevision = api_exports.TypedDataRevision;
21832
21836
  export {