@morpho-org/midnight-sdk 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +56 -46
- package/lib/cjs/abis.d.ts +1185 -996
- package/lib/cjs/abis.js +1445 -1211
- package/lib/cjs/api/MidnightApi.d.ts +22 -16
- package/lib/cjs/api/MidnightApi.js +22 -16
- package/lib/cjs/api/helpers.js +6 -3
- package/lib/cjs/api/types.d.ts +35 -9
- package/lib/cjs/constants.d.ts +12 -11
- package/lib/cjs/constants.js +14 -12
- package/lib/cjs/errors.d.ts +74 -1
- package/lib/cjs/errors.js +94 -3
- package/lib/cjs/fetch/Market.js +0 -1
- package/lib/cjs/fetch/index.d.ts +0 -1
- package/lib/cjs/fetch/index.js +0 -1
- package/lib/cjs/index.d.ts +1 -1
- package/lib/cjs/index.js +2 -23
- package/lib/cjs/market/Market.d.ts +99 -33
- package/lib/cjs/market/Market.js +137 -28
- package/lib/cjs/market/MarketUtils.d.ts +61 -41
- package/lib/cjs/market/MarketUtils.js +108 -48
- package/lib/cjs/market/Position.d.ts +16 -11
- package/lib/cjs/market/Position.js +18 -12
- package/lib/cjs/market/PositionUtils.d.ts +6 -4
- package/lib/cjs/market/PositionUtils.js +9 -6
- package/lib/cjs/math/TickLib.d.ts +22 -0
- package/lib/cjs/math/TickLib.js +30 -0
- package/lib/cjs/math/index.d.ts +0 -1
- package/lib/cjs/math/index.js +0 -1
- package/lib/cjs/offers/Offer.d.ts +217 -11
- package/lib/cjs/offers/Offer.js +215 -7
- package/lib/cjs/offers/OfferUtils.d.ts +245 -139
- package/lib/cjs/offers/OfferUtils.js +304 -36
- package/lib/cjs/queries/GetPosition.d.ts +1 -1
- package/lib/cjs/queries/GetPosition.js +1 -1
- package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +66 -38
- package/lib/cjs/signatures/EcrecoverRatifierUtils.js +147 -79
- package/lib/cjs/signatures/Group.d.ts +12 -4
- package/lib/cjs/signatures/Group.js +12 -4
- package/lib/cjs/signatures/GroupUtils.d.ts +12 -4
- package/lib/cjs/signatures/GroupUtils.js +6 -2
- package/lib/cjs/signatures/Payload.d.ts +277 -277
- package/lib/cjs/signatures/Payload.js +517 -534
- package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
- package/lib/cjs/signatures/RatifierUtils.js +94 -0
- package/lib/cjs/signatures/SetterRatifierUtils.d.ts +21 -13
- package/lib/cjs/signatures/SetterRatifierUtils.js +25 -20
- package/lib/cjs/signatures/Tree.d.ts +28 -16
- package/lib/cjs/signatures/Tree.js +25 -13
- package/lib/cjs/signatures/TreeUtils.d.ts +181 -62
- package/lib/cjs/signatures/TreeUtils.js +73 -76
- package/lib/cjs/signatures/index.d.ts +1 -0
- package/lib/cjs/signatures/index.js +3 -0
- package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
- package/lib/cjs/signatures/offerStructInternal.js +72 -0
- package/lib/esm/abis.d.ts +1185 -996
- package/lib/esm/abis.js +1445 -1211
- package/lib/esm/api/MidnightApi.d.ts +22 -16
- package/lib/esm/api/MidnightApi.js +23 -17
- package/lib/esm/api/helpers.js +6 -3
- package/lib/esm/api/types.d.ts +35 -9
- package/lib/esm/constants.d.ts +12 -11
- package/lib/esm/constants.js +13 -11
- package/lib/esm/errors.d.ts +74 -1
- package/lib/esm/errors.js +89 -2
- package/lib/esm/fetch/Market.js +0 -1
- package/lib/esm/fetch/index.d.ts +0 -1
- package/lib/esm/fetch/index.js +0 -1
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/market/Market.d.ts +99 -33
- package/lib/esm/market/Market.js +137 -28
- package/lib/esm/market/MarketUtils.d.ts +61 -41
- package/lib/esm/market/MarketUtils.js +109 -49
- package/lib/esm/market/Position.d.ts +16 -11
- package/lib/esm/market/Position.js +19 -13
- package/lib/esm/market/PositionUtils.d.ts +6 -4
- package/lib/esm/market/PositionUtils.js +9 -6
- package/lib/esm/math/TickLib.d.ts +22 -0
- package/lib/esm/math/TickLib.js +31 -1
- package/lib/esm/math/index.d.ts +0 -1
- package/lib/esm/math/index.js +0 -1
- package/lib/esm/offers/Offer.d.ts +217 -11
- package/lib/esm/offers/Offer.js +216 -8
- package/lib/esm/offers/OfferUtils.d.ts +245 -139
- package/lib/esm/offers/OfferUtils.js +306 -38
- package/lib/esm/queries/GetPosition.d.ts +1 -1
- package/lib/esm/queries/GetPosition.js +1 -1
- package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +66 -38
- package/lib/esm/signatures/EcrecoverRatifierUtils.js +149 -81
- package/lib/esm/signatures/Group.d.ts +12 -4
- package/lib/esm/signatures/Group.js +12 -4
- package/lib/esm/signatures/GroupUtils.d.ts +12 -4
- package/lib/esm/signatures/GroupUtils.js +6 -2
- package/lib/esm/signatures/Payload.d.ts +277 -277
- package/lib/esm/signatures/Payload.js +517 -531
- package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
- package/lib/esm/signatures/RatifierUtils.js +94 -0
- package/lib/esm/signatures/SetterRatifierUtils.d.ts +21 -13
- package/lib/esm/signatures/SetterRatifierUtils.js +25 -20
- package/lib/esm/signatures/Tree.d.ts +28 -16
- package/lib/esm/signatures/Tree.js +25 -13
- package/lib/esm/signatures/TreeUtils.d.ts +181 -62
- package/lib/esm/signatures/TreeUtils.js +75 -78
- package/lib/esm/signatures/index.d.ts +1 -0
- package/lib/esm/signatures/index.js +1 -0
- package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
- package/lib/esm/signatures/offerStructInternal.js +67 -0
- package/package.json +28 -32
- package/src/index.ts +1 -1
- package/lib/cjs/fetch/Offer.d.ts +0 -74
- package/lib/cjs/fetch/Offer.js +0 -119
- package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
- package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
- package/lib/esm/fetch/Offer.d.ts +0 -74
- package/lib/esm/fetch/Offer.js +0 -116
- package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
- package/lib/esm/math/ConsumableUnitsLib.js +0 -74
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
import { assertNonNegative,
|
|
1
|
+
import { assertNonNegative, MathLib, } from "@morpho-org/morpho-ts";
|
|
2
2
|
import { concat, encodeAbiParameters, encodePacked, keccak256 } from "viem";
|
|
3
3
|
import { CBP, COLLATERAL_PARAMS_TYPEHASH, MARKET_TYPEHASH, SETTLEMENT_FEE_BREAKPOINTS, } from "../constants.js";
|
|
4
|
+
import { InvalidMarketParameterError, UnknownCollateralIndexError, } from "../errors.js";
|
|
4
5
|
import { MarketParams, } from "./Market.js";
|
|
5
6
|
const SSTORE2_PREFIX = "0x600b380380600b5f395ff3";
|
|
6
7
|
const collateralParamsHashParams = [
|
|
7
8
|
{ name: "typehash", type: "bytes32" },
|
|
8
9
|
{ name: "token", type: "address" },
|
|
9
10
|
{ name: "lltv", type: "uint256" },
|
|
10
|
-
{ name: "
|
|
11
|
+
{ name: "liquidationCursor", type: "uint256" },
|
|
11
12
|
{ name: "oracle", type: "address" },
|
|
12
13
|
];
|
|
13
14
|
const marketHashParams = [
|
|
14
15
|
{ name: "typehash", type: "bytes32" },
|
|
16
|
+
{ name: "chainId", type: "uint256" },
|
|
17
|
+
{ name: "midnight", type: "address" },
|
|
15
18
|
{ name: "loanToken", type: "address" },
|
|
16
19
|
{ name: "collateralParamsHash", type: "bytes32" },
|
|
17
20
|
{ name: "maturity", type: "uint256" },
|
|
@@ -22,6 +25,8 @@ const marketHashParams = [
|
|
|
22
25
|
const marketParamsAbiParameter = {
|
|
23
26
|
type: "tuple",
|
|
24
27
|
components: [
|
|
28
|
+
{ name: "chainId", type: "uint256" },
|
|
29
|
+
{ name: "midnight", type: "address" },
|
|
25
30
|
{ name: "loanToken", type: "address" },
|
|
26
31
|
{
|
|
27
32
|
name: "collateralParams",
|
|
@@ -29,7 +34,7 @@ const marketParamsAbiParameter = {
|
|
|
29
34
|
components: [
|
|
30
35
|
{ name: "token", type: "address" },
|
|
31
36
|
{ name: "lltv", type: "uint256" },
|
|
32
|
-
{ name: "
|
|
37
|
+
{ name: "liquidationCursor", type: "uint256" },
|
|
33
38
|
{ name: "oracle", type: "address" },
|
|
34
39
|
],
|
|
35
40
|
},
|
|
@@ -56,7 +61,7 @@ export var MarketUtils;
|
|
|
56
61
|
*
|
|
57
62
|
* @param params.token - Collateral token address.
|
|
58
63
|
* @param params.lltv - WAD-scaled liquidation loan-to-value.
|
|
59
|
-
* @param params.
|
|
64
|
+
* @param params.liquidationCursor - WAD-scaled liquidation cursor.
|
|
60
65
|
* @param params.oracle - Oracle address used to price this collateral.
|
|
61
66
|
* @returns Collateral params with bigint fields.
|
|
62
67
|
* @example
|
|
@@ -66,17 +71,31 @@ export var MarketUtils;
|
|
|
66
71
|
* const collateral = MarketUtils.toCollateralParams({
|
|
67
72
|
* token: "0x0000000000000000000000000000000000000001",
|
|
68
73
|
* lltv: 770000000000000000n,
|
|
69
|
-
*
|
|
74
|
+
* liquidationCursor: 250000000000000000n,
|
|
70
75
|
* oracle: "0x0000000000000000000000000000000000000002",
|
|
71
76
|
* });
|
|
72
|
-
* console.log(collateral.
|
|
77
|
+
* console.log(collateral.liquidationCursor);
|
|
73
78
|
* ```
|
|
74
79
|
*/
|
|
75
80
|
function toCollateralParams(params) {
|
|
81
|
+
let lltv;
|
|
82
|
+
let liquidationCursor;
|
|
83
|
+
try {
|
|
84
|
+
lltv = BigInt(params.lltv);
|
|
85
|
+
liquidationCursor = BigInt(params.liquidationCursor);
|
|
86
|
+
}
|
|
87
|
+
catch (cause) {
|
|
88
|
+
throw new InvalidMarketParameterError({
|
|
89
|
+
parameter: "collateralParams",
|
|
90
|
+
value: params,
|
|
91
|
+
instruction: "Provide bigint-compatible lltv and liquidationCursor values.",
|
|
92
|
+
cause,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
76
95
|
return {
|
|
77
96
|
token: params.token,
|
|
78
|
-
lltv
|
|
79
|
-
|
|
97
|
+
lltv,
|
|
98
|
+
liquidationCursor,
|
|
80
99
|
oracle: params.oracle,
|
|
81
100
|
};
|
|
82
101
|
}
|
|
@@ -91,12 +110,14 @@ export var MarketUtils;
|
|
|
91
110
|
* import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
92
111
|
*
|
|
93
112
|
* const struct = MarketUtils.toStruct({
|
|
113
|
+
* chainId: 8453,
|
|
114
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
94
115
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
95
116
|
* collateralParams: [
|
|
96
117
|
* {
|
|
97
118
|
* token: "0x0000000000000000000000000000000000007000",
|
|
98
119
|
* lltv: 770000000000000000n,
|
|
99
|
-
*
|
|
120
|
+
* liquidationCursor: 250000000000000000n,
|
|
100
121
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
101
122
|
* },
|
|
102
123
|
* ],
|
|
@@ -111,11 +132,13 @@ export var MarketUtils;
|
|
|
111
132
|
function toStruct(market) {
|
|
112
133
|
const params = MarketParams.from(market);
|
|
113
134
|
return {
|
|
135
|
+
chainId: params.chainId,
|
|
136
|
+
midnight: params.midnight,
|
|
114
137
|
loanToken: params.loanToken,
|
|
115
138
|
collateralParams: params.collateralParams.map((collateral) => ({
|
|
116
139
|
token: collateral.token,
|
|
117
140
|
lltv: collateral.lltv,
|
|
118
|
-
|
|
141
|
+
liquidationCursor: collateral.liquidationCursor,
|
|
119
142
|
oracle: collateral.oracle,
|
|
120
143
|
})),
|
|
121
144
|
maturity: params.maturity,
|
|
@@ -125,19 +148,66 @@ export var MarketUtils;
|
|
|
125
148
|
};
|
|
126
149
|
}
|
|
127
150
|
MarketUtils.toStruct = toStruct;
|
|
151
|
+
/**
|
|
152
|
+
* Returns configured collateral by index.
|
|
153
|
+
*
|
|
154
|
+
* @param market - Market params or hydrated market.
|
|
155
|
+
* @param index - Collateral index to look up.
|
|
156
|
+
* @returns Collateral entry for the configured index.
|
|
157
|
+
* @throws {UnknownCollateralIndexError} when the index is not configured.
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
161
|
+
*
|
|
162
|
+
* const collateral = MarketUtils.getCollateralByIndex({
|
|
163
|
+
* chainId: 8453,
|
|
164
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
165
|
+
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
166
|
+
* collateralParams: [
|
|
167
|
+
* {
|
|
168
|
+
* token: "0x0000000000000000000000000000000000000002",
|
|
169
|
+
* lltv: 770000000000000000n,
|
|
170
|
+
* liquidationCursor: 250000000000000000n,
|
|
171
|
+
* oracle: "0x0000000000000000000000000000000000000003",
|
|
172
|
+
* },
|
|
173
|
+
* ],
|
|
174
|
+
* maturity: 1n,
|
|
175
|
+
* rcfThreshold: 0n,
|
|
176
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
177
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
178
|
+
* }, 0n);
|
|
179
|
+
* console.log(collateral.token);
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
function getCollateralByIndex(market, index) {
|
|
183
|
+
const normalizedIndex = BigInt(index);
|
|
184
|
+
const params = MarketParams.from(market);
|
|
185
|
+
const collateral = normalizedIndex >= 0n &&
|
|
186
|
+
normalizedIndex <= BigInt(Number.MAX_SAFE_INTEGER)
|
|
187
|
+
? params.collateralParams[Number(normalizedIndex)]
|
|
188
|
+
: undefined;
|
|
189
|
+
if (collateral == null) {
|
|
190
|
+
throw new UnknownCollateralIndexError({
|
|
191
|
+
market: toId(params),
|
|
192
|
+
collateralIndex: normalizedIndex,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
return collateral;
|
|
196
|
+
}
|
|
197
|
+
MarketUtils.getCollateralByIndex = getCollateralByIndex;
|
|
128
198
|
/**
|
|
129
199
|
* Returns the liquidation incentive factor for an LLTV and liquidation cursor.
|
|
130
200
|
*
|
|
131
201
|
* @param input - WAD-scaled LLTV or collateral params carrying an LLTV.
|
|
132
|
-
* @param cursor - WAD-scaled liquidation cursor
|
|
202
|
+
* @param cursor - WAD-scaled liquidation cursor.
|
|
133
203
|
* @returns WAD-scaled liquidation incentive factor.
|
|
134
204
|
* @example
|
|
135
205
|
* ```ts
|
|
136
|
-
* import {
|
|
206
|
+
* import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
137
207
|
*
|
|
138
208
|
* const liquidationIncentiveFactor = MarketUtils.getLiquidationIncentiveFactor(
|
|
139
209
|
* 770000000000000000n,
|
|
140
|
-
*
|
|
210
|
+
* 250000000000000000n,
|
|
141
211
|
* );
|
|
142
212
|
* console.log(liquidationIncentiveFactor);
|
|
143
213
|
* ```
|
|
@@ -206,12 +276,14 @@ export var MarketUtils;
|
|
|
206
276
|
* import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
207
277
|
*
|
|
208
278
|
* const hash = MarketUtils.hash({
|
|
279
|
+
* chainId: 8453,
|
|
280
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
209
281
|
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
210
282
|
* collateralParams: [
|
|
211
283
|
* {
|
|
212
284
|
* token: "0x0000000000000000000000000000000000000002",
|
|
213
285
|
* lltv: 770000000000000000n,
|
|
214
|
-
*
|
|
286
|
+
* liquidationCursor: 250000000000000000n,
|
|
215
287
|
* oracle: "0x0000000000000000000000000000000000000003",
|
|
216
288
|
* },
|
|
217
289
|
* ],
|
|
@@ -232,12 +304,14 @@ export var MarketUtils;
|
|
|
232
304
|
COLLATERAL_PARAMS_TYPEHASH,
|
|
233
305
|
params.token,
|
|
234
306
|
BigInt(params.lltv),
|
|
235
|
-
BigInt(params.
|
|
307
|
+
BigInt(params.liquidationCursor),
|
|
236
308
|
params.oracle,
|
|
237
309
|
])));
|
|
238
310
|
const collateralParamsHash = keccak256(concat(collateralParamHashes));
|
|
239
311
|
return keccak256(encodeAbiParameters(marketHashParams, [
|
|
240
312
|
MARKET_TYPEHASH,
|
|
313
|
+
BigInt(marketParams.chainId),
|
|
314
|
+
marketParams.midnight,
|
|
241
315
|
marketParams.loanToken,
|
|
242
316
|
collateralParamsHash,
|
|
243
317
|
BigInt(marketParams.maturity),
|
|
@@ -250,51 +324,37 @@ export var MarketUtils;
|
|
|
250
324
|
/**
|
|
251
325
|
* Computes the Midnight id for a market using `IdLib.toId`.
|
|
252
326
|
*
|
|
253
|
-
* @param
|
|
254
|
-
* @param params.chainId - EIP-155 chain id used to resolve the core Midnight address from the registry.
|
|
327
|
+
* @param market - Market to hash.
|
|
255
328
|
* @returns Market id.
|
|
256
|
-
* @throws {UnsupportedChainIdError} when no address registry exists for `chainId`.
|
|
257
|
-
* @throws {UnknownAddressError} when the registry has no Midnight address for `chainId`.
|
|
258
329
|
* @example
|
|
259
330
|
* ```ts
|
|
260
|
-
* import { ChainId, registerCustomAddresses } from "@morpho-org/morpho-ts";
|
|
261
331
|
* import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
262
332
|
*
|
|
263
|
-
* registerCustomAddresses({
|
|
264
|
-
* addresses: {
|
|
265
|
-
* [ChainId.BaseMainnet]: {
|
|
266
|
-
* midnight: "0x0000000000000000000000000000000000000001",
|
|
267
|
-
* },
|
|
268
|
-
* },
|
|
269
|
-
* });
|
|
270
|
-
*
|
|
271
333
|
* const id = MarketUtils.toId({
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
287
|
-
* chainId: ChainId.BaseMainnet,
|
|
334
|
+
* chainId: 8453,
|
|
335
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
336
|
+
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
337
|
+
* collateralParams: [
|
|
338
|
+
* {
|
|
339
|
+
* token: "0x0000000000000000000000000000000000000002",
|
|
340
|
+
* lltv: 770000000000000000n,
|
|
341
|
+
* liquidationCursor: 250000000000000000n,
|
|
342
|
+
* oracle: "0x0000000000000000000000000000000000000003",
|
|
343
|
+
* },
|
|
344
|
+
* ],
|
|
345
|
+
* maturity: 1n,
|
|
346
|
+
* rcfThreshold: 0n,
|
|
347
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
348
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
288
349
|
* });
|
|
289
350
|
* console.log(id);
|
|
290
351
|
* ```
|
|
291
352
|
*/
|
|
292
|
-
function toId(
|
|
293
|
-
const
|
|
294
|
-
const
|
|
295
|
-
const encodedMarket = encodeAbiParameters([marketParamsAbiParameter], [toStruct(params.market)]);
|
|
353
|
+
function toId(market) {
|
|
354
|
+
const marketParams = MarketParams.from(market);
|
|
355
|
+
const encodedMarket = encodeAbiParameters([marketParamsAbiParameter], [toStruct(marketParams)]);
|
|
296
356
|
const creationHash = keccak256(concat([SSTORE2_PREFIX, encodedMarket]));
|
|
297
|
-
return keccak256(encodePacked(["uint8", "address", "uint256", "bytes32"], [255, midnight,
|
|
357
|
+
return keccak256(encodePacked(["uint8", "address", "uint256", "bytes32"], [255, marketParams.midnight, 0n, creationHash]));
|
|
298
358
|
}
|
|
299
359
|
MarketUtils.toId = toId;
|
|
300
360
|
})(MarketUtils || (MarketUtils = {}));
|
|
@@ -125,14 +125,15 @@ export declare class Position {
|
|
|
125
125
|
* collateral: [50n],
|
|
126
126
|
* },
|
|
127
127
|
* {
|
|
128
|
-
* chainId: 31337,
|
|
129
128
|
* params: {
|
|
129
|
+
* chainId: 31337,
|
|
130
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
130
131
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
131
132
|
* collateralParams: [
|
|
132
133
|
* {
|
|
133
134
|
* token: "0x0000000000000000000000000000000000007000",
|
|
134
135
|
* lltv: 770000000000000000n,
|
|
135
|
-
*
|
|
136
|
+
* liquidationCursor: 250000000000000000n,
|
|
136
137
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
137
138
|
* },
|
|
138
139
|
* ],
|
|
@@ -156,7 +157,7 @@ export declare class Position {
|
|
|
156
157
|
export declare class AccrualPosition extends Position {
|
|
157
158
|
/** Hydrated market for this position. */
|
|
158
159
|
readonly market: Market;
|
|
159
|
-
constructor(position: IPosition, market: IMarket);
|
|
160
|
+
constructor(position: IPosition, market: IMarket | Market);
|
|
160
161
|
/**
|
|
161
162
|
* Returns the position collateral balance at an index configured by the market.
|
|
162
163
|
*
|
|
@@ -192,14 +193,15 @@ export declare class AccrualPosition extends Position {
|
|
|
192
193
|
* collateral: [50n],
|
|
193
194
|
* },
|
|
194
195
|
* {
|
|
195
|
-
* chainId: 31337,
|
|
196
196
|
* params: {
|
|
197
|
+
* chainId: 31337,
|
|
198
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
197
199
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
198
200
|
* collateralParams: [
|
|
199
201
|
* {
|
|
200
202
|
* token: "0x0000000000000000000000000000000000007000",
|
|
201
203
|
* lltv: 770000000000000000n,
|
|
202
|
-
*
|
|
204
|
+
* liquidationCursor: 250000000000000000n,
|
|
203
205
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
204
206
|
* },
|
|
205
207
|
* ],
|
|
@@ -258,14 +260,15 @@ export declare class AccrualPosition extends Position {
|
|
|
258
260
|
* collateral: [50n],
|
|
259
261
|
* },
|
|
260
262
|
* {
|
|
261
|
-
* chainId: 31337,
|
|
262
263
|
* params: {
|
|
264
|
+
* chainId: 31337,
|
|
265
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
263
266
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
264
267
|
* collateralParams: [
|
|
265
268
|
* {
|
|
266
269
|
* token: collateralToken,
|
|
267
270
|
* lltv: 770000000000000000n,
|
|
268
|
-
*
|
|
271
|
+
* liquidationCursor: 250000000000000000n,
|
|
269
272
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
270
273
|
* },
|
|
271
274
|
* ],
|
|
@@ -323,14 +326,15 @@ export declare class AccrualPosition extends Position {
|
|
|
323
326
|
* collateral: [50n],
|
|
324
327
|
* },
|
|
325
328
|
* {
|
|
326
|
-
* chainId: 31337,
|
|
327
329
|
* params: {
|
|
330
|
+
* chainId: 31337,
|
|
331
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
328
332
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
329
333
|
* collateralParams: [
|
|
330
334
|
* {
|
|
331
335
|
* token: "0x0000000000000000000000000000000000007000",
|
|
332
336
|
* lltv: 770000000000000000n,
|
|
333
|
-
*
|
|
337
|
+
* liquidationCursor: 250000000000000000n,
|
|
334
338
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
335
339
|
* },
|
|
336
340
|
* ],
|
|
@@ -391,14 +395,15 @@ export declare class AccrualPosition extends Position {
|
|
|
391
395
|
* collateral: [50n],
|
|
392
396
|
* },
|
|
393
397
|
* {
|
|
394
|
-
* chainId: 31337,
|
|
395
398
|
* params: {
|
|
399
|
+
* chainId: 31337,
|
|
400
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
396
401
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
397
402
|
* collateralParams: [
|
|
398
403
|
* {
|
|
399
404
|
* token: "0x0000000000000000000000000000000000007000",
|
|
400
405
|
* lltv: 770000000000000000n,
|
|
401
|
-
*
|
|
406
|
+
* liquidationCursor: 250000000000000000n,
|
|
402
407
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
403
408
|
* },
|
|
404
409
|
* ],
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { MathLib } from "@morpho-org/morpho-ts";
|
|
1
|
+
import { _try, MathLib } from "@morpho-org/morpho-ts";
|
|
2
|
+
import { UnknownCollateralIndexError } from "../errors.js";
|
|
2
3
|
import { Market } from "./Market.js";
|
|
3
4
|
import { PositionUtils } from "./PositionUtils.js";
|
|
4
5
|
/**
|
|
@@ -101,14 +102,15 @@ export class Position {
|
|
|
101
102
|
* collateral: [50n],
|
|
102
103
|
* },
|
|
103
104
|
* {
|
|
104
|
-
* chainId: 31337,
|
|
105
105
|
* params: {
|
|
106
|
+
* chainId: 31337,
|
|
107
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
106
108
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
107
109
|
* collateralParams: [
|
|
108
110
|
* {
|
|
109
111
|
* token: "0x0000000000000000000000000000000000007000",
|
|
110
112
|
* lltv: 770000000000000000n,
|
|
111
|
-
*
|
|
113
|
+
* liquidationCursor: 250000000000000000n,
|
|
112
114
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
113
115
|
* },
|
|
114
116
|
* ],
|
|
@@ -134,7 +136,7 @@ export class AccrualPosition extends Position {
|
|
|
134
136
|
market;
|
|
135
137
|
constructor(position, market) {
|
|
136
138
|
super(position);
|
|
137
|
-
this.market =
|
|
139
|
+
this.market = Market.from(market);
|
|
138
140
|
}
|
|
139
141
|
/**
|
|
140
142
|
* Returns the position collateral balance at an index configured by the market.
|
|
@@ -171,14 +173,15 @@ export class AccrualPosition extends Position {
|
|
|
171
173
|
* collateral: [50n],
|
|
172
174
|
* },
|
|
173
175
|
* {
|
|
174
|
-
* chainId: 31337,
|
|
175
176
|
* params: {
|
|
177
|
+
* chainId: 31337,
|
|
178
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
176
179
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
177
180
|
* collateralParams: [
|
|
178
181
|
* {
|
|
179
182
|
* token: "0x0000000000000000000000000000000000007000",
|
|
180
183
|
* lltv: 770000000000000000n,
|
|
181
|
-
*
|
|
184
|
+
* liquidationCursor: 250000000000000000n,
|
|
182
185
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
183
186
|
* },
|
|
184
187
|
* ],
|
|
@@ -202,7 +205,7 @@ export class AccrualPosition extends Position {
|
|
|
202
205
|
*/
|
|
203
206
|
getCollateralBalanceByIndex(index) {
|
|
204
207
|
const normalizedIndex = BigInt(index);
|
|
205
|
-
if (this.market.
|
|
208
|
+
if (_try(() => this.market.getCollateralByIndex(normalizedIndex), UnknownCollateralIndexError) == null)
|
|
206
209
|
return undefined;
|
|
207
210
|
return this.collateral[Number(normalizedIndex)] ?? 0n;
|
|
208
211
|
}
|
|
@@ -242,14 +245,15 @@ export class AccrualPosition extends Position {
|
|
|
242
245
|
* collateral: [50n],
|
|
243
246
|
* },
|
|
244
247
|
* {
|
|
245
|
-
* chainId: 31337,
|
|
246
248
|
* params: {
|
|
249
|
+
* chainId: 31337,
|
|
250
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
247
251
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
248
252
|
* collateralParams: [
|
|
249
253
|
* {
|
|
250
254
|
* token: collateralToken,
|
|
251
255
|
* lltv: 770000000000000000n,
|
|
252
|
-
*
|
|
256
|
+
* liquidationCursor: 250000000000000000n,
|
|
253
257
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
254
258
|
* },
|
|
255
259
|
* ],
|
|
@@ -310,14 +314,15 @@ export class AccrualPosition extends Position {
|
|
|
310
314
|
* collateral: [50n],
|
|
311
315
|
* },
|
|
312
316
|
* {
|
|
313
|
-
* chainId: 31337,
|
|
314
317
|
* params: {
|
|
318
|
+
* chainId: 31337,
|
|
319
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
315
320
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
316
321
|
* collateralParams: [
|
|
317
322
|
* {
|
|
318
323
|
* token: "0x0000000000000000000000000000000000007000",
|
|
319
324
|
* lltv: 770000000000000000n,
|
|
320
|
-
*
|
|
325
|
+
* liquidationCursor: 250000000000000000n,
|
|
321
326
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
322
327
|
* },
|
|
323
328
|
* ],
|
|
@@ -380,14 +385,15 @@ export class AccrualPosition extends Position {
|
|
|
380
385
|
* collateral: [50n],
|
|
381
386
|
* },
|
|
382
387
|
* {
|
|
383
|
-
* chainId: 31337,
|
|
384
388
|
* params: {
|
|
389
|
+
* chainId: 31337,
|
|
390
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
385
391
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
386
392
|
* collateralParams: [
|
|
387
393
|
* {
|
|
388
394
|
* token: "0x0000000000000000000000000000000000007000",
|
|
389
395
|
* lltv: 770000000000000000n,
|
|
390
|
-
*
|
|
396
|
+
* liquidationCursor: 250000000000000000n,
|
|
391
397
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
392
398
|
* },
|
|
393
399
|
* ],
|
|
@@ -19,14 +19,15 @@ import type { IPosition } from "./Position.js";
|
|
|
19
19
|
* collateral: [50n],
|
|
20
20
|
* },
|
|
21
21
|
* market: {
|
|
22
|
-
* chainId: 8453,
|
|
23
22
|
* params: {
|
|
23
|
+
* chainId: 8453,
|
|
24
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
24
25
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
25
26
|
* collateralParams: [
|
|
26
27
|
* {
|
|
27
28
|
* token: "0x0000000000000000000000000000000000007000",
|
|
28
29
|
* lltv: 770000000000000000n,
|
|
29
|
-
*
|
|
30
|
+
* liquidationCursor: 250000000000000000n,
|
|
30
31
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
31
32
|
* },
|
|
32
33
|
* ],
|
|
@@ -74,14 +75,15 @@ export declare namespace PositionUtils {
|
|
|
74
75
|
* collateral: [50n],
|
|
75
76
|
* },
|
|
76
77
|
* market: {
|
|
77
|
-
* chainId: 8453,
|
|
78
78
|
* params: {
|
|
79
|
+
* chainId: 8453,
|
|
80
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
79
81
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
80
82
|
* collateralParams: [
|
|
81
83
|
* {
|
|
82
84
|
* token: "0x0000000000000000000000000000000000007000",
|
|
83
85
|
* lltv: 770000000000000000n,
|
|
84
|
-
*
|
|
86
|
+
* liquidationCursor: 250000000000000000n,
|
|
85
87
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
86
88
|
* },
|
|
87
89
|
* ],
|
|
@@ -18,14 +18,15 @@ import { InvalidPositionAccrualStateError, InvalidPositionAccrualTimestampError,
|
|
|
18
18
|
* collateral: [50n],
|
|
19
19
|
* },
|
|
20
20
|
* market: {
|
|
21
|
-
* chainId: 8453,
|
|
22
21
|
* params: {
|
|
22
|
+
* chainId: 8453,
|
|
23
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
23
24
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
24
25
|
* collateralParams: [
|
|
25
26
|
* {
|
|
26
27
|
* token: "0x0000000000000000000000000000000000007000",
|
|
27
28
|
* lltv: 770000000000000000n,
|
|
28
|
-
*
|
|
29
|
+
* liquidationCursor: 250000000000000000n,
|
|
29
30
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
30
31
|
* },
|
|
31
32
|
* ],
|
|
@@ -74,14 +75,15 @@ export var PositionUtils;
|
|
|
74
75
|
* collateral: [50n],
|
|
75
76
|
* },
|
|
76
77
|
* market: {
|
|
77
|
-
* chainId: 8453,
|
|
78
78
|
* params: {
|
|
79
|
+
* chainId: 8453,
|
|
80
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
79
81
|
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
80
82
|
* collateralParams: [
|
|
81
83
|
* {
|
|
82
84
|
* token: "0x0000000000000000000000000000000000007000",
|
|
83
85
|
* lltv: 770000000000000000n,
|
|
84
|
-
*
|
|
86
|
+
* liquidationCursor: 250000000000000000n,
|
|
85
87
|
* oracle: "0x0000000000000000000000000000000000008000",
|
|
86
88
|
* },
|
|
87
89
|
* ],
|
|
@@ -143,10 +145,12 @@ export var PositionUtils;
|
|
|
143
145
|
const collateralParams = params.market.params.collateralParams.map((collateralParam) => ({
|
|
144
146
|
token: collateralParam.token,
|
|
145
147
|
lltv: BigInt(collateralParam.lltv),
|
|
146
|
-
|
|
148
|
+
liquidationCursor: BigInt(collateralParam.liquidationCursor),
|
|
147
149
|
oracle: collateralParam.oracle,
|
|
148
150
|
}));
|
|
149
151
|
const marketParams = {
|
|
152
|
+
chainId: BigInt(params.market.params.chainId),
|
|
153
|
+
midnight: params.market.params.midnight,
|
|
150
154
|
loanToken: params.market.params.loanToken,
|
|
151
155
|
collateralParams,
|
|
152
156
|
maturity,
|
|
@@ -174,7 +178,6 @@ export var PositionUtils;
|
|
|
174
178
|
collateral,
|
|
175
179
|
},
|
|
176
180
|
market: {
|
|
177
|
-
chainId: BigInt(params.market.chainId),
|
|
178
181
|
params: marketParams,
|
|
179
182
|
totalUnits: BigInt(params.market.totalUnits),
|
|
180
183
|
lossFactor: marketLossFactor,
|
|
@@ -178,6 +178,28 @@ export declare namespace TickLib {
|
|
|
178
178
|
* ```
|
|
179
179
|
*/
|
|
180
180
|
function tickToRate(tick: BigIntish): bigint;
|
|
181
|
+
/**
|
|
182
|
+
* Converts a Midnight tick into a WAD simple annual percentage rate.
|
|
183
|
+
*
|
|
184
|
+
* This is an SDK-only rate display convenience. It annualizes the fixed
|
|
185
|
+
* period rate from {@link tickToRate} over `timeToMaturity` seconds.
|
|
186
|
+
*
|
|
187
|
+
* @param tick - Midnight tick.
|
|
188
|
+
* @param timeToMaturity - Seconds until the market matures.
|
|
189
|
+
* @returns WAD simple APR rounded up.
|
|
190
|
+
* @throws {NegativeValueError} when `tick` or `timeToMaturity` is negative.
|
|
191
|
+
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
|
|
192
|
+
* @throws {DivisionByZeroError} when the tick price or `timeToMaturity` is zero.
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* import { TickLib } from "@morpho-org/midnight-sdk";
|
|
196
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
197
|
+
*
|
|
198
|
+
* const apr = TickLib.tickToApr(6744n, Time.s.from.y(1n));
|
|
199
|
+
* console.log(apr);
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
function tickToApr(tick: BigIntish, timeToMaturity: BigIntish): bigint;
|
|
181
203
|
/**
|
|
182
204
|
* Asserts that a tick is aligned to the provided spacing.
|
|
183
205
|
*
|
package/lib/esm/math/TickLib.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assertNonNegative, DivisionByZeroError, MathLib, } from "@morpho-org/morpho-ts";
|
|
1
|
+
import { assertNonNegative, DivisionByZeroError, MathLib, Time, } from "@morpho-org/morpho-ts";
|
|
2
2
|
import { DEFAULT_TICK_SPACING, MAX_TICK, PRICE_ROUNDING_STEP, } from "../constants.js";
|
|
3
3
|
import { InvalidTickSpacingError, PriceGreaterThanOneError, TickOutOfRangeError, } from "../errors.js";
|
|
4
4
|
/**
|
|
@@ -236,6 +236,36 @@ export var TickLib;
|
|
|
236
236
|
return MathLib.mulDiv(MathLib.WAD, MathLib.WAD, price, "Up") - MathLib.WAD;
|
|
237
237
|
}
|
|
238
238
|
TickLib.tickToRate = tickToRate;
|
|
239
|
+
/**
|
|
240
|
+
* Converts a Midnight tick into a WAD simple annual percentage rate.
|
|
241
|
+
*
|
|
242
|
+
* This is an SDK-only rate display convenience. It annualizes the fixed
|
|
243
|
+
* period rate from {@link tickToRate} over `timeToMaturity` seconds.
|
|
244
|
+
*
|
|
245
|
+
* @param tick - Midnight tick.
|
|
246
|
+
* @param timeToMaturity - Seconds until the market matures.
|
|
247
|
+
* @returns WAD simple APR rounded up.
|
|
248
|
+
* @throws {NegativeValueError} when `tick` or `timeToMaturity` is negative.
|
|
249
|
+
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
|
|
250
|
+
* @throws {DivisionByZeroError} when the tick price or `timeToMaturity` is zero.
|
|
251
|
+
* @example
|
|
252
|
+
* ```ts
|
|
253
|
+
* import { TickLib } from "@morpho-org/midnight-sdk";
|
|
254
|
+
* import { Time } from "@morpho-org/morpho-ts";
|
|
255
|
+
*
|
|
256
|
+
* const apr = TickLib.tickToApr(6744n, Time.s.from.y(1n));
|
|
257
|
+
* console.log(apr);
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
function tickToApr(tick, timeToMaturity) {
|
|
261
|
+
const normalizedTimeToMaturity = BigInt(timeToMaturity);
|
|
262
|
+
assertNonNegative("timeToMaturity", normalizedTimeToMaturity);
|
|
263
|
+
if (normalizedTimeToMaturity === 0n) {
|
|
264
|
+
throw new DivisionByZeroError("timeToMaturity");
|
|
265
|
+
}
|
|
266
|
+
return MathLib.mulDiv(tickToRate(tick), Time.s.from.y(1n), normalizedTimeToMaturity, "Up");
|
|
267
|
+
}
|
|
268
|
+
TickLib.tickToApr = tickToApr;
|
|
239
269
|
/**
|
|
240
270
|
* Asserts that a tick is aligned to the provided spacing.
|
|
241
271
|
*
|
package/lib/esm/math/index.d.ts
CHANGED
package/lib/esm/math/index.js
CHANGED