@nadohq/shared 0.53.0 → 0.54.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/{decimalAdjustment-DD7Q2eMZ.d.cts → decimalAdjustment-DCwilVOA.d.cts} +9 -7
- package/dist/decimalAdjustment-DCwilVOA.d.cts.map +1 -0
- package/dist/{decimalAdjustment-DU0dlRcS.d.ts → decimalAdjustment-pLqKqD8x.d.ts} +9 -7
- package/dist/decimalAdjustment-pLqKqD8x.d.ts.map +1 -0
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/utils/index.d.cts +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/math/decimalAdjustment.cjs +6 -4
- package/dist/utils/math/decimalAdjustment.cjs.map +1 -1
- package/dist/utils/math/decimalAdjustment.d.cts +1 -1
- package/dist/utils/math/decimalAdjustment.d.ts +1 -1
- package/dist/utils/math/decimalAdjustment.js +6 -4
- package/dist/utils/math/decimalAdjustment.js.map +1 -1
- package/dist/utils/math/index.d.cts +1 -1
- package/dist/utils/math/index.d.ts +1 -1
- package/package.json +2 -2
- package/src/utils/math/decimalAdjustment.test.ts +85 -0
- package/src/utils/math/decimalAdjustment.ts +11 -10
- package/dist/decimalAdjustment-DD7Q2eMZ.d.cts.map +0 -1
- package/dist/decimalAdjustment-DU0dlRcS.d.ts.map +0 -1
|
@@ -9,25 +9,27 @@ declare const NADO_PRODUCT_DECIMALS = 18;
|
|
|
9
9
|
/**
|
|
10
10
|
* Determines the result type after adjusting decimals based on the input type `T`.
|
|
11
11
|
*
|
|
12
|
-
* - If `T` is `undefined`, the result
|
|
12
|
+
* - If `T` is `null` or `undefined`, the result preserves that value.
|
|
13
13
|
* - If `T` is a `number`, the result is a `number`.
|
|
14
14
|
* - Otherwise, the result is a `BigNumber`.
|
|
15
15
|
*/
|
|
16
|
-
type AdjustDecimalsResult<T extends BigNumberish | undefined> = T extends undefined ?
|
|
16
|
+
type AdjustDecimalsResult<T extends BigNumberish | null | undefined> = T extends null | undefined ? T : T extends number ? number : BigNumber;
|
|
17
17
|
/**
|
|
18
18
|
* Adds the specified # of decimals to the number. For example, value = 1, decimals = 2, returns 100.
|
|
19
19
|
*
|
|
20
|
-
* @param value can be
|
|
20
|
+
* @param value can be null or undefined; either is returned unchanged.
|
|
21
21
|
* @param decimals number of decimal places to add, defaults to 18, which is the standard within Nado
|
|
22
|
+
* @returns The adjusted number or BigNumber, preserving null and undefined inputs.
|
|
22
23
|
*/
|
|
23
|
-
declare function addDecimals<T extends BigNumberish | undefined>(value: T, decimals?: number): AdjustDecimalsResult<T>;
|
|
24
|
+
declare function addDecimals<T extends BigNumberish | null | undefined>(value: T, decimals?: number): AdjustDecimalsResult<T>;
|
|
24
25
|
/**
|
|
25
26
|
* Removes the specified # of decimals from the number. For example, value = 100, decimals = 2, returns 1.
|
|
26
27
|
*
|
|
27
|
-
* @param value can be
|
|
28
|
+
* @param value can be null or undefined; either is returned unchanged.
|
|
28
29
|
* @param decimals number of decimal places to remove, defaults to 18, which is the standard within Nado
|
|
30
|
+
* @returns The adjusted number or BigNumber, preserving null and undefined inputs.
|
|
29
31
|
*/
|
|
30
|
-
declare function removeDecimals<T extends BigNumberish | undefined>(value: T, decimals?: number): AdjustDecimalsResult<T>;
|
|
32
|
+
declare function removeDecimals<T extends BigNumberish | null | undefined>(value: T, decimals?: number): AdjustDecimalsResult<T>;
|
|
31
33
|
//#endregion
|
|
32
34
|
export { addDecimals as n, removeDecimals as r, NADO_PRODUCT_DECIMALS as t };
|
|
33
|
-
//# sourceMappingURL=decimalAdjustment-
|
|
35
|
+
//# sourceMappingURL=decimalAdjustment-DCwilVOA.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decimalAdjustment-DCwilVOA.d.cts","names":[],"sources":["../src/utils/math/src/utils/math/decimalAdjustment.ts"],"mappings":";;;;;;AAMA;cAAa,qBAAA;;;AAAqB;AAAM;;;;KASnC,oBAAA,WAA+B,YAAA,uBAClC,CAAA,4BAA6B,CAAA,GAAI,CAAA,2BAA4B,SAAA;;;;;;;;iBAS/C,WAAA,WAAsB,YAAA,oBAAA,CACpC,KAAA,EAAO,CAAA,EACP,QAAA,YACC,oBAAA,CAAqB,CAAA;;;;;AAZgD;AASxE;;iBAyBgB,cAAA,WAAyB,YAAA,oBAAA,CACvC,KAAA,EAAO,CAAA,EACP,QAAA,YACC,oBAAA,CAAqB,CAAA"}
|
|
@@ -9,25 +9,27 @@ declare const NADO_PRODUCT_DECIMALS = 18;
|
|
|
9
9
|
/**
|
|
10
10
|
* Determines the result type after adjusting decimals based on the input type `T`.
|
|
11
11
|
*
|
|
12
|
-
* - If `T` is `undefined`, the result
|
|
12
|
+
* - If `T` is `null` or `undefined`, the result preserves that value.
|
|
13
13
|
* - If `T` is a `number`, the result is a `number`.
|
|
14
14
|
* - Otherwise, the result is a `BigNumber`.
|
|
15
15
|
*/
|
|
16
|
-
type AdjustDecimalsResult<T extends BigNumberish | undefined> = T extends undefined ?
|
|
16
|
+
type AdjustDecimalsResult<T extends BigNumberish | null | undefined> = T extends null | undefined ? T : T extends number ? number : BigNumber;
|
|
17
17
|
/**
|
|
18
18
|
* Adds the specified # of decimals to the number. For example, value = 1, decimals = 2, returns 100.
|
|
19
19
|
*
|
|
20
|
-
* @param value can be
|
|
20
|
+
* @param value can be null or undefined; either is returned unchanged.
|
|
21
21
|
* @param decimals number of decimal places to add, defaults to 18, which is the standard within Nado
|
|
22
|
+
* @returns The adjusted number or BigNumber, preserving null and undefined inputs.
|
|
22
23
|
*/
|
|
23
|
-
declare function addDecimals<T extends BigNumberish | undefined>(value: T, decimals?: number): AdjustDecimalsResult<T>;
|
|
24
|
+
declare function addDecimals<T extends BigNumberish | null | undefined>(value: T, decimals?: number): AdjustDecimalsResult<T>;
|
|
24
25
|
/**
|
|
25
26
|
* Removes the specified # of decimals from the number. For example, value = 100, decimals = 2, returns 1.
|
|
26
27
|
*
|
|
27
|
-
* @param value can be
|
|
28
|
+
* @param value can be null or undefined; either is returned unchanged.
|
|
28
29
|
* @param decimals number of decimal places to remove, defaults to 18, which is the standard within Nado
|
|
30
|
+
* @returns The adjusted number or BigNumber, preserving null and undefined inputs.
|
|
29
31
|
*/
|
|
30
|
-
declare function removeDecimals<T extends BigNumberish | undefined>(value: T, decimals?: number): AdjustDecimalsResult<T>;
|
|
32
|
+
declare function removeDecimals<T extends BigNumberish | null | undefined>(value: T, decimals?: number): AdjustDecimalsResult<T>;
|
|
31
33
|
//#endregion
|
|
32
34
|
export { addDecimals as n, removeDecimals as r, NADO_PRODUCT_DECIMALS as t };
|
|
33
|
-
//# sourceMappingURL=decimalAdjustment-
|
|
35
|
+
//# sourceMappingURL=decimalAdjustment-pLqKqD8x.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decimalAdjustment-pLqKqD8x.d.ts","names":[],"sources":["../src/utils/math/src/utils/math/decimalAdjustment.ts"],"mappings":";;;;;;AAMA;cAAa,qBAAA;;;AAAqB;AAAM;;;;KASnC,oBAAA,WAA+B,YAAA,uBAClC,CAAA,4BAA6B,CAAA,GAAI,CAAA,2BAA4B,SAAA;;;;;;;;iBAS/C,WAAA,WAAsB,YAAA,oBAAA,CACpC,KAAA,EAAO,CAAA,EACP,QAAA,YACC,oBAAA,CAAqB,CAAA;;;;;AAZgD;AASxE;;iBAyBgB,cAAA,WAAyB,YAAA,oBAAA,CACvC,KAAA,EAAO,CAAA,EACP,QAAA,YACC,oBAAA,CAAqB,CAAA"}
|
package/dist/index.d.cts
CHANGED
|
@@ -21,7 +21,7 @@ import { n as WithContract, r as WithContracts, t as NadoContracts } from "./nad
|
|
|
21
21
|
import { n as toBigNumber, t as BigNumberish } from "./bigNumber-BqgJEjFs.cjs";
|
|
22
22
|
import { t as BigNumbers } from "./BigNumbers-Dky57CpC.cjs";
|
|
23
23
|
import { t as clampBigNumber } from "./clamp-D9lICt7X.cjs";
|
|
24
|
-
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "./decimalAdjustment-
|
|
24
|
+
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "./decimalAdjustment-DCwilVOA.cjs";
|
|
25
25
|
import { t as sumBigNumberBy } from "./sumBigNumberBy-BvDeov4-.cjs";
|
|
26
26
|
import { t as toBigInt } from "./toBigInt-BDiOjNl-.cjs";
|
|
27
27
|
import { t as toIntegerString } from "./toIntegerString-BUESqiY1.cjs";
|
package/dist/index.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ import { n as WithContract, r as WithContracts, t as NadoContracts } from "./nad
|
|
|
21
21
|
import { n as toBigNumber, t as BigNumberish } from "./bigNumber-BqgJEjFs.js";
|
|
22
22
|
import { t as BigNumbers } from "./BigNumbers-Dky57CpC.js";
|
|
23
23
|
import { t as clampBigNumber } from "./clamp-D9lICt7X.js";
|
|
24
|
-
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "./decimalAdjustment-
|
|
24
|
+
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "./decimalAdjustment-pLqKqD8x.js";
|
|
25
25
|
import { t as sumBigNumberBy } from "./sumBigNumberBy-BvDeov4-.js";
|
|
26
26
|
import { t as toBigInt } from "./toBigInt-CBRk3mgz.js";
|
|
27
27
|
import { t as toIntegerString } from "./toIntegerString-CyhOUxbE.js";
|
package/dist/utils/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { n as toBigNumber, t as BigNumberish } from "../bigNumber-BqgJEjFs.cjs";
|
|
2
2
|
import { t as BigNumbers } from "../BigNumbers-Dky57CpC.cjs";
|
|
3
3
|
import { t as clampBigNumber } from "../clamp-D9lICt7X.cjs";
|
|
4
|
-
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../decimalAdjustment-
|
|
4
|
+
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../decimalAdjustment-DCwilVOA.cjs";
|
|
5
5
|
import { t as sumBigNumberBy } from "../sumBigNumberBy-BvDeov4-.cjs";
|
|
6
6
|
import { t as toBigInt } from "../toBigInt-BDiOjNl-.cjs";
|
|
7
7
|
import { t as toIntegerString } from "../toIntegerString-BUESqiY1.cjs";
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { n as toBigNumber, t as BigNumberish } from "../bigNumber-BqgJEjFs.js";
|
|
2
2
|
import { t as BigNumbers } from "../BigNumbers-Dky57CpC.js";
|
|
3
3
|
import { t as clampBigNumber } from "../clamp-D9lICt7X.js";
|
|
4
|
-
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../decimalAdjustment-
|
|
4
|
+
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../decimalAdjustment-pLqKqD8x.js";
|
|
5
5
|
import { t as sumBigNumberBy } from "../sumBigNumberBy-BvDeov4-.js";
|
|
6
6
|
import { t as toBigInt } from "../toBigInt-CBRk3mgz.js";
|
|
7
7
|
import { t as toIntegerString } from "../toIntegerString-CyhOUxbE.js";
|
|
@@ -8,12 +8,13 @@ const NADO_PRODUCT_DECIMALS = 18;
|
|
|
8
8
|
/**
|
|
9
9
|
* Adds the specified # of decimals to the number. For example, value = 1, decimals = 2, returns 100.
|
|
10
10
|
*
|
|
11
|
-
* @param value can be
|
|
11
|
+
* @param value can be null or undefined; either is returned unchanged.
|
|
12
12
|
* @param decimals number of decimal places to add, defaults to 18, which is the standard within Nado
|
|
13
|
+
* @returns The adjusted number or BigNumber, preserving null and undefined inputs.
|
|
13
14
|
*/
|
|
14
15
|
function addDecimals(value, decimals = 18) {
|
|
15
16
|
const getResult = () => {
|
|
16
|
-
if (value == null) return;
|
|
17
|
+
if (value == null) return value;
|
|
17
18
|
const adjustedValue = require_utils_math_bigNumber.toBigNumber(value).multipliedBy(require_utils_math_bigNumber.toBigNumber(10).pow(decimals));
|
|
18
19
|
return typeof value === "number" ? adjustedValue.toNumber() : adjustedValue;
|
|
19
20
|
};
|
|
@@ -22,12 +23,13 @@ function addDecimals(value, decimals = 18) {
|
|
|
22
23
|
/**
|
|
23
24
|
* Removes the specified # of decimals from the number. For example, value = 100, decimals = 2, returns 1.
|
|
24
25
|
*
|
|
25
|
-
* @param value can be
|
|
26
|
+
* @param value can be null or undefined; either is returned unchanged.
|
|
26
27
|
* @param decimals number of decimal places to remove, defaults to 18, which is the standard within Nado
|
|
28
|
+
* @returns The adjusted number or BigNumber, preserving null and undefined inputs.
|
|
27
29
|
*/
|
|
28
30
|
function removeDecimals(value, decimals = 18) {
|
|
29
31
|
const getResult = () => {
|
|
30
|
-
if (value == null) return;
|
|
32
|
+
if (value == null) return value;
|
|
31
33
|
const adjustedValue = require_utils_math_bigNumber.toBigNumber(value).dividedBy(require_utils_math_bigNumber.toBigNumber(10).pow(decimals));
|
|
32
34
|
return typeof value === "number" ? adjustedValue.toNumber() : adjustedValue;
|
|
33
35
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decimalAdjustment.cjs","names":["toBigNumber"],"sources":["../../../src/utils/math/decimalAdjustment.ts"],"sourcesContent":["import BigNumber from 'bignumber.js';\nimport { BigNumberish, toBigNumber } from './bigNumber';\n\n/**\n * All Nado balances have 18 decimals. Ex. 1e18 = 1.0\n */\nexport const NADO_PRODUCT_DECIMALS = 18;\n\n/**\n * Determines the result type after adjusting decimals based on the input type `T`.\n *\n * - If `T` is `undefined`, the result
|
|
1
|
+
{"version":3,"file":"decimalAdjustment.cjs","names":["toBigNumber"],"sources":["../../../src/utils/math/decimalAdjustment.ts"],"sourcesContent":["import BigNumber from 'bignumber.js';\nimport { BigNumberish, toBigNumber } from './bigNumber';\n\n/**\n * All Nado balances have 18 decimals. Ex. 1e18 = 1.0\n */\nexport const NADO_PRODUCT_DECIMALS = 18;\n\n/**\n * Determines the result type after adjusting decimals based on the input type `T`.\n *\n * - If `T` is `null` or `undefined`, the result preserves that value.\n * - If `T` is a `number`, the result is a `number`.\n * - Otherwise, the result is a `BigNumber`.\n */\ntype AdjustDecimalsResult<T extends BigNumberish | null | undefined> =\n T extends null | undefined ? T : T extends number ? number : BigNumber;\n\n/**\n * Adds the specified # of decimals to the number. For example, value = 1, decimals = 2, returns 100.\n *\n * @param value can be null or undefined; either is returned unchanged.\n * @param decimals number of decimal places to add, defaults to 18, which is the standard within Nado\n * @returns The adjusted number or BigNumber, preserving null and undefined inputs.\n */\nexport function addDecimals<T extends BigNumberish | null | undefined>(\n value: T,\n decimals: number = NADO_PRODUCT_DECIMALS,\n): AdjustDecimalsResult<T> {\n const getResult = () => {\n if (value == null) {\n return value;\n }\n\n const adjustedValue = toBigNumber(value).multipliedBy(\n toBigNumber(10).pow(decimals),\n );\n return typeof value === 'number' ? adjustedValue.toNumber() : adjustedValue;\n };\n\n return getResult() as AdjustDecimalsResult<T>;\n}\n\n/**\n * Removes the specified # of decimals from the number. For example, value = 100, decimals = 2, returns 1.\n *\n * @param value can be null or undefined; either is returned unchanged.\n * @param decimals number of decimal places to remove, defaults to 18, which is the standard within Nado\n * @returns The adjusted number or BigNumber, preserving null and undefined inputs.\n */\nexport function removeDecimals<T extends BigNumberish | null | undefined>(\n value: T,\n decimals: number = NADO_PRODUCT_DECIMALS,\n): AdjustDecimalsResult<T> {\n const getResult = () => {\n if (value == null) {\n return value;\n }\n\n const adjustedValue = toBigNumber(value).dividedBy(\n toBigNumber(10).pow(decimals),\n );\n return typeof value === 'number' ? adjustedValue.toNumber() : adjustedValue;\n };\n\n return getResult() as AdjustDecimalsResult<T>;\n}\n"],"mappings":";;;;;;AAMA,MAAa,wBAAwB;;;;;;;;AAmBrC,SAAgB,YACd,OACA,WAAA,IACyB;CACzB,MAAM,kBAAkB;EACtB,IAAI,SAAS,MACX,OAAO;EAGT,MAAM,gBAAgBA,6BAAAA,YAAY,KAAK,EAAE,aACvCA,6BAAAA,YAAY,EAAE,EAAE,IAAI,QAAQ,CAC9B;EACA,OAAO,OAAO,UAAU,WAAW,cAAc,SAAS,IAAI;CAChE;CAEA,OAAO,UAAU;AACnB;;;;;;;;AASA,SAAgB,eACd,OACA,WAAA,IACyB;CACzB,MAAM,kBAAkB;EACtB,IAAI,SAAS,MACX,OAAO;EAGT,MAAM,gBAAgBA,6BAAAA,YAAY,KAAK,EAAE,UACvCA,6BAAAA,YAAY,EAAE,EAAE,IAAI,QAAQ,CAC9B;EACA,OAAO,OAAO,UAAU,WAAW,cAAc,SAAS,IAAI;CAChE;CAEA,OAAO,UAAU;AACnB"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../../decimalAdjustment-
|
|
1
|
+
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../../decimalAdjustment-DCwilVOA.cjs";
|
|
2
2
|
export { NADO_PRODUCT_DECIMALS, addDecimals, removeDecimals };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../../decimalAdjustment-
|
|
1
|
+
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../../decimalAdjustment-pLqKqD8x.js";
|
|
2
2
|
export { NADO_PRODUCT_DECIMALS, addDecimals, removeDecimals };
|
|
@@ -7,12 +7,13 @@ const NADO_PRODUCT_DECIMALS = 18;
|
|
|
7
7
|
/**
|
|
8
8
|
* Adds the specified # of decimals to the number. For example, value = 1, decimals = 2, returns 100.
|
|
9
9
|
*
|
|
10
|
-
* @param value can be
|
|
10
|
+
* @param value can be null or undefined; either is returned unchanged.
|
|
11
11
|
* @param decimals number of decimal places to add, defaults to 18, which is the standard within Nado
|
|
12
|
+
* @returns The adjusted number or BigNumber, preserving null and undefined inputs.
|
|
12
13
|
*/
|
|
13
14
|
function addDecimals(value, decimals = 18) {
|
|
14
15
|
const getResult = () => {
|
|
15
|
-
if (value == null) return;
|
|
16
|
+
if (value == null) return value;
|
|
16
17
|
const adjustedValue = toBigNumber(value).multipliedBy(toBigNumber(10).pow(decimals));
|
|
17
18
|
return typeof value === "number" ? adjustedValue.toNumber() : adjustedValue;
|
|
18
19
|
};
|
|
@@ -21,12 +22,13 @@ function addDecimals(value, decimals = 18) {
|
|
|
21
22
|
/**
|
|
22
23
|
* Removes the specified # of decimals from the number. For example, value = 100, decimals = 2, returns 1.
|
|
23
24
|
*
|
|
24
|
-
* @param value can be
|
|
25
|
+
* @param value can be null or undefined; either is returned unchanged.
|
|
25
26
|
* @param decimals number of decimal places to remove, defaults to 18, which is the standard within Nado
|
|
27
|
+
* @returns The adjusted number or BigNumber, preserving null and undefined inputs.
|
|
26
28
|
*/
|
|
27
29
|
function removeDecimals(value, decimals = 18) {
|
|
28
30
|
const getResult = () => {
|
|
29
|
-
if (value == null) return;
|
|
31
|
+
if (value == null) return value;
|
|
30
32
|
const adjustedValue = toBigNumber(value).dividedBy(toBigNumber(10).pow(decimals));
|
|
31
33
|
return typeof value === "number" ? adjustedValue.toNumber() : adjustedValue;
|
|
32
34
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decimalAdjustment.js","names":[],"sources":["../../../src/utils/math/decimalAdjustment.ts"],"sourcesContent":["import BigNumber from 'bignumber.js';\nimport { BigNumberish, toBigNumber } from './bigNumber';\n\n/**\n * All Nado balances have 18 decimals. Ex. 1e18 = 1.0\n */\nexport const NADO_PRODUCT_DECIMALS = 18;\n\n/**\n * Determines the result type after adjusting decimals based on the input type `T`.\n *\n * - If `T` is `undefined`, the result
|
|
1
|
+
{"version":3,"file":"decimalAdjustment.js","names":[],"sources":["../../../src/utils/math/decimalAdjustment.ts"],"sourcesContent":["import BigNumber from 'bignumber.js';\nimport { BigNumberish, toBigNumber } from './bigNumber';\n\n/**\n * All Nado balances have 18 decimals. Ex. 1e18 = 1.0\n */\nexport const NADO_PRODUCT_DECIMALS = 18;\n\n/**\n * Determines the result type after adjusting decimals based on the input type `T`.\n *\n * - If `T` is `null` or `undefined`, the result preserves that value.\n * - If `T` is a `number`, the result is a `number`.\n * - Otherwise, the result is a `BigNumber`.\n */\ntype AdjustDecimalsResult<T extends BigNumberish | null | undefined> =\n T extends null | undefined ? T : T extends number ? number : BigNumber;\n\n/**\n * Adds the specified # of decimals to the number. For example, value = 1, decimals = 2, returns 100.\n *\n * @param value can be null or undefined; either is returned unchanged.\n * @param decimals number of decimal places to add, defaults to 18, which is the standard within Nado\n * @returns The adjusted number or BigNumber, preserving null and undefined inputs.\n */\nexport function addDecimals<T extends BigNumberish | null | undefined>(\n value: T,\n decimals: number = NADO_PRODUCT_DECIMALS,\n): AdjustDecimalsResult<T> {\n const getResult = () => {\n if (value == null) {\n return value;\n }\n\n const adjustedValue = toBigNumber(value).multipliedBy(\n toBigNumber(10).pow(decimals),\n );\n return typeof value === 'number' ? adjustedValue.toNumber() : adjustedValue;\n };\n\n return getResult() as AdjustDecimalsResult<T>;\n}\n\n/**\n * Removes the specified # of decimals from the number. For example, value = 100, decimals = 2, returns 1.\n *\n * @param value can be null or undefined; either is returned unchanged.\n * @param decimals number of decimal places to remove, defaults to 18, which is the standard within Nado\n * @returns The adjusted number or BigNumber, preserving null and undefined inputs.\n */\nexport function removeDecimals<T extends BigNumberish | null | undefined>(\n value: T,\n decimals: number = NADO_PRODUCT_DECIMALS,\n): AdjustDecimalsResult<T> {\n const getResult = () => {\n if (value == null) {\n return value;\n }\n\n const adjustedValue = toBigNumber(value).dividedBy(\n toBigNumber(10).pow(decimals),\n );\n return typeof value === 'number' ? adjustedValue.toNumber() : adjustedValue;\n };\n\n return getResult() as AdjustDecimalsResult<T>;\n}\n"],"mappings":";;;;;AAMA,MAAa,wBAAwB;;;;;;;;AAmBrC,SAAgB,YACd,OACA,WAAA,IACyB;CACzB,MAAM,kBAAkB;EACtB,IAAI,SAAS,MACX,OAAO;EAGT,MAAM,gBAAgB,YAAY,KAAK,EAAE,aACvC,YAAY,EAAE,EAAE,IAAI,QAAQ,CAC9B;EACA,OAAO,OAAO,UAAU,WAAW,cAAc,SAAS,IAAI;CAChE;CAEA,OAAO,UAAU;AACnB;;;;;;;;AASA,SAAgB,eACd,OACA,WAAA,IACyB;CACzB,MAAM,kBAAkB;EACtB,IAAI,SAAS,MACX,OAAO;EAGT,MAAM,gBAAgB,YAAY,KAAK,EAAE,UACvC,YAAY,EAAE,EAAE,IAAI,QAAQ,CAC9B;EACA,OAAO,OAAO,UAAU,WAAW,cAAc,SAAS,IAAI;CAChE;CAEA,OAAO,UAAU;AACnB"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { n as toBigNumber, t as BigNumberish } from "../../bigNumber-BqgJEjFs.cjs";
|
|
2
2
|
import { t as BigNumbers } from "../../BigNumbers-Dky57CpC.cjs";
|
|
3
3
|
import { t as clampBigNumber } from "../../clamp-D9lICt7X.cjs";
|
|
4
|
-
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../../decimalAdjustment-
|
|
4
|
+
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../../decimalAdjustment-DCwilVOA.cjs";
|
|
5
5
|
import { t as sumBigNumberBy } from "../../sumBigNumberBy-BvDeov4-.cjs";
|
|
6
6
|
import { t as toBigInt } from "../../toBigInt-BDiOjNl-.cjs";
|
|
7
7
|
import { t as toIntegerString } from "../../toIntegerString-BUESqiY1.cjs";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { n as toBigNumber, t as BigNumberish } from "../../bigNumber-BqgJEjFs.js";
|
|
2
2
|
import { t as BigNumbers } from "../../BigNumbers-Dky57CpC.js";
|
|
3
3
|
import { t as clampBigNumber } from "../../clamp-D9lICt7X.js";
|
|
4
|
-
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../../decimalAdjustment-
|
|
4
|
+
import { n as addDecimals, r as removeDecimals, t as NADO_PRODUCT_DECIMALS } from "../../decimalAdjustment-pLqKqD8x.js";
|
|
5
5
|
import { t as sumBigNumberBy } from "../../sumBigNumberBy-BvDeov4-.js";
|
|
6
6
|
import { t as toBigInt } from "../../toBigInt-CBRk3mgz.js";
|
|
7
7
|
import { t as toIntegerString } from "../../toIntegerString-CyhOUxbE.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nadohq/shared",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.54.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "Shared utilities, types, and contract helpers for Nado SDK",
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
"bignumber.js": "11.1.1",
|
|
53
53
|
"viem": "2.52.0"
|
|
54
54
|
},
|
|
55
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "83485ee66ad20b239387e271a2945386b39e824e"
|
|
56
56
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { describe, expect, it } from '@jest/globals';
|
|
2
|
+
import BigNumber from 'bignumber.js';
|
|
3
|
+
import { addDecimals, removeDecimals } from './decimalAdjustment';
|
|
4
|
+
|
|
5
|
+
describe('removeDecimals', () => {
|
|
6
|
+
it('preserves null and its return type', () => {
|
|
7
|
+
const result: null = removeDecimals(null);
|
|
8
|
+
expect(result).toBeNull();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('preserves undefined and its return type', () => {
|
|
12
|
+
const result: undefined = removeDecimals(undefined);
|
|
13
|
+
expect(result).toBeUndefined();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it.each([0, 12345, -12345])(
|
|
17
|
+
'keeps numeric input %s as a number with custom decimals',
|
|
18
|
+
(value) => {
|
|
19
|
+
const result: number = removeDecimals(value, 2);
|
|
20
|
+
expect(result).toBe(value / 100);
|
|
21
|
+
},
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
it.each([
|
|
25
|
+
'12345678901234567890123',
|
|
26
|
+
12345678901234567890123n,
|
|
27
|
+
new BigNumber('12345678901234567890123'),
|
|
28
|
+
])('preserves x18 precision for %s', (value) => {
|
|
29
|
+
const result: BigNumber = removeDecimals(value);
|
|
30
|
+
expect(result.toFixed()).toBe('12345.678901234567890123');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('preserves the distinction between missing, null, and zero prices', () => {
|
|
34
|
+
const prices: Array<string | null | undefined> = [undefined, null, '0'];
|
|
35
|
+
const results: Array<BigNumber | null | undefined> = prices.map((price) =>
|
|
36
|
+
removeDecimals(price),
|
|
37
|
+
);
|
|
38
|
+
expect(results[0]).toBeUndefined();
|
|
39
|
+
expect(results[1]).toBeNull();
|
|
40
|
+
expect(results[2]?.isZero()).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('addDecimals', () => {
|
|
45
|
+
it('preserves null and its return type', () => {
|
|
46
|
+
const result: null = addDecimals(null);
|
|
47
|
+
expect(result).toBeNull();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('preserves undefined and its return type', () => {
|
|
51
|
+
const result: undefined = addDecimals(undefined);
|
|
52
|
+
expect(result).toBeUndefined();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it.each([0, 1.25, -1.25])(
|
|
56
|
+
'keeps numeric input %s as a number with custom decimals',
|
|
57
|
+
(value) => {
|
|
58
|
+
const result: number = addDecimals(value, 2);
|
|
59
|
+
expect(result).toBe(value * 100);
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
it.each([
|
|
64
|
+
'12345.678901234567890123',
|
|
65
|
+
new BigNumber('12345.678901234567890123'),
|
|
66
|
+
])('preserves precision when converting %s to x18 units', (value) => {
|
|
67
|
+
const result: BigNumber = addDecimals(value);
|
|
68
|
+
expect(result.toFixed()).toBe('12345678901234567890123');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('scales bigint values without losing precision', () => {
|
|
72
|
+
const result: BigNumber = addDecimals(12345678901234567890123n, 2);
|
|
73
|
+
expect(result.toFixed()).toBe('1234567890123456789012300');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('preserves the distinction between missing, null, and zero values', () => {
|
|
77
|
+
const values: Array<string | null | undefined> = [undefined, null, '0'];
|
|
78
|
+
const results: Array<BigNumber | null | undefined> = values.map((value) =>
|
|
79
|
+
addDecimals(value),
|
|
80
|
+
);
|
|
81
|
+
expect(results[0]).toBeUndefined();
|
|
82
|
+
expect(results[1]).toBeNull();
|
|
83
|
+
expect(results[2]?.isZero()).toBe(true);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -9,27 +9,27 @@ export const NADO_PRODUCT_DECIMALS = 18;
|
|
|
9
9
|
/**
|
|
10
10
|
* Determines the result type after adjusting decimals based on the input type `T`.
|
|
11
11
|
*
|
|
12
|
-
* - If `T` is `undefined`, the result
|
|
12
|
+
* - If `T` is `null` or `undefined`, the result preserves that value.
|
|
13
13
|
* - If `T` is a `number`, the result is a `number`.
|
|
14
14
|
* - Otherwise, the result is a `BigNumber`.
|
|
15
15
|
*/
|
|
16
|
-
type AdjustDecimalsResult<T extends BigNumberish | undefined> =
|
|
17
|
-
T extends undefined ?
|
|
16
|
+
type AdjustDecimalsResult<T extends BigNumberish | null | undefined> =
|
|
17
|
+
T extends null | undefined ? T : T extends number ? number : BigNumber;
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Adds the specified # of decimals to the number. For example, value = 1, decimals = 2, returns 100.
|
|
21
21
|
*
|
|
22
|
-
* @param value can be
|
|
22
|
+
* @param value can be null or undefined; either is returned unchanged.
|
|
23
23
|
* @param decimals number of decimal places to add, defaults to 18, which is the standard within Nado
|
|
24
|
+
* @returns The adjusted number or BigNumber, preserving null and undefined inputs.
|
|
24
25
|
*/
|
|
25
|
-
|
|
26
|
-
export function addDecimals<T extends BigNumberish | undefined>(
|
|
26
|
+
export function addDecimals<T extends BigNumberish | null | undefined>(
|
|
27
27
|
value: T,
|
|
28
28
|
decimals: number = NADO_PRODUCT_DECIMALS,
|
|
29
29
|
): AdjustDecimalsResult<T> {
|
|
30
30
|
const getResult = () => {
|
|
31
31
|
if (value == null) {
|
|
32
|
-
return
|
|
32
|
+
return value;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
const adjustedValue = toBigNumber(value).multipliedBy(
|
|
@@ -44,16 +44,17 @@ export function addDecimals<T extends BigNumberish | undefined>(
|
|
|
44
44
|
/**
|
|
45
45
|
* Removes the specified # of decimals from the number. For example, value = 100, decimals = 2, returns 1.
|
|
46
46
|
*
|
|
47
|
-
* @param value can be
|
|
47
|
+
* @param value can be null or undefined; either is returned unchanged.
|
|
48
48
|
* @param decimals number of decimal places to remove, defaults to 18, which is the standard within Nado
|
|
49
|
+
* @returns The adjusted number or BigNumber, preserving null and undefined inputs.
|
|
49
50
|
*/
|
|
50
|
-
export function removeDecimals<T extends BigNumberish | undefined>(
|
|
51
|
+
export function removeDecimals<T extends BigNumberish | null | undefined>(
|
|
51
52
|
value: T,
|
|
52
53
|
decimals: number = NADO_PRODUCT_DECIMALS,
|
|
53
54
|
): AdjustDecimalsResult<T> {
|
|
54
55
|
const getResult = () => {
|
|
55
56
|
if (value == null) {
|
|
56
|
-
return
|
|
57
|
+
return value;
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
const adjustedValue = toBigNumber(value).dividedBy(
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"decimalAdjustment-DD7Q2eMZ.d.cts","names":[],"sources":["../src/utils/math/src/utils/math/decimalAdjustment.ts"],"mappings":";;;;;;AAMA;cAAa,qBAAA;;;AAAqB;AAAM;;;;KASnC,oBAAA,WAA+B,YAAA,gBAClC,CAAA,iCAAkC,CAAA,2BAA4B,SAAA;;;;;;;iBAShD,WAAA,WAAsB,YAAA,aAAA,CACpC,KAAA,EAAO,CAAA,EACP,QAAA,YACC,oBAAA,CAAqB,CAAA;;;;AAZiD;AASzE;;iBAwBgB,cAAA,WAAyB,YAAA,aAAA,CACvC,KAAA,EAAO,CAAA,EACP,QAAA,YACC,oBAAA,CAAqB,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"decimalAdjustment-DU0dlRcS.d.ts","names":[],"sources":["../src/utils/math/src/utils/math/decimalAdjustment.ts"],"mappings":";;;;;;AAMA;cAAa,qBAAA;;;AAAqB;AAAM;;;;KASnC,oBAAA,WAA+B,YAAA,gBAClC,CAAA,iCAAkC,CAAA,2BAA4B,SAAA;;;;;;;iBAShD,WAAA,WAAsB,YAAA,aAAA,CACpC,KAAA,EAAO,CAAA,EACP,QAAA,YACC,oBAAA,CAAqB,CAAA;;;;AAZiD;AASzE;;iBAwBgB,cAAA,WAAyB,YAAA,aAAA,CACvC,KAAA,EAAO,CAAA,EACP,QAAA,YACC,oBAAA,CAAqB,CAAA"}
|