@black-market/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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +111 -0
  3. package/dist/abis.d.ts +2573 -0
  4. package/dist/abis.d.ts.map +1 -0
  5. package/dist/abis.js +1875 -0
  6. package/dist/abis.js.map +1 -0
  7. package/dist/abyss.d.ts +3650 -0
  8. package/dist/abyss.d.ts.map +1 -0
  9. package/dist/abyss.js +1522 -0
  10. package/dist/abyss.js.map +1 -0
  11. package/dist/addresses.d.ts +211 -0
  12. package/dist/addresses.d.ts.map +1 -0
  13. package/dist/addresses.js +234 -0
  14. package/dist/addresses.js.map +1 -0
  15. package/dist/auction.d.ts +57 -0
  16. package/dist/auction.d.ts.map +1 -0
  17. package/dist/auction.js +88 -0
  18. package/dist/auction.js.map +1 -0
  19. package/dist/client.d.ts +12 -0
  20. package/dist/client.d.ts.map +1 -0
  21. package/dist/client.js +18 -0
  22. package/dist/client.js.map +1 -0
  23. package/dist/format.d.ts +15 -0
  24. package/dist/format.d.ts.map +1 -0
  25. package/dist/format.js +78 -0
  26. package/dist/format.js.map +1 -0
  27. package/dist/index.d.ts +9 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +9 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/launch.d.ts +225 -0
  32. package/dist/launch.d.ts.map +1 -0
  33. package/dist/launch.js +511 -0
  34. package/dist/launch.js.map +1 -0
  35. package/dist/live.d.ts +185 -0
  36. package/dist/live.d.ts.map +1 -0
  37. package/dist/live.js +205 -0
  38. package/dist/live.js.map +1 -0
  39. package/package.json +51 -0
  40. package/src/abis.ts +1896 -0
  41. package/src/abyss.ts +1574 -0
  42. package/src/addresses.ts +342 -0
  43. package/src/auction.ts +107 -0
  44. package/src/client.ts +37 -0
  45. package/src/format.ts +73 -0
  46. package/src/index.ts +8 -0
  47. package/src/launch.ts +724 -0
  48. package/src/live.ts +393 -0
package/src/live.ts ADDED
@@ -0,0 +1,393 @@
1
+ import type { Address } from "viem";
2
+ import { RAY, rayAprToApyPercent } from "./format.js";
3
+
4
+ /** Native ETH sentinel used by `WalletBalanceProvider`. */
5
+ export const MOCK_ETH_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE" as Address;
6
+
7
+ export const MARKET_REFERENCE_CURRENCY_UNIT = 10n ** 18n;
8
+
9
+ export function rayMul(a: bigint, b: bigint): bigint {
10
+ return (a * b + RAY / 2n) / RAY;
11
+ }
12
+
13
+ export function usd18ToNumber(price: bigint): number {
14
+ if (price === 0n) return 0;
15
+ return Number(price) / 1e18;
16
+ }
17
+
18
+ export function tokensToNumber(amount: bigint, decimals: number): number {
19
+ if (amount === 0n) return 0;
20
+ return Number(amount) / 10 ** decimals;
21
+ }
22
+
23
+ export type CatalogReserveFields = {
24
+ token: Address;
25
+ decimals: number;
26
+ priceUsd: number;
27
+ supplyApy: number;
28
+ variableBorrowApy: number;
29
+ totalSupplyUsd: number;
30
+ totalBorrowUsd: number;
31
+ supplyCapUsd?: number;
32
+ borrowCapUsd?: number;
33
+ borrowingEnabled: boolean;
34
+ stableBorrowEnabled: boolean;
35
+ collateralEnabled: boolean;
36
+ ltvBps: number;
37
+ liquidationThresholdBps: number;
38
+ liquidationBonusBps: number;
39
+ reserveFactorBps: number;
40
+ };
41
+
42
+ export type LiveReserve = {
43
+ token: Address;
44
+ symbol: string;
45
+ name: string;
46
+ isActive: boolean;
47
+ decimals: number;
48
+ priceUsd: number;
49
+ collateralPriceUsd?: number;
50
+ debtPriceUsd?: number;
51
+ supplyApy: number;
52
+ variableBorrowApy: number;
53
+ totalSupplyUsd: number;
54
+ totalBorrowUsd: number;
55
+ supplyCapUsd?: number;
56
+ borrowCapUsd?: number;
57
+ isFrozen?: boolean;
58
+ isIsolated?: boolean;
59
+ borrowableInIsolation?: boolean;
60
+ debtCeilingUsd?: number;
61
+ isolationModeTotalDebtUsd?: number;
62
+ borrowingEnabled?: boolean;
63
+ totalSupply: bigint;
64
+ totalBorrow: bigint;
65
+ supplyCap: bigint;
66
+ borrowCap: bigint;
67
+ stableBorrowEnabled?: boolean;
68
+ collateralEnabled?: boolean;
69
+ ltvBps?: number;
70
+ liquidationThresholdBps?: number;
71
+ liquidationBonusBps?: number;
72
+ reserveFactorBps?: number;
73
+ availableLiquidity: bigint;
74
+ liquidityIndex: bigint;
75
+ variableBorrowIndex: bigint;
76
+ aToken?: Address;
77
+ stableDebtToken?: Address;
78
+ variableDebtToken?: Address;
79
+ };
80
+
81
+ export type LiveUserReserve = {
82
+ token: Address;
83
+ supplied: number;
84
+ borrowed: number;
85
+ isCollateral: boolean;
86
+ suppliedRaw: bigint;
87
+ borrowedRaw: bigint;
88
+ };
89
+
90
+ export type UiAggregatedReserve = {
91
+ underlyingAsset: Address;
92
+ name: string;
93
+ symbol: string;
94
+ decimals: bigint;
95
+ baseLTVasCollateral: bigint;
96
+ reserveLiquidationThreshold: bigint;
97
+ reserveLiquidationBonus: bigint;
98
+ reserveFactor: bigint;
99
+ usageAsCollateralEnabled: boolean;
100
+ borrowingEnabled: boolean;
101
+ stableBorrowRateEnabled: boolean;
102
+ isActive: boolean;
103
+ isFrozen: boolean;
104
+ liquidityIndex: bigint;
105
+ variableBorrowIndex: bigint;
106
+ liquidityRate: bigint;
107
+ variableBorrowRate: bigint;
108
+ availableLiquidity: bigint;
109
+ totalPrincipalStableDebt: bigint;
110
+ totalScaledVariableDebt: bigint;
111
+ priceInUsd: bigint;
112
+ supplyCap: bigint;
113
+ borrowCap: bigint;
114
+ };
115
+
116
+ export type UiUserReserve = {
117
+ underlyingAsset: Address;
118
+ scaledATokenBalance: bigint;
119
+ usageAsCollateralEnabledOnUser: boolean;
120
+ scaledVariableDebt: bigint;
121
+ principalStableDebt: bigint;
122
+ };
123
+
124
+ /** Index helper rows by underlying token. The pool reserve list is the listing set. */
125
+ export function indexByToken<T extends { token: Address }>(rows: T[]): Map<string, T> {
126
+ const map = new Map<string, T>();
127
+ for (const row of rows) map.set(row.token.toLowerCase(), row);
128
+ return map;
129
+ }
130
+
131
+ export function liveReserveFromUi(row: UiAggregatedReserve): LiveReserve {
132
+ const decimals = Number(row.decimals);
133
+ const variableDebt = rayMul(row.totalScaledVariableDebt, row.variableBorrowIndex);
134
+ const totalBorrow = row.totalPrincipalStableDebt + variableDebt;
135
+ const totalSupply = row.availableLiquidity + totalBorrow;
136
+ const priceUsd = usd18ToNumber(row.priceInUsd);
137
+ return {
138
+ token: row.underlyingAsset,
139
+ symbol: row.symbol,
140
+ name: row.name,
141
+ isActive: row.isActive,
142
+ decimals,
143
+ priceUsd,
144
+ supplyApy: rayAprToApyPercent(row.liquidityRate),
145
+ variableBorrowApy: rayAprToApyPercent(row.variableBorrowRate),
146
+ totalSupplyUsd: tokensToNumber(totalSupply, decimals) * priceUsd,
147
+ totalBorrowUsd: tokensToNumber(totalBorrow, decimals) * priceUsd,
148
+ supplyCapUsd: Number(row.supplyCap) * priceUsd,
149
+ borrowCapUsd: Number(row.borrowCap) * priceUsd,
150
+ totalSupply,
151
+ totalBorrow,
152
+ supplyCap: row.supplyCap,
153
+ borrowCap: row.borrowCap,
154
+ borrowingEnabled: row.borrowingEnabled && row.isActive && !row.isFrozen,
155
+ stableBorrowEnabled: row.stableBorrowRateEnabled && row.isActive && !row.isFrozen,
156
+ collateralEnabled: row.usageAsCollateralEnabled,
157
+ ltvBps: Number(row.baseLTVasCollateral),
158
+ liquidationThresholdBps: Number(row.reserveLiquidationThreshold),
159
+ liquidationBonusBps: Number(row.reserveLiquidationBonus),
160
+ reserveFactorBps: Number(row.reserveFactor),
161
+ availableLiquidity: row.availableLiquidity,
162
+ liquidityIndex: row.liquidityIndex,
163
+ variableBorrowIndex: row.variableBorrowIndex,
164
+ };
165
+ }
166
+
167
+ export function liveUserFromUi(user: UiUserReserve, reserve: LiveReserve): LiveUserReserve {
168
+ const suppliedRaw = rayMul(user.scaledATokenBalance, reserve.liquidityIndex);
169
+ const variableDebt = rayMul(user.scaledVariableDebt, reserve.variableBorrowIndex);
170
+ const borrowedRaw = variableDebt + user.principalStableDebt;
171
+ return {
172
+ token: user.underlyingAsset,
173
+ supplied: tokensToNumber(suppliedRaw, reserve.decimals),
174
+ borrowed: tokensToNumber(borrowedRaw, reserve.decimals),
175
+ isCollateral: user.usageAsCollateralEnabledOnUser,
176
+ suppliedRaw,
177
+ borrowedRaw,
178
+ };
179
+ }
180
+
181
+ export type LensReserve = {
182
+ token: Address;
183
+ aToken: Address;
184
+ stableDebtToken: Address;
185
+ variableDebtToken: Address;
186
+ decimals: bigint;
187
+ isActive: boolean;
188
+ isFrozen: boolean;
189
+ borrowingEnabled: boolean;
190
+ stableBorrowEnabled: boolean;
191
+ collateralEnabled: boolean;
192
+ ltvBps: bigint;
193
+ liquidationThresholdBps: bigint;
194
+ liquidationBonusBps: bigint;
195
+ reserveFactorBps: bigint;
196
+ priceUsd: bigint;
197
+ collateralPriceUsd: bigint;
198
+ debtPriceUsd: bigint;
199
+ isIsolated: boolean;
200
+ borrowableInIsolation: boolean;
201
+ debtCeiling: bigint;
202
+ isolationModeTotalDebt: bigint;
203
+ liquidityRate: bigint;
204
+ variableBorrowRate: bigint;
205
+ availableLiquidity: bigint;
206
+ totalStableDebt: bigint;
207
+ totalVariableDebt: bigint;
208
+ supplyCap: bigint;
209
+ borrowCap: bigint;
210
+ };
211
+
212
+ export type LensPosition = {
213
+ token: Address;
214
+ supplied: bigint;
215
+ borrowed: bigint;
216
+ wallet: bigint;
217
+ isCollateral: boolean;
218
+ };
219
+
220
+ export function liveReserveFromLens(row: LensReserve): LiveReserve {
221
+ const decimals = Number(row.decimals);
222
+ const totalBorrow = row.totalStableDebt + row.totalVariableDebt;
223
+ const totalSupply = row.availableLiquidity + totalBorrow;
224
+ const priceUsd = usd18ToNumber(row.priceUsd);
225
+ return {
226
+ token: row.token,
227
+ symbol: "",
228
+ name: "",
229
+ isActive: row.isActive,
230
+ decimals,
231
+ priceUsd,
232
+ collateralPriceUsd: usd18ToNumber(row.collateralPriceUsd),
233
+ debtPriceUsd: usd18ToNumber(row.debtPriceUsd),
234
+ supplyApy: rayAprToApyPercent(row.liquidityRate),
235
+ variableBorrowApy: rayAprToApyPercent(row.variableBorrowRate),
236
+ totalSupplyUsd: tokensToNumber(totalSupply, decimals) * priceUsd,
237
+ totalBorrowUsd: tokensToNumber(totalBorrow, decimals) * priceUsd,
238
+ supplyCapUsd: Number(row.supplyCap) * priceUsd,
239
+ borrowCapUsd: Number(row.borrowCap) * priceUsd,
240
+ isFrozen: row.isFrozen,
241
+ isIsolated: row.isIsolated,
242
+ borrowableInIsolation: row.borrowableInIsolation,
243
+ debtCeilingUsd: Number(row.debtCeiling),
244
+ totalSupply,
245
+ totalBorrow,
246
+ supplyCap: row.supplyCap,
247
+ borrowCap: row.borrowCap,
248
+ isolationModeTotalDebtUsd: usd18ToNumber(row.isolationModeTotalDebt),
249
+ borrowingEnabled: row.borrowingEnabled && row.isActive && !row.isFrozen,
250
+ stableBorrowEnabled: row.stableBorrowEnabled && row.isActive && !row.isFrozen,
251
+ collateralEnabled: row.collateralEnabled,
252
+ ltvBps: Number(row.ltvBps),
253
+ liquidationThresholdBps: Number(row.liquidationThresholdBps),
254
+ liquidationBonusBps: Number(row.liquidationBonusBps),
255
+ reserveFactorBps: Number(row.reserveFactorBps),
256
+ availableLiquidity: row.availableLiquidity,
257
+ liquidityIndex: 0n,
258
+ variableBorrowIndex: 0n,
259
+ aToken: row.aToken,
260
+ stableDebtToken: row.stableDebtToken,
261
+ variableDebtToken: row.variableDebtToken,
262
+ };
263
+ }
264
+
265
+ export function liveUserFromLens(row: LensPosition, decimals: number): LiveUserReserve {
266
+ return {
267
+ token: row.token,
268
+ supplied: tokensToNumber(row.supplied, decimals),
269
+ borrowed: tokensToNumber(row.borrowed, decimals),
270
+ isCollateral: row.isCollateral,
271
+ suppliedRaw: row.supplied,
272
+ borrowedRaw: row.borrowed,
273
+ };
274
+ }
275
+
276
+ export function liveReserveFromPdp(opts: {
277
+ token: Address;
278
+ symbol: string;
279
+ name?: string;
280
+ decimals: number;
281
+ priceUsd18: bigint;
282
+ availableLiquidity: bigint;
283
+ totalStableDebt: bigint;
284
+ totalVariableDebt: bigint;
285
+ liquidityRate: bigint;
286
+ variableBorrowRate: bigint;
287
+ isActive?: boolean;
288
+ isFrozen?: boolean;
289
+ borrowingEnabled?: boolean;
290
+ stableBorrowEnabled?: boolean;
291
+ collateralEnabled?: boolean;
292
+ ltvBps?: number;
293
+ liquidationThresholdBps?: number;
294
+ liquidationBonusBps?: number;
295
+ reserveFactorBps?: number;
296
+ supplyCapTokens?: bigint;
297
+ borrowCapTokens?: bigint;
298
+ }): LiveReserve {
299
+ const totalBorrow = opts.totalStableDebt + opts.totalVariableDebt;
300
+ const totalSupply = opts.availableLiquidity + totalBorrow;
301
+ const priceUsd = usd18ToNumber(opts.priceUsd18);
302
+ const active = opts.isActive ?? true;
303
+ const frozen = opts.isFrozen ?? false;
304
+ return {
305
+ token: opts.token,
306
+ symbol: opts.symbol,
307
+ name: opts.name ?? opts.symbol,
308
+ isActive: active,
309
+ decimals: opts.decimals,
310
+ priceUsd,
311
+ supplyApy: rayAprToApyPercent(opts.liquidityRate),
312
+ variableBorrowApy: rayAprToApyPercent(opts.variableBorrowRate),
313
+ totalSupplyUsd: tokensToNumber(totalSupply, opts.decimals) * priceUsd,
314
+ totalBorrowUsd: tokensToNumber(totalBorrow, opts.decimals) * priceUsd,
315
+ supplyCapUsd:
316
+ opts.supplyCapTokens !== undefined ? Number(opts.supplyCapTokens) * priceUsd : undefined,
317
+ borrowCapUsd:
318
+ opts.borrowCapTokens !== undefined ? Number(opts.borrowCapTokens) * priceUsd : undefined,
319
+ borrowingEnabled: (opts.borrowingEnabled ?? true) && active && !frozen,
320
+ stableBorrowEnabled: (opts.stableBorrowEnabled ?? false) && active && !frozen,
321
+ collateralEnabled: opts.collateralEnabled,
322
+ ltvBps: opts.ltvBps,
323
+ liquidationThresholdBps: opts.liquidationThresholdBps,
324
+ liquidationBonusBps: opts.liquidationBonusBps,
325
+ reserveFactorBps: opts.reserveFactorBps,
326
+ availableLiquidity: opts.availableLiquidity,
327
+ liquidityIndex: 0n,
328
+ totalSupply,
329
+ totalBorrow,
330
+ supplyCap: opts.supplyCapTokens ?? 0n,
331
+ borrowCap: opts.borrowCapTokens ?? 0n,
332
+ variableBorrowIndex: 0n,
333
+ };
334
+ }
335
+
336
+ export function liveUserFromPdp(opts: {
337
+ token: Address;
338
+ decimals: number;
339
+ currentATokenBalance: bigint;
340
+ currentStableDebt: bigint;
341
+ currentVariableDebt: bigint;
342
+ usageAsCollateralEnabled: boolean;
343
+ }): LiveUserReserve {
344
+ return {
345
+ token: opts.token,
346
+ supplied: tokensToNumber(opts.currentATokenBalance, opts.decimals),
347
+ borrowed: tokensToNumber(opts.currentStableDebt + opts.currentVariableDebt, opts.decimals),
348
+ isCollateral: opts.usageAsCollateralEnabled,
349
+ suppliedRaw: opts.currentATokenBalance,
350
+ borrowedRaw: opts.currentStableDebt + opts.currentVariableDebt,
351
+ };
352
+ }
353
+
354
+ /** Join catalog display metadata onto a deployed reserve. Does not add or drop listings. */
355
+ export function overlayCatalogWithLive<T extends CatalogReserveFields>(
356
+ catalog: T,
357
+ live: LiveReserve | undefined,
358
+ ): T {
359
+ if (!live) return catalog;
360
+ return {
361
+ ...catalog,
362
+ priceUsd: live.priceUsd,
363
+ supplyApy: live.supplyApy,
364
+ variableBorrowApy: live.variableBorrowApy,
365
+ totalSupplyUsd: live.totalSupplyUsd,
366
+ totalBorrowUsd: live.totalBorrowUsd,
367
+ supplyCapUsd: live.supplyCapUsd ?? catalog.supplyCapUsd,
368
+ borrowCapUsd: live.borrowCapUsd ?? catalog.borrowCapUsd,
369
+ borrowingEnabled: live.borrowingEnabled ?? catalog.borrowingEnabled,
370
+ stableBorrowEnabled: live.stableBorrowEnabled ?? catalog.stableBorrowEnabled,
371
+ collateralEnabled: live.collateralEnabled ?? catalog.collateralEnabled,
372
+ ltvBps: live.ltvBps ?? catalog.ltvBps,
373
+ liquidationThresholdBps: live.liquidationThresholdBps ?? catalog.liquidationThresholdBps,
374
+ liquidationBonusBps: live.liquidationBonusBps ?? catalog.liquidationBonusBps,
375
+ reserveFactorBps: live.reserveFactorBps ?? catalog.reserveFactorBps,
376
+ };
377
+ }
378
+
379
+ /** Native ETH reported by the lens. ERC-20 wallet balances must be read separately. */
380
+ export function walletMapFromLens(nativeEth: bigint): Map<string, bigint> {
381
+ return new Map([[MOCK_ETH_ADDRESS.toLowerCase(), nativeEth]]);
382
+ }
383
+
384
+ export function indexWalletBalances(
385
+ tokens: readonly Address[],
386
+ balances: readonly bigint[],
387
+ ): Map<string, bigint> {
388
+ const map = new Map<string, bigint>();
389
+ for (let i = 0; i < tokens.length && i < balances.length; i++) {
390
+ map.set(tokens[i]!.toLowerCase(), balances[i]!);
391
+ }
392
+ return map;
393
+ }