@morpho-org/blue-sdk 1.0.1 → 1.0.2
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/package.json +10 -6
- package/src/addresses.ts +261 -0
- package/src/chain/chain.constants.ts +235 -0
- package/src/chain/chain.test.ts +51 -0
- package/src/chain/chain.types.ts +42 -0
- package/src/chain/chain.utils.ts +44 -0
- package/src/constants.ts +18 -0
- package/src/errors.ts +75 -0
- package/src/ethers/ethers.test.ts +17 -0
- package/src/ethers/safeGetAddress.ts +4 -0
- package/src/ethers/safeParseUnits.ts +29 -0
- package/src/evm.ts +172 -0
- package/src/helpers/format/format.test.ts +340 -0
- package/src/helpers/format/format.ts +416 -0
- package/src/helpers/getChecksumedAddress.ts +15 -0
- package/{lib/helpers/isZeroAddressOrUnset.d.ts → src/helpers/isZeroAddressOrUnset.ts} +7 -1
- package/src/helpers/locale.ts +108 -0
- package/src/holding/Holding.ts +109 -0
- package/src/market/Market.ts +479 -0
- package/src/market/MarketConfig.ts +108 -0
- package/src/market/MarketUtils.test.ts +25 -0
- package/src/market/MarketUtils.ts +467 -0
- package/src/maths/AdaptiveCurveIrmLib.ts +143 -0
- package/src/maths/MathLib.ts +208 -0
- package/src/maths/MathUtils.ts +31 -0
- package/src/maths/SharesMath.ts +40 -0
- package/src/notifications.ts +167 -0
- package/src/position/Position.ts +251 -0
- package/src/signatures/index.ts +18 -0
- package/src/signatures/manager.ts +50 -0
- package/src/signatures/permit.ts +126 -0
- package/src/signatures/permit2.ts +120 -0
- package/src/signatures/types.ts +18 -0
- package/src/signatures/utils.ts +83 -0
- package/src/tests/mocks/markets.ts +110 -0
- package/src/token/ERC20Metadata.ts +124 -0
- package/src/token/Token.ts +83 -0
- package/src/token/TokenNamespace.ts +76 -0
- package/src/token/WrappedToken.ts +142 -0
- package/src/types.ts +37 -0
- package/src/user/User.ts +32 -0
- package/src/user/user.types.ts +23 -0
- package/src/vault/Vault.ts +370 -0
- package/src/vault/VaultAllocation.ts +58 -0
- package/src/vault/VaultConfig.ts +55 -0
- package/src/vault/VaultUtils.ts +47 -0
- package/lib/addresses.d.ts +0 -168
- package/lib/addresses.js +0 -169
- package/lib/chain/chain.constants.d.ts +0 -3
- package/lib/chain/chain.constants.js +0 -232
- package/lib/chain/chain.types.d.ts +0 -20
- package/lib/chain/chain.types.js +0 -30
- package/lib/chain/chain.utils.d.ts +0 -14
- package/lib/chain/chain.utils.js +0 -30
- package/lib/chain/index.js +0 -18
- package/lib/constants.d.ts +0 -8
- package/lib/constants.js +0 -13
- package/lib/errors.d.ts +0 -37
- package/lib/errors.js +0 -71
- package/lib/ethers/index.js +0 -18
- package/lib/ethers/safeGetAddress.d.ts +0 -1
- package/lib/ethers/safeGetAddress.js +0 -6
- package/lib/ethers/safeParseUnits.d.ts +0 -2
- package/lib/ethers/safeParseUnits.js +0 -25
- package/lib/evm.d.ts +0 -36
- package/lib/evm.js +0 -113
- package/lib/helpers/format/format.d.ts +0 -98
- package/lib/helpers/format/format.js +0 -301
- package/lib/helpers/format/index.js +0 -17
- package/lib/helpers/getChecksumedAddress.d.ts +0 -7
- package/lib/helpers/getChecksumedAddress.js +0 -17
- package/lib/helpers/index.js +0 -20
- package/lib/helpers/isZeroAddressOrUnset.js +0 -14
- package/lib/helpers/locale.d.ts +0 -36
- package/lib/helpers/locale.js +0 -86
- package/lib/holding/Holding.d.ts +0 -60
- package/lib/holding/Holding.js +0 -31
- package/lib/holding/index.js +0 -17
- package/lib/index.d.ts +0 -33
- package/lib/index.js +0 -62
- package/lib/market/Market.d.ts +0 -159
- package/lib/market/Market.js +0 -240
- package/lib/market/MarketConfig.d.ts +0 -44
- package/lib/market/MarketConfig.js +0 -56
- package/lib/market/MarketUtils.d.ts +0 -165
- package/lib/market/MarketUtils.js +0 -182
- package/lib/market/index.js +0 -19
- package/lib/maths/AdaptiveCurveIrmLib.d.ts +0 -37
- package/lib/maths/AdaptiveCurveIrmLib.js +0 -116
- package/lib/maths/MathLib.d.ts +0 -94
- package/lib/maths/MathLib.js +0 -153
- package/lib/maths/MathUtils.d.ts +0 -15
- package/lib/maths/MathUtils.js +0 -33
- package/lib/maths/SharesMath.d.ts +0 -12
- package/lib/maths/SharesMath.js +0 -22
- package/lib/maths/index.js +0 -20
- package/lib/notifications.d.ts +0 -98
- package/lib/notifications.js +0 -52
- package/lib/position/Position.d.ts +0 -118
- package/lib/position/Position.js +0 -145
- package/lib/position/index.js +0 -17
- package/lib/signatures/index.d.ts +0 -12
- package/lib/signatures/index.js +0 -39
- package/lib/signatures/manager.d.ts +0 -10
- package/lib/signatures/manager.js +0 -37
- package/lib/signatures/permit.d.ts +0 -21
- package/lib/signatures/permit.js +0 -101
- package/lib/signatures/permit2.d.ts +0 -20
- package/lib/signatures/permit2.js +0 -91
- package/lib/signatures/types.d.ts +0 -13
- package/lib/signatures/types.js +0 -2
- package/lib/signatures/utils.d.ts +0 -6
- package/lib/signatures/utils.js +0 -44
- package/lib/tests/mocks/markets.d.ts +0 -17
- package/lib/tests/mocks/markets.js +0 -108
- package/lib/token/ERC20Metadata.d.ts +0 -249
- package/lib/token/ERC20Metadata.js +0 -81
- package/lib/token/Token.d.ts +0 -45
- package/lib/token/Token.js +0 -39
- package/lib/token/TokenNamespace.d.ts +0 -18
- package/lib/token/TokenNamespace.js +0 -55
- package/lib/token/WrappedToken.d.ts +0 -42
- package/lib/token/WrappedToken.js +0 -87
- package/lib/token/index.js +0 -18
- package/lib/types.d.ts +0 -29
- package/lib/types.js +0 -23
- package/lib/user/User.d.ts +0 -20
- package/lib/user/User.js +0 -11
- package/lib/user/index.js +0 -18
- package/lib/user/user.types.d.ts +0 -18
- package/lib/user/user.types.js +0 -2
- package/lib/vault/Vault.d.ts +0 -167
- package/lib/vault/Vault.js +0 -156
- package/lib/vault/VaultAllocation.d.ts +0 -38
- package/lib/vault/VaultAllocation.js +0 -18
- package/lib/vault/VaultConfig.d.ts +0 -23
- package/lib/vault/VaultConfig.js +0 -26
- package/lib/vault/VaultUtils.d.ts +0 -17
- package/lib/vault/VaultUtils.js +0 -17
- package/lib/vault/index.js +0 -20
- /package/{lib/chain/index.d.ts → src/chain/index.ts} +0 -0
- /package/{lib/ethers/index.d.ts → src/ethers/index.ts} +0 -0
- /package/{lib/helpers/format/index.d.ts → src/helpers/format/index.ts} +0 -0
- /package/{lib/helpers/index.d.ts → src/helpers/index.ts} +0 -0
- /package/{lib/holding/index.d.ts → src/holding/index.ts} +0 -0
- /package/{lib/market/index.d.ts → src/market/index.ts} +0 -0
- /package/{lib/maths/index.d.ts → src/maths/index.ts} +0 -0
- /package/{lib/position/index.d.ts → src/position/index.ts} +0 -0
- /package/{lib/token/index.d.ts → src/token/index.ts} +0 -0
- /package/{lib/user/index.d.ts → src/user/index.ts} +0 -0
- /package/{lib/vault/index.d.ts → src/vault/index.ts} +0 -0
package/lib/ethers/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
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("./safeGetAddress"), exports);
|
|
18
|
-
__exportStar(require("./safeParseUnits"), exports);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const safeGetAddress: (address: string) => string;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.safeGetAddress = void 0;
|
|
4
|
-
const ethers_1 = require("ethers");
|
|
5
|
-
const safeGetAddress = (address) => (0, ethers_1.getAddress)(address.toLowerCase());
|
|
6
|
-
exports.safeGetAddress = safeGetAddress;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.safeParseNumber = exports.safeParseUnits = void 0;
|
|
4
|
-
const ethers_1 = require("ethers");
|
|
5
|
-
const safeParseUnits = (strValue, decimals = 18) => {
|
|
6
|
-
if (!/[-+]?[0-9]*\.?[0-9]+/.test(strValue))
|
|
7
|
-
throw new Error("invalid number: " + strValue);
|
|
8
|
-
let [whole, dec = ""] = strValue.split(".");
|
|
9
|
-
dec = dec.slice(0, decimals);
|
|
10
|
-
return (0, ethers_1.parseUnits)([whole || "0", dec].filter((v) => v.length > 0).join("."), decimals);
|
|
11
|
-
};
|
|
12
|
-
exports.safeParseUnits = safeParseUnits;
|
|
13
|
-
// Alternative to Number.toFixed that doesn't use scientific notation for excessively small or large numbers
|
|
14
|
-
function toFixed(x, decimals) {
|
|
15
|
-
return new Intl.NumberFormat("en-US", {
|
|
16
|
-
style: "decimal",
|
|
17
|
-
useGrouping: false,
|
|
18
|
-
maximumFractionDigits: decimals,
|
|
19
|
-
minimumFractionDigits: decimals,
|
|
20
|
-
}).format(x);
|
|
21
|
-
}
|
|
22
|
-
function safeParseNumber(value, decimals = 18) {
|
|
23
|
-
return (0, exports.safeParseUnits)(toFixed(value, decimals), decimals);
|
|
24
|
-
}
|
|
25
|
-
exports.safeParseNumber = safeParseNumber;
|
package/lib/evm.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { TransactionRequest, Signer } from "ethers";
|
|
2
|
-
import { ChainId } from "./chain";
|
|
3
|
-
import { NotificationProducer, NotificationTopic } from "./notifications";
|
|
4
|
-
import { SignatureMessage } from "./signatures/types";
|
|
5
|
-
export interface NotificationOptions<Topic extends NotificationTopic> {
|
|
6
|
-
producer: NotificationProducer<Topic>;
|
|
7
|
-
id: string;
|
|
8
|
-
args: Record<PropertyKey, any>;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Sends a transaction via the provided signer, optionnally waiting for the corresponding transaction receipt.
|
|
12
|
-
* @param signer The signer to send the tx with.
|
|
13
|
-
* @param tx The transaction request.
|
|
14
|
-
* @param wait Whether to wait for the transaction receipt. Defaults to true.
|
|
15
|
-
* @param notificationOptions The optional notification options. Warning: if `wait` is set to true, the provided topic will never complete.
|
|
16
|
-
*/
|
|
17
|
-
export declare const sendTransactionWithProducer: (signer: Signer, req: TransactionRequest, defaultGasLimit?: bigint, confirms?: number, notificationOptions?: NotificationOptions<NotificationTopic.tx>) => Promise<void>;
|
|
18
|
-
/**
|
|
19
|
-
* Sends a transaction via the provided signer, optionnally waiting for the corresponding transaction receipt.
|
|
20
|
-
* @param signer The signer to send the tx with.
|
|
21
|
-
* @param tx The transaction request.
|
|
22
|
-
* @param wait Whether to wait for the transaction receipt. Defaults to true.
|
|
23
|
-
* @param topic The type of notifications to emit.
|
|
24
|
-
* @param args The optional notification arguments.
|
|
25
|
-
* @return The notification consumer which receives the transaction notifications.
|
|
26
|
-
*/
|
|
27
|
-
export declare const sendTransaction: (signer: Signer, tx: TransactionRequest, defaultGasLimit?: bigint, confirms?: number, args?: Record<PropertyKey, any>) => import("./notifications").NotificationConsumer<NotificationTopic.tx>;
|
|
28
|
-
export declare const signMessageWithProducer: (signer: Signer, message: SignatureMessage, notificationOptions?: NotificationOptions<NotificationTopic.signature>) => Promise<void>;
|
|
29
|
-
/**
|
|
30
|
-
* Sends a signature request to the provided signer.
|
|
31
|
-
* @param signer The signer to send the approval with.
|
|
32
|
-
* @param args The approval parameters.
|
|
33
|
-
* @param encodeMessage The approval encoder to pass the approval parameters to get the approval transactions.
|
|
34
|
-
* @return The notification consumer which receives the transaction notifications.
|
|
35
|
-
*/
|
|
36
|
-
export declare const signMessage: <T extends object>(signer: Signer, args: T, encodeMessage: (args: T, chainId: ChainId) => SignatureMessage, chainId: ChainId) => import("./notifications").NotificationConsumer<NotificationTopic.signature>;
|
package/lib/evm.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.signMessage = exports.signMessageWithProducer = exports.sendTransaction = exports.sendTransactionWithProducer = void 0;
|
|
4
|
-
const ethers_1 = require("ethers");
|
|
5
|
-
const maths_1 = require("./maths");
|
|
6
|
-
const notifications_1 = require("./notifications");
|
|
7
|
-
const signatures_1 = require("./signatures");
|
|
8
|
-
/**
|
|
9
|
-
* Sends a transaction via the provided signer, optionnally waiting for the corresponding transaction receipt.
|
|
10
|
-
* @param signer The signer to send the tx with.
|
|
11
|
-
* @param tx The transaction request.
|
|
12
|
-
* @param wait Whether to wait for the transaction receipt. Defaults to true.
|
|
13
|
-
* @param notificationOptions The optional notification options. Warning: if `wait` is set to true, the provided topic will never complete.
|
|
14
|
-
*/
|
|
15
|
-
const sendTransactionWithProducer = async (signer, req, defaultGasLimit, confirms = 1, notificationOptions) => {
|
|
16
|
-
let response;
|
|
17
|
-
try {
|
|
18
|
-
let gasLimit;
|
|
19
|
-
try {
|
|
20
|
-
gasLimit = maths_1.MathLib.wMulUp(await signer.estimateGas(req), (0, ethers_1.parseUnits)("1.1"));
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
if (defaultGasLimit == null)
|
|
24
|
-
throw error;
|
|
25
|
-
gasLimit = defaultGasLimit;
|
|
26
|
-
}
|
|
27
|
-
const tx = { ...req, gasLimit };
|
|
28
|
-
notificationOptions?.producer.next({
|
|
29
|
-
id: notificationOptions.id,
|
|
30
|
-
status: notifications_1.NotificationStatus.signing,
|
|
31
|
-
context: { tx },
|
|
32
|
-
});
|
|
33
|
-
response = await signer.sendTransaction(tx);
|
|
34
|
-
notificationOptions?.producer.next({
|
|
35
|
-
id: notificationOptions.id,
|
|
36
|
-
status: notifications_1.NotificationStatus.pending,
|
|
37
|
-
context: { args: notificationOptions.args, tx, response },
|
|
38
|
-
});
|
|
39
|
-
const receipt = await response.wait(confirms);
|
|
40
|
-
notificationOptions?.producer.next({
|
|
41
|
-
id: notificationOptions.id,
|
|
42
|
-
status: notifications_1.NotificationStatus.success,
|
|
43
|
-
context: { args: notificationOptions.args, tx, response, receipt },
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
notificationOptions?.producer.next({
|
|
48
|
-
id: notificationOptions.id,
|
|
49
|
-
status: notifications_1.NotificationStatus.error,
|
|
50
|
-
context: { args: notificationOptions.args, tx: req, response, error },
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
exports.sendTransactionWithProducer = sendTransactionWithProducer;
|
|
55
|
-
/**
|
|
56
|
-
* Sends a transaction via the provided signer, optionnally waiting for the corresponding transaction receipt.
|
|
57
|
-
* @param signer The signer to send the tx with.
|
|
58
|
-
* @param tx The transaction request.
|
|
59
|
-
* @param wait Whether to wait for the transaction receipt. Defaults to true.
|
|
60
|
-
* @param topic The type of notifications to emit.
|
|
61
|
-
* @param args The optional notification arguments.
|
|
62
|
-
* @return The notification consumer which receives the transaction notifications.
|
|
63
|
-
*/
|
|
64
|
-
const sendTransaction = (signer, tx, defaultGasLimit, confirms, args = {}) => {
|
|
65
|
-
const producer = new notifications_1.NotificationProducer(notifications_1.NotificationTopic.tx);
|
|
66
|
-
(0, exports.sendTransactionWithProducer)(signer, tx, defaultGasLimit, confirms, {
|
|
67
|
-
producer,
|
|
68
|
-
id: Date.now().toString(),
|
|
69
|
-
args,
|
|
70
|
-
});
|
|
71
|
-
return producer.consumer;
|
|
72
|
-
};
|
|
73
|
-
exports.sendTransaction = sendTransaction;
|
|
74
|
-
const signMessageWithProducer = async (signer, message, notificationOptions) => {
|
|
75
|
-
notificationOptions?.producer.next({
|
|
76
|
-
id: notificationOptions.id,
|
|
77
|
-
status: notifications_1.NotificationStatus.signing,
|
|
78
|
-
});
|
|
79
|
-
try {
|
|
80
|
-
const signature = await signatures_1.SignatureUtils.safeSignTypedData(signer, message.data.domain, message.data.types, message.data.value);
|
|
81
|
-
signatures_1.SignatureUtils.verifySignature(signature, message.hash, await signer.getAddress());
|
|
82
|
-
notificationOptions?.producer.next({
|
|
83
|
-
id: notificationOptions.id,
|
|
84
|
-
status: notifications_1.NotificationStatus.success,
|
|
85
|
-
context: { message, args: notificationOptions.args, signature },
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
catch (error) {
|
|
89
|
-
notificationOptions?.producer.next({
|
|
90
|
-
id: notificationOptions.id,
|
|
91
|
-
status: notifications_1.NotificationStatus.error,
|
|
92
|
-
context: { message, args: notificationOptions.args, error },
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
exports.signMessageWithProducer = signMessageWithProducer;
|
|
97
|
-
/**
|
|
98
|
-
* Sends a signature request to the provided signer.
|
|
99
|
-
* @param signer The signer to send the approval with.
|
|
100
|
-
* @param args The approval parameters.
|
|
101
|
-
* @param encodeMessage The approval encoder to pass the approval parameters to get the approval transactions.
|
|
102
|
-
* @return The notification consumer which receives the transaction notifications.
|
|
103
|
-
*/
|
|
104
|
-
const signMessage = (signer, args, encodeMessage, chainId) => {
|
|
105
|
-
const producer = new notifications_1.NotificationProducer(notifications_1.NotificationTopic.signature);
|
|
106
|
-
(0, exports.signMessageWithProducer)(signer, encodeMessage(args, chainId), {
|
|
107
|
-
producer,
|
|
108
|
-
id: Date.now().toString(),
|
|
109
|
-
args,
|
|
110
|
-
});
|
|
111
|
-
return producer.consumer;
|
|
112
|
-
};
|
|
113
|
-
exports.signMessage = signMessage;
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
declare enum Format {
|
|
2
|
-
number = "number",
|
|
3
|
-
commas = "commas",
|
|
4
|
-
short = "short",
|
|
5
|
-
hex = "hex",
|
|
6
|
-
percent = "percent"
|
|
7
|
-
}
|
|
8
|
-
interface BaseFormatOptions {
|
|
9
|
-
format: Format;
|
|
10
|
-
digits?: number;
|
|
11
|
-
removeTrailingZero?: boolean;
|
|
12
|
-
min?: number;
|
|
13
|
-
max?: number;
|
|
14
|
-
sign?: boolean;
|
|
15
|
-
unit?: string;
|
|
16
|
-
readable?: boolean;
|
|
17
|
-
locale?: string;
|
|
18
|
-
}
|
|
19
|
-
interface FormatShortOptions extends BaseFormatOptions {
|
|
20
|
-
format: Format.short;
|
|
21
|
-
smallValuesWithCommas?: boolean;
|
|
22
|
-
}
|
|
23
|
-
interface FormatHexOptions {
|
|
24
|
-
format: Format.hex;
|
|
25
|
-
}
|
|
26
|
-
interface FormatCommasOptions extends BaseFormatOptions {
|
|
27
|
-
format: Format.commas;
|
|
28
|
-
}
|
|
29
|
-
interface FormatNumberOptions extends BaseFormatOptions {
|
|
30
|
-
format: Format.number;
|
|
31
|
-
}
|
|
32
|
-
interface FormatPercentOptions extends BaseFormatOptions {
|
|
33
|
-
format: Format.percent;
|
|
34
|
-
}
|
|
35
|
-
type FormatOptions = FormatHexOptions | FormatShortOptions | FormatNumberOptions | FormatCommasOptions | FormatPercentOptions;
|
|
36
|
-
declare global {
|
|
37
|
-
interface String {
|
|
38
|
-
insert(index: number, substr: string, fillWith?: string): string;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export declare abstract class BaseFormatter {
|
|
42
|
-
protected abstract _options: FormatOptions;
|
|
43
|
-
constructor();
|
|
44
|
-
of(value: bigint, decimals: number): string;
|
|
45
|
-
of(value: number): string;
|
|
46
|
-
}
|
|
47
|
-
export declare class HexFormatter extends BaseFormatter {
|
|
48
|
-
protected _options: {
|
|
49
|
-
format: Format;
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
export declare abstract class CommonFormatter extends BaseFormatter {
|
|
53
|
-
protected abstract _options: BaseFormatOptions;
|
|
54
|
-
digits(_d: number): this;
|
|
55
|
-
removeTrailingZero(): this;
|
|
56
|
-
readable(): this;
|
|
57
|
-
min(_m: number): this;
|
|
58
|
-
max(_m: number): this;
|
|
59
|
-
sign(): this;
|
|
60
|
-
unit(_u: string): this;
|
|
61
|
-
locale(_l: string): this;
|
|
62
|
-
}
|
|
63
|
-
export declare class NumberFormatter extends CommonFormatter {
|
|
64
|
-
protected _options: FormatNumberOptions;
|
|
65
|
-
}
|
|
66
|
-
export declare class CommasFormatter extends CommonFormatter {
|
|
67
|
-
protected _options: FormatCommasOptions;
|
|
68
|
-
}
|
|
69
|
-
export declare class ShortFormatter extends CommonFormatter {
|
|
70
|
-
protected _options: FormatShortOptions;
|
|
71
|
-
smallValuesWithCommas(): this;
|
|
72
|
-
}
|
|
73
|
-
export declare class PercentFormatter extends CommonFormatter {
|
|
74
|
-
protected _options: FormatPercentOptions;
|
|
75
|
-
}
|
|
76
|
-
export declare const format: {
|
|
77
|
-
/**
|
|
78
|
-
* Return the value as an integer in hex format
|
|
79
|
-
*/
|
|
80
|
-
readonly hex: HexFormatter;
|
|
81
|
-
/**
|
|
82
|
-
* Return the value as a stringified number (12345.6789)
|
|
83
|
-
*/
|
|
84
|
-
readonly number: NumberFormatter;
|
|
85
|
-
/**
|
|
86
|
-
* Return the value as a commas-separated stringified number (12,345.6789)
|
|
87
|
-
*/
|
|
88
|
-
readonly commas: CommasFormatter;
|
|
89
|
-
/**
|
|
90
|
-
* Return the value as a shorted stringified number (12.3456789k)
|
|
91
|
-
*/
|
|
92
|
-
readonly short: ShortFormatter;
|
|
93
|
-
/**
|
|
94
|
-
* Return the value as a percent based stringified number (10.00 instead of 0.1)
|
|
95
|
-
*/
|
|
96
|
-
readonly percent: PercentFormatter;
|
|
97
|
-
};
|
|
98
|
-
export {};
|
|
@@ -1,301 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
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);
|
|
@@ -1,17 +0,0 @@
|
|
|
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;
|
package/lib/helpers/index.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
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);
|