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/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## [11.7.2](https://github.com/starknet-io/starknet.js/compare/v11.7.1...v11.7.2) (2026-11-17)
2
+
3
+ ### Bug Fixes
4
+
5
+ - units() refuses malformed amounts and handles negative amounts ([4b7d396](https://github.com/starknet-io/starknet.js/commit/4b7d39644ca19402087b6926f200503e3f639157))
6
+
7
+ ## [11.7.1](https://github.com/starknet-io/starknet.js/compare/v11.7.0...v11.7.1) (2026-11-17)
8
+
9
+ ### Bug Fixes
10
+
11
+ - units() rejects strk amounts with more than 18 decimals and keeps zero ([4c1e43f](https://github.com/starknet-io/starknet.js/commit/4c1e43f17f6ccf23b7ebc5ad3018751ffc830d0e)), closes [#1756](https://github.com/starknet-io/starknet.js/issues/1756)
12
+
1
13
  # [11.7.0](https://github.com/starknet-io/starknet.js/compare/v11.6.3...v11.7.0) (2026-11-16)
2
14
 
3
15
  ### Features
package/dist/index.d.ts CHANGED
@@ -18547,14 +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
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
18551
18558
  * @example
18552
18559
  * ```typescript
18553
18560
  * units(1000n, 'fri') // '0.000000000000001' strk
18554
18561
  * units('1', 'strk') // '1000000000000000000' fri
18562
+ * units(-1n, 'fri') // '-0.000000000000000001' strk
18555
18563
  * ```
18556
18564
  */
18557
- declare function units(amount: string | bigint, simbol?: 'fri' | 'strk'): string;
18565
+ declare function units(amount: string | bigint, symbol?: 'fri' | 'strk'): string;
18558
18566
 
18559
18567
  /**
18560
18568
  * Request Permission for wallet account, return addresses that are allowed by user
@@ -27565,29 +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
- }
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`);
27578
27577
  }
27579
- const [integer, decimal = "0"] = numStr.split(".");
27580
- const pdec = decimal.padEnd(18, "0");
27581
- return `${integer}${pdec}`.replace(/\b0+/g, "");
27578
+ const [, sign3, integer, decimal = ""] = match;
27579
+ if (/[1-9]/.test(decimal.slice(18))) {
27580
+ throw new Error(`units: '${numStr}' strk has more than 18 decimals`);
27581
+ }
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");
27582
27587
  }
27583
- const bis = BigInt(amount).toString();
27588
+ const fri = toBigInt(amount);
27589
+ const sign2 = fri < 0n ? "-" : "";
27590
+ const bis = (fri < 0n ? -fri : fri).toString();
27584
27591
  let strk;
27585
27592
  if (bis.length <= 18) {
27586
27593
  strk = `0.${bis.padStart(18, "0")}`;
27587
27594
  } else {
27588
27595
  strk = `${bis.slice(0, bis.length - 18)}.${bis.slice(bis.length - 18)}`;
27589
27596
  }
27590
- return strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
27597
+ return sign2 + strk.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, "$1");
27591
27598
  }
27592
27599
  return __toCommonJS(index_exports);
27593
27600
  })();