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