@morpho-org/blue-sdk 1.0.2 → 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.
- package/lib/addresses.d.ts +156 -0
- package/lib/addresses.js +160 -0
- package/lib/chain/chain.constants.d.ts +3 -0
- package/lib/chain/chain.constants.js +232 -0
- package/lib/chain/chain.test.d.ts +1 -0
- package/lib/chain/chain.test.js +37 -0
- package/lib/chain/chain.types.d.ts +20 -0
- package/lib/chain/chain.types.js +30 -0
- package/lib/chain/chain.utils.d.ts +14 -0
- package/lib/chain/chain.utils.js +30 -0
- package/lib/chain/index.js +18 -0
- package/lib/constants.d.ts +8 -0
- package/lib/constants.js +13 -0
- package/lib/errors.d.ts +37 -0
- package/lib/errors.js +71 -0
- package/lib/ethers/ethers.test.d.ts +1 -0
- package/lib/ethers/ethers.test.js +11 -0
- package/lib/ethers/index.js +18 -0
- package/lib/ethers/safeGetAddress.d.ts +1 -0
- package/lib/ethers/safeGetAddress.js +6 -0
- package/lib/ethers/safeParseUnits.d.ts +2 -0
- package/lib/ethers/safeParseUnits.js +25 -0
- package/lib/evm.d.ts +36 -0
- package/lib/evm.js +113 -0
- package/lib/helpers/format/format.d.ts +98 -0
- package/lib/helpers/format/format.js +301 -0
- package/lib/helpers/format/format.test.d.ts +1 -0
- package/lib/helpers/format/format.test.js +257 -0
- package/lib/helpers/format/index.js +17 -0
- package/lib/helpers/getChecksumedAddress.d.ts +7 -0
- package/lib/helpers/getChecksumedAddress.js +17 -0
- package/lib/helpers/index.js +20 -0
- package/{src/helpers/isZeroAddressOrUnset.ts → lib/helpers/isZeroAddressOrUnset.d.ts} +1 -7
- package/lib/helpers/isZeroAddressOrUnset.js +14 -0
- package/lib/helpers/locale.d.ts +36 -0
- package/lib/helpers/locale.js +86 -0
- package/lib/holding/Holding.d.ts +60 -0
- package/lib/holding/Holding.js +31 -0
- package/lib/holding/index.js +17 -0
- package/{src/index.ts → lib/index.d.ts} +0 -1
- package/lib/index.js +62 -0
- package/lib/market/Market.d.ts +159 -0
- package/lib/market/Market.js +240 -0
- package/lib/market/MarketConfig.d.ts +44 -0
- package/lib/market/MarketConfig.js +56 -0
- package/lib/market/MarketUtils.d.ts +165 -0
- package/lib/market/MarketUtils.js +182 -0
- package/lib/market/MarketUtils.test.d.ts +1 -0
- package/lib/market/MarketUtils.test.js +19 -0
- package/lib/market/index.js +19 -0
- package/lib/maths/AdaptiveCurveIrmLib.d.ts +37 -0
- package/lib/maths/AdaptiveCurveIrmLib.js +116 -0
- package/lib/maths/MathLib.d.ts +94 -0
- package/lib/maths/MathLib.js +153 -0
- package/lib/maths/MathUtils.d.ts +15 -0
- package/lib/maths/MathUtils.js +33 -0
- package/lib/maths/SharesMath.d.ts +12 -0
- package/lib/maths/SharesMath.js +22 -0
- package/lib/maths/index.js +20 -0
- package/lib/notifications.d.ts +98 -0
- package/lib/notifications.js +52 -0
- package/lib/position/Position.d.ts +118 -0
- package/lib/position/Position.js +145 -0
- package/lib/position/index.js +17 -0
- package/lib/signatures/index.d.ts +12 -0
- package/lib/signatures/index.js +39 -0
- package/lib/signatures/manager.d.ts +10 -0
- package/lib/signatures/manager.js +37 -0
- package/lib/signatures/permit.d.ts +21 -0
- package/lib/signatures/permit.js +101 -0
- package/lib/signatures/permit2.d.ts +20 -0
- package/lib/signatures/permit2.js +91 -0
- package/lib/signatures/types.d.ts +13 -0
- package/lib/signatures/types.js +2 -0
- package/lib/signatures/utils.d.ts +6 -0
- package/lib/signatures/utils.js +44 -0
- package/lib/tests/mocks/markets.d.ts +17 -0
- package/lib/tests/mocks/markets.js +108 -0
- package/lib/token/ERC20Metadata.d.ts +249 -0
- package/lib/token/ERC20Metadata.js +81 -0
- package/lib/token/Token.d.ts +45 -0
- package/lib/token/Token.js +39 -0
- package/lib/token/TokenNamespace.d.ts +18 -0
- package/lib/token/TokenNamespace.js +55 -0
- package/lib/token/WrappedToken.d.ts +42 -0
- package/lib/token/WrappedToken.js +87 -0
- package/lib/token/index.js +18 -0
- package/lib/types.d.ts +29 -0
- package/lib/types.js +23 -0
- package/lib/user/User.d.ts +20 -0
- package/lib/user/User.js +11 -0
- package/lib/user/index.js +18 -0
- package/lib/user/user.types.d.ts +18 -0
- package/lib/user/user.types.js +2 -0
- package/lib/vault/Vault.d.ts +167 -0
- package/lib/vault/Vault.js +156 -0
- package/lib/vault/VaultAllocation.d.ts +38 -0
- package/lib/vault/VaultAllocation.js +18 -0
- package/lib/vault/VaultConfig.d.ts +23 -0
- package/lib/vault/VaultConfig.js +26 -0
- package/lib/vault/VaultUtils.d.ts +17 -0
- package/lib/vault/VaultUtils.js +17 -0
- package/lib/vault/index.js +20 -0
- package/package.json +7 -11
- package/src/addresses.ts +0 -261
- package/src/chain/chain.constants.ts +0 -235
- package/src/chain/chain.test.ts +0 -51
- package/src/chain/chain.types.ts +0 -42
- package/src/chain/chain.utils.ts +0 -44
- package/src/constants.ts +0 -18
- package/src/errors.ts +0 -75
- package/src/ethers/ethers.test.ts +0 -17
- package/src/ethers/safeGetAddress.ts +0 -4
- package/src/ethers/safeParseUnits.ts +0 -29
- package/src/evm.ts +0 -172
- package/src/helpers/format/format.test.ts +0 -340
- package/src/helpers/format/format.ts +0 -416
- package/src/helpers/getChecksumedAddress.ts +0 -15
- package/src/helpers/locale.ts +0 -108
- package/src/holding/Holding.ts +0 -109
- package/src/market/Market.ts +0 -479
- package/src/market/MarketConfig.ts +0 -108
- package/src/market/MarketUtils.test.ts +0 -25
- package/src/market/MarketUtils.ts +0 -467
- package/src/maths/AdaptiveCurveIrmLib.ts +0 -143
- package/src/maths/MathLib.ts +0 -208
- package/src/maths/MathUtils.ts +0 -31
- package/src/maths/SharesMath.ts +0 -40
- package/src/notifications.ts +0 -167
- package/src/position/Position.ts +0 -251
- package/src/signatures/index.ts +0 -18
- package/src/signatures/manager.ts +0 -50
- package/src/signatures/permit.ts +0 -126
- package/src/signatures/permit2.ts +0 -120
- package/src/signatures/types.ts +0 -18
- package/src/signatures/utils.ts +0 -83
- package/src/tests/mocks/markets.ts +0 -110
- package/src/token/ERC20Metadata.ts +0 -124
- package/src/token/Token.ts +0 -83
- package/src/token/TokenNamespace.ts +0 -76
- package/src/token/WrappedToken.ts +0 -142
- package/src/types.ts +0 -37
- package/src/user/User.ts +0 -32
- package/src/user/user.types.ts +0 -23
- package/src/vault/Vault.ts +0 -370
- package/src/vault/VaultAllocation.ts +0 -58
- package/src/vault/VaultConfig.ts +0 -55
- package/src/vault/VaultUtils.ts +0 -47
- /package/{src/chain/index.ts → lib/chain/index.d.ts} +0 -0
- /package/{src/ethers/index.ts → lib/ethers/index.d.ts} +0 -0
- /package/{src/helpers/format/index.ts → lib/helpers/format/index.d.ts} +0 -0
- /package/{src/helpers/index.ts → lib/helpers/index.d.ts} +0 -0
- /package/{src/holding/index.ts → lib/holding/index.d.ts} +0 -0
- /package/{src/market/index.ts → lib/market/index.d.ts} +0 -0
- /package/{src/maths/index.ts → lib/maths/index.d.ts} +0 -0
- /package/{src/position/index.ts → lib/position/index.d.ts} +0 -0
- /package/{src/token/index.ts → lib/token/index.d.ts} +0 -0
- /package/{src/user/index.ts → lib/user/index.d.ts} +0 -0
- /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 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const format_1 = require("./format");
|
|
4
|
+
describe("format", () => {
|
|
5
|
+
const number = 12345.6789;
|
|
6
|
+
const bigint = 123456789n;
|
|
7
|
+
const decimals = 4;
|
|
8
|
+
describe("hex", () => {
|
|
9
|
+
describe("should properly format number in hex format", () => {
|
|
10
|
+
it("without option", () => {
|
|
11
|
+
expect(format_1.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_1.format.hex.of(bigint, decimals)).toEqual((123456789).toString(16));
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
describe("number", () => {
|
|
21
|
+
describe("should properly format number in number format", () => {
|
|
22
|
+
it("without option", () => {
|
|
23
|
+
expect(format_1.format.number.of(number)).toEqual("12345.6789");
|
|
24
|
+
});
|
|
25
|
+
it("with digits", () => {
|
|
26
|
+
expect(format_1.format.number.digits(2).of(number)).toEqual("12345.67");
|
|
27
|
+
});
|
|
28
|
+
it("with min", () => {
|
|
29
|
+
expect(format_1.format.number.min(20000).of(number)).toEqual("< 20000.0000");
|
|
30
|
+
});
|
|
31
|
+
it("with max", () => {
|
|
32
|
+
expect(format_1.format.number.max(10000).of(number)).toEqual("> 10000.0000");
|
|
33
|
+
});
|
|
34
|
+
it("with sign", () => {
|
|
35
|
+
expect(format_1.format.number.sign().of(number)).toEqual("+12345.6789");
|
|
36
|
+
});
|
|
37
|
+
it("with unit", () => {
|
|
38
|
+
expect(format_1.format.number.unit("$").of(number)).toEqual("$12345.6789");
|
|
39
|
+
});
|
|
40
|
+
it("without trailing zeros", () => {
|
|
41
|
+
expect(format_1.format.number.digits(6).of(number)).toEqual("12345.678900");
|
|
42
|
+
expect(format_1.format.number.digits(6).removeTrailingZero().of(number)).toEqual("12345.6789");
|
|
43
|
+
});
|
|
44
|
+
it("with locale", () => {
|
|
45
|
+
expect(format_1.format.number.locale("fr-FR").of(number)).toEqual("12345,6789");
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
describe("should properly format bigint in number format", () => {
|
|
49
|
+
it("without option", () => {
|
|
50
|
+
expect(format_1.format.number.of(bigint, decimals)).toEqual("12345.6789");
|
|
51
|
+
});
|
|
52
|
+
it("with digits", () => {
|
|
53
|
+
expect(format_1.format.number.digits(2).of(bigint, decimals)).toEqual("12345.67");
|
|
54
|
+
});
|
|
55
|
+
it("with min", () => {
|
|
56
|
+
expect(format_1.format.number.min(20000).of(bigint, decimals)).toEqual("< 20000.0000");
|
|
57
|
+
});
|
|
58
|
+
it("with max", () => {
|
|
59
|
+
expect(format_1.format.number.max(10000).of(bigint, decimals)).toEqual("> 10000.0000");
|
|
60
|
+
});
|
|
61
|
+
it("with sign", () => {
|
|
62
|
+
expect(format_1.format.number.sign().of(bigint, decimals)).toEqual("+12345.6789");
|
|
63
|
+
});
|
|
64
|
+
it("with unit", () => {
|
|
65
|
+
expect(format_1.format.number.unit("$").of(bigint, decimals)).toEqual("$12345.6789");
|
|
66
|
+
});
|
|
67
|
+
it("without trailing zeros", () => {
|
|
68
|
+
expect(format_1.format.number.digits(6).of(bigint, decimals)).toEqual("12345.678900");
|
|
69
|
+
expect(format_1.format.number.digits(6).removeTrailingZero().of(bigint, decimals)).toEqual("12345.6789");
|
|
70
|
+
});
|
|
71
|
+
it("with locale", () => {
|
|
72
|
+
expect(format_1.format.number.locale("fr-FR").of(bigint, decimals)).toEqual("12345,6789");
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe("short", () => {
|
|
77
|
+
describe("should properly format number in short format", () => {
|
|
78
|
+
it("without option", () => {
|
|
79
|
+
expect(format_1.format.short.of(number)).toEqual("12.3456789k");
|
|
80
|
+
});
|
|
81
|
+
it("with digits", () => {
|
|
82
|
+
expect(format_1.format.short.digits(2).of(number)).toEqual("12.34k");
|
|
83
|
+
});
|
|
84
|
+
it("with min", () => {
|
|
85
|
+
expect(format_1.format.short.min(20000).of(number)).toEqual("< 20.0000000k");
|
|
86
|
+
});
|
|
87
|
+
it("with max", () => {
|
|
88
|
+
expect(format_1.format.short.max(10000).of(number)).toEqual("> 10.0000000k");
|
|
89
|
+
});
|
|
90
|
+
it("with sign", () => {
|
|
91
|
+
expect(format_1.format.short.sign().of(number)).toEqual("+12.3456789k");
|
|
92
|
+
});
|
|
93
|
+
it("with unit", () => {
|
|
94
|
+
expect(format_1.format.short.unit("€").of(number)).toEqual("12.3456789k €");
|
|
95
|
+
});
|
|
96
|
+
it("without trailing zeros", () => {
|
|
97
|
+
expect(format_1.format.short.digits(8).of(number)).toEqual("12.34567890k");
|
|
98
|
+
expect(format_1.format.short.digits(8).removeTrailingZero().of(number)).toEqual("12.3456789k");
|
|
99
|
+
});
|
|
100
|
+
it("with small numbers with commas", () => {
|
|
101
|
+
expect(format_1.format.short.smallValuesWithCommas().of(number / 10)).toEqual("1,234.56789");
|
|
102
|
+
});
|
|
103
|
+
it("with locale", () => {
|
|
104
|
+
expect(format_1.format.short.locale("fr-FR").of(number)).toEqual("12,3456789k");
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
describe("should properly format bigint in short format", () => {
|
|
108
|
+
it("without option", () => {
|
|
109
|
+
expect(format_1.format.short.of(bigint, decimals)).toEqual("12.3456789k");
|
|
110
|
+
});
|
|
111
|
+
it("with digits", () => {
|
|
112
|
+
expect(format_1.format.short.digits(2).of(bigint, decimals)).toEqual("12.34k");
|
|
113
|
+
});
|
|
114
|
+
it("with min", () => {
|
|
115
|
+
expect(format_1.format.short.min(20000).of(bigint, decimals)).toEqual("< 20.0000000k");
|
|
116
|
+
});
|
|
117
|
+
it("with max", () => {
|
|
118
|
+
expect(format_1.format.short.max(10000).of(bigint, decimals)).toEqual("> 10.0000000k");
|
|
119
|
+
});
|
|
120
|
+
it("with sign", () => {
|
|
121
|
+
expect(format_1.format.short.sign().of(bigint, decimals)).toEqual("+12.3456789k");
|
|
122
|
+
});
|
|
123
|
+
it("with unit", () => {
|
|
124
|
+
expect(format_1.format.short.unit("€").of(bigint, decimals)).toEqual("12.3456789k €");
|
|
125
|
+
});
|
|
126
|
+
it("without trailing zeros", () => {
|
|
127
|
+
expect(format_1.format.short.digits(8).of(bigint, decimals)).toEqual("12.34567890k");
|
|
128
|
+
expect(format_1.format.short.digits(8).removeTrailingZero().of(bigint, decimals)).toEqual("12.3456789k");
|
|
129
|
+
});
|
|
130
|
+
it("with locale", () => {
|
|
131
|
+
expect(format_1.format.short.locale("fr-FR").of(bigint, decimals)).toEqual("12,3456789k");
|
|
132
|
+
});
|
|
133
|
+
it("with small numbers with commas", () => {
|
|
134
|
+
expect(format_1.format.short.smallValuesWithCommas().of(bigint, decimals + 1)).toEqual("1,234.56789");
|
|
135
|
+
});
|
|
136
|
+
it("with small numbers with commas with locale", () => {
|
|
137
|
+
expect(format_1.format.short
|
|
138
|
+
.smallValuesWithCommas()
|
|
139
|
+
.locale("fr-FR")
|
|
140
|
+
.of(bigint, decimals + 1)
|
|
141
|
+
// the correct space in fr-FR is narrow no-break space (U+202F)
|
|
142
|
+
).toEqual("1\u202F234,56789");
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
describe("commas", () => {
|
|
147
|
+
describe("should properly format number in commas format", () => {
|
|
148
|
+
it("without option", () => {
|
|
149
|
+
expect(format_1.format.commas.of(number)).toEqual("12,345.6789");
|
|
150
|
+
});
|
|
151
|
+
it("with digits", () => {
|
|
152
|
+
expect(format_1.format.commas.digits(2).of(number)).toEqual("12,345.67");
|
|
153
|
+
});
|
|
154
|
+
it("with min", () => {
|
|
155
|
+
expect(format_1.format.commas.min(20000).of(number)).toEqual("< 20,000.0000");
|
|
156
|
+
});
|
|
157
|
+
it("with max", () => {
|
|
158
|
+
expect(format_1.format.commas.max(10000).of(number)).toEqual("> 10,000.0000");
|
|
159
|
+
});
|
|
160
|
+
it("with sign", () => {
|
|
161
|
+
expect(format_1.format.commas.sign().of(number)).toEqual("+12,345.6789");
|
|
162
|
+
});
|
|
163
|
+
it("with unit", () => {
|
|
164
|
+
expect(format_1.format.commas.unit("ETH").of(number)).toEqual("12,345.6789 ETH");
|
|
165
|
+
});
|
|
166
|
+
it("without trailing zeros", () => {
|
|
167
|
+
expect(format_1.format.commas.digits(6).of(number)).toEqual("12,345.678900");
|
|
168
|
+
expect(format_1.format.commas.digits(6).removeTrailingZero().of(number)).toEqual("12,345.6789");
|
|
169
|
+
});
|
|
170
|
+
it("with locale", () => {
|
|
171
|
+
expect(format_1.format.commas.locale("fr-FR").of(number)).toEqual("12\u202F345,6789");
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
describe("should properly format bigint in commas format", () => {
|
|
175
|
+
it("without option", () => {
|
|
176
|
+
expect(format_1.format.commas.of(bigint, decimals)).toEqual("12,345.6789");
|
|
177
|
+
});
|
|
178
|
+
it("with digits", () => {
|
|
179
|
+
expect(format_1.format.commas.digits(2).of(bigint, decimals)).toEqual("12,345.67");
|
|
180
|
+
});
|
|
181
|
+
it("with min", () => {
|
|
182
|
+
expect(format_1.format.commas.min(20000).of(bigint, decimals)).toEqual("< 20,000.0000");
|
|
183
|
+
});
|
|
184
|
+
it("with max", () => {
|
|
185
|
+
expect(format_1.format.commas.max(10000).of(bigint, decimals)).toEqual("> 10,000.0000");
|
|
186
|
+
});
|
|
187
|
+
it("with sign", () => {
|
|
188
|
+
expect(format_1.format.commas.sign().of(bigint, decimals)).toEqual("+12,345.6789");
|
|
189
|
+
});
|
|
190
|
+
it("with unit", () => {
|
|
191
|
+
expect(format_1.format.commas.unit("ETH").of(bigint, decimals)).toEqual("12,345.6789 ETH");
|
|
192
|
+
});
|
|
193
|
+
it("without trailing zeros", () => {
|
|
194
|
+
expect(format_1.format.commas.digits(6).of(bigint, decimals)).toEqual("12,345.678900");
|
|
195
|
+
expect(format_1.format.commas.digits(6).removeTrailingZero().of(bigint, decimals)).toEqual("12,345.6789");
|
|
196
|
+
});
|
|
197
|
+
it("with locale", () => {
|
|
198
|
+
// the correct space in fr-FR is narrow no-break space (U+202F)
|
|
199
|
+
expect(format_1.format.commas.locale("fr-FR").of(bigint, decimals)).toEqual("12\u202F345,6789");
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
describe("percent", () => {
|
|
204
|
+
describe("should properly format number in percent format", () => {
|
|
205
|
+
it("without option", () => {
|
|
206
|
+
expect(format_1.format.percent.of(number)).toEqual("1234567.8900");
|
|
207
|
+
});
|
|
208
|
+
it("with digits", () => {
|
|
209
|
+
expect(format_1.format.percent.digits(1).of(number)).toEqual("1234567.8");
|
|
210
|
+
});
|
|
211
|
+
it("with min", () => {
|
|
212
|
+
expect(format_1.format.percent.min(20000).of(number)).toEqual("< 2000000.0000");
|
|
213
|
+
});
|
|
214
|
+
it("with max", () => {
|
|
215
|
+
expect(format_1.format.percent.max(10000).of(number)).toEqual("> 1000000.0000");
|
|
216
|
+
});
|
|
217
|
+
it("with sign", () => {
|
|
218
|
+
expect(format_1.format.percent.sign().of(number)).toEqual("+1234567.8900");
|
|
219
|
+
});
|
|
220
|
+
it("with unit", () => {
|
|
221
|
+
expect(format_1.format.percent.unit("%").of(number)).toEqual("1234567.8900%");
|
|
222
|
+
});
|
|
223
|
+
it("without trailing zeros", () => {
|
|
224
|
+
expect(format_1.format.percent.removeTrailingZero().of(number)).toEqual("1234567.89");
|
|
225
|
+
});
|
|
226
|
+
it("with locale", () => {
|
|
227
|
+
expect(format_1.format.percent.locale("fr-FR").of(number)).toEqual("1234567,8900");
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
describe("should properly format bigint in percent format", () => {
|
|
231
|
+
it("without option", () => {
|
|
232
|
+
expect(format_1.format.percent.of(bigint, decimals)).toEqual("1234567.8900");
|
|
233
|
+
});
|
|
234
|
+
it("with digits", () => {
|
|
235
|
+
expect(format_1.format.percent.digits(1).of(bigint, decimals)).toEqual("1234567.8");
|
|
236
|
+
});
|
|
237
|
+
it("with min", () => {
|
|
238
|
+
expect(format_1.format.percent.min(20000).of(bigint, decimals)).toEqual("< 2000000.0000");
|
|
239
|
+
});
|
|
240
|
+
it("with max", () => {
|
|
241
|
+
expect(format_1.format.percent.max(10000).of(bigint, decimals)).toEqual("> 1000000.0000");
|
|
242
|
+
});
|
|
243
|
+
it("with sign", () => {
|
|
244
|
+
expect(format_1.format.percent.sign().of(bigint, decimals)).toEqual("+1234567.8900");
|
|
245
|
+
});
|
|
246
|
+
it("with unit", () => {
|
|
247
|
+
expect(format_1.format.percent.unit("%").of(bigint, decimals)).toEqual("1234567.8900%");
|
|
248
|
+
});
|
|
249
|
+
it("without trailing zeros", () => {
|
|
250
|
+
expect(format_1.format.percent.removeTrailingZero().of(bigint, decimals)).toEqual("1234567.89");
|
|
251
|
+
});
|
|
252
|
+
it("with locale", () => {
|
|
253
|
+
expect(format_1.format.percent.locale("fr-FR").of(bigint, decimals)).toEqual("1234567,8900");
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
});
|
|
@@ -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,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;
|