@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.
Files changed (128) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -46
  3. package/lib/cjs/abis.d.ts +1185 -996
  4. package/lib/cjs/abis.js +1445 -1211
  5. package/lib/cjs/api/MidnightApi.d.ts +22 -16
  6. package/lib/cjs/api/MidnightApi.js +89 -19
  7. package/lib/cjs/api/helpers.d.ts +11 -1
  8. package/lib/cjs/api/helpers.js +54 -4
  9. package/lib/cjs/api/types.d.ts +35 -9
  10. package/lib/cjs/constants.d.ts +12 -11
  11. package/lib/cjs/constants.js +14 -12
  12. package/lib/cjs/errors.d.ts +89 -1
  13. package/lib/cjs/errors.js +114 -3
  14. package/lib/cjs/fetch/Market.js +37 -3
  15. package/lib/cjs/fetch/index.d.ts +0 -1
  16. package/lib/cjs/fetch/index.js +0 -1
  17. package/lib/cjs/fetch/utils.d.ts +1 -1
  18. package/lib/cjs/fetch/utils.js +9 -2
  19. package/lib/cjs/index.d.ts +1 -1
  20. package/lib/cjs/index.js +2 -23
  21. package/lib/cjs/market/Market.d.ts +99 -33
  22. package/lib/cjs/market/Market.js +137 -28
  23. package/lib/cjs/market/MarketUtils.d.ts +61 -41
  24. package/lib/cjs/market/MarketUtils.js +108 -48
  25. package/lib/cjs/market/Position.d.ts +16 -11
  26. package/lib/cjs/market/Position.js +18 -12
  27. package/lib/cjs/market/PositionUtils.d.ts +6 -4
  28. package/lib/cjs/market/PositionUtils.js +9 -6
  29. package/lib/cjs/math/TickLib.d.ts +22 -0
  30. package/lib/cjs/math/TickLib.js +30 -0
  31. package/lib/cjs/math/index.d.ts +0 -1
  32. package/lib/cjs/math/index.js +0 -1
  33. package/lib/cjs/offers/Offer.d.ts +218 -12
  34. package/lib/cjs/offers/Offer.js +215 -7
  35. package/lib/cjs/offers/OfferUtils.d.ts +249 -143
  36. package/lib/cjs/offers/OfferUtils.js +326 -45
  37. package/lib/cjs/queries/GetPosition.d.ts +1 -1
  38. package/lib/cjs/queries/GetPosition.js +1 -1
  39. package/lib/cjs/signatures/EcrecoverRatifierUtils.d.ts +184 -38
  40. package/lib/cjs/signatures/EcrecoverRatifierUtils.js +296 -80
  41. package/lib/cjs/signatures/Group.d.ts +12 -4
  42. package/lib/cjs/signatures/Group.js +17 -7
  43. package/lib/cjs/signatures/GroupUtils.d.ts +60 -4
  44. package/lib/cjs/signatures/GroupUtils.js +57 -2
  45. package/lib/cjs/signatures/Payload.d.ts +277 -277
  46. package/lib/cjs/signatures/Payload.js +517 -534
  47. package/lib/cjs/signatures/RatifierUtils.d.ts +57 -0
  48. package/lib/cjs/signatures/RatifierUtils.js +105 -0
  49. package/lib/cjs/signatures/SetterRatifierUtils.d.ts +52 -13
  50. package/lib/cjs/signatures/SetterRatifierUtils.js +62 -19
  51. package/lib/cjs/signatures/Tree.d.ts +28 -16
  52. package/lib/cjs/signatures/Tree.js +35 -15
  53. package/lib/cjs/signatures/TreeUtils.d.ts +185 -63
  54. package/lib/cjs/signatures/TreeUtils.js +89 -79
  55. package/lib/cjs/signatures/eip712.d.ts +22 -0
  56. package/lib/cjs/signatures/eip712.js +26 -0
  57. package/lib/cjs/signatures/index.d.ts +2 -0
  58. package/lib/cjs/signatures/index.js +4 -0
  59. package/lib/cjs/signatures/offerStructInternal.d.ts +14 -0
  60. package/lib/cjs/signatures/offerStructInternal.js +72 -0
  61. package/lib/esm/abis.d.ts +1185 -996
  62. package/lib/esm/abis.js +1445 -1211
  63. package/lib/esm/api/MidnightApi.d.ts +22 -16
  64. package/lib/esm/api/MidnightApi.js +91 -21
  65. package/lib/esm/api/helpers.d.ts +11 -1
  66. package/lib/esm/api/helpers.js +54 -5
  67. package/lib/esm/api/types.d.ts +35 -9
  68. package/lib/esm/constants.d.ts +12 -11
  69. package/lib/esm/constants.js +13 -11
  70. package/lib/esm/errors.d.ts +89 -1
  71. package/lib/esm/errors.js +108 -2
  72. package/lib/esm/fetch/Market.js +38 -4
  73. package/lib/esm/fetch/index.d.ts +0 -1
  74. package/lib/esm/fetch/index.js +0 -1
  75. package/lib/esm/fetch/utils.d.ts +1 -1
  76. package/lib/esm/fetch/utils.js +9 -2
  77. package/lib/esm/index.d.ts +1 -1
  78. package/lib/esm/index.js +1 -1
  79. package/lib/esm/market/Market.d.ts +99 -33
  80. package/lib/esm/market/Market.js +137 -28
  81. package/lib/esm/market/MarketUtils.d.ts +61 -41
  82. package/lib/esm/market/MarketUtils.js +109 -49
  83. package/lib/esm/market/Position.d.ts +16 -11
  84. package/lib/esm/market/Position.js +19 -13
  85. package/lib/esm/market/PositionUtils.d.ts +6 -4
  86. package/lib/esm/market/PositionUtils.js +9 -6
  87. package/lib/esm/math/TickLib.d.ts +22 -0
  88. package/lib/esm/math/TickLib.js +31 -1
  89. package/lib/esm/math/index.d.ts +0 -1
  90. package/lib/esm/math/index.js +0 -1
  91. package/lib/esm/offers/Offer.d.ts +218 -12
  92. package/lib/esm/offers/Offer.js +216 -8
  93. package/lib/esm/offers/OfferUtils.d.ts +249 -143
  94. package/lib/esm/offers/OfferUtils.js +329 -48
  95. package/lib/esm/queries/GetPosition.d.ts +1 -1
  96. package/lib/esm/queries/GetPosition.js +1 -1
  97. package/lib/esm/signatures/EcrecoverRatifierUtils.d.ts +184 -38
  98. package/lib/esm/signatures/EcrecoverRatifierUtils.js +298 -82
  99. package/lib/esm/signatures/Group.d.ts +12 -4
  100. package/lib/esm/signatures/Group.js +17 -7
  101. package/lib/esm/signatures/GroupUtils.d.ts +60 -4
  102. package/lib/esm/signatures/GroupUtils.js +57 -2
  103. package/lib/esm/signatures/Payload.d.ts +277 -277
  104. package/lib/esm/signatures/Payload.js +517 -531
  105. package/lib/esm/signatures/RatifierUtils.d.ts +57 -0
  106. package/lib/esm/signatures/RatifierUtils.js +105 -0
  107. package/lib/esm/signatures/SetterRatifierUtils.d.ts +52 -13
  108. package/lib/esm/signatures/SetterRatifierUtils.js +62 -19
  109. package/lib/esm/signatures/Tree.d.ts +28 -16
  110. package/lib/esm/signatures/Tree.js +36 -16
  111. package/lib/esm/signatures/TreeUtils.d.ts +185 -63
  112. package/lib/esm/signatures/TreeUtils.js +91 -81
  113. package/lib/esm/signatures/eip712.d.ts +22 -0
  114. package/lib/esm/signatures/eip712.js +22 -0
  115. package/lib/esm/signatures/index.d.ts +2 -0
  116. package/lib/esm/signatures/index.js +2 -0
  117. package/lib/esm/signatures/offerStructInternal.d.ts +14 -0
  118. package/lib/esm/signatures/offerStructInternal.js +67 -0
  119. package/package.json +31 -34
  120. package/lib/cjs/fetch/Offer.d.ts +0 -74
  121. package/lib/cjs/fetch/Offer.js +0 -119
  122. package/lib/cjs/math/ConsumableUnitsLib.d.ts +0 -57
  123. package/lib/cjs/math/ConsumableUnitsLib.js +0 -77
  124. package/lib/esm/fetch/Offer.d.ts +0 -74
  125. package/lib/esm/fetch/Offer.js +0 -116
  126. package/lib/esm/math/ConsumableUnitsLib.d.ts +0 -57
  127. package/lib/esm/math/ConsumableUnitsLib.js +0 -74
  128. 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.maxLif - WAD-scaled maximum liquidation incentive factor.
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
- * maxLif: 1061007957559681697n,
30
+ * liquidationCursor: 250000000000000000n,
31
31
  * oracle: "0x0000000000000000000000000000000000000002",
32
32
  * });
33
- * console.log(collateral.maxLif);
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
- * maxLif: 1061007957559681697n,
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, usually `LIQUIDATION_CURSOR_LOW` or `LIQUIDATION_CURSOR_HIGH`.
103
+ * @param cursor - WAD-scaled liquidation cursor.
70
104
  * @returns WAD-scaled liquidation incentive factor.
71
105
  * @example
72
106
  * ```ts
73
- * import { LIQUIDATION_CURSOR_LOW, MarketUtils } from "@morpho-org/midnight-sdk";
107
+ * import { MarketUtils } from "@morpho-org/midnight-sdk";
74
108
  *
75
109
  * const liquidationIncentiveFactor = MarketUtils.getLiquidationIncentiveFactor(
76
110
  * 770000000000000000n,
77
- * LIQUIDATION_CURSOR_LOW,
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
- * maxLif: 1061007957559681697n,
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 params.market - Market to hash.
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
- * market: {
165
- * loanToken: "0x0000000000000000000000000000000000000001",
166
- * collateralParams: [
167
- * {
168
- * token: "0x0000000000000000000000000000000000000002",
169
- * lltv: 770000000000000000n,
170
- * maxLif: 1061007957559681697n,
171
- * oracle: "0x0000000000000000000000000000000000000003",
172
- * },
173
- * ],
174
- * maturity: 1n,
175
- * rcfThreshold: 0n,
176
- * enterGate: "0x0000000000000000000000000000000000000000",
177
- * liquidatorGate: "0x0000000000000000000000000000000000000000",
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(params: {
185
- readonly market: MarketInput;
186
- readonly chainId: BigIntish;
187
- }): `0x${string}`;
207
+ function toId(market: MarketInput): `0x${string}`;
188
208
  }
189
209
  export {};
@@ -1,17 +1,20 @@
1
- import { assertNonNegative, getChainAddress, MathLib, } from "@morpho-org/morpho-ts";
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: "maxLif", type: "uint256" },
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: "maxLif", type: "uint256" },
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.maxLif - WAD-scaled maximum liquidation incentive factor.
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
- * maxLif: 1061007957559681697n,
74
+ * liquidationCursor: 250000000000000000n,
70
75
  * oracle: "0x0000000000000000000000000000000000000002",
71
76
  * });
72
- * console.log(collateral.maxLif);
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: BigInt(params.lltv),
79
- maxLif: BigInt(params.maxLif),
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
- * maxLif: 1061007957559681697n,
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
- maxLif: collateral.maxLif,
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, usually `LIQUIDATION_CURSOR_LOW` or `LIQUIDATION_CURSOR_HIGH`.
202
+ * @param cursor - WAD-scaled liquidation cursor.
133
203
  * @returns WAD-scaled liquidation incentive factor.
134
204
  * @example
135
205
  * ```ts
136
- * import { LIQUIDATION_CURSOR_LOW, MarketUtils } from "@morpho-org/midnight-sdk";
206
+ * import { MarketUtils } from "@morpho-org/midnight-sdk";
137
207
  *
138
208
  * const liquidationIncentiveFactor = MarketUtils.getLiquidationIncentiveFactor(
139
209
  * 770000000000000000n,
140
- * LIQUIDATION_CURSOR_LOW,
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
- * maxLif: 1061007957559681697n,
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.maxLif),
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 params.market - Market to hash.
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
- * 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,
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(params) {
293
- const chainId = BigInt(params.chainId);
294
- const midnight = getChainAddress(Number(chainId), "midnight");
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, chainId, creationHash]));
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
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 = market instanceof Market ? market : new Market(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
- * maxLif: 1061007957559681697n,
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.getCollateralParamsByIndex(normalizedIndex) == null)
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
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
- * maxLif: 1061007957559681697n,
396
+ * liquidationCursor: 250000000000000000n,
391
397
  * oracle: "0x0000000000000000000000000000000000008000",
392
398
  * },
393
399
  * ],