@varla/sdk 2.18.0 → 2.19.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/core.ts CHANGED
@@ -904,6 +904,27 @@ export type ReadCoreDashboard = {
904
904
  ];
905
905
  };
906
906
 
907
+ export type ReadConfiguredPositionDepositRow = {
908
+ positionId: bigint;
909
+ conditionId: `0x${string}`;
910
+ depositedBalance: bigint;
911
+ priceOk: boolean;
912
+ spotPriceE8: bigint;
913
+ twapPriceE8: bigint;
914
+ priceE8: bigint;
915
+ effectivePriceE8: bigint;
916
+ usedFallbackPrice: boolean;
917
+ depositValue: bigint;
918
+ };
919
+
920
+ export type ReadConfiguredPositionDepositsSummary = {
921
+ configuredCount: number;
922
+ pricedCount: number;
923
+ totalDepositedBalance: bigint;
924
+ totalDepositedValue: bigint;
925
+ rows: ReadConfiguredPositionDepositRow[];
926
+ };
927
+
907
928
  /**
908
929
  * Normalized wrapper over `VarlaCore.getTierLiquidationConfig(tier)`.
909
930
  */
@@ -1076,6 +1097,166 @@ export async function readCoreDashboard(params: {
1076
1097
  };
1077
1098
  }
1078
1099
 
1100
+ /**
1101
+ * Reads deposited balances for all oracle-configured positions.
1102
+ *
1103
+ * Uses:
1104
+ * - `oracle.getConfiguredPositions()`
1105
+ * - `positionsToken.balanceOf(core, positionId)`
1106
+ * - `oracle.getPositionSnapshot(positionId)` for pricing
1107
+ * - `oracle.getPriceData(positionId)` for fallback pricing
1108
+ * - `oracle.getConditionId(positionId)` for market mapping
1109
+ */
1110
+ // wraps: VarlaOracle.getConfiguredPositions,ERC1155.balanceOf,VarlaOracle.getPositionSnapshot,VarlaOracle.getPriceData,VarlaOracle.getConditionId
1111
+ export async function readConfiguredPositionDepositsSummary(params: {
1112
+ core: { address: Address };
1113
+ oracle: { address: Address };
1114
+ positionsToken: { address: Address };
1115
+ client: { multicall: (args: any) => Promise<any> };
1116
+ chunkSize?: number;
1117
+ }): Promise<ReadConfiguredPositionDepositsSummary> {
1118
+ const chunkSize = params.chunkSize ?? 256;
1119
+
1120
+ const cfgRes = await multicallChunks({
1121
+ client: params.client as any,
1122
+ contracts: [
1123
+ {
1124
+ address: params.oracle.address,
1125
+ abi: abis.VARLAORACLE_ABI,
1126
+ functionName: "getConfiguredPositions" as const,
1127
+ },
1128
+ ] as any,
1129
+ chunkSize,
1130
+ });
1131
+
1132
+ const cfg0: any = (cfgRes as any[])[0];
1133
+ if (!cfg0 || cfg0.status !== "success" || !Array.isArray(cfg0.result)) {
1134
+ throw new Error(`Configured positions read failed: ${String(cfg0?.error ?? "unknown")}`);
1135
+ }
1136
+
1137
+ const positionIds = [...(cfg0.result as readonly bigint[])];
1138
+ if (positionIds.length === 0) {
1139
+ return {
1140
+ configuredCount: 0,
1141
+ pricedCount: 0,
1142
+ totalDepositedBalance: 0n,
1143
+ totalDepositedValue: 0n,
1144
+ rows: [],
1145
+ };
1146
+ }
1147
+
1148
+ const calls = positionIds.flatMap((pid) => [
1149
+ {
1150
+ address: params.positionsToken.address,
1151
+ abi: erc1155Abi,
1152
+ functionName: "balanceOf" as const,
1153
+ args: [params.core.address, pid] as const,
1154
+ },
1155
+ {
1156
+ address: params.oracle.address,
1157
+ abi: abis.VARLAORACLE_ABI,
1158
+ functionName: "getPositionSnapshot" as const,
1159
+ args: [pid] as const,
1160
+ },
1161
+ {
1162
+ address: params.oracle.address,
1163
+ abi: abis.VARLAORACLE_ABI,
1164
+ functionName: "getPriceData" as const,
1165
+ args: [pid] as const,
1166
+ },
1167
+ {
1168
+ address: params.oracle.address,
1169
+ abi: abis.VARLAORACLE_ABI,
1170
+ functionName: "getConditionId" as const,
1171
+ args: [pid] as const,
1172
+ },
1173
+ ]);
1174
+
1175
+ const res = await multicallChunks({
1176
+ client: params.client as any,
1177
+ contracts: calls as any,
1178
+ chunkSize,
1179
+ });
1180
+
1181
+ const rows: ReadConfiguredPositionDepositRow[] = [];
1182
+ let totalDepositedBalance = 0n;
1183
+ let totalDepositedValue = 0n;
1184
+ let pricedCount = 0;
1185
+
1186
+ for (let i = 0; i < positionIds.length; i++) {
1187
+ const balR: any = (res as any[])[i * 4];
1188
+ const snapR: any = (res as any[])[i * 4 + 1];
1189
+ const priceR: any = (res as any[])[i * 4 + 2];
1190
+ const condR: any = (res as any[])[i * 4 + 3];
1191
+ const pid = positionIds[i]!;
1192
+
1193
+ if (balR?.status !== "success") {
1194
+ throw new Error(
1195
+ `Configured position balance read failed: ${String(balR?.error ?? "unknown")}`,
1196
+ );
1197
+ }
1198
+ if (snapR?.status !== "success") {
1199
+ throw new Error(
1200
+ `Configured position snapshot read failed: ${String(snapR?.error ?? "unknown")}`,
1201
+ );
1202
+ }
1203
+ if (priceR?.status !== "success") {
1204
+ throw new Error(
1205
+ `Configured position priceData read failed: ${String(priceR?.error ?? "unknown")}`,
1206
+ );
1207
+ }
1208
+ if (condR?.status !== "success") {
1209
+ throw new Error(
1210
+ `Configured position condition read failed: ${String(condR?.error ?? "unknown")}`,
1211
+ );
1212
+ }
1213
+
1214
+ const depositedBalance = balR.result as bigint;
1215
+ if (typeof depositedBalance !== "bigint") {
1216
+ throw new Error("Unexpected ERC1155.balanceOf return type");
1217
+ }
1218
+
1219
+ const snap = _normalizeOraclePositionSnapshot(snapR.result, pid);
1220
+ const priceData = _normalizeOraclePriceData(priceR.result, pid);
1221
+ const conditionId = condR.result as `0x${string}`;
1222
+ if (typeof conditionId !== "string" || !conditionId.startsWith("0x")) {
1223
+ throw new Error("Unexpected getConditionId() return shape");
1224
+ }
1225
+
1226
+ const fallbackPriceE8 = _minPositive(priceData.priceE8, priceData.twapE8);
1227
+ const hasSnapshotPrice = snap.priceOk && snap.priceE8 > 0n;
1228
+ const effectivePriceE8 = hasSnapshotPrice ? snap.priceE8 : fallbackPriceE8;
1229
+ const priced = effectivePriceE8 > 0n;
1230
+ const usedFallbackPrice = !hasSnapshotPrice && priced;
1231
+ const depositValue = priced ? (depositedBalance * effectivePriceE8) / _E8 : 0n;
1232
+
1233
+ totalDepositedBalance += depositedBalance;
1234
+ totalDepositedValue += depositValue;
1235
+ if (priced) pricedCount += 1;
1236
+
1237
+ rows.push({
1238
+ positionId: pid,
1239
+ conditionId,
1240
+ depositedBalance,
1241
+ priceOk: snap.priceOk,
1242
+ spotPriceE8: priceData.priceE8,
1243
+ twapPriceE8: priceData.twapE8,
1244
+ priceE8: snap.priceE8,
1245
+ effectivePriceE8,
1246
+ usedFallbackPrice,
1247
+ depositValue,
1248
+ });
1249
+ }
1250
+
1251
+ return {
1252
+ configuredCount: positionIds.length,
1253
+ pricedCount,
1254
+ totalDepositedBalance,
1255
+ totalDepositedValue,
1256
+ rows,
1257
+ };
1258
+ }
1259
+
1079
1260
  export type ReadLiquidationParamsForPosition = {
1080
1261
  positionId: bigint;
1081
1262
  maxLiquidationBonusBps: bigint;
@@ -1590,6 +1771,12 @@ type _OraclePositionSnapshot = {
1590
1771
  earlyClosureFactorWad: bigint;
1591
1772
  };
1592
1773
 
1774
+ type _OraclePriceData = {
1775
+ positionId: bigint;
1776
+ priceE8: bigint;
1777
+ twapE8: bigint;
1778
+ };
1779
+
1593
1780
  function _normalizeOraclePositionSnapshot(
1594
1781
  raw: unknown,
1595
1782
  positionId: bigint,
@@ -1621,6 +1808,31 @@ function _normalizeOraclePositionSnapshot(
1621
1808
  };
1622
1809
  }
1623
1810
 
1811
+ function _normalizeOraclePriceData(raw: unknown, positionId: bigint): _OraclePriceData {
1812
+ const r: any = raw as any;
1813
+ const priceE8 = r.price ?? r[0];
1814
+ const twapE8 = r.twap ?? r[1];
1815
+
1816
+ if (typeof priceE8 !== "bigint" || typeof twapE8 !== "bigint") {
1817
+ throw new Error("Unexpected getPriceData() return shape");
1818
+ }
1819
+
1820
+ return {
1821
+ positionId,
1822
+ priceE8,
1823
+ twapE8,
1824
+ };
1825
+ }
1826
+
1827
+ function _minPositive(a: bigint, b: bigint): bigint {
1828
+ const aOk = a > 0n;
1829
+ const bOk = b > 0n;
1830
+ if (aOk && bOk) return a < b ? a : b;
1831
+ if (aOk) return a;
1832
+ if (bOk) return b;
1833
+ return 0n;
1834
+ }
1835
+
1624
1836
  function _tierDefaultLtv(defaultLtv: ReadDefaultLtvConfig, riskTier: number): bigint {
1625
1837
  if (riskTier === 0) return defaultLtv.conservative;
1626
1838
  if (riskTier === 1) return defaultLtv.moderate;
@@ -1,6 +1,6 @@
1
1
  // Note: explicit .js extension is required for Node ESM resolution.
2
2
 
3
- import type { ReadDefaultLtvConfig, ReadLiquidationConfig } from "./core.js";
3
+ import type { ReadCoreAddresses, ReadDefaultLtvConfig, ReadLiquidationConfig } from "./core.js";
4
4
  import { readCoreDashboard, readDefaultLtvConfig, readLiquidationConfig } from "./core.js";
5
5
  import type { ReadOracleConfig } from "./oracle.js";
6
6
  import { readOracleConfig } from "./oracle.js";
@@ -25,6 +25,7 @@ export type ReadSystemSnapshot = {
25
25
  export type ReadSystemDashboardSnapshot = ReadSystemSnapshot & {
26
26
  poolSharePrice: ReadPoolSharePrice;
27
27
  poolHealthScore: ReadPoolHealthScore;
28
+ coreAddresses: ReadCoreAddresses;
28
29
  };
29
30
 
30
31
  /**
@@ -86,5 +87,6 @@ export async function readSystemDashboardSnapshot(params: {
86
87
  oracle,
87
88
  poolSharePrice: poolDashboard.sharePrice,
88
89
  poolHealthScore: poolDashboard.healthScore,
90
+ coreAddresses: coreDashboard.coreAddresses,
89
91
  };
90
92
  }