@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,425 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccrualPosition = exports.Position = void 0;
|
|
4
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
5
|
+
const Market_js_1 = require("./Market.js");
|
|
6
|
+
const PositionUtils_js_1 = require("./PositionUtils.js");
|
|
7
|
+
/**
|
|
8
|
+
* Midnight user position as stored by the core contract.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { Position } from "@morpho-org/midnight-sdk";
|
|
13
|
+
*
|
|
14
|
+
* const position = new Position({
|
|
15
|
+
* credit: 0n,
|
|
16
|
+
* pendingFee: 0n,
|
|
17
|
+
* lastLossFactor: 0n,
|
|
18
|
+
* lastAccrual: 0n,
|
|
19
|
+
* debt: 0n,
|
|
20
|
+
* collateralBitmap: 0n,
|
|
21
|
+
* collateral: [],
|
|
22
|
+
* });
|
|
23
|
+
* console.log(position.debt);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
class Position {
|
|
27
|
+
/** User credit. */
|
|
28
|
+
credit;
|
|
29
|
+
/** Pending continuous fee. */
|
|
30
|
+
pendingFee;
|
|
31
|
+
/** Last loss factor seen by the position. */
|
|
32
|
+
lastLossFactor;
|
|
33
|
+
/** Last accrual timestamp. */
|
|
34
|
+
lastAccrual;
|
|
35
|
+
/** User debt. */
|
|
36
|
+
debt;
|
|
37
|
+
/** Collateral bitmap. */
|
|
38
|
+
collateralBitmap;
|
|
39
|
+
/** Collateral balances by index. */
|
|
40
|
+
collateral;
|
|
41
|
+
constructor(position) {
|
|
42
|
+
this.credit = BigInt(position.credit);
|
|
43
|
+
this.pendingFee = BigInt(position.pendingFee);
|
|
44
|
+
this.lastLossFactor = BigInt(position.lastLossFactor);
|
|
45
|
+
this.lastAccrual = BigInt(position.lastAccrual);
|
|
46
|
+
this.debt = BigInt(position.debt);
|
|
47
|
+
this.collateralBitmap = BigInt(position.collateralBitmap);
|
|
48
|
+
this.collateral = position.collateral.map((assets) => BigInt(assets));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns credit net of pending fee, floored to zero for invalid raw inputs.
|
|
52
|
+
*
|
|
53
|
+
* @returns Credit minus pending fee.
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { Position } from "@morpho-org/midnight-sdk";
|
|
57
|
+
*
|
|
58
|
+
* const position = new Position({
|
|
59
|
+
* credit: 1_000n,
|
|
60
|
+
* pendingFee: 100n,
|
|
61
|
+
* lastLossFactor: 0n,
|
|
62
|
+
* lastAccrual: 0n,
|
|
63
|
+
* debt: 0n,
|
|
64
|
+
* collateralBitmap: 1n,
|
|
65
|
+
* collateral: [50n],
|
|
66
|
+
* });
|
|
67
|
+
* console.log(position.faceValue);
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
get faceValue() {
|
|
71
|
+
return morpho_ts_1.MathLib.zeroFloorSub(this.credit, this.pendingFee);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.Position = Position;
|
|
75
|
+
/**
|
|
76
|
+
* Midnight position paired with its hydrated market.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
|
|
81
|
+
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
|
|
82
|
+
*
|
|
83
|
+
* registerCustomAddresses({
|
|
84
|
+
* addresses: {
|
|
85
|
+
* 31337: {
|
|
86
|
+
* morpho: "0x0000000000000000000000000000000000000001",
|
|
87
|
+
* bundler3: {
|
|
88
|
+
* bundler3: "0x0000000000000000000000000000000000000002",
|
|
89
|
+
* generalAdapter1: "0x0000000000000000000000000000000000000003",
|
|
90
|
+
* },
|
|
91
|
+
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
|
|
92
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
93
|
+
* },
|
|
94
|
+
* },
|
|
95
|
+
* });
|
|
96
|
+
*
|
|
97
|
+
* const position = new AccrualPosition(
|
|
98
|
+
* {
|
|
99
|
+
* credit: 1_000n,
|
|
100
|
+
* pendingFee: 100n,
|
|
101
|
+
* lastLossFactor: 0n,
|
|
102
|
+
* lastAccrual: 1_000n,
|
|
103
|
+
* debt: 0n,
|
|
104
|
+
* collateralBitmap: 1n,
|
|
105
|
+
* collateral: [50n],
|
|
106
|
+
* },
|
|
107
|
+
* {
|
|
108
|
+
* chainId: 31337,
|
|
109
|
+
* params: {
|
|
110
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
111
|
+
* collateralParams: [
|
|
112
|
+
* {
|
|
113
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
114
|
+
* lltv: 770000000000000000n,
|
|
115
|
+
* maxLif: 1061007957559681697n,
|
|
116
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
117
|
+
* },
|
|
118
|
+
* ],
|
|
119
|
+
* maturity: 54_000n,
|
|
120
|
+
* rcfThreshold: 0n,
|
|
121
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
122
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
123
|
+
* },
|
|
124
|
+
* totalUnits: 1_000n,
|
|
125
|
+
* lossFactor: 0n,
|
|
126
|
+
* withdrawable: 500n,
|
|
127
|
+
* continuousFeeCredit: 0n,
|
|
128
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
129
|
+
* continuousFee: 10,
|
|
130
|
+
* tickSpacing: 4,
|
|
131
|
+
* },
|
|
132
|
+
* );
|
|
133
|
+
* console.log(position.market.id);
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
class AccrualPosition extends Position {
|
|
137
|
+
/** Hydrated market for this position. */
|
|
138
|
+
market;
|
|
139
|
+
constructor(position, market) {
|
|
140
|
+
super(position);
|
|
141
|
+
this.market = market instanceof Market_js_1.Market ? market : new Market_js_1.Market(market);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Returns the position collateral balance at an index configured by the market.
|
|
145
|
+
*
|
|
146
|
+
* @param index - Collateral index.
|
|
147
|
+
* @returns Collateral balance, or undefined when the index is not configured.
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
|
|
151
|
+
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
|
|
152
|
+
*
|
|
153
|
+
* registerCustomAddresses({
|
|
154
|
+
* addresses: {
|
|
155
|
+
* 31337: {
|
|
156
|
+
* morpho: "0x0000000000000000000000000000000000000001",
|
|
157
|
+
* bundler3: {
|
|
158
|
+
* bundler3: "0x0000000000000000000000000000000000000002",
|
|
159
|
+
* generalAdapter1: "0x0000000000000000000000000000000000000003",
|
|
160
|
+
* },
|
|
161
|
+
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
|
|
162
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
163
|
+
* },
|
|
164
|
+
* },
|
|
165
|
+
* });
|
|
166
|
+
*
|
|
167
|
+
* const position = new AccrualPosition(
|
|
168
|
+
* {
|
|
169
|
+
* credit: 1_000n,
|
|
170
|
+
* pendingFee: 100n,
|
|
171
|
+
* lastLossFactor: 0n,
|
|
172
|
+
* lastAccrual: 1_000n,
|
|
173
|
+
* debt: 0n,
|
|
174
|
+
* collateralBitmap: 1n,
|
|
175
|
+
* collateral: [50n],
|
|
176
|
+
* },
|
|
177
|
+
* {
|
|
178
|
+
* chainId: 31337,
|
|
179
|
+
* params: {
|
|
180
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
181
|
+
* collateralParams: [
|
|
182
|
+
* {
|
|
183
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
184
|
+
* lltv: 770000000000000000n,
|
|
185
|
+
* maxLif: 1061007957559681697n,
|
|
186
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
187
|
+
* },
|
|
188
|
+
* ],
|
|
189
|
+
* maturity: 54_000n,
|
|
190
|
+
* rcfThreshold: 0n,
|
|
191
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
192
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
193
|
+
* },
|
|
194
|
+
* totalUnits: 1_000n,
|
|
195
|
+
* lossFactor: 0n,
|
|
196
|
+
* withdrawable: 500n,
|
|
197
|
+
* continuousFeeCredit: 0n,
|
|
198
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
199
|
+
* continuousFee: 10,
|
|
200
|
+
* tickSpacing: 4,
|
|
201
|
+
* },
|
|
202
|
+
* );
|
|
203
|
+
* const balance = position.getCollateralBalanceByIndex(0);
|
|
204
|
+
* console.log(balance);
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
getCollateralBalanceByIndex(index) {
|
|
208
|
+
const normalizedIndex = BigInt(index);
|
|
209
|
+
if (this.market.getCollateralParamsByIndex(normalizedIndex) == null)
|
|
210
|
+
return undefined;
|
|
211
|
+
return this.collateral[Number(normalizedIndex)] ?? 0n;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Returns the position collateral balance for a configured token.
|
|
215
|
+
*
|
|
216
|
+
* @param token - Collateral token address.
|
|
217
|
+
* @returns Collateral balance, or undefined when the token is not configured.
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
|
|
221
|
+
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
|
|
222
|
+
*
|
|
223
|
+
* registerCustomAddresses({
|
|
224
|
+
* addresses: {
|
|
225
|
+
* 31337: {
|
|
226
|
+
* morpho: "0x0000000000000000000000000000000000000001",
|
|
227
|
+
* bundler3: {
|
|
228
|
+
* bundler3: "0x0000000000000000000000000000000000000002",
|
|
229
|
+
* generalAdapter1: "0x0000000000000000000000000000000000000003",
|
|
230
|
+
* },
|
|
231
|
+
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
|
|
232
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
233
|
+
* },
|
|
234
|
+
* },
|
|
235
|
+
* });
|
|
236
|
+
*
|
|
237
|
+
* const collateralToken = "0x0000000000000000000000000000000000007000";
|
|
238
|
+
* const position = new AccrualPosition(
|
|
239
|
+
* {
|
|
240
|
+
* credit: 1_000n,
|
|
241
|
+
* pendingFee: 100n,
|
|
242
|
+
* lastLossFactor: 0n,
|
|
243
|
+
* lastAccrual: 1_000n,
|
|
244
|
+
* debt: 0n,
|
|
245
|
+
* collateralBitmap: 1n,
|
|
246
|
+
* collateral: [50n],
|
|
247
|
+
* },
|
|
248
|
+
* {
|
|
249
|
+
* chainId: 31337,
|
|
250
|
+
* params: {
|
|
251
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
252
|
+
* collateralParams: [
|
|
253
|
+
* {
|
|
254
|
+
* token: collateralToken,
|
|
255
|
+
* lltv: 770000000000000000n,
|
|
256
|
+
* maxLif: 1061007957559681697n,
|
|
257
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
258
|
+
* },
|
|
259
|
+
* ],
|
|
260
|
+
* maturity: 54_000n,
|
|
261
|
+
* rcfThreshold: 0n,
|
|
262
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
263
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
264
|
+
* },
|
|
265
|
+
* totalUnits: 1_000n,
|
|
266
|
+
* lossFactor: 0n,
|
|
267
|
+
* withdrawable: 500n,
|
|
268
|
+
* continuousFeeCredit: 0n,
|
|
269
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
270
|
+
* continuousFee: 10,
|
|
271
|
+
* tickSpacing: 4,
|
|
272
|
+
* },
|
|
273
|
+
* );
|
|
274
|
+
* const balance = position.getCollateralBalanceByToken(collateralToken);
|
|
275
|
+
* console.log(balance);
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
getCollateralBalanceByToken(token) {
|
|
279
|
+
const index = this.market.getCollateralIndexByToken(token);
|
|
280
|
+
return index == null ? undefined : this.getCollateralBalanceByIndex(index);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Returns the market settlement fee at a timestamp.
|
|
284
|
+
*
|
|
285
|
+
* @param timestamp - Timestamp used to compute time to maturity.
|
|
286
|
+
* @returns WAD-scaled settlement fee.
|
|
287
|
+
* @example
|
|
288
|
+
* ```ts
|
|
289
|
+
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
|
|
290
|
+
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
|
|
291
|
+
*
|
|
292
|
+
* registerCustomAddresses({
|
|
293
|
+
* addresses: {
|
|
294
|
+
* 31337: {
|
|
295
|
+
* morpho: "0x0000000000000000000000000000000000000001",
|
|
296
|
+
* bundler3: {
|
|
297
|
+
* bundler3: "0x0000000000000000000000000000000000000002",
|
|
298
|
+
* generalAdapter1: "0x0000000000000000000000000000000000000003",
|
|
299
|
+
* },
|
|
300
|
+
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
|
|
301
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
302
|
+
* },
|
|
303
|
+
* },
|
|
304
|
+
* });
|
|
305
|
+
*
|
|
306
|
+
* const position = new AccrualPosition(
|
|
307
|
+
* {
|
|
308
|
+
* credit: 1_000n,
|
|
309
|
+
* pendingFee: 100n,
|
|
310
|
+
* lastLossFactor: 0n,
|
|
311
|
+
* lastAccrual: 1_000n,
|
|
312
|
+
* debt: 0n,
|
|
313
|
+
* collateralBitmap: 1n,
|
|
314
|
+
* collateral: [50n],
|
|
315
|
+
* },
|
|
316
|
+
* {
|
|
317
|
+
* chainId: 31337,
|
|
318
|
+
* params: {
|
|
319
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
320
|
+
* collateralParams: [
|
|
321
|
+
* {
|
|
322
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
323
|
+
* lltv: 770000000000000000n,
|
|
324
|
+
* maxLif: 1061007957559681697n,
|
|
325
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
326
|
+
* },
|
|
327
|
+
* ],
|
|
328
|
+
* maturity: 54_000n,
|
|
329
|
+
* rcfThreshold: 0n,
|
|
330
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
331
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
332
|
+
* },
|
|
333
|
+
* totalUnits: 1_000n,
|
|
334
|
+
* lossFactor: 0n,
|
|
335
|
+
* withdrawable: 500n,
|
|
336
|
+
* continuousFeeCredit: 0n,
|
|
337
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
338
|
+
* continuousFee: 10,
|
|
339
|
+
* tickSpacing: 4,
|
|
340
|
+
* },
|
|
341
|
+
* );
|
|
342
|
+
* const fee = position.getSettlementFee(0n);
|
|
343
|
+
* console.log(fee);
|
|
344
|
+
* ```
|
|
345
|
+
*/
|
|
346
|
+
getSettlementFee(timestamp) {
|
|
347
|
+
return this.market.getSettlementFee(this.market.timeToMaturity(timestamp));
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Returns a new position locally accrued like Midnight `updatePositionView`.
|
|
351
|
+
*
|
|
352
|
+
* @param timestamp - Timestamp at which to accrue. Must be greater than or equal to `lastAccrual`.
|
|
353
|
+
* @returns New accrual position with updated credit, pending fee, last loss factor, last accrual, and market continuous-fee credit.
|
|
354
|
+
* @throws {InvalidPositionAccrualTimestampError} when timestamp is before `lastAccrual`.
|
|
355
|
+
* @throws {InvalidPositionLossFactorError} when the market loss factor is older than the position loss factor.
|
|
356
|
+
* @throws {InvalidPositionAccrualStateError} when raw inputs violate Midnight accounting invariants.
|
|
357
|
+
* @example
|
|
358
|
+
* ```ts
|
|
359
|
+
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
|
|
360
|
+
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
|
|
361
|
+
*
|
|
362
|
+
* registerCustomAddresses({
|
|
363
|
+
* addresses: {
|
|
364
|
+
* 31337: {
|
|
365
|
+
* morpho: "0x0000000000000000000000000000000000000001",
|
|
366
|
+
* bundler3: {
|
|
367
|
+
* bundler3: "0x0000000000000000000000000000000000000002",
|
|
368
|
+
* generalAdapter1: "0x0000000000000000000000000000000000000003",
|
|
369
|
+
* },
|
|
370
|
+
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
|
|
371
|
+
* midnight: "0x0000000000000000000000000000000000001000",
|
|
372
|
+
* },
|
|
373
|
+
* },
|
|
374
|
+
* });
|
|
375
|
+
*
|
|
376
|
+
* const position = new AccrualPosition(
|
|
377
|
+
* {
|
|
378
|
+
* credit: 1_000n,
|
|
379
|
+
* pendingFee: 100n,
|
|
380
|
+
* lastLossFactor: 0n,
|
|
381
|
+
* lastAccrual: 1_000n,
|
|
382
|
+
* debt: 0n,
|
|
383
|
+
* collateralBitmap: 1n,
|
|
384
|
+
* collateral: [50n],
|
|
385
|
+
* },
|
|
386
|
+
* {
|
|
387
|
+
* chainId: 31337,
|
|
388
|
+
* params: {
|
|
389
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
390
|
+
* collateralParams: [
|
|
391
|
+
* {
|
|
392
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
393
|
+
* lltv: 770000000000000000n,
|
|
394
|
+
* maxLif: 1061007957559681697n,
|
|
395
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
396
|
+
* },
|
|
397
|
+
* ],
|
|
398
|
+
* maturity: 54_000n,
|
|
399
|
+
* rcfThreshold: 0n,
|
|
400
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
401
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
402
|
+
* },
|
|
403
|
+
* totalUnits: 1_000n,
|
|
404
|
+
* lossFactor: 0n,
|
|
405
|
+
* withdrawable: 500n,
|
|
406
|
+
* continuousFeeCredit: 0n,
|
|
407
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
408
|
+
* continuousFee: 10,
|
|
409
|
+
* tickSpacing: 4,
|
|
410
|
+
* },
|
|
411
|
+
* );
|
|
412
|
+
* const accrued = position.accrueInterest(1_500n);
|
|
413
|
+
* console.log(accrued.credit);
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
accrueInterest(timestamp) {
|
|
417
|
+
const { position, market } = PositionUtils_js_1.PositionUtils.accrueInterest({
|
|
418
|
+
position: this,
|
|
419
|
+
market: this.market,
|
|
420
|
+
timestamp,
|
|
421
|
+
});
|
|
422
|
+
return new AccrualPosition(position, market);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
exports.AccrualPosition = AccrualPosition;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { type BigIntish } from "@morpho-org/morpho-ts";
|
|
2
|
+
import type { IMarket } from "./Market.js";
|
|
3
|
+
import type { IPosition } from "./Position.js";
|
|
4
|
+
/**
|
|
5
|
+
* Domain helpers for Midnight positions.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { PositionUtils } from "@morpho-org/midnight-sdk";
|
|
10
|
+
*
|
|
11
|
+
* const accrued = PositionUtils.accrueInterest({
|
|
12
|
+
* position: {
|
|
13
|
+
* credit: 1_000n,
|
|
14
|
+
* pendingFee: 100n,
|
|
15
|
+
* lastLossFactor: 0n,
|
|
16
|
+
* lastAccrual: 1_000n,
|
|
17
|
+
* debt: 0n,
|
|
18
|
+
* collateralBitmap: 1n,
|
|
19
|
+
* collateral: [50n],
|
|
20
|
+
* },
|
|
21
|
+
* market: {
|
|
22
|
+
* chainId: 8453,
|
|
23
|
+
* params: {
|
|
24
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
25
|
+
* collateralParams: [
|
|
26
|
+
* {
|
|
27
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
28
|
+
* lltv: 770000000000000000n,
|
|
29
|
+
* maxLif: 1061007957559681697n,
|
|
30
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
31
|
+
* },
|
|
32
|
+
* ],
|
|
33
|
+
* maturity: 54_000n,
|
|
34
|
+
* rcfThreshold: 0n,
|
|
35
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
36
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
37
|
+
* },
|
|
38
|
+
* totalUnits: 1_000n,
|
|
39
|
+
* lossFactor: 0n,
|
|
40
|
+
* withdrawable: 500n,
|
|
41
|
+
* continuousFeeCredit: 0n,
|
|
42
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
43
|
+
* continuousFee: 10,
|
|
44
|
+
* tickSpacing: 4,
|
|
45
|
+
* },
|
|
46
|
+
* timestamp: 1_500n,
|
|
47
|
+
* });
|
|
48
|
+
* console.log(accrued.position.credit);
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare namespace PositionUtils {
|
|
52
|
+
/**
|
|
53
|
+
* Returns plain Midnight position and market objects accrued like Midnight `updatePositionView`.
|
|
54
|
+
*
|
|
55
|
+
* @param params.position - Position state to accrue.
|
|
56
|
+
* @param params.market - Hydrated market state used for loss factor and continuous-fee accrual.
|
|
57
|
+
* @param params.timestamp - Timestamp at which to accrue.
|
|
58
|
+
* @returns New plain position and market objects with updated credit, pending fee, last loss factor, last accrual, and market continuous-fee credit.
|
|
59
|
+
* @throws {InvalidPositionAccrualTimestampError} when timestamp is before `lastAccrual`.
|
|
60
|
+
* @throws {InvalidPositionLossFactorError} when the market loss factor is older than the position loss factor.
|
|
61
|
+
* @throws {InvalidPositionAccrualStateError} when raw inputs violate Midnight accounting invariants.
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { PositionUtils } from "@morpho-org/midnight-sdk";
|
|
65
|
+
*
|
|
66
|
+
* const { position, market } = PositionUtils.accrueInterest({
|
|
67
|
+
* position: {
|
|
68
|
+
* credit: 1_000n,
|
|
69
|
+
* pendingFee: 100n,
|
|
70
|
+
* lastLossFactor: 0n,
|
|
71
|
+
* lastAccrual: 1_000n,
|
|
72
|
+
* debt: 0n,
|
|
73
|
+
* collateralBitmap: 1n,
|
|
74
|
+
* collateral: [50n],
|
|
75
|
+
* },
|
|
76
|
+
* market: {
|
|
77
|
+
* chainId: 8453,
|
|
78
|
+
* params: {
|
|
79
|
+
* loanToken: "0x0000000000000000000000000000000000006000",
|
|
80
|
+
* collateralParams: [
|
|
81
|
+
* {
|
|
82
|
+
* token: "0x0000000000000000000000000000000000007000",
|
|
83
|
+
* lltv: 770000000000000000n,
|
|
84
|
+
* maxLif: 1061007957559681697n,
|
|
85
|
+
* oracle: "0x0000000000000000000000000000000000008000",
|
|
86
|
+
* },
|
|
87
|
+
* ],
|
|
88
|
+
* maturity: 54_000n,
|
|
89
|
+
* rcfThreshold: 0n,
|
|
90
|
+
* enterGate: "0x0000000000000000000000000000000000000000",
|
|
91
|
+
* liquidatorGate: "0x0000000000000000000000000000000000000000",
|
|
92
|
+
* },
|
|
93
|
+
* totalUnits: 1_000n,
|
|
94
|
+
* lossFactor: 0n,
|
|
95
|
+
* withdrawable: 500n,
|
|
96
|
+
* continuousFeeCredit: 0n,
|
|
97
|
+
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
|
|
98
|
+
* continuousFee: 10,
|
|
99
|
+
* tickSpacing: 4,
|
|
100
|
+
* },
|
|
101
|
+
* timestamp: 1_500n,
|
|
102
|
+
* });
|
|
103
|
+
* console.log(position.lastAccrual, market.continuousFeeCredit);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
function accrueInterest(params: {
|
|
107
|
+
readonly position: IPosition;
|
|
108
|
+
readonly market: IMarket;
|
|
109
|
+
readonly timestamp: BigIntish;
|
|
110
|
+
}): {
|
|
111
|
+
readonly position: IPosition;
|
|
112
|
+
readonly market: IMarket;
|
|
113
|
+
readonly accruedFee: bigint;
|
|
114
|
+
};
|
|
115
|
+
}
|