@varla/sdk 2.17.0 → 2.18.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/src/views/pool.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  // Note: explicit .js extension is required for Node ESM resolution.
2
2
 
3
3
  import type { Address } from "viem";
4
+ import { multicallChunks } from "../batch.js";
5
+ import { abis } from "../generated.js";
4
6
 
5
7
  export type ReadPoolSnapshot = {
6
8
  totalAssets: bigint;
@@ -154,6 +156,16 @@ export type ReadPoolHealthScore =
154
156
  kind: "no-liquidity";
155
157
  };
156
158
 
159
+ export type ReadPoolDashboard = {
160
+ snapshot: ReadPoolSnapshot;
161
+ caps: ReadPoolCaps;
162
+ accounting: ReadPoolAccounting;
163
+ rates: ReadPoolRates;
164
+ sharePrice: ReadPoolSharePrice;
165
+ healthScore: ReadPoolHealthScore;
166
+ debtState: ReadPoolDebtState;
167
+ };
168
+
157
169
  /**
158
170
  * Reads pool “rates” and near-term capacity in one convenience call.
159
171
  */
@@ -275,3 +287,144 @@ export async function readPoolCoreAddress(params: {
275
287
  }): Promise<Address> {
276
288
  return params.pool.read.varlaCore();
277
289
  }
290
+
291
+ /**
292
+ * Multicall-backed aggregate reader for pool-heavy UIs.
293
+ *
294
+ * Deduplicates the overlapping primitives used across snapshot/caps/accounting/rates/share price
295
+ * so frontends can hydrate the full pool page with one batched request group.
296
+ */
297
+ // wraps: VarlaPool.getPoolStats,VarlaPool.depositCap,VarlaPool.borrowCap,VarlaPool.reserveBalance,VarlaPool.scaledTotalBorrowed,VarlaPool.lastUpdateTimestamp,VarlaPool.totalSupply,VarlaPool.maxBorrow,VarlaPool.getCurrentBorrowIndex
298
+ export async function readPoolDashboard(params: {
299
+ pool: { address: Address };
300
+ client: { multicall: (args: any) => Promise<any> };
301
+ chunkSize?: number;
302
+ }): Promise<ReadPoolDashboard> {
303
+ const chunkSize = params.chunkSize ?? 256;
304
+
305
+ const calls = [
306
+ {
307
+ address: params.pool.address,
308
+ abi: abis.VARLAPOOL_ABI,
309
+ functionName: "getPoolStats" as const,
310
+ },
311
+ {
312
+ address: params.pool.address,
313
+ abi: abis.VARLAPOOL_ABI,
314
+ functionName: "depositCap" as const,
315
+ },
316
+ {
317
+ address: params.pool.address,
318
+ abi: abis.VARLAPOOL_ABI,
319
+ functionName: "borrowCap" as const,
320
+ },
321
+ {
322
+ address: params.pool.address,
323
+ abi: abis.VARLAPOOL_ABI,
324
+ functionName: "reserveBalance" as const,
325
+ },
326
+ {
327
+ address: params.pool.address,
328
+ abi: abis.VARLAPOOL_ABI,
329
+ functionName: "scaledTotalBorrowed" as const,
330
+ },
331
+ {
332
+ address: params.pool.address,
333
+ abi: abis.VARLAPOOL_ABI,
334
+ functionName: "lastUpdateTimestamp" as const,
335
+ },
336
+ {
337
+ address: params.pool.address,
338
+ abi: abis.VARLAPOOL_ABI,
339
+ functionName: "totalSupply" as const,
340
+ },
341
+ {
342
+ address: params.pool.address,
343
+ abi: abis.VARLAPOOL_ABI,
344
+ functionName: "maxBorrow" as const,
345
+ },
346
+ {
347
+ address: params.pool.address,
348
+ abi: abis.VARLAPOOL_ABI,
349
+ functionName: "getCurrentBorrowIndex" as const,
350
+ },
351
+ ];
352
+
353
+ const res = await multicallChunks({
354
+ client: params.client as any,
355
+ contracts: calls as any,
356
+ chunkSize,
357
+ });
358
+
359
+ for (const r of res as any[]) {
360
+ if (r.status !== "success") {
361
+ throw new Error(`Pool dashboard multicall failed: ${String(r.error ?? "unknown")}`);
362
+ }
363
+ }
364
+
365
+ const snapshot = await readPoolSnapshot({
366
+ pool: {
367
+ read: {
368
+ getPoolStats: async () => (res as any[])[0]!.result,
369
+ },
370
+ },
371
+ });
372
+
373
+ const depositCap = (res as any[])[1]!.result as bigint;
374
+ const borrowCap = (res as any[])[2]!.result as bigint;
375
+ const reserveBalance = (res as any[])[3]!.result as bigint;
376
+ const scaledTotalBorrowed = (res as any[])[4]!.result as bigint;
377
+ const lastUpdateTimestamp = BigInt((res as any[])[5]!.result);
378
+ const totalShares = (res as any[])[6]!.result as bigint;
379
+ const maxBorrow = (res as any[])[7]!.result as bigint;
380
+ const currentBorrowIndex = (res as any[])[8]!.result as bigint;
381
+
382
+ const sharePrice: ReadPoolSharePrice = {
383
+ totalAssets: snapshot.totalAssets,
384
+ totalShares,
385
+ assetsPerShareWad:
386
+ totalShares === 0n ? 0n : (snapshot.totalAssets * 1_000_000_000_000_000_000n) / totalShares,
387
+ };
388
+
389
+ const healthScore: ReadPoolHealthScore =
390
+ snapshot.totalAssets === 0n
391
+ ? { kind: "no-liquidity" }
392
+ : {
393
+ kind: "ok",
394
+ utilizationWad: snapshot.utilization,
395
+ scoreBps:
396
+ ((1_000_000_000_000_000_000n -
397
+ (snapshot.utilization > 1_000_000_000_000_000_000n
398
+ ? 1_000_000_000_000_000_000n
399
+ : snapshot.utilization < 0n
400
+ ? 0n
401
+ : snapshot.utilization)) *
402
+ 10_000n) /
403
+ 1_000_000_000_000_000_000n,
404
+ };
405
+
406
+ return {
407
+ snapshot,
408
+ caps: { depositCap, borrowCap },
409
+ accounting: {
410
+ reserveBalance,
411
+ scaledTotalBorrowed,
412
+ borrowIndex: snapshot.borrowIndex,
413
+ lastUpdateTimestamp,
414
+ },
415
+ rates: {
416
+ utilization: snapshot.utilization,
417
+ borrowRate: snapshot.borrowRate,
418
+ supplyAPY: snapshot.supplyAPY,
419
+ availableLiquidity: snapshot.available,
420
+ maxBorrow,
421
+ },
422
+ sharePrice,
423
+ healthScore,
424
+ debtState: {
425
+ totalBorrowed: snapshot.totalBorrowed,
426
+ currentBorrowIndex,
427
+ borrowRate: snapshot.borrowRate,
428
+ },
429
+ };
430
+ }
@@ -1,11 +1,17 @@
1
1
  // Note: explicit .js extension is required for Node ESM resolution.
2
2
 
3
3
  import type { ReadDefaultLtvConfig, ReadLiquidationConfig } from "./core.js";
4
- import { readDefaultLtvConfig, readLiquidationConfig } from "./core.js";
4
+ import { readCoreDashboard, readDefaultLtvConfig, readLiquidationConfig } from "./core.js";
5
5
  import type { ReadOracleConfig } from "./oracle.js";
6
6
  import { readOracleConfig } from "./oracle.js";
7
- import type { ReadPoolAccounting, ReadPoolCaps, ReadPoolSnapshot } from "./pool.js";
8
- import { readPoolAccounting, readPoolCaps, readPoolSnapshot } from "./pool.js";
7
+ import type {
8
+ ReadPoolAccounting,
9
+ ReadPoolCaps,
10
+ ReadPoolHealthScore,
11
+ ReadPoolSharePrice,
12
+ ReadPoolSnapshot,
13
+ } from "./pool.js";
14
+ import { readPoolAccounting, readPoolCaps, readPoolDashboard, readPoolSnapshot } from "./pool.js";
9
15
 
10
16
  export type ReadSystemSnapshot = {
11
17
  pool: ReadPoolSnapshot;
@@ -16,6 +22,11 @@ export type ReadSystemSnapshot = {
16
22
  oracle: ReadOracleConfig;
17
23
  };
18
24
 
25
+ export type ReadSystemDashboardSnapshot = ReadSystemSnapshot & {
26
+ poolSharePrice: ReadPoolSharePrice;
27
+ poolHealthScore: ReadPoolHealthScore;
28
+ };
29
+
19
30
  /**
20
31
  * A lightweight “global protocol state” read for dashboards.
21
32
  *
@@ -40,3 +51,40 @@ export async function readSystemSnapshot(params: {
40
51
 
41
52
  return { pool, poolCaps, poolAccounting, coreLtv, coreLiquidation, oracle };
42
53
  }
54
+
55
+ /**
56
+ * Multicall-backed system snapshot used by dashboards.
57
+ */
58
+ export async function readSystemDashboardSnapshot(params: {
59
+ pool: { address: `0x${string}` };
60
+ core: { address: `0x${string}` };
61
+ oracle: any;
62
+ client: { multicall: (args: any) => Promise<any> };
63
+ }): Promise<ReadSystemDashboardSnapshot> {
64
+ const [poolDashboard, coreDashboard, oracle] = await Promise.all([
65
+ readPoolDashboard({
66
+ pool: { address: params.pool.address },
67
+ client: params.client,
68
+ }),
69
+ readCoreDashboard({
70
+ core: { address: params.core.address },
71
+ client: params.client,
72
+ }),
73
+ readOracleConfig({ oracle: params.oracle }),
74
+ ]);
75
+
76
+ return {
77
+ pool: poolDashboard.snapshot,
78
+ poolCaps: poolDashboard.caps,
79
+ poolAccounting: poolDashboard.accounting,
80
+ coreLtv: {
81
+ conservative: coreDashboard.ltvConfig.tier0,
82
+ moderate: coreDashboard.ltvConfig.tier1,
83
+ risk: coreDashboard.ltvConfig.tier2,
84
+ },
85
+ coreLiquidation: coreDashboard.liqConfig,
86
+ oracle,
87
+ poolSharePrice: poolDashboard.sharePrice,
88
+ poolHealthScore: poolDashboard.healthScore,
89
+ };
90
+ }