@morpho-org/midnight-sdk 0.1.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/README.md +252 -0
- package/lib/cjs/abis.d.ts +3170 -0
- package/lib/cjs/abis.js +4073 -0
- package/lib/cjs/api/MidnightApi.d.ts +532 -0
- package/lib/cjs/api/MidnightApi.js +713 -0
- package/lib/cjs/api/helpers.d.ts +20 -0
- package/lib/cjs/api/helpers.js +189 -0
- package/lib/cjs/api/index.d.ts +2 -0
- package/lib/cjs/api/index.js +21 -0
- package/lib/cjs/api/types.d.ts +453 -0
- package/lib/cjs/api/types.js +2 -0
- package/lib/cjs/constants.d.ts +168 -0
- package/lib/cjs/constants.js +187 -0
- package/lib/cjs/errors.d.ts +253 -0
- package/lib/cjs/errors.js +309 -0
- package/lib/cjs/fetch/Market.d.ts +65 -0
- package/lib/cjs/fetch/Market.js +117 -0
- package/lib/cjs/fetch/Offer.d.ts +74 -0
- package/lib/cjs/fetch/Offer.js +119 -0
- package/lib/cjs/fetch/Position.d.ts +85 -0
- package/lib/cjs/fetch/Position.js +148 -0
- package/lib/cjs/fetch/Ratifier.d.ts +38 -0
- package/lib/cjs/fetch/Ratifier.js +57 -0
- package/lib/cjs/fetch/index.d.ts +5 -0
- package/lib/cjs/fetch/index.js +21 -0
- package/lib/cjs/fetch/types.d.ts +57 -0
- package/lib/cjs/fetch/types.js +2 -0
- package/lib/cjs/fetch/utils.d.ts +8 -0
- package/lib/cjs/fetch/utils.js +18 -0
- package/lib/cjs/index.d.ts +18 -0
- package/lib/cjs/index.js +57 -0
- package/lib/cjs/market/Market.d.ts +613 -0
- package/lib/cjs/market/Market.js +503 -0
- package/lib/cjs/market/MarketUtils.d.ts +189 -0
- package/lib/cjs/market/MarketUtils.js +303 -0
- package/lib/cjs/market/Position.d.ts +424 -0
- package/lib/cjs/market/Position.js +425 -0
- package/lib/cjs/market/PositionUtils.d.ts +115 -0
- package/lib/cjs/market/PositionUtils.js +194 -0
- package/lib/cjs/market/index.d.ts +4 -0
- package/lib/cjs/market/index.js +20 -0
- package/lib/cjs/math/ConsumableUnitsLib.d.ts +57 -0
- package/lib/cjs/math/ConsumableUnitsLib.js +77 -0
- package/lib/cjs/math/TakeAmountsLib.d.ts +175 -0
- package/lib/cjs/math/TakeAmountsLib.js +199 -0
- package/lib/cjs/math/TickLib.d.ts +200 -0
- package/lib/cjs/math/TickLib.js +272 -0
- package/lib/cjs/math/index.d.ts +3 -0
- package/lib/cjs/math/index.js +19 -0
- package/lib/cjs/offers/Offer.d.ts +534 -0
- package/lib/cjs/offers/Offer.js +343 -0
- package/lib/cjs/offers/OfferUtils.d.ts +565 -0
- package/lib/cjs/offers/OfferUtils.js +659 -0
- package/lib/cjs/offers/index.d.ts +2 -0
- package/lib/cjs/offers/index.js +18 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/queries/GetPosition.d.ts +55 -0
- package/lib/cjs/queries/GetPosition.js +74 -0
- package/lib/cjs/queries/index.d.ts +1 -0
- package/lib/cjs/queries/index.js +37 -0
- package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +733 -0
- package/lib/cjs/signatures/EcrecoverRatifierUtils.js +618 -0
- package/lib/cjs/signatures/Group.d.ts +178 -0
- package/lib/cjs/signatures/Group.js +193 -0
- package/lib/cjs/signatures/GroupUtils.d.ts +191 -0
- package/lib/cjs/signatures/GroupUtils.js +127 -0
- package/lib/cjs/signatures/Payload.d.ts +283 -0
- package/lib/cjs/signatures/Payload.js +570 -0
- package/lib/cjs/signatures/RatifierUtils.d.ts +109 -0
- package/lib/cjs/signatures/RatifierUtils.js +76 -0
- package/lib/cjs/signatures/SetterRatifierUtils.d.ts +243 -0
- package/lib/cjs/signatures/SetterRatifierUtils.js +217 -0
- package/lib/cjs/signatures/Tree.d.ts +241 -0
- package/lib/cjs/signatures/Tree.js +263 -0
- package/lib/cjs/signatures/TreeUtils.d.ts +573 -0
- package/lib/cjs/signatures/TreeUtils.js +461 -0
- package/lib/cjs/signatures/index.d.ts +7 -0
- package/lib/cjs/signatures/index.js +23 -0
- package/lib/esm/abis.d.ts +3170 -0
- package/lib/esm/abis.js +4070 -0
- package/lib/esm/api/MidnightApi.d.ts +532 -0
- package/lib/esm/api/MidnightApi.js +709 -0
- package/lib/esm/api/helpers.d.ts +20 -0
- package/lib/esm/api/helpers.js +180 -0
- package/lib/esm/api/index.d.ts +2 -0
- package/lib/esm/api/index.js +2 -0
- package/lib/esm/api/types.d.ts +453 -0
- package/lib/esm/api/types.js +1 -0
- package/lib/esm/constants.d.ts +168 -0
- package/lib/esm/constants.js +184 -0
- package/lib/esm/errors.d.ts +253 -0
- package/lib/esm/errors.js +291 -0
- package/lib/esm/fetch/Market.d.ts +65 -0
- package/lib/esm/fetch/Market.js +113 -0
- package/lib/esm/fetch/Offer.d.ts +74 -0
- package/lib/esm/fetch/Offer.js +116 -0
- package/lib/esm/fetch/Position.d.ts +85 -0
- package/lib/esm/fetch/Position.js +144 -0
- package/lib/esm/fetch/Ratifier.d.ts +38 -0
- package/lib/esm/fetch/Ratifier.js +54 -0
- package/lib/esm/fetch/index.d.ts +5 -0
- package/lib/esm/fetch/index.js +5 -0
- package/lib/esm/fetch/types.d.ts +57 -0
- package/lib/esm/fetch/types.js +1 -0
- package/lib/esm/fetch/utils.d.ts +8 -0
- package/lib/esm/fetch/utils.js +12 -0
- package/lib/esm/index.d.ts +18 -0
- package/lib/esm/index.js +18 -0
- package/lib/esm/market/Market.d.ts +613 -0
- package/lib/esm/market/Market.js +498 -0
- package/lib/esm/market/MarketUtils.d.ts +189 -0
- package/lib/esm/market/MarketUtils.js +300 -0
- package/lib/esm/market/Position.d.ts +424 -0
- package/lib/esm/market/Position.js +420 -0
- package/lib/esm/market/PositionUtils.d.ts +115 -0
- package/lib/esm/market/PositionUtils.js +191 -0
- package/lib/esm/market/index.d.ts +4 -0
- package/lib/esm/market/index.js +4 -0
- package/lib/esm/math/ConsumableUnitsLib.d.ts +57 -0
- package/lib/esm/math/ConsumableUnitsLib.js +74 -0
- package/lib/esm/math/TakeAmountsLib.d.ts +175 -0
- package/lib/esm/math/TakeAmountsLib.js +196 -0
- package/lib/esm/math/TickLib.d.ts +200 -0
- package/lib/esm/math/TickLib.js +269 -0
- package/lib/esm/math/index.d.ts +3 -0
- package/lib/esm/math/index.js +3 -0
- package/lib/esm/offers/Offer.d.ts +534 -0
- package/lib/esm/offers/Offer.js +339 -0
- package/lib/esm/offers/OfferUtils.d.ts +565 -0
- package/lib/esm/offers/OfferUtils.js +656 -0
- package/lib/esm/offers/index.d.ts +2 -0
- package/lib/esm/offers/index.js +2 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/queries/GetPosition.d.ts +55 -0
- package/lib/esm/queries/GetPosition.js +71 -0
- package/lib/esm/queries/index.d.ts +1 -0
- package/lib/esm/queries/index.js +1 -0
- package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +733 -0
- package/lib/esm/signatures/EcrecoverRatifierUtils.js +615 -0
- package/lib/esm/signatures/Group.d.ts +178 -0
- package/lib/esm/signatures/Group.js +189 -0
- package/lib/esm/signatures/GroupUtils.d.ts +191 -0
- package/lib/esm/signatures/GroupUtils.js +124 -0
- package/lib/esm/signatures/Payload.d.ts +283 -0
- package/lib/esm/signatures/Payload.js +564 -0
- package/lib/esm/signatures/RatifierUtils.d.ts +109 -0
- package/lib/esm/signatures/RatifierUtils.js +73 -0
- package/lib/esm/signatures/SetterRatifierUtils.d.ts +243 -0
- package/lib/esm/signatures/SetterRatifierUtils.js +214 -0
- package/lib/esm/signatures/Tree.d.ts +241 -0
- package/lib/esm/signatures/Tree.js +259 -0
- package/lib/esm/signatures/TreeUtils.d.ts +573 -0
- package/lib/esm/signatures/TreeUtils.js +458 -0
- package/lib/esm/signatures/index.d.ts +7 -0
- package/lib/esm/signatures/index.js +7 -0
- package/package.json +69 -0
- package/src/index.ts +19 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { assertNonNegative, getChainAddress, MathLib, } from "@morpho-org/morpho-ts";
|
|
2
|
+
import { concat, encodeAbiParameters, encodePacked, keccak256 } from "viem";
|
|
3
|
+
import { CBP, COLLATERAL_PARAMS_TYPEHASH, MARKET_TYPEHASH, SETTLEMENT_FEE_BREAKPOINTS, } from "../constants.js";
|
|
4
|
+
import { MarketParams, } from "./Market.js";
|
|
5
|
+
const SSTORE2_PREFIX = "0x600b380380600b5f395ff3";
|
|
6
|
+
const collateralParamsHashParams = [
|
|
7
|
+
{ name: "typehash", type: "bytes32" },
|
|
8
|
+
{ name: "token", type: "address" },
|
|
9
|
+
{ name: "lltv", type: "uint256" },
|
|
10
|
+
{ name: "maxLif", type: "uint256" },
|
|
11
|
+
{ name: "oracle", type: "address" },
|
|
12
|
+
];
|
|
13
|
+
const marketHashParams = [
|
|
14
|
+
{ name: "typehash", type: "bytes32" },
|
|
15
|
+
{ name: "loanToken", type: "address" },
|
|
16
|
+
{ name: "collateralParamsHash", type: "bytes32" },
|
|
17
|
+
{ name: "maturity", type: "uint256" },
|
|
18
|
+
{ name: "rcfThreshold", type: "uint256" },
|
|
19
|
+
{ name: "enterGate", type: "address" },
|
|
20
|
+
{ name: "liquidatorGate", type: "address" },
|
|
21
|
+
];
|
|
22
|
+
const marketParamsAbiParameter = {
|
|
23
|
+
type: "tuple",
|
|
24
|
+
components: [
|
|
25
|
+
{ name: "loanToken", type: "address" },
|
|
26
|
+
{
|
|
27
|
+
name: "collateralParams",
|
|
28
|
+
type: "tuple[]",
|
|
29
|
+
components: [
|
|
30
|
+
{ name: "token", type: "address" },
|
|
31
|
+
{ name: "lltv", type: "uint256" },
|
|
32
|
+
{ name: "maxLif", type: "uint256" },
|
|
33
|
+
{ name: "oracle", type: "address" },
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
{ name: "maturity", type: "uint256" },
|
|
37
|
+
{ name: "rcfThreshold", type: "uint256" },
|
|
38
|
+
{ name: "enterGate", type: "address" },
|
|
39
|
+
{ name: "liquidatorGate", type: "address" },
|
|
40
|
+
],
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Domain helpers for Midnight markets.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
48
|
+
*
|
|
49
|
+
* console.log(typeof MarketUtils.hash);
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export var MarketUtils;
|
|
53
|
+
(function (MarketUtils) {
|
|
54
|
+
/**
|
|
55
|
+
* Converts collateral params from a plain input or ABI tuple.
|
|
56
|
+
*
|
|
57
|
+
* @param params.token - Collateral token address.
|
|
58
|
+
* @param params.lltv - WAD-scaled liquidation loan-to-value.
|
|
59
|
+
* @param params.maxLif - WAD-scaled maximum liquidation incentive factor.
|
|
60
|
+
* @param params.oracle - Oracle address used to price this collateral.
|
|
61
|
+
* @returns Collateral params with bigint fields.
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
65
|
+
*
|
|
66
|
+
* const collateral = MarketUtils.toCollateralParams({
|
|
67
|
+
* token: "0x0000000000000000000000000000000000000001",
|
|
68
|
+
* lltv: 770000000000000000n,
|
|
69
|
+
* maxLif: 1061007957559681697n,
|
|
70
|
+
* oracle: "0x0000000000000000000000000000000000000002",
|
|
71
|
+
* });
|
|
72
|
+
* console.log(collateral.maxLif);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
function toCollateralParams(params) {
|
|
76
|
+
return {
|
|
77
|
+
token: params.token,
|
|
78
|
+
lltv: BigInt(params.lltv),
|
|
79
|
+
maxLif: BigInt(params.maxLif),
|
|
80
|
+
oracle: params.oracle,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
MarketUtils.toCollateralParams = toCollateralParams;
|
|
84
|
+
/**
|
|
85
|
+
* Converts market params into the tuple object expected by viem ABI encoders.
|
|
86
|
+
*
|
|
87
|
+
* @param market - Market params or hydrated market.
|
|
88
|
+
* @returns ABI-compatible market params.
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
92
|
+
*
|
|
93
|
+
* const struct = MarketUtils.toStruct({
|
|
94
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
95
|
+
* collateralParams: [
|
|
96
|
+
* {
|
|
97
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
98
|
+
* lltv: 770000000000000000n,
|
|
99
|
+
* maxLif: 1061007957559681697n,
|
|
100
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
101
|
+
* },
|
|
102
|
+
* ],
|
|
103
|
+
* maturity: 54_000n,
|
|
104
|
+
* rcfThreshold: 0n,
|
|
105
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
106
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
107
|
+
* });
|
|
108
|
+
* console.log(struct.maturity);
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
function toStruct(market) {
|
|
112
|
+
const params = MarketParams.from(market);
|
|
113
|
+
return {
|
|
114
|
+
loanToken: params.loanToken,
|
|
115
|
+
collateralParams: params.collateralParams.map((collateral) => ({
|
|
116
|
+
token: collateral.token,
|
|
117
|
+
lltv: collateral.lltv,
|
|
118
|
+
maxLif: collateral.maxLif,
|
|
119
|
+
oracle: collateral.oracle,
|
|
120
|
+
})),
|
|
121
|
+
maturity: params.maturity,
|
|
122
|
+
rcfThreshold: params.rcfThreshold,
|
|
123
|
+
enterGate: params.enterGate,
|
|
124
|
+
liquidatorGate: params.liquidatorGate,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
MarketUtils.toStruct = toStruct;
|
|
128
|
+
/**
|
|
129
|
+
* Returns the liquidation incentive factor for an LLTV and liquidation cursor.
|
|
130
|
+
*
|
|
131
|
+
* @param input - WAD-scaled LLTV or collateral params carrying an LLTV.
|
|
132
|
+
* @param cursor - WAD-scaled liquidation cursor, usually `LIQUIDATION_CURSOR_LOW` or `LIQUIDATION_CURSOR_HIGH`.
|
|
133
|
+
* @returns WAD-scaled liquidation incentive factor.
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* import { LIQUIDATION_CURSOR_LOW, MarketUtils } from "@morpho-org/midnight-sdk";
|
|
137
|
+
*
|
|
138
|
+
* const liquidationIncentiveFactor = MarketUtils.getLiquidationIncentiveFactor(
|
|
139
|
+
* 770000000000000000n,
|
|
140
|
+
* LIQUIDATION_CURSOR_LOW,
|
|
141
|
+
* );
|
|
142
|
+
* console.log(liquidationIncentiveFactor);
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
function getLiquidationIncentiveFactor(input, cursor) {
|
|
146
|
+
const lltv = typeof input === "object" ? input.lltv : input;
|
|
147
|
+
const cursorValue = BigInt(cursor);
|
|
148
|
+
const denominator = MathLib.WAD -
|
|
149
|
+
MathLib.mulDiv(cursorValue, MathLib.WAD - BigInt(lltv), MathLib.WAD, "Down");
|
|
150
|
+
return MathLib.mulDiv(MathLib.WAD, MathLib.WAD, denominator, "Down");
|
|
151
|
+
}
|
|
152
|
+
MarketUtils.getLiquidationIncentiveFactor = getLiquidationIncentiveFactor;
|
|
153
|
+
/**
|
|
154
|
+
* Computes the Midnight settlement fee from market cbp buckets and time to maturity.
|
|
155
|
+
*
|
|
156
|
+
* This mirrors Midnight `settlementFee`: cbp buckets are scaled by {@link CBP},
|
|
157
|
+
* values are linearly interpolated between {@link SETTLEMENT_FEE_BREAKPOINTS},
|
|
158
|
+
* and any time to maturity at or above 360 days uses the last bucket.
|
|
159
|
+
*
|
|
160
|
+
* @param params.settlementFeeCbps - Seven settlement-fee centibip buckets from the market state.
|
|
161
|
+
* @param params.timeToMaturity - Seconds until market maturity.
|
|
162
|
+
* @returns WAD-scaled settlement fee.
|
|
163
|
+
* @throws {NegativeValueError} when `timeToMaturity` is negative.
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
167
|
+
*
|
|
168
|
+
* const fee = MarketUtils.getSettlementFee({
|
|
169
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
170
|
+
* timeToMaturity: 12n * 60n * 60n,
|
|
171
|
+
* });
|
|
172
|
+
* console.log(fee);
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
function getSettlementFee(params) {
|
|
176
|
+
const timeToMaturity = BigInt(params.timeToMaturity);
|
|
177
|
+
assertNonNegative("timeToMaturity", timeToMaturity);
|
|
178
|
+
const lastIndex = SETTLEMENT_FEE_BREAKPOINTS.length - 1;
|
|
179
|
+
const lastBreakpoint = SETTLEMENT_FEE_BREAKPOINTS[lastIndex];
|
|
180
|
+
if (timeToMaturity >= lastBreakpoint) {
|
|
181
|
+
return BigInt(params.settlementFeeCbps[lastIndex]) * CBP;
|
|
182
|
+
}
|
|
183
|
+
const upperIndex = SETTLEMENT_FEE_BREAKPOINTS.findIndex((breakpoint) => timeToMaturity < breakpoint);
|
|
184
|
+
const lowerIndex = upperIndex - 1;
|
|
185
|
+
const start = SETTLEMENT_FEE_BREAKPOINTS[lowerIndex];
|
|
186
|
+
const end = SETTLEMENT_FEE_BREAKPOINTS[upperIndex];
|
|
187
|
+
const feeLower = BigInt(params.settlementFeeCbps[lowerIndex]) * CBP;
|
|
188
|
+
const feeUpper = BigInt(params.settlementFeeCbps[upperIndex]) * CBP;
|
|
189
|
+
return ((feeLower * (end - timeToMaturity) +
|
|
190
|
+
feeUpper * (timeToMaturity - start)) /
|
|
191
|
+
(end - start));
|
|
192
|
+
}
|
|
193
|
+
MarketUtils.getSettlementFee = getSettlementFee;
|
|
194
|
+
/**
|
|
195
|
+
* Computes the HashLib market params struct hash.
|
|
196
|
+
*
|
|
197
|
+
* Hashing canonicalizes non-empty collateral params by token order and
|
|
198
|
+
* preserves the protocol-zero empty market used for tree padding. It does
|
|
199
|
+
* not validate every market creation rule. Use {@link MarketParams} when
|
|
200
|
+
* constructing user-facing market params.
|
|
201
|
+
*
|
|
202
|
+
* @param market - Market to hash.
|
|
203
|
+
* @returns EIP-712 market struct hash.
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
207
|
+
*
|
|
208
|
+
* const hash = MarketUtils.hash({
|
|
209
|
+
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
210
|
+
* collateralParams: [
|
|
211
|
+
* {
|
|
212
|
+
* token: "0x0000000000000000000000000000000000000002",
|
|
213
|
+
* lltv: 770000000000000000n,
|
|
214
|
+
* maxLif: 1061007957559681697n,
|
|
215
|
+
* oracle: "0x0000000000000000000000000000000000000003",
|
|
216
|
+
* },
|
|
217
|
+
* ],
|
|
218
|
+
* maturity: 1n,
|
|
219
|
+
* rcfThreshold: 0n,
|
|
220
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
221
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
222
|
+
* });
|
|
223
|
+
* console.log(hash);
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
function hash(market) {
|
|
227
|
+
const marketParams = "params" in market ? market.params : market;
|
|
228
|
+
const collateralParams = marketParams.collateralParams.length === 0
|
|
229
|
+
? marketParams.collateralParams
|
|
230
|
+
: [...marketParams.collateralParams].sort((a, b) => a.token.toLowerCase() < b.token.toLowerCase() ? -1 : 1);
|
|
231
|
+
const collateralParamHashes = collateralParams.map((params) => keccak256(encodeAbiParameters(collateralParamsHashParams, [
|
|
232
|
+
COLLATERAL_PARAMS_TYPEHASH,
|
|
233
|
+
params.token,
|
|
234
|
+
BigInt(params.lltv),
|
|
235
|
+
BigInt(params.maxLif),
|
|
236
|
+
params.oracle,
|
|
237
|
+
])));
|
|
238
|
+
const collateralParamsHash = keccak256(concat(collateralParamHashes));
|
|
239
|
+
return keccak256(encodeAbiParameters(marketHashParams, [
|
|
240
|
+
MARKET_TYPEHASH,
|
|
241
|
+
marketParams.loanToken,
|
|
242
|
+
collateralParamsHash,
|
|
243
|
+
BigInt(marketParams.maturity),
|
|
244
|
+
BigInt(marketParams.rcfThreshold),
|
|
245
|
+
marketParams.enterGate,
|
|
246
|
+
marketParams.liquidatorGate,
|
|
247
|
+
]));
|
|
248
|
+
}
|
|
249
|
+
MarketUtils.hash = hash;
|
|
250
|
+
/**
|
|
251
|
+
* Computes the Midnight id for a market using `IdLib.toId`.
|
|
252
|
+
*
|
|
253
|
+
* @param params.market - Market to hash.
|
|
254
|
+
* @param params.chainId - EIP-155 chain id used to resolve the core Midnight address from the registry.
|
|
255
|
+
* @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
|
+
* @example
|
|
259
|
+
* ```ts
|
|
260
|
+
* import { ChainId, registerCustomAddresses } from "@morpho-org/morpho-ts";
|
|
261
|
+
* import { MarketUtils } from "@morpho-org/midnight-sdk";
|
|
262
|
+
*
|
|
263
|
+
* registerCustomAddresses({
|
|
264
|
+
* addresses: {
|
|
265
|
+
* [ChainId.BaseMainnet]: {
|
|
266
|
+
* midnight: "0x0000000000000000000000000000000000000001",
|
|
267
|
+
* },
|
|
268
|
+
* },
|
|
269
|
+
* });
|
|
270
|
+
*
|
|
271
|
+
* const id = MarketUtils.toId({
|
|
272
|
+
* market: {
|
|
273
|
+
* loanToken: "0x0000000000000000000000000000000000000001",
|
|
274
|
+
* collateralParams: [
|
|
275
|
+
* {
|
|
276
|
+
* token: "0x0000000000000000000000000000000000000002",
|
|
277
|
+
* lltv: 770000000000000000n,
|
|
278
|
+
* maxLif: 1061007957559681697n,
|
|
279
|
+
* oracle: "0x0000000000000000000000000000000000000003",
|
|
280
|
+
* },
|
|
281
|
+
* ],
|
|
282
|
+
* maturity: 1n,
|
|
283
|
+
* rcfThreshold: 0n,
|
|
284
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
285
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
286
|
+
* },
|
|
287
|
+
* chainId: ChainId.BaseMainnet,
|
|
288
|
+
* });
|
|
289
|
+
* console.log(id);
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
function toId(params) {
|
|
293
|
+
const chainId = BigInt(params.chainId);
|
|
294
|
+
const midnight = getChainAddress(Number(chainId), "midnight");
|
|
295
|
+
const encodedMarket = encodeAbiParameters([marketParamsAbiParameter], [toStruct(params.market)]);
|
|
296
|
+
const creationHash = keccak256(concat([SSTORE2_PREFIX, encodedMarket]));
|
|
297
|
+
return keccak256(encodePacked(["uint8", "address", "uint256", "bytes32"], [255, midnight, chainId, creationHash]));
|
|
298
|
+
}
|
|
299
|
+
MarketUtils.toId = toId;
|
|
300
|
+
})(MarketUtils || (MarketUtils = {}));
|