@morpho-org/blue-sdk 1.0.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.
Files changed (59) hide show
  1. package/README.md +98 -0
  2. package/package.json +54 -0
  3. package/src/addresses.ts +261 -0
  4. package/src/chain/chain.constants.ts +235 -0
  5. package/src/chain/chain.test.ts +51 -0
  6. package/src/chain/chain.types.ts +42 -0
  7. package/src/chain/chain.utils.ts +44 -0
  8. package/src/chain/index.ts +2 -0
  9. package/src/constants.ts +18 -0
  10. package/src/errors.ts +75 -0
  11. package/src/ethers/ethers.test.ts +17 -0
  12. package/src/ethers/index.ts +2 -0
  13. package/src/ethers/safeGetAddress.ts +4 -0
  14. package/src/ethers/safeParseUnits.ts +29 -0
  15. package/src/evm.ts +172 -0
  16. package/src/helpers/format/format.test.ts +340 -0
  17. package/src/helpers/format/format.ts +416 -0
  18. package/src/helpers/format/index.ts +1 -0
  19. package/src/helpers/getChecksumedAddress.ts +15 -0
  20. package/src/helpers/index.ts +4 -0
  21. package/src/helpers/isZeroAddressOrUnset.ts +13 -0
  22. package/src/helpers/locale.ts +108 -0
  23. package/src/holding/Holding.ts +109 -0
  24. package/src/holding/index.ts +1 -0
  25. package/src/index.ts +34 -0
  26. package/src/market/Market.ts +479 -0
  27. package/src/market/MarketConfig.ts +108 -0
  28. package/src/market/MarketUtils.test.ts +25 -0
  29. package/src/market/MarketUtils.ts +467 -0
  30. package/src/market/index.ts +3 -0
  31. package/src/maths/AdaptiveCurveIrmLib.ts +143 -0
  32. package/src/maths/MathLib.ts +208 -0
  33. package/src/maths/MathUtils.ts +31 -0
  34. package/src/maths/SharesMath.ts +40 -0
  35. package/src/maths/index.ts +4 -0
  36. package/src/notifications.ts +167 -0
  37. package/src/position/Position.ts +251 -0
  38. package/src/position/index.ts +1 -0
  39. package/src/signatures/index.ts +18 -0
  40. package/src/signatures/manager.ts +50 -0
  41. package/src/signatures/permit.ts +126 -0
  42. package/src/signatures/permit2.ts +120 -0
  43. package/src/signatures/types.ts +18 -0
  44. package/src/signatures/utils.ts +83 -0
  45. package/src/tests/mocks/markets.ts +110 -0
  46. package/src/token/ERC20Metadata.ts +124 -0
  47. package/src/token/Token.ts +83 -0
  48. package/src/token/TokenNamespace.ts +76 -0
  49. package/src/token/WrappedToken.ts +142 -0
  50. package/src/token/index.ts +2 -0
  51. package/src/types.ts +37 -0
  52. package/src/user/User.ts +32 -0
  53. package/src/user/index.ts +2 -0
  54. package/src/user/user.types.ts +23 -0
  55. package/src/vault/Vault.ts +370 -0
  56. package/src/vault/VaultAllocation.ts +58 -0
  57. package/src/vault/VaultConfig.ts +55 -0
  58. package/src/vault/VaultUtils.ts +47 -0
  59. package/src/vault/index.ts +4 -0
@@ -0,0 +1,340 @@
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
+ });