@morpho-org/blue-sdk 1.0.3 → 1.0.4

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.
Files changed (158) hide show
  1. package/lib/addresses.d.ts +156 -0
  2. package/lib/addresses.js +160 -0
  3. package/lib/chain/chain.constants.d.ts +3 -0
  4. package/lib/chain/chain.constants.js +232 -0
  5. package/lib/chain/chain.test.d.ts +1 -0
  6. package/lib/chain/chain.test.js +37 -0
  7. package/lib/chain/chain.types.d.ts +20 -0
  8. package/lib/chain/chain.types.js +30 -0
  9. package/lib/chain/chain.utils.d.ts +14 -0
  10. package/lib/chain/chain.utils.js +30 -0
  11. package/lib/chain/index.js +18 -0
  12. package/lib/constants.d.ts +8 -0
  13. package/lib/constants.js +13 -0
  14. package/lib/errors.d.ts +37 -0
  15. package/lib/errors.js +71 -0
  16. package/lib/ethers/ethers.test.d.ts +1 -0
  17. package/lib/ethers/ethers.test.js +11 -0
  18. package/lib/ethers/index.js +18 -0
  19. package/lib/ethers/safeGetAddress.d.ts +1 -0
  20. package/lib/ethers/safeGetAddress.js +6 -0
  21. package/lib/ethers/safeParseUnits.d.ts +2 -0
  22. package/lib/ethers/safeParseUnits.js +25 -0
  23. package/lib/evm.d.ts +36 -0
  24. package/lib/evm.js +113 -0
  25. package/lib/helpers/format/format.d.ts +98 -0
  26. package/lib/helpers/format/format.js +301 -0
  27. package/lib/helpers/format/format.test.d.ts +1 -0
  28. package/lib/helpers/format/format.test.js +257 -0
  29. package/lib/helpers/format/index.js +17 -0
  30. package/lib/helpers/getChecksumedAddress.d.ts +7 -0
  31. package/lib/helpers/getChecksumedAddress.js +17 -0
  32. package/lib/helpers/index.js +20 -0
  33. package/{src/helpers/isZeroAddressOrUnset.ts → lib/helpers/isZeroAddressOrUnset.d.ts} +1 -7
  34. package/lib/helpers/isZeroAddressOrUnset.js +14 -0
  35. package/lib/helpers/locale.d.ts +36 -0
  36. package/lib/helpers/locale.js +86 -0
  37. package/lib/holding/Holding.d.ts +60 -0
  38. package/lib/holding/Holding.js +31 -0
  39. package/lib/holding/index.js +17 -0
  40. package/{src/index.ts → lib/index.d.ts} +0 -1
  41. package/lib/market/Market.d.ts +159 -0
  42. package/lib/market/Market.js +240 -0
  43. package/lib/market/MarketConfig.d.ts +44 -0
  44. package/lib/market/MarketConfig.js +56 -0
  45. package/lib/market/MarketUtils.d.ts +165 -0
  46. package/lib/market/MarketUtils.js +182 -0
  47. package/lib/market/MarketUtils.test.d.ts +1 -0
  48. package/lib/market/MarketUtils.test.js +19 -0
  49. package/lib/market/index.js +19 -0
  50. package/lib/maths/AdaptiveCurveIrmLib.d.ts +37 -0
  51. package/lib/maths/AdaptiveCurveIrmLib.js +116 -0
  52. package/lib/maths/MathLib.d.ts +94 -0
  53. package/lib/maths/MathLib.js +153 -0
  54. package/lib/maths/MathUtils.d.ts +15 -0
  55. package/lib/maths/MathUtils.js +33 -0
  56. package/lib/maths/SharesMath.d.ts +12 -0
  57. package/lib/maths/SharesMath.js +22 -0
  58. package/lib/maths/index.js +20 -0
  59. package/lib/notifications.d.ts +98 -0
  60. package/lib/notifications.js +52 -0
  61. package/lib/position/Position.d.ts +118 -0
  62. package/lib/position/Position.js +145 -0
  63. package/lib/position/index.js +17 -0
  64. package/lib/signatures/index.d.ts +12 -0
  65. package/lib/signatures/index.js +39 -0
  66. package/lib/signatures/manager.d.ts +10 -0
  67. package/lib/signatures/manager.js +37 -0
  68. package/lib/signatures/permit.d.ts +21 -0
  69. package/lib/signatures/permit.js +101 -0
  70. package/lib/signatures/permit2.d.ts +20 -0
  71. package/lib/signatures/permit2.js +91 -0
  72. package/lib/signatures/types.d.ts +13 -0
  73. package/lib/signatures/types.js +2 -0
  74. package/lib/signatures/utils.d.ts +6 -0
  75. package/lib/signatures/utils.js +44 -0
  76. package/lib/tests/mocks/markets.d.ts +17 -0
  77. package/lib/tests/mocks/markets.js +108 -0
  78. package/lib/token/ERC20Metadata.d.ts +249 -0
  79. package/lib/token/ERC20Metadata.js +81 -0
  80. package/lib/token/Token.d.ts +45 -0
  81. package/lib/token/Token.js +39 -0
  82. package/lib/token/TokenNamespace.d.ts +18 -0
  83. package/lib/token/TokenNamespace.js +55 -0
  84. package/lib/token/WrappedToken.d.ts +42 -0
  85. package/lib/token/WrappedToken.js +87 -0
  86. package/lib/token/index.js +18 -0
  87. package/lib/types.d.ts +29 -0
  88. package/lib/types.js +23 -0
  89. package/lib/user/User.d.ts +20 -0
  90. package/lib/user/User.js +11 -0
  91. package/lib/user/index.js +18 -0
  92. package/lib/user/user.types.d.ts +18 -0
  93. package/lib/user/user.types.js +2 -0
  94. package/lib/vault/Vault.d.ts +167 -0
  95. package/lib/vault/Vault.js +156 -0
  96. package/lib/vault/VaultAllocation.d.ts +38 -0
  97. package/lib/vault/VaultAllocation.js +18 -0
  98. package/lib/vault/VaultConfig.d.ts +23 -0
  99. package/lib/vault/VaultConfig.js +26 -0
  100. package/lib/vault/VaultUtils.d.ts +17 -0
  101. package/lib/vault/VaultUtils.js +17 -0
  102. package/lib/vault/index.js +20 -0
  103. package/package.json +6 -9
  104. package/src/addresses.ts +0 -247
  105. package/src/chain/chain.constants.ts +0 -235
  106. package/src/chain/chain.test.ts +0 -51
  107. package/src/chain/chain.types.ts +0 -42
  108. package/src/chain/chain.utils.ts +0 -44
  109. package/src/constants.ts +0 -18
  110. package/src/errors.ts +0 -75
  111. package/src/ethers/ethers.test.ts +0 -17
  112. package/src/ethers/safeGetAddress.ts +0 -4
  113. package/src/ethers/safeParseUnits.ts +0 -29
  114. package/src/evm.ts +0 -172
  115. package/src/helpers/format/format.test.ts +0 -340
  116. package/src/helpers/format/format.ts +0 -416
  117. package/src/helpers/getChecksumedAddress.ts +0 -15
  118. package/src/helpers/locale.ts +0 -108
  119. package/src/holding/Holding.ts +0 -109
  120. package/src/market/Market.ts +0 -479
  121. package/src/market/MarketConfig.ts +0 -108
  122. package/src/market/MarketUtils.test.ts +0 -25
  123. package/src/market/MarketUtils.ts +0 -467
  124. package/src/maths/AdaptiveCurveIrmLib.ts +0 -143
  125. package/src/maths/MathLib.ts +0 -208
  126. package/src/maths/MathUtils.ts +0 -31
  127. package/src/maths/SharesMath.ts +0 -40
  128. package/src/notifications.ts +0 -167
  129. package/src/position/Position.ts +0 -251
  130. package/src/signatures/index.ts +0 -18
  131. package/src/signatures/manager.ts +0 -50
  132. package/src/signatures/permit.ts +0 -126
  133. package/src/signatures/permit2.ts +0 -120
  134. package/src/signatures/types.ts +0 -18
  135. package/src/signatures/utils.ts +0 -83
  136. package/src/tests/mocks/markets.ts +0 -110
  137. package/src/token/ERC20Metadata.ts +0 -124
  138. package/src/token/Token.ts +0 -83
  139. package/src/token/TokenNamespace.ts +0 -76
  140. package/src/token/WrappedToken.ts +0 -142
  141. package/src/types.ts +0 -37
  142. package/src/user/User.ts +0 -32
  143. package/src/user/user.types.ts +0 -25
  144. package/src/vault/Vault.ts +0 -370
  145. package/src/vault/VaultAllocation.ts +0 -58
  146. package/src/vault/VaultConfig.ts +0 -55
  147. package/src/vault/VaultUtils.ts +0 -47
  148. /package/{src/chain/index.ts → lib/chain/index.d.ts} +0 -0
  149. /package/{src/ethers/index.ts → lib/ethers/index.d.ts} +0 -0
  150. /package/{src/helpers/format/index.ts → lib/helpers/format/index.d.ts} +0 -0
  151. /package/{src/helpers/index.ts → lib/helpers/index.d.ts} +0 -0
  152. /package/{src/holding/index.ts → lib/holding/index.d.ts} +0 -0
  153. /package/{src/market/index.ts → lib/market/index.d.ts} +0 -0
  154. /package/{src/maths/index.ts → lib/maths/index.d.ts} +0 -0
  155. /package/{src/position/index.ts → lib/position/index.d.ts} +0 -0
  156. /package/{src/token/index.ts → lib/token/index.d.ts} +0 -0
  157. /package/{src/user/index.ts → lib/user/index.d.ts} +0 -0
  158. /package/{src/vault/index.ts → lib/vault/index.d.ts} +0 -0
@@ -1,44 +0,0 @@
1
- import { values } from "@morpho-org/morpho-ts";
2
-
3
- import { UnsupportedChainIdError } from "../errors";
4
-
5
- import { BLUE_AVAILABLE_CHAINS, CHAIN_METADATA } from "./chain.constants";
6
- import {
7
- ChainId as _ChainId,
8
- ChainMetadata as _ChainMetadata,
9
- } from "./chain.types";
10
-
11
- export namespace ChainUtils {
12
- export const blueAvailableChains = BLUE_AVAILABLE_CHAINS;
13
- export const chainMetadata = CHAIN_METADATA;
14
-
15
- export interface ChainMetadata extends _ChainMetadata {}
16
- export import ChainId = _ChainId;
17
-
18
- export const toHexChainId = (chainId: ChainId) => {
19
- return `0x${chainId.toString(16)}`;
20
- };
21
-
22
- export const getAllChainIds = () =>
23
- values(ChainId).filter((value) => typeof value === "number");
24
-
25
- export const getAllChainLabels = () =>
26
- Object.values(ChainId).filter(
27
- (value) => typeof value === "string"
28
- ) as (keyof typeof ChainId)[];
29
-
30
- export const getTestnets = () =>
31
- getAllChainIds().filter((chainId) => CHAIN_METADATA[chainId].isTestnet);
32
-
33
- export function isSupported(chainId: number): chainId is ChainId {
34
- return blueAvailableChains.includes(chainId as ChainId);
35
- }
36
-
37
- export function parseSupportedChainId(candidate: any): ChainId {
38
- const chainId = parseInt(candidate);
39
-
40
- if (!isSupported(chainId)) throw new UnsupportedChainIdError(candidate);
41
-
42
- return chainId;
43
- }
44
- }
package/src/constants.ts DELETED
@@ -1,18 +0,0 @@
1
- import { parseUnits, parseEther } from "ethers";
2
-
3
- import { Time } from "@morpho-org/morpho-ts";
4
-
5
- export const LIQUIDATION_CURSOR = parseEther("0.3");
6
-
7
- export const MAX_LIQUIDATION_INCENTIVE_FACTOR = parseEther("1.15");
8
-
9
- export const ORACLE_PRICE_OFFSET = 36;
10
- export const ORACLE_PRICE_SCALE = parseUnits("1", ORACLE_PRICE_OFFSET);
11
-
12
- export const MAX_FEE = parseEther("0.25"); // 25%
13
-
14
- export const DEFAULT_SLIPPAGE_TOLERANCE = parseEther("0.0003"); // 0.03%
15
-
16
- export const DEFAULT_TARGET_UTILIZATION = parseEther("0.92"); // 92%
17
-
18
- export const SECONDS_PER_YEAR = Time.s.from.y(1n);
package/src/errors.ts DELETED
@@ -1,75 +0,0 @@
1
- import { Address, MarketId } from "./types";
2
-
3
- export class UnknownDataError extends Error {}
4
-
5
- export class UnknownTokenError extends UnknownDataError {
6
- constructor(public readonly address: Address) {
7
- super(`unknown token ${address}`);
8
- }
9
- }
10
-
11
- export class UnknownTokenPriceError extends UnknownDataError {
12
- constructor(public readonly address: Address) {
13
- super(`unknown price of token ${address}`);
14
- }
15
- }
16
-
17
- export class UnknownMarketConfigError extends UnknownDataError {
18
- constructor(public readonly marketId: MarketId) {
19
- super(`unknown config for market ${marketId}`);
20
- }
21
- }
22
-
23
- export class UnknownVaultConfigError extends UnknownDataError {
24
- constructor(public readonly vault: Address) {
25
- super(`unknown config for vault ${vault}`);
26
- }
27
- }
28
-
29
- export class UnsupportedChainIdError extends Error {
30
- constructor(public readonly chainId: number) {
31
- super(`unsupported chain ${chainId}`);
32
- }
33
- }
34
-
35
- export class InvalidInterestAccrualError extends UnknownDataError {
36
- constructor(
37
- public readonly marketId: MarketId,
38
- public readonly timestamp: bigint,
39
- public readonly lastUpdate: bigint
40
- ) {
41
- super(
42
- `invalid interest accrual on market ${marketId}: accrual timestamp ${timestamp} can't be prior to last update ${lastUpdate}`
43
- );
44
- }
45
- }
46
-
47
- export class InvalidSignatureError extends Error {
48
- constructor(
49
- public readonly hash: string,
50
- public readonly signer: Address,
51
- public readonly recovered: Address
52
- ) {
53
- super(
54
- `invalid signature for hash ${hash}: expected ${signer}, recovered ${recovered}`
55
- );
56
- }
57
- }
58
-
59
- export type ErrorClass = new (...args: any[]) => Error;
60
- export function _try<T>(
61
- accessor: () => T,
62
- ...errorClasses: ErrorClass[]
63
- ): T | undefined {
64
- try {
65
- return accessor();
66
- } catch (error) {
67
- if (
68
- errorClasses.length === 0 ||
69
- errorClasses.some((errorClass) => error instanceof errorClass)
70
- )
71
- return;
72
-
73
- throw error;
74
- }
75
- }
@@ -1,17 +0,0 @@
1
- import { safeParseNumber } from "./safeParseUnits";
2
-
3
- describe("safeParseNumber", () => {
4
- it("should parse excessively small number", () => {
5
- expect(safeParseNumber(0.000000000000000000000000000000042, 18)).toEqual(
6
- 0n
7
- );
8
- });
9
-
10
- it("should parse excessively large number", () => {
11
- expect(
12
- safeParseNumber(4200000000000000000000000000000000000, 18).toString()
13
- ).toEqual(
14
- 4200000000000000000000000000000000000000000000000000000n.toString()
15
- );
16
- });
17
- });
@@ -1,4 +0,0 @@
1
- import { getAddress } from "ethers";
2
-
3
- export const safeGetAddress = (address: string) =>
4
- getAddress(address.toLowerCase());
@@ -1,29 +0,0 @@
1
- import { parseUnits } from "ethers";
2
-
3
- export const safeParseUnits = (strValue: string, decimals = 18) => {
4
- if (!/[-+]?[0-9]*\.?[0-9]+/.test(strValue))
5
- throw new Error("invalid number: " + strValue);
6
-
7
- let [whole, dec = ""] = strValue.split(".");
8
-
9
- dec = dec.slice(0, decimals);
10
-
11
- return parseUnits(
12
- [whole || "0", dec].filter((v) => v.length > 0).join("."),
13
- decimals
14
- );
15
- };
16
-
17
- // Alternative to Number.toFixed that doesn't use scientific notation for excessively small or large numbers
18
- function toFixed(x: number, decimals: number) {
19
- return new Intl.NumberFormat("en-US", {
20
- style: "decimal",
21
- useGrouping: false,
22
- maximumFractionDigits: decimals,
23
- minimumFractionDigits: decimals,
24
- }).format(x);
25
- }
26
-
27
- export function safeParseNumber(value: number, decimals = 18) {
28
- return safeParseUnits(toFixed(value, decimals), decimals);
29
- }
package/src/evm.ts DELETED
@@ -1,172 +0,0 @@
1
- import {
2
- TransactionRequest,
3
- Signer,
4
- parseUnits,
5
- TransactionResponse,
6
- } from "ethers";
7
-
8
- import { ChainId } from "./chain";
9
- import { MathLib } from "./maths";
10
- import {
11
- NotificationProducer,
12
- NotificationStatus,
13
- NotificationTopic,
14
- } from "./notifications";
15
- import { SignatureUtils } from "./signatures";
16
- import { SignatureMessage } from "./signatures/types";
17
-
18
- export interface NotificationOptions<Topic extends NotificationTopic> {
19
- producer: NotificationProducer<Topic>;
20
- id: string;
21
- args: Record<PropertyKey, any>;
22
- }
23
-
24
- /**
25
- * Sends a transaction via the provided signer, optionnally waiting for the corresponding transaction receipt.
26
- * @param signer The signer to send the tx with.
27
- * @param tx The transaction request.
28
- * @param wait Whether to wait for the transaction receipt. Defaults to true.
29
- * @param notificationOptions The optional notification options. Warning: if `wait` is set to true, the provided topic will never complete.
30
- */
31
- export const sendTransactionWithProducer = async (
32
- signer: Signer,
33
- req: TransactionRequest,
34
- defaultGasLimit?: bigint,
35
- confirms = 1,
36
- notificationOptions?: NotificationOptions<NotificationTopic.tx>
37
- ) => {
38
- let response: TransactionResponse | undefined;
39
-
40
- try {
41
- let gasLimit: bigint;
42
- try {
43
- gasLimit = MathLib.wMulUp(
44
- await signer.estimateGas(req),
45
- parseUnits("1.1")
46
- );
47
- } catch (error: any) {
48
- if (defaultGasLimit == null) throw error;
49
-
50
- gasLimit = defaultGasLimit;
51
- }
52
-
53
- const tx = { ...req, gasLimit };
54
-
55
- notificationOptions?.producer.next({
56
- id: notificationOptions.id,
57
- status: NotificationStatus.signing,
58
- context: { tx },
59
- });
60
-
61
- response = await signer.sendTransaction(tx);
62
-
63
- notificationOptions?.producer.next({
64
- id: notificationOptions.id,
65
- status: NotificationStatus.pending,
66
- context: { args: notificationOptions.args, tx, response },
67
- });
68
-
69
- const receipt = await response.wait(confirms);
70
-
71
- notificationOptions?.producer.next({
72
- id: notificationOptions.id,
73
- status: NotificationStatus.success,
74
- context: { args: notificationOptions.args, tx, response, receipt },
75
- });
76
- } catch (error: any) {
77
- notificationOptions?.producer.next({
78
- id: notificationOptions.id,
79
- status: NotificationStatus.error,
80
- context: { args: notificationOptions.args, tx: req, response, error },
81
- });
82
- }
83
- };
84
-
85
- /**
86
- * Sends a transaction via the provided signer, optionnally waiting for the corresponding transaction receipt.
87
- * @param signer The signer to send the tx with.
88
- * @param tx The transaction request.
89
- * @param wait Whether to wait for the transaction receipt. Defaults to true.
90
- * @param topic The type of notifications to emit.
91
- * @param args The optional notification arguments.
92
- * @return The notification consumer which receives the transaction notifications.
93
- */
94
- export const sendTransaction = (
95
- signer: Signer,
96
- tx: TransactionRequest,
97
- defaultGasLimit?: bigint,
98
- confirms?: number,
99
- args: Record<PropertyKey, any> = {}
100
- ) => {
101
- const producer = new NotificationProducer(NotificationTopic.tx);
102
-
103
- sendTransactionWithProducer(signer, tx, defaultGasLimit, confirms, {
104
- producer,
105
- id: Date.now().toString(),
106
- args,
107
- });
108
-
109
- return producer.consumer;
110
- };
111
-
112
- export const signMessageWithProducer = async (
113
- signer: Signer,
114
- message: SignatureMessage,
115
- notificationOptions?: NotificationOptions<NotificationTopic.signature>
116
- ) => {
117
- notificationOptions?.producer.next({
118
- id: notificationOptions.id,
119
- status: NotificationStatus.signing,
120
- });
121
-
122
- try {
123
- const signature = await SignatureUtils.safeSignTypedData(
124
- signer,
125
- message.data.domain,
126
- message.data.types,
127
- message.data.value
128
- );
129
-
130
- SignatureUtils.verifySignature(
131
- signature,
132
- message.hash,
133
- await signer.getAddress()
134
- );
135
-
136
- notificationOptions?.producer.next({
137
- id: notificationOptions.id,
138
- status: NotificationStatus.success,
139
- context: { message, args: notificationOptions.args, signature },
140
- });
141
- } catch (error: any) {
142
- notificationOptions?.producer.next({
143
- id: notificationOptions.id,
144
- status: NotificationStatus.error,
145
- context: { message, args: notificationOptions.args, error },
146
- });
147
- }
148
- };
149
-
150
- /**
151
- * Sends a signature request to the provided signer.
152
- * @param signer The signer to send the approval with.
153
- * @param args The approval parameters.
154
- * @param encodeMessage The approval encoder to pass the approval parameters to get the approval transactions.
155
- * @return The notification consumer which receives the transaction notifications.
156
- */
157
- export const signMessage = <T extends object>(
158
- signer: Signer,
159
- args: T,
160
- encodeMessage: (args: T, chainId: ChainId) => SignatureMessage,
161
- chainId: ChainId
162
- ) => {
163
- const producer = new NotificationProducer(NotificationTopic.signature);
164
-
165
- signMessageWithProducer(signer, encodeMessage(args, chainId), {
166
- producer,
167
- id: Date.now().toString(),
168
- args,
169
- });
170
-
171
- return producer.consumer;
172
- };
@@ -1,340 +0,0 @@
1
- import { format } from "./format";
2
-
3
- describe("format", () => {
4
- const number = 12345.6789;
5
- const bigint = 123456789n;
6
- const decimals = 4;
7
-
8
- describe("hex", () => {
9
- describe("should properly format number in hex format", () => {
10
- it("without option", () => {
11
- expect(format.hex.of(number)).toEqual((123456789).toString(16));
12
- });
13
- });
14
- describe("should properly format bigint in hex format", () => {
15
- it("without option", () => {
16
- expect(format.hex.of(bigint, decimals)).toEqual(
17
- (123456789).toString(16)
18
- );
19
- });
20
- });
21
- });
22
-
23
- describe("number", () => {
24
- describe("should properly format number in number format", () => {
25
- it("without option", () => {
26
- expect(format.number.of(number)).toEqual("12345.6789");
27
- });
28
- it("with digits", () => {
29
- expect(format.number.digits(2).of(number)).toEqual("12345.67");
30
- });
31
- it("with min", () => {
32
- expect(format.number.min(20000).of(number)).toEqual("< 20000.0000");
33
- });
34
- it("with max", () => {
35
- expect(format.number.max(10000).of(number)).toEqual("> 10000.0000");
36
- });
37
- it("with sign", () => {
38
- expect(format.number.sign().of(number)).toEqual("+12345.6789");
39
- });
40
- it("with unit", () => {
41
- expect(format.number.unit("$").of(number)).toEqual("$12345.6789");
42
- });
43
- it("without trailing zeros", () => {
44
- expect(format.number.digits(6).of(number)).toEqual("12345.678900");
45
- expect(format.number.digits(6).removeTrailingZero().of(number)).toEqual(
46
- "12345.6789"
47
- );
48
- });
49
- it("with locale", () => {
50
- expect(format.number.locale("fr-FR").of(number)).toEqual("12345,6789");
51
- });
52
- });
53
- describe("should properly format bigint in number format", () => {
54
- it("without option", () => {
55
- expect(format.number.of(bigint, decimals)).toEqual("12345.6789");
56
- });
57
- it("with digits", () => {
58
- expect(format.number.digits(2).of(bigint, decimals)).toEqual(
59
- "12345.67"
60
- );
61
- });
62
- it("with min", () => {
63
- expect(format.number.min(20000).of(bigint, decimals)).toEqual(
64
- "< 20000.0000"
65
- );
66
- });
67
- it("with max", () => {
68
- expect(format.number.max(10000).of(bigint, decimals)).toEqual(
69
- "> 10000.0000"
70
- );
71
- });
72
- it("with sign", () => {
73
- expect(format.number.sign().of(bigint, decimals)).toEqual(
74
- "+12345.6789"
75
- );
76
- });
77
- it("with unit", () => {
78
- expect(format.number.unit("$").of(bigint, decimals)).toEqual(
79
- "$12345.6789"
80
- );
81
- });
82
- it("without trailing zeros", () => {
83
- expect(format.number.digits(6).of(bigint, decimals)).toEqual(
84
- "12345.678900"
85
- );
86
- expect(
87
- format.number.digits(6).removeTrailingZero().of(bigint, decimals)
88
- ).toEqual("12345.6789");
89
- });
90
- it("with locale", () => {
91
- expect(format.number.locale("fr-FR").of(bigint, decimals)).toEqual(
92
- "12345,6789"
93
- );
94
- });
95
- });
96
- });
97
-
98
- describe("short", () => {
99
- describe("should properly format number in short format", () => {
100
- it("without option", () => {
101
- expect(format.short.of(number)).toEqual("12.3456789k");
102
- });
103
- it("with digits", () => {
104
- expect(format.short.digits(2).of(number)).toEqual("12.34k");
105
- });
106
- it("with min", () => {
107
- expect(format.short.min(20000).of(number)).toEqual("< 20.0000000k");
108
- });
109
- it("with max", () => {
110
- expect(format.short.max(10000).of(number)).toEqual("> 10.0000000k");
111
- });
112
- it("with sign", () => {
113
- expect(format.short.sign().of(number)).toEqual("+12.3456789k");
114
- });
115
- it("with unit", () => {
116
- expect(format.short.unit("€").of(number)).toEqual("12.3456789k €");
117
- });
118
- it("without trailing zeros", () => {
119
- expect(format.short.digits(8).of(number)).toEqual("12.34567890k");
120
- expect(format.short.digits(8).removeTrailingZero().of(number)).toEqual(
121
- "12.3456789k"
122
- );
123
- });
124
- it("with small numbers with commas", () => {
125
- expect(format.short.smallValuesWithCommas().of(number / 10)).toEqual(
126
- "1,234.56789"
127
- );
128
- });
129
- it("with locale", () => {
130
- expect(format.short.locale("fr-FR").of(number)).toEqual("12,3456789k");
131
- });
132
- });
133
- describe("should properly format bigint in short format", () => {
134
- it("without option", () => {
135
- expect(format.short.of(bigint, decimals)).toEqual("12.3456789k");
136
- });
137
- it("with digits", () => {
138
- expect(format.short.digits(2).of(bigint, decimals)).toEqual("12.34k");
139
- });
140
- it("with min", () => {
141
- expect(format.short.min(20000).of(bigint, decimals)).toEqual(
142
- "< 20.0000000k"
143
- );
144
- });
145
- it("with max", () => {
146
- expect(format.short.max(10000).of(bigint, decimals)).toEqual(
147
- "> 10.0000000k"
148
- );
149
- });
150
- it("with sign", () => {
151
- expect(format.short.sign().of(bigint, decimals)).toEqual(
152
- "+12.3456789k"
153
- );
154
- });
155
- it("with unit", () => {
156
- expect(format.short.unit("€").of(bigint, decimals)).toEqual(
157
- "12.3456789k €"
158
- );
159
- });
160
- it("without trailing zeros", () => {
161
- expect(format.short.digits(8).of(bigint, decimals)).toEqual(
162
- "12.34567890k"
163
- );
164
- expect(
165
- format.short.digits(8).removeTrailingZero().of(bigint, decimals)
166
- ).toEqual("12.3456789k");
167
- });
168
- it("with locale", () => {
169
- expect(format.short.locale("fr-FR").of(bigint, decimals)).toEqual(
170
- "12,3456789k"
171
- );
172
- });
173
- it("with small numbers with commas", () => {
174
- expect(
175
- format.short.smallValuesWithCommas().of(bigint, decimals + 1)
176
- ).toEqual("1,234.56789");
177
- });
178
- it("with small numbers with commas with locale", () => {
179
- expect(
180
- format.short
181
- .smallValuesWithCommas()
182
- .locale("fr-FR")
183
- .of(bigint, decimals + 1)
184
- // the correct space in fr-FR is narrow no-break space (U+202F)
185
- ).toEqual("1\u202F234,56789");
186
- });
187
- });
188
- });
189
-
190
- describe("commas", () => {
191
- describe("should properly format number in commas format", () => {
192
- it("without option", () => {
193
- expect(format.commas.of(number)).toEqual("12,345.6789");
194
- });
195
- it("with digits", () => {
196
- expect(format.commas.digits(2).of(number)).toEqual("12,345.67");
197
- });
198
- it("with min", () => {
199
- expect(format.commas.min(20000).of(number)).toEqual("< 20,000.0000");
200
- });
201
- it("with max", () => {
202
- expect(format.commas.max(10000).of(number)).toEqual("> 10,000.0000");
203
- });
204
- it("with sign", () => {
205
- expect(format.commas.sign().of(number)).toEqual("+12,345.6789");
206
- });
207
- it("with unit", () => {
208
- expect(format.commas.unit("ETH").of(number)).toEqual("12,345.6789 ETH");
209
- });
210
- it("without trailing zeros", () => {
211
- expect(format.commas.digits(6).of(number)).toEqual("12,345.678900");
212
- expect(format.commas.digits(6).removeTrailingZero().of(number)).toEqual(
213
- "12,345.6789"
214
- );
215
- });
216
- it("with locale", () => {
217
- expect(format.commas.locale("fr-FR").of(number)).toEqual(
218
- "12\u202F345,6789"
219
- );
220
- });
221
- });
222
- describe("should properly format bigint in commas format", () => {
223
- it("without option", () => {
224
- expect(format.commas.of(bigint, decimals)).toEqual("12,345.6789");
225
- });
226
- it("with digits", () => {
227
- expect(format.commas.digits(2).of(bigint, decimals)).toEqual(
228
- "12,345.67"
229
- );
230
- });
231
- it("with min", () => {
232
- expect(format.commas.min(20000).of(bigint, decimals)).toEqual(
233
- "< 20,000.0000"
234
- );
235
- });
236
- it("with max", () => {
237
- expect(format.commas.max(10000).of(bigint, decimals)).toEqual(
238
- "> 10,000.0000"
239
- );
240
- });
241
- it("with sign", () => {
242
- expect(format.commas.sign().of(bigint, decimals)).toEqual(
243
- "+12,345.6789"
244
- );
245
- });
246
- it("with unit", () => {
247
- expect(format.commas.unit("ETH").of(bigint, decimals)).toEqual(
248
- "12,345.6789 ETH"
249
- );
250
- });
251
- it("without trailing zeros", () => {
252
- expect(format.commas.digits(6).of(bigint, decimals)).toEqual(
253
- "12,345.678900"
254
- );
255
- expect(
256
- format.commas.digits(6).removeTrailingZero().of(bigint, decimals)
257
- ).toEqual("12,345.6789");
258
- });
259
- it("with locale", () => {
260
- // the correct space in fr-FR is narrow no-break space (U+202F)
261
- expect(format.commas.locale("fr-FR").of(bigint, decimals)).toEqual(
262
- "12\u202F345,6789"
263
- );
264
- });
265
- });
266
- });
267
-
268
- describe("percent", () => {
269
- describe("should properly format number in percent format", () => {
270
- it("without option", () => {
271
- expect(format.percent.of(number)).toEqual("1234567.8900");
272
- });
273
- it("with digits", () => {
274
- expect(format.percent.digits(1).of(number)).toEqual("1234567.8");
275
- });
276
- it("with min", () => {
277
- expect(format.percent.min(20000).of(number)).toEqual("< 2000000.0000");
278
- });
279
- it("with max", () => {
280
- expect(format.percent.max(10000).of(number)).toEqual("> 1000000.0000");
281
- });
282
- it("with sign", () => {
283
- expect(format.percent.sign().of(number)).toEqual("+1234567.8900");
284
- });
285
- it("with unit", () => {
286
- expect(format.percent.unit("%").of(number)).toEqual("1234567.8900%");
287
- });
288
- it("without trailing zeros", () => {
289
- expect(format.percent.removeTrailingZero().of(number)).toEqual(
290
- "1234567.89"
291
- );
292
- });
293
- it("with locale", () => {
294
- expect(format.percent.locale("fr-FR").of(number)).toEqual(
295
- "1234567,8900"
296
- );
297
- });
298
- });
299
- describe("should properly format bigint in percent format", () => {
300
- it("without option", () => {
301
- expect(format.percent.of(bigint, decimals)).toEqual("1234567.8900");
302
- });
303
- it("with digits", () => {
304
- expect(format.percent.digits(1).of(bigint, decimals)).toEqual(
305
- "1234567.8"
306
- );
307
- });
308
- it("with min", () => {
309
- expect(format.percent.min(20000).of(bigint, decimals)).toEqual(
310
- "< 2000000.0000"
311
- );
312
- });
313
- it("with max", () => {
314
- expect(format.percent.max(10000).of(bigint, decimals)).toEqual(
315
- "> 1000000.0000"
316
- );
317
- });
318
- it("with sign", () => {
319
- expect(format.percent.sign().of(bigint, decimals)).toEqual(
320
- "+1234567.8900"
321
- );
322
- });
323
- it("with unit", () => {
324
- expect(format.percent.unit("%").of(bigint, decimals)).toEqual(
325
- "1234567.8900%"
326
- );
327
- });
328
- it("without trailing zeros", () => {
329
- expect(
330
- format.percent.removeTrailingZero().of(bigint, decimals)
331
- ).toEqual("1234567.89");
332
- });
333
- it("with locale", () => {
334
- expect(format.percent.locale("fr-FR").of(bigint, decimals)).toEqual(
335
- "1234567,8900"
336
- );
337
- });
338
- });
339
- });
340
- });