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.d.ts CHANGED
@@ -18547,15 +18547,22 @@ declare function contractClassResponseToLegacyCompiledContract(ccr: ContractClas
18547
18547
  declare function createTransactionReceipt(receipt: GetTxReceiptResponseWithoutHelper): GetTransactionReceiptResponse;
18548
18548
 
18549
18549
  /**
18550
- * Convert strk to fri or fri to strk
18551
- * @throws {Error} if a strk amount has more than 18 decimals (1 strk = 10^18 fri)
18550
+ * Convert strk to fri or fri to strk. Negative amounts are accepted in both directions.
18551
+ * @param {string | bigint} amount amount to convert, in the unit given by `symbol`
18552
+ * @param {'fri' | 'strk'} symbol unit of `amount`: 'fri' (default) gives strk, 'strk' gives fri
18553
+ * @throws {Error} if a strk amount is not a bigint, a hex string or a decimal string
18554
+ * (an optional minus sign, then digits with at most one dot)
18555
+ * @throws {Error} if a strk amount has more than 18 decimals, not counting zeros at the end
18556
+ * (1 strk = 10^18 fri)
18557
+ * @throws {Error} if a fri amount is an empty string
18552
18558
  * @example
18553
18559
  * ```typescript
18554
18560
  * units(1000n, 'fri') // '0.000000000000001' strk
18555
18561
  * units('1', 'strk') // '1000000000000000000' fri
18562
+ * units(-1n, 'fri') // '-0.000000000000000001' strk
18556
18563
  * ```
18557
18564
  */
18558
- declare function units(amount: string | bigint, simbol?: 'fri' | 'strk'): string;
18565
+ declare function units(amount: string | bigint, symbol?: 'fri' | 'strk'): string;
18559
18566
 
18560
18567
  /**
18561
18568
  * Request Permission for wallet account, return addresses that are allowed by user
@@ -27565,32 +27565,36 @@ ${indent}}` : "}";
27565
27565
  };
27566
27566
 
27567
27567
  // src/utils/units.ts
27568
- function units(amount, simbol = "fri") {
27569
- if (simbol === "strk") {
27570
- let numStr = "";
27571
- if (isBigInt(amount)) numStr = amount.toString();
27572
- else if (isString(amount)) {
27573
- if (isHex2(amount)) {
27574
- numStr = BigInt(amount).toString();
27575
- } else {
27576
- numStr = amount;
27577
- }
27578
- }
27579
- const [integer, decimal = "0"] = numStr.split(".");
27580
- if (decimal.length > 18) {
27568
+ function units(amount, symbol = "fri") {
27569
+ if (symbol === "strk") {
27570
+ if (!isBigInt(amount) && !isString(amount)) {
27571
+ throw new Error(`units: a strk amount must be a string or a bigint, not ${typeof amount}`);
27572
+ }
27573
+ const numStr = isBigInt(amount) || isHex2(amount) ? toBigInt(amount).toString() : amount;
27574
+ const match = /^(-?)(\d*)(?:\.(\d*))?$/.exec(numStr);
27575
+ if (!match || `${match[2]}${match[3] ?? ""}` === "") {
27576
+ throw new Error(`units: '${numStr}' is not a strk amount`);
27577
+ }
27578
+ const [, sign3, integer, decimal = ""] = match;
27579
+ if (/[1-9]/.test(decimal.slice(18))) {
27581
27580
  throw new Error(`units: '${numStr}' strk has more than 18 decimals`);
27582
27581
  }
27583
- const pdec = decimal.padEnd(18, "0");
27584
- return `${integer}${pdec}`.replace(/\b0+(?=\d)/g, "");
27582
+ const fri2 = BigInt(`${integer}${decimal.slice(0, 18).padEnd(18, "0")}`);
27583
+ return fri2 === 0n ? "0" : `${sign3}${fri2.toString()}`;
27584
+ }
27585
+ if (isString(amount) && amount.trim() === "") {
27586
+ throw new Error("units: the fri amount is an empty string");
27585
27587
  }
27586
- const bis = BigInt(amount).toString();
27588
+ const fri = toBigInt(amount);
27589
+ const sign2 = fri < 0n ? "-" : "";
27590
+ const bis = (fri < 0n ? -fri : fri).toString();
27587
27591
  let strk;
27588
27592
  if (bis.length <= 18) {
27589
27593
  strk = `0.${bis.padStart(18, "0")}`;
27590
27594
  } else {
27591
27595
  strk = `${bis.slice(0, bis.length - 18)}.${bis.slice(bis.length - 18)}`;
27592
27596
  }
27593
- return strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
27597
+ return sign2 + strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
27594
27598
  }
27595
27599
  return __toCommonJS(index_exports);
27596
27600
  })();