@morpho-org/blue-sdk 1.0.0 → 1.0.1

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 (151) hide show
  1. package/lib/addresses.d.ts +168 -0
  2. package/lib/addresses.js +169 -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.types.d.ts +20 -0
  6. package/lib/chain/chain.types.js +30 -0
  7. package/lib/chain/chain.utils.d.ts +14 -0
  8. package/lib/chain/chain.utils.js +30 -0
  9. package/lib/chain/index.js +18 -0
  10. package/lib/constants.d.ts +8 -0
  11. package/lib/constants.js +13 -0
  12. package/lib/errors.d.ts +37 -0
  13. package/lib/errors.js +71 -0
  14. package/lib/ethers/index.js +18 -0
  15. package/lib/ethers/safeGetAddress.d.ts +1 -0
  16. package/lib/ethers/safeGetAddress.js +6 -0
  17. package/lib/ethers/safeParseUnits.d.ts +2 -0
  18. package/lib/ethers/safeParseUnits.js +25 -0
  19. package/lib/evm.d.ts +36 -0
  20. package/lib/evm.js +113 -0
  21. package/lib/helpers/format/format.d.ts +98 -0
  22. package/lib/helpers/format/format.js +301 -0
  23. package/lib/helpers/format/index.js +17 -0
  24. package/lib/helpers/getChecksumedAddress.d.ts +7 -0
  25. package/lib/helpers/getChecksumedAddress.js +17 -0
  26. package/lib/helpers/index.js +20 -0
  27. package/{src/helpers/isZeroAddressOrUnset.ts → lib/helpers/isZeroAddressOrUnset.d.ts} +1 -7
  28. package/lib/helpers/isZeroAddressOrUnset.js +14 -0
  29. package/lib/helpers/locale.d.ts +36 -0
  30. package/lib/helpers/locale.js +86 -0
  31. package/lib/holding/Holding.d.ts +60 -0
  32. package/lib/holding/Holding.js +31 -0
  33. package/lib/holding/index.js +17 -0
  34. package/lib/index.d.ts +33 -0
  35. package/lib/index.js +62 -0
  36. package/lib/market/Market.d.ts +159 -0
  37. package/lib/market/Market.js +240 -0
  38. package/lib/market/MarketConfig.d.ts +44 -0
  39. package/lib/market/MarketConfig.js +56 -0
  40. package/lib/market/MarketUtils.d.ts +165 -0
  41. package/lib/market/MarketUtils.js +182 -0
  42. package/lib/market/index.js +19 -0
  43. package/lib/maths/AdaptiveCurveIrmLib.d.ts +37 -0
  44. package/lib/maths/AdaptiveCurveIrmLib.js +116 -0
  45. package/lib/maths/MathLib.d.ts +94 -0
  46. package/lib/maths/MathLib.js +153 -0
  47. package/lib/maths/MathUtils.d.ts +15 -0
  48. package/lib/maths/MathUtils.js +33 -0
  49. package/lib/maths/SharesMath.d.ts +12 -0
  50. package/lib/maths/SharesMath.js +22 -0
  51. package/lib/maths/index.js +20 -0
  52. package/lib/notifications.d.ts +98 -0
  53. package/lib/notifications.js +52 -0
  54. package/lib/position/Position.d.ts +118 -0
  55. package/lib/position/Position.js +145 -0
  56. package/lib/position/index.js +17 -0
  57. package/lib/signatures/index.d.ts +12 -0
  58. package/lib/signatures/index.js +39 -0
  59. package/lib/signatures/manager.d.ts +10 -0
  60. package/lib/signatures/manager.js +37 -0
  61. package/lib/signatures/permit.d.ts +21 -0
  62. package/lib/signatures/permit.js +101 -0
  63. package/lib/signatures/permit2.d.ts +20 -0
  64. package/lib/signatures/permit2.js +91 -0
  65. package/lib/signatures/types.d.ts +13 -0
  66. package/lib/signatures/types.js +2 -0
  67. package/lib/signatures/utils.d.ts +6 -0
  68. package/lib/signatures/utils.js +44 -0
  69. package/lib/tests/mocks/markets.d.ts +17 -0
  70. package/lib/tests/mocks/markets.js +108 -0
  71. package/lib/token/ERC20Metadata.d.ts +249 -0
  72. package/lib/token/ERC20Metadata.js +81 -0
  73. package/lib/token/Token.d.ts +45 -0
  74. package/lib/token/Token.js +39 -0
  75. package/lib/token/TokenNamespace.d.ts +18 -0
  76. package/lib/token/TokenNamespace.js +55 -0
  77. package/lib/token/WrappedToken.d.ts +42 -0
  78. package/lib/token/WrappedToken.js +87 -0
  79. package/lib/token/index.js +18 -0
  80. package/lib/types.d.ts +29 -0
  81. package/lib/types.js +23 -0
  82. package/lib/user/User.d.ts +20 -0
  83. package/lib/user/User.js +11 -0
  84. package/lib/user/index.js +18 -0
  85. package/lib/user/user.types.d.ts +18 -0
  86. package/lib/user/user.types.js +2 -0
  87. package/lib/vault/Vault.d.ts +167 -0
  88. package/lib/vault/Vault.js +156 -0
  89. package/lib/vault/VaultAllocation.d.ts +38 -0
  90. package/lib/vault/VaultAllocation.js +18 -0
  91. package/lib/vault/VaultConfig.d.ts +23 -0
  92. package/lib/vault/VaultConfig.js +26 -0
  93. package/lib/vault/VaultUtils.d.ts +17 -0
  94. package/lib/vault/VaultUtils.js +17 -0
  95. package/lib/vault/index.js +20 -0
  96. package/package.json +5 -5
  97. package/src/addresses.ts +0 -261
  98. package/src/chain/chain.constants.ts +0 -235
  99. package/src/chain/chain.test.ts +0 -51
  100. package/src/chain/chain.types.ts +0 -42
  101. package/src/chain/chain.utils.ts +0 -44
  102. package/src/constants.ts +0 -18
  103. package/src/errors.ts +0 -75
  104. package/src/ethers/ethers.test.ts +0 -17
  105. package/src/ethers/safeGetAddress.ts +0 -4
  106. package/src/ethers/safeParseUnits.ts +0 -29
  107. package/src/evm.ts +0 -172
  108. package/src/helpers/format/format.test.ts +0 -340
  109. package/src/helpers/format/format.ts +0 -416
  110. package/src/helpers/getChecksumedAddress.ts +0 -15
  111. package/src/helpers/locale.ts +0 -108
  112. package/src/holding/Holding.ts +0 -109
  113. package/src/market/Market.ts +0 -479
  114. package/src/market/MarketConfig.ts +0 -108
  115. package/src/market/MarketUtils.test.ts +0 -25
  116. package/src/market/MarketUtils.ts +0 -467
  117. package/src/maths/AdaptiveCurveIrmLib.ts +0 -143
  118. package/src/maths/MathLib.ts +0 -208
  119. package/src/maths/MathUtils.ts +0 -31
  120. package/src/maths/SharesMath.ts +0 -40
  121. package/src/notifications.ts +0 -167
  122. package/src/position/Position.ts +0 -251
  123. package/src/signatures/index.ts +0 -18
  124. package/src/signatures/manager.ts +0 -50
  125. package/src/signatures/permit.ts +0 -126
  126. package/src/signatures/permit2.ts +0 -120
  127. package/src/signatures/types.ts +0 -18
  128. package/src/signatures/utils.ts +0 -83
  129. package/src/tests/mocks/markets.ts +0 -110
  130. package/src/token/ERC20Metadata.ts +0 -124
  131. package/src/token/Token.ts +0 -83
  132. package/src/token/TokenNamespace.ts +0 -76
  133. package/src/token/WrappedToken.ts +0 -142
  134. package/src/types.ts +0 -37
  135. package/src/user/User.ts +0 -32
  136. package/src/user/user.types.ts +0 -23
  137. package/src/vault/Vault.ts +0 -370
  138. package/src/vault/VaultAllocation.ts +0 -58
  139. package/src/vault/VaultConfig.ts +0 -55
  140. package/src/vault/VaultUtils.ts +0 -47
  141. /package/{src/chain/index.ts → lib/chain/index.d.ts} +0 -0
  142. /package/{src/ethers/index.ts → lib/ethers/index.d.ts} +0 -0
  143. /package/{src/helpers/format/index.ts → lib/helpers/format/index.d.ts} +0 -0
  144. /package/{src/helpers/index.ts → lib/helpers/index.d.ts} +0 -0
  145. /package/{src/holding/index.ts → lib/holding/index.d.ts} +0 -0
  146. /package/{src/market/index.ts → lib/market/index.d.ts} +0 -0
  147. /package/{src/maths/index.ts → lib/maths/index.d.ts} +0 -0
  148. /package/{src/position/index.ts → lib/position/index.d.ts} +0 -0
  149. /package/{src/token/index.ts → lib/token/index.d.ts} +0 -0
  150. /package/{src/user/index.ts → lib/user/index.d.ts} +0 -0
  151. /package/{src/vault/index.ts → lib/vault/index.d.ts} +0 -0
@@ -0,0 +1,301 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.format = exports.PercentFormatter = exports.ShortFormatter = exports.CommasFormatter = exports.NumberFormatter = exports.CommonFormatter = exports.HexFormatter = exports.BaseFormatter = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const locale_1 = require("../locale");
6
+ var Format;
7
+ (function (Format) {
8
+ Format["number"] = "number";
9
+ Format["commas"] = "commas";
10
+ Format["short"] = "short";
11
+ Format["hex"] = "hex";
12
+ Format["percent"] = "percent";
13
+ })(Format || (Format = {}));
14
+ String.prototype.insert = function (index, substr, fillWith) {
15
+ if (index < 0)
16
+ index = this.length + index;
17
+ let filler = "";
18
+ if (index < 0) {
19
+ if (fillWith)
20
+ filler = fillWith.repeat(-index).slice(index);
21
+ index = 0;
22
+ }
23
+ return this.slice(0, index) + substr + filler + this.slice(index);
24
+ };
25
+ const RANGES = [
26
+ {
27
+ minDecimals: 24,
28
+ symbol: "Y",
29
+ },
30
+ {
31
+ minDecimals: 21,
32
+ symbol: "Z",
33
+ },
34
+ {
35
+ minDecimals: 18,
36
+ symbol: "E",
37
+ },
38
+ {
39
+ minDecimals: 15,
40
+ symbol: "P",
41
+ },
42
+ {
43
+ minDecimals: 12,
44
+ symbol: "T",
45
+ },
46
+ {
47
+ minDecimals: 9,
48
+ symbol: "B",
49
+ },
50
+ {
51
+ minDecimals: 6,
52
+ symbol: "M",
53
+ },
54
+ {
55
+ minDecimals: 4,
56
+ power: 3,
57
+ symbol: "k",
58
+ },
59
+ ];
60
+ const _formatShort = (bi, decimals, formatOptions) => {
61
+ const stringValue = bi.toString();
62
+ const params = RANGES.find((range) => stringValue.length > range.minDecimals + decimals);
63
+ if (params) {
64
+ return (_applyOptions(stringValue.insert(-((params.power ?? params.minDecimals) + decimals), ".", "0"), formatOptions) + params.symbol);
65
+ }
66
+ if (formatOptions.smallValuesWithCommas) {
67
+ return _formatCommas(bi, decimals, formatOptions);
68
+ }
69
+ return _applyOptions(stringValue.insert(-decimals, ".", "0"), formatOptions);
70
+ };
71
+ const _formatCommas = (bi, decimals, formatOptions) => {
72
+ const stringValue = decimals
73
+ ? bi.toString().insert(-decimals, ".", "0")
74
+ : bi.toString();
75
+ const [wholePart, decimalPart] = stringValue.split(".");
76
+ return _applyOptions(wholePart
77
+ .split("")
78
+ .reduce((formattedNumber, digit, i, arr) => `${formattedNumber}${!i || (arr.length - i) % 3 ? "" : ","}${digit}`, "") + (decimalPart ? "." + decimalPart : ""), formatOptions);
79
+ };
80
+ const _formatNumber = (bi, decimals, formatOptions) => {
81
+ if (decimals === 0)
82
+ return _applyOptions(bi.toString(), formatOptions);
83
+ return _applyOptions(bi.toString().insert(-decimals, ".", "0"), formatOptions);
84
+ };
85
+ const _withUnit = (value, unit) => {
86
+ switch (unit) {
87
+ case "$":
88
+ return `$${value}`;
89
+ case "":
90
+ case "%":
91
+ return `${value}${unit}`;
92
+ default:
93
+ return `${value} ${unit}`;
94
+ }
95
+ };
96
+ const _applyOptions = (value, formatOptions) => {
97
+ let [wholePart, decimalPart = ""] = value.split(".");
98
+ const isZero = value.match(/[1-9]/)?.index === undefined;
99
+ if (formatOptions.digits !== undefined) {
100
+ decimalPart = decimalPart + "0".repeat(formatOptions.digits);
101
+ decimalPart = decimalPart.slice(0, formatOptions.digits);
102
+ }
103
+ if (formatOptions.removeTrailingZero) {
104
+ decimalPart = decimalPart.replace(/\.?0+$/, "");
105
+ }
106
+ value = (wholePart || "0") + (decimalPart ? "." + decimalPart : "");
107
+ const { value: _value, decimalSymbol } = (0, locale_1.getEnUSNumberToLocalParts)(value, formatOptions.locale);
108
+ value = _value;
109
+ const firstNonZero = value.match(/[1-9]/);
110
+ if (firstNonZero?.index === undefined &&
111
+ formatOptions.digits &&
112
+ !isZero &&
113
+ formatOptions.readable)
114
+ return `< 0${decimalSymbol}${"0".repeat(formatOptions.digits - 1)}1`;
115
+ return value;
116
+ };
117
+ function formatBI(bi, decimals, formatOptions = { format: Format.hex }) {
118
+ if (formatOptions.format === Format.hex)
119
+ return bi.toString(16);
120
+ const maxBI = formatOptions.max !== undefined &&
121
+ (0, ethers_1.parseUnits)(formatOptions.max.toString(), decimals);
122
+ if (maxBI && bi > maxBI) {
123
+ return `> ${formatBI(maxBI, decimals, formatOptions)}`;
124
+ }
125
+ const minBI = formatOptions.min !== undefined &&
126
+ (0, ethers_1.parseUnits)(formatOptions.min.toString(), decimals);
127
+ if (minBI !== false && bi < minBI && bi !== 0n) {
128
+ return `< ${formatBI(minBI, decimals, formatOptions)}`;
129
+ }
130
+ let value;
131
+ const isNegative = bi < 0n;
132
+ const absBI = bi > 0n ? bi : -bi;
133
+ switch (formatOptions.format) {
134
+ case Format.commas:
135
+ value = _formatCommas(absBI, decimals, formatOptions);
136
+ break;
137
+ case Format.number:
138
+ value = _formatNumber(absBI, decimals, formatOptions);
139
+ break;
140
+ case Format.short:
141
+ value = _formatShort(absBI, decimals, formatOptions);
142
+ break;
143
+ case Format.percent:
144
+ value = _formatNumber(absBI * 100n, decimals, formatOptions);
145
+ break;
146
+ }
147
+ const formattedValue = (isNegative && !/^0\.0+$/.test(value)
148
+ ? "-"
149
+ : formatOptions.sign
150
+ ? "+"
151
+ : "") + value;
152
+ if (formatOptions.unit) {
153
+ return _withUnit(formattedValue, formatOptions.unit);
154
+ }
155
+ return formattedValue;
156
+ }
157
+ /**
158
+ * returns a string representation of a number, always in a decimal format (avoid the 1.34e-15 format for small numbers)
159
+ * @param number
160
+ */
161
+ const safeNumberToString = (number) => {
162
+ const str = number.toString();
163
+ const [a, n] = str.split(/[eE]/);
164
+ if (a == null || n == null)
165
+ return str;
166
+ if (n[0] === "+") {
167
+ const [whole, decimal = ""] = a.split(".");
168
+ return whole + decimal + "0".repeat(+n - decimal.length);
169
+ }
170
+ if (n[0] === "-") {
171
+ const isNegative = a[0] === "-";
172
+ return ((isNegative ? "-" : "") +
173
+ "0." +
174
+ "0".repeat(-+n - 1) +
175
+ (isNegative ? a.slice(1) : a).split(".").join(""));
176
+ }
177
+ throw new Error(`Unhandled case: ${str}`);
178
+ };
179
+ class BaseFormatter {
180
+ constructor() { }
181
+ of(value, decimals) {
182
+ let _decimals;
183
+ if (typeof value === "number") {
184
+ const strNumber = safeNumberToString(value);
185
+ let [, d = ""] = strNumber.split(".");
186
+ _decimals = d.length;
187
+ value = (0, ethers_1.parseUnits)(strNumber, _decimals);
188
+ }
189
+ else {
190
+ _decimals = decimals;
191
+ }
192
+ return formatBI(value, _decimals, this._options);
193
+ }
194
+ }
195
+ exports.BaseFormatter = BaseFormatter;
196
+ class HexFormatter extends BaseFormatter {
197
+ constructor() {
198
+ super(...arguments);
199
+ this._options = { format: Format.hex };
200
+ }
201
+ }
202
+ exports.HexFormatter = HexFormatter;
203
+ class CommonFormatter extends BaseFormatter {
204
+ digits(_d) {
205
+ this._options.digits = _d;
206
+ return this;
207
+ }
208
+ removeTrailingZero() {
209
+ this._options.removeTrailingZero = true;
210
+ return this;
211
+ }
212
+ readable() {
213
+ this._options.readable = true;
214
+ return this;
215
+ }
216
+ min(_m) {
217
+ this._options.min = _m;
218
+ return this;
219
+ }
220
+ max(_m) {
221
+ this._options.max = _m;
222
+ return this;
223
+ }
224
+ sign() {
225
+ this._options.sign = true;
226
+ return this;
227
+ }
228
+ unit(_u) {
229
+ this._options.unit = _u;
230
+ return this;
231
+ }
232
+ locale(_l) {
233
+ this._options.locale = _l;
234
+ return this;
235
+ }
236
+ }
237
+ exports.CommonFormatter = CommonFormatter;
238
+ class NumberFormatter extends CommonFormatter {
239
+ constructor() {
240
+ super(...arguments);
241
+ this._options = { format: Format.number };
242
+ }
243
+ }
244
+ exports.NumberFormatter = NumberFormatter;
245
+ class CommasFormatter extends CommonFormatter {
246
+ constructor() {
247
+ super(...arguments);
248
+ this._options = { format: Format.commas };
249
+ }
250
+ }
251
+ exports.CommasFormatter = CommasFormatter;
252
+ class ShortFormatter extends CommonFormatter {
253
+ constructor() {
254
+ super(...arguments);
255
+ this._options = { format: Format.short };
256
+ }
257
+ smallValuesWithCommas() {
258
+ this._options.smallValuesWithCommas = true;
259
+ return this;
260
+ }
261
+ }
262
+ exports.ShortFormatter = ShortFormatter;
263
+ class PercentFormatter extends CommonFormatter {
264
+ constructor() {
265
+ super(...arguments);
266
+ this._options = { format: Format.percent };
267
+ }
268
+ }
269
+ exports.PercentFormatter = PercentFormatter;
270
+ exports.format = {
271
+ /**
272
+ * Return the value as an integer in hex format
273
+ */
274
+ get hex() {
275
+ return new HexFormatter();
276
+ },
277
+ /**
278
+ * Return the value as a stringified number (12345.6789)
279
+ */
280
+ get number() {
281
+ return new NumberFormatter();
282
+ },
283
+ /**
284
+ * Return the value as a commas-separated stringified number (12,345.6789)
285
+ */
286
+ get commas() {
287
+ return new CommasFormatter();
288
+ },
289
+ /**
290
+ * Return the value as a shorted stringified number (12.3456789k)
291
+ */
292
+ get short() {
293
+ return new ShortFormatter();
294
+ },
295
+ /**
296
+ * Return the value as a percent based stringified number (10.00 instead of 0.1)
297
+ */
298
+ get percent() {
299
+ return new PercentFormatter();
300
+ },
301
+ };
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./format"), exports);
@@ -0,0 +1,7 @@
1
+ import { AddressLike } from "ethers";
2
+ /**
3
+ * Transform an AddressLike into a checksumed address
4
+ *
5
+ * @param address Address to transform
6
+ */
7
+ export declare const getChecksumedAddress: (address: AddressLike) => Promise<string>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getChecksumedAddress = void 0;
4
+ const ethers_1 = require("ethers");
5
+ /**
6
+ * Transform an AddressLike into a checksumed address
7
+ *
8
+ * @param address Address to transform
9
+ */
10
+ const getChecksumedAddress = async (address) => {
11
+ const awaited = await address;
12
+ // Ethers getAddress function is throwing an error if the address is already checksumed
13
+ if (typeof awaited === "string")
14
+ return (0, ethers_1.getAddress)(awaited.toLowerCase());
15
+ return (0, ethers_1.getAddress)((await awaited.getAddress()).toLowerCase());
16
+ };
17
+ exports.getChecksumedAddress = getChecksumedAddress;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./getChecksumedAddress"), exports);
18
+ __exportStar(require("./isZeroAddressOrUnset"), exports);
19
+ __exportStar(require("./locale"), exports);
20
+ __exportStar(require("./format"), exports);
@@ -1,13 +1,7 @@
1
- import { ZeroAddress } from "ethers";
2
-
3
1
  /**
4
2
  * Check if an address is the zero address, null, or undefined.
5
3
  *
6
4
  * @param address - The address to check, which can be a valid address, null, or undefined.
7
5
  * @returns True if the address is zero, null, or undefined; otherwise, false.
8
6
  */
9
- export function isZeroAddressOrUnset(
10
- address: string | null | undefined
11
- ): boolean {
12
- return address === undefined || address === null || address === ZeroAddress;
13
- }
7
+ export declare function isZeroAddressOrUnset(address: string | null | undefined): boolean;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isZeroAddressOrUnset = void 0;
4
+ const ethers_1 = require("ethers");
5
+ /**
6
+ * Check if an address is the zero address, null, or undefined.
7
+ *
8
+ * @param address - The address to check, which can be a valid address, null, or undefined.
9
+ * @returns True if the address is zero, null, or undefined; otherwise, false.
10
+ */
11
+ function isZeroAddressOrUnset(address) {
12
+ return address === undefined || address === null || address === ethers_1.ZeroAddress;
13
+ }
14
+ exports.isZeroAddressOrUnset = isZeroAddressOrUnset;
@@ -0,0 +1,36 @@
1
+ type LocaleSymbols = {
2
+ decimalSymbol: string;
3
+ groupSymbol: string;
4
+ locale: string;
5
+ };
6
+ export type LocaleParts = LocaleSymbols & {
7
+ value: string;
8
+ };
9
+ /**
10
+ * @returns the locale symbols for the given locale defaulting to en-US
11
+ */
12
+ export declare const getLocaleSymbols: (locale: string) => LocaleSymbols;
13
+ /**
14
+ * @returns the effective browser locale
15
+ */
16
+ export declare const getEffectiveLocale: () => string;
17
+ /**
18
+ * @returns the value as a string with the given locale symbols
19
+ * @param numStr the number as a string in the "from" locale (e.g. "1,2345.6" for en-US)
20
+ * @param from the locale the numStr is in (e.g. "en-US")
21
+ * @param to the locale to convert to (e.g. "fr-FR")
22
+ */
23
+ export declare const convertNumStrToLocal: (numStr: string, from: string, to: string) => string;
24
+ /**
25
+ * @returns the value as a string in the effective browser locale
26
+ * @param numStr the number as a string in the effective browser locale (e.g. "1,2345.6" for en-US)
27
+ * @param to the locale to use (e.g. "fr-FR")
28
+ */
29
+ export declare const convertNumStrFromEffectiveTo: (numStr: string, to: string) => string;
30
+ /**
31
+ * @returns the value as a string with the given locale symbols either from the given locale or the effective browser locale
32
+ * @param numStr the number as a string in english format (e.g. "1,2345.6")
33
+ * @param locale optional - the locale to use (e.g. "fr-FR")
34
+ */
35
+ export declare const getEnUSNumberToLocalParts: (numStr: string, locale?: string) => LocaleParts;
36
+ export {};
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEnUSNumberToLocalParts = exports.convertNumStrFromEffectiveTo = exports.convertNumStrToLocal = exports.getEffectiveLocale = exports.getLocaleSymbols = void 0;
4
+ const _convertEnNumStrToLocale = (numStr, localSymbols) => {
5
+ return numStr
6
+ .replaceAll(",", "#TEMP#")
7
+ .replaceAll(".", localSymbols.decimalSymbol)
8
+ .replaceAll("#TEMP#", localSymbols.groupSymbol);
9
+ };
10
+ /**
11
+ * @returns the locale symbols for the given locale defaulting to en-US
12
+ */
13
+ const getLocaleSymbols = (locale) => {
14
+ let formatter;
15
+ const formatterOptions = {
16
+ useGrouping: true,
17
+ maximumFractionDigits: 1,
18
+ minimumFractionDigits: 1,
19
+ };
20
+ try {
21
+ formatter = new Intl.NumberFormat(locale, formatterOptions);
22
+ }
23
+ catch {
24
+ formatter = new Intl.NumberFormat("en-US", formatterOptions);
25
+ }
26
+ const parts = formatter.formatToParts(12345.6);
27
+ const decimalSymbol = parts.find((part) => part.type === "decimal").value;
28
+ const groupSymbol = parts.find((part) => part.type === "group").value;
29
+ return { decimalSymbol, groupSymbol, locale };
30
+ };
31
+ exports.getLocaleSymbols = getLocaleSymbols;
32
+ /**
33
+ * @returns the effective browser locale
34
+ */
35
+ const getEffectiveLocale = () => {
36
+ if (typeof window !== "undefined") {
37
+ try {
38
+ const locale = navigator?.language || document?.documentElement?.lang || "en-US";
39
+ new Intl.NumberFormat(locale);
40
+ return locale;
41
+ }
42
+ catch (e) {
43
+ return "en-US";
44
+ }
45
+ }
46
+ return "en-US";
47
+ };
48
+ exports.getEffectiveLocale = getEffectiveLocale;
49
+ /**
50
+ * @returns the value as a string with the given locale symbols
51
+ * @param numStr the number as a string in the "from" locale (e.g. "1,2345.6" for en-US)
52
+ * @param from the locale the numStr is in (e.g. "en-US")
53
+ * @param to the locale to convert to (e.g. "fr-FR")
54
+ */
55
+ const convertNumStrToLocal = (numStr, from, to) => {
56
+ const fromSymbols = (0, exports.getLocaleSymbols)(from);
57
+ const toSymbols = (0, exports.getLocaleSymbols)(to);
58
+ return numStr
59
+ .replaceAll(fromSymbols.groupSymbol, "#GROUP#")
60
+ .replaceAll(fromSymbols.decimalSymbol, "#DECIMAL#")
61
+ .replaceAll("#GROUP#", toSymbols.groupSymbol)
62
+ .replaceAll("#DECIMAL#", toSymbols.decimalSymbol);
63
+ };
64
+ exports.convertNumStrToLocal = convertNumStrToLocal;
65
+ /**
66
+ * @returns the value as a string in the effective browser locale
67
+ * @param numStr the number as a string in the effective browser locale (e.g. "1,2345.6" for en-US)
68
+ * @param to the locale to use (e.g. "fr-FR")
69
+ */
70
+ const convertNumStrFromEffectiveTo = (numStr, to) => {
71
+ const from = (0, exports.getEffectiveLocale)();
72
+ return (0, exports.convertNumStrToLocal)(numStr, from, to);
73
+ };
74
+ exports.convertNumStrFromEffectiveTo = convertNumStrFromEffectiveTo;
75
+ /**
76
+ * @returns the value as a string with the given locale symbols either from the given locale or the effective browser locale
77
+ * @param numStr the number as a string in english format (e.g. "1,2345.6")
78
+ * @param locale optional - the locale to use (e.g. "fr-FR")
79
+ */
80
+ const getEnUSNumberToLocalParts = (numStr, locale) => {
81
+ const _locale = locale || (0, exports.getEffectiveLocale)();
82
+ const localSymbols = (0, exports.getLocaleSymbols)(_locale);
83
+ const value = _convertEnNumStrToLocale(numStr, localSymbols);
84
+ return { ...localSymbols, value };
85
+ };
86
+ exports.getEnUSNumberToLocalParts = getEnUSNumberToLocalParts;
@@ -0,0 +1,60 @@
1
+ import { BigNumberish } from "ethers";
2
+ import { Address } from "../types";
3
+ export declare const ERC20_ALLOWANCE_RECIPIENTS: readonly ["morpho", "permit2", "bundler"];
4
+ export declare const PERMIT2_ALLOWANCE_RECIPIENTS: readonly ["morpho", "bundler"];
5
+ export type Erc20AllowanceRecipient = (typeof ERC20_ALLOWANCE_RECIPIENTS)[number];
6
+ export type Permit2AllowanceRecipient = (typeof PERMIT2_ALLOWANCE_RECIPIENTS)[number];
7
+ export interface Permit2Allowance {
8
+ amount: bigint;
9
+ expiration: bigint;
10
+ nonce: bigint;
11
+ }
12
+ export interface InputHolding {
13
+ user: Address;
14
+ token: Address;
15
+ erc20Allowances: {
16
+ [key in Erc20AllowanceRecipient]: BigNumberish;
17
+ };
18
+ permit2Allowances: {
19
+ [key in Permit2AllowanceRecipient]: Permit2Allowance;
20
+ };
21
+ erc2612Nonce?: BigNumberish;
22
+ canTransfer?: boolean;
23
+ balance: BigNumberish;
24
+ }
25
+ export declare class Holding implements InputHolding {
26
+ /**
27
+ * The user of this holding.
28
+ */
29
+ readonly user: Address;
30
+ /**
31
+ * The token in which this holding is denominated.
32
+ */
33
+ readonly token: Address;
34
+ /**
35
+ * The balance of the user for this token.
36
+ */
37
+ balance: bigint;
38
+ /**
39
+ * Whether the user is allowed to transfer this holding's balance.
40
+ */
41
+ canTransfer?: boolean;
42
+ /**
43
+ * ERC20 allowance for this token from the user to the allowance recipient.
44
+ */
45
+ readonly erc20Allowances: {
46
+ [key in Erc20AllowanceRecipient]: bigint;
47
+ };
48
+ /**
49
+ * Permit2 allowance for this token from the user to the allowance recipient.
50
+ */
51
+ readonly permit2Allowances: {
52
+ [key in Permit2AllowanceRecipient]: Permit2Allowance;
53
+ };
54
+ /**
55
+ * ERC-2612 Permit nonce of the user for this token.
56
+ * `undefined` if the token does not support ERC-2612.
57
+ */
58
+ erc2612Nonce?: bigint;
59
+ constructor({ user, token, erc20Allowances, permit2Allowances, balance, erc2612Nonce, canTransfer, }: InputHolding);
60
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Holding = exports.PERMIT2_ALLOWANCE_RECIPIENTS = exports.ERC20_ALLOWANCE_RECIPIENTS = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
+ const addresses_1 = require("../addresses");
7
+ exports.ERC20_ALLOWANCE_RECIPIENTS = [
8
+ "morpho",
9
+ "permit2",
10
+ "bundler",
11
+ ];
12
+ exports.PERMIT2_ALLOWANCE_RECIPIENTS = [
13
+ "morpho",
14
+ "bundler",
15
+ ];
16
+ class Holding {
17
+ constructor({ user, token, erc20Allowances, permit2Allowances, balance, erc2612Nonce, canTransfer = true, }) {
18
+ this.user = user;
19
+ this.token = token;
20
+ this.balance = (0, ethers_1.toBigInt)(balance);
21
+ this.canTransfer = token === addresses_1.NATIVE_ADDRESS || canTransfer;
22
+ this.erc20Allowances = (0, morpho_ts_1.fromEntries)((0, morpho_ts_1.entries)(erc20Allowances).map(([address, allowance]) => [
23
+ address,
24
+ (0, ethers_1.toBigInt)(allowance),
25
+ ]));
26
+ this.permit2Allowances = permit2Allowances;
27
+ if (erc2612Nonce != null)
28
+ this.erc2612Nonce = (0, ethers_1.toBigInt)(erc2612Nonce);
29
+ }
30
+ }
31
+ exports.Holding = Holding;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Holding"), exports);
package/lib/index.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ export * from "./addresses";
2
+ export * from "./evm";
3
+ export * from "./constants";
4
+ export * from "./errors";
5
+ export * from "./market";
6
+ export * from "./chain";
7
+ export * from "./token";
8
+ export * from "./types";
9
+ export * from "./maths";
10
+ export * from "./notifications";
11
+ export * from "./signatures";
12
+ export * from "./user";
13
+ export * from "./holding";
14
+ export * from "./position";
15
+ export * from "./helpers";
16
+ export * from "./ethers";
17
+ export * from "./vault";
18
+ export * as evm from "./evm";
19
+ export * as constants from "./constants";
20
+ export * as errors from "./errors";
21
+ export * as market from "./market";
22
+ export * as chain from "./chain";
23
+ export * as token from "./token";
24
+ export * as position from "./position";
25
+ export * as holding from "./holding";
26
+ export * as types from "./types";
27
+ export * as maths from "./maths";
28
+ export * as notifications from "./notifications";
29
+ export * as signatures from "./signatures";
30
+ export * as user from "./user";
31
+ export * as helpers from "./helpers";
32
+ export * as ethers from "./ethers";
33
+ export * as vault from "./vault";