@varla/sdk 2.3.1 → 2.5.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 (42) hide show
  1. package/AGENTS.md +7 -7
  2. package/CHANGELOG.md +17 -0
  3. package/README.md +47 -0
  4. package/dist/abi/full/PolymarketNegRiskMergeAdapter.d.ts +248 -0
  5. package/dist/abi/full/PolymarketNegRiskMergeAdapter.d.ts.map +1 -0
  6. package/dist/abi/full/PolymarketNegRiskMergeAdapter.js +326 -0
  7. package/dist/abi/full/PolymarketNegRiskMergeAdapter.js.map +1 -0
  8. package/dist/abi/full/VarlaMergeLiquidator.d.ts +39 -0
  9. package/dist/abi/full/VarlaMergeLiquidator.d.ts.map +1 -1
  10. package/dist/abi/full/VarlaMergeLiquidator.js +50 -0
  11. package/dist/abi/full/VarlaMergeLiquidator.js.map +1 -1
  12. package/dist/abi/full/VarlaStaking.d.ts +640 -0
  13. package/dist/abi/full/VarlaStaking.d.ts.map +1 -0
  14. package/dist/abi/full/VarlaStaking.js +842 -0
  15. package/dist/abi/full/VarlaStaking.js.map +1 -0
  16. package/dist/abi/index.d.ts +2 -0
  17. package/dist/abi/index.d.ts.map +1 -1
  18. package/dist/abi/index.js +2 -0
  19. package/dist/abi/index.js.map +1 -1
  20. package/dist/contracts.d.ts +3 -1
  21. package/dist/contracts.d.ts.map +1 -1
  22. package/dist/contracts.js +16 -0
  23. package/dist/contracts.js.map +1 -1
  24. package/dist/generated.d.ts +960 -35
  25. package/dist/generated.d.ts.map +1 -1
  26. package/dist/generated.js +2 -0
  27. package/dist/generated.js.map +1 -1
  28. package/dist/types.d.ts +2 -0
  29. package/dist/types.d.ts.map +1 -1
  30. package/dist/views/core.d.ts +45 -1
  31. package/dist/views/core.d.ts.map +1 -1
  32. package/dist/views/core.js +66 -3
  33. package/dist/views/core.js.map +1 -1
  34. package/package.json +1 -1
  35. package/src/abi/full/PolymarketNegRiskMergeAdapter.ts +330 -0
  36. package/src/abi/full/VarlaMergeLiquidator.ts +50 -0
  37. package/src/abi/full/VarlaStaking.ts +846 -0
  38. package/src/abi/index.ts +2 -0
  39. package/src/contracts.ts +22 -0
  40. package/src/generated.ts +2 -0
  41. package/src/types.ts +2 -0
  42. package/src/views/core.ts +108 -5
package/src/abi/index.ts CHANGED
@@ -11,7 +11,9 @@ export { VARLAPROXYADMIN_ABI } from "./full/VarlaProxyAdmin.js";
11
11
  export { VARLALIQUIDATOR_ABI } from "./full/VarlaLiquidator.js";
12
12
  export { VARLAMERGELIQUIDATOR_ABI } from "./full/VarlaMergeLiquidator.js";
13
13
  export { VARLACONVERTLIQUIDATOR_ABI } from "./full/VarlaConvertLiquidator.js";
14
+ export { VARLASTAKING_ABI } from "./full/VarlaStaking.js";
14
15
  export { POLYMARKETCTFADAPTER_ABI } from "./full/PolymarketCtfAdapter.js";
16
+ export { POLYMARKETNEGRISKMERGEADAPTER_ABI } from "./full/PolymarketNegRiskMergeAdapter.js";
15
17
  export { OPINIONCTFEXECUTIONENGINEADAPTER_ABI } from "./full/OpinionCtfExecutionEngineAdapter.js";
16
18
  export { VARLAORACLE_ABI_REGISTRY } from "./subsets/VarlaOracle.registry.js";
17
19
 
package/src/contracts.ts CHANGED
@@ -41,8 +41,10 @@ export type VarlaContracts = {
41
41
  liquidator?: ReadonlyContract<typeof abis.VARLALIQUIDATOR_ABI>;
42
42
  mergeLiquidator?: ReadonlyContract<typeof abis.VARLAMERGELIQUIDATOR_ABI>;
43
43
  convertLiquidator?: ReadonlyContract<typeof abis.VARLACONVERTLIQUIDATOR_ABI>;
44
+ staking?: ReadonlyContract<typeof abis.VARLASTAKING_ABI>;
44
45
 
45
46
  polymarketCtfAdapter?: ReadonlyContract<typeof abis.POLYMARKETCTFADAPTER_ABI>;
47
+ negRiskMergeAdapter?: ReadonlyContract<typeof abis.POLYMARKETNEGRISKMERGEADAPTER_ABI>;
46
48
  opinionCtfExecutionEngineAdapter?: ReadonlyContract<
47
49
  typeof abis.OPINIONCTFEXECUTIONENGINEADAPTER_ABI
48
50
  >;
@@ -150,6 +152,14 @@ export function getVarlaContracts({ chain, client }: GetVarlaContractsParams): V
150
152
  })
151
153
  : undefined;
152
154
 
155
+ const staking = book.staking
156
+ ? getContract({
157
+ address: book.staking as Address,
158
+ abi: abis.VARLASTAKING_ABI,
159
+ client,
160
+ })
161
+ : undefined;
162
+
153
163
  const polymarketCtfAdapter = book.polymarketCtfAdapter
154
164
  ? getContract({
155
165
  address: book.polymarketCtfAdapter as Address,
@@ -158,6 +168,14 @@ export function getVarlaContracts({ chain, client }: GetVarlaContractsParams): V
158
168
  })
159
169
  : undefined;
160
170
 
171
+ const negRiskMergeAdapter = book.negRiskMergeAdapter
172
+ ? getContract({
173
+ address: book.negRiskMergeAdapter as Address,
174
+ abi: abis.POLYMARKETNEGRISKMERGEADAPTER_ABI,
175
+ client,
176
+ })
177
+ : undefined;
178
+
161
179
  const opinionCtfExecutionEngineAdapter = book.opinionCtfExecutionEngineAdapter
162
180
  ? getContract({
163
181
  address: book.opinionCtfExecutionEngineAdapter as Address,
@@ -179,7 +197,9 @@ export function getVarlaContracts({ chain, client }: GetVarlaContractsParams): V
179
197
  liquidator,
180
198
  mergeLiquidator,
181
199
  convertLiquidator,
200
+ staking,
182
201
  polymarketCtfAdapter,
202
+ negRiskMergeAdapter,
183
203
  opinionCtfExecutionEngineAdapter,
184
204
  };
185
205
  }
@@ -197,7 +217,9 @@ export function getRequiredVarlaContracts(
197
217
  | "liquidator"
198
218
  | "mergeLiquidator"
199
219
  | "convertLiquidator"
220
+ | "staking"
200
221
  | "polymarketCtfAdapter"
222
+ | "negRiskMergeAdapter"
201
223
  | "opinionCtfExecutionEngineAdapter"
202
224
  >;
203
225
  },
package/src/generated.ts CHANGED
@@ -15,7 +15,9 @@ export const abis = {
15
15
  VARLALIQUIDATOR_ABI: abi_.VARLALIQUIDATOR_ABI,
16
16
  VARLAMERGELIQUIDATOR_ABI: abi_.VARLAMERGELIQUIDATOR_ABI,
17
17
  VARLACONVERTLIQUIDATOR_ABI: abi_.VARLACONVERTLIQUIDATOR_ABI,
18
+ VARLASTAKING_ABI: abi_.VARLASTAKING_ABI,
18
19
  POLYMARKETCTFADAPTER_ABI: abi_.POLYMARKETCTFADAPTER_ABI,
20
+ POLYMARKETNEGRISKMERGEADAPTER_ABI: abi_.POLYMARKETNEGRISKMERGEADAPTER_ABI,
19
21
  OPINIONCTFEXECUTIONENGINEADAPTER_ABI: abi_.OPINIONCTFEXECUTIONENGINEADAPTER_ABI,
20
22
  VARLAORACLE_ABI_REGISTRY: abi_.VARLAORACLE_ABI_REGISTRY,
21
23
  } as const;
package/src/types.ts CHANGED
@@ -19,7 +19,9 @@ export type AddressBook = {
19
19
  liquidator?: `0x${string}`;
20
20
  mergeLiquidator?: `0x${string}`;
21
21
  convertLiquidator?: `0x${string}`;
22
+ staking?: `0x${string}`;
22
23
 
23
24
  polymarketCtfAdapter?: `0x${string}`;
25
+ negRiskMergeAdapter?: `0x${string}`;
24
26
  opinionCtfExecutionEngineAdapter?: `0x${string}`;
25
27
  };
package/src/views/core.ts CHANGED
@@ -893,6 +893,78 @@ export type ReadBorrowLimits = {
893
893
  canBeLiquidated: boolean;
894
894
  };
895
895
 
896
+ export type ReadWalletPositionIds = {
897
+ user: Address;
898
+ /** Total oracle-configured position IDs that were checked. */
899
+ candidateCount: number;
900
+ /** Wallet positionIds where ERC1155 balance > 0. */
901
+ positionIds: bigint[];
902
+ /** Balances aligned to positionIds. */
903
+ balances: bigint[];
904
+ };
905
+
906
+ /**
907
+ * Discovers which Varla-configured ERC1155 positions a wallet holds.
908
+ *
909
+ * Strategy: reads ALL configured positionIds from the oracle registry, then
910
+ * batch-checks `ERC1155.balanceOf(user, pid)` via multicall, filtering to
911
+ * those with balance > 0.
912
+ *
913
+ * This is the canonical way to find "what Varla positions does this wallet have?"
914
+ * without an indexer — ERC1155 has no `tokenIdsOf(owner)` in the standard.
915
+ */
916
+ export async function readWalletPositionIds(params: {
917
+ oracle: {
918
+ read: {
919
+ getConfiguredPositions: () => Promise<readonly bigint[]>;
920
+ };
921
+ };
922
+ positionsToken: { address: Address };
923
+ client: { multicall: (args: any) => Promise<any> };
924
+ user: Address;
925
+ chunkSize?: number;
926
+ }): Promise<ReadWalletPositionIds> {
927
+ const chunkSize = params.chunkSize ?? 256;
928
+
929
+ // 1. Get all oracle-configured position IDs (the canonical candidate set).
930
+ const allIds = await params.oracle.read.getConfiguredPositions();
931
+ if (allIds.length === 0) {
932
+ return { user: params.user, candidateCount: 0, positionIds: [], balances: [] };
933
+ }
934
+
935
+ // 2. Batch ERC1155.balanceOf(user, pid) for every candidate.
936
+ const balCalls = allIds.map((pid) => ({
937
+ address: params.positionsToken.address,
938
+ abi: erc1155Abi,
939
+ functionName: "balanceOf" as const,
940
+ args: [params.user, pid] as const,
941
+ }));
942
+
943
+ const balRes = await multicallChunks({
944
+ client: params.client as any,
945
+ contracts: balCalls as any,
946
+ chunkSize,
947
+ });
948
+
949
+ // 3. Filter to balance > 0.
950
+ const positionIds: bigint[] = [];
951
+ const balances: bigint[] = [];
952
+ for (let i = 0; i < balRes.length; i++) {
953
+ const r: any = (balRes as any[])[i];
954
+ if (r.status !== "success") {
955
+ throw new Error(`ERC1155 balanceOf multicall failed: ${String(r.error ?? "unknown")}`);
956
+ }
957
+ const bal = r.result as bigint;
958
+ if (typeof bal !== "bigint") throw new Error("Unexpected ERC1155 balanceOf return type");
959
+ if (bal > 0n) {
960
+ positionIds.push(allIds[i]!);
961
+ balances.push(bal);
962
+ }
963
+ }
964
+
965
+ return { user: params.user, candidateCount: allIds.length, positionIds, balances };
966
+ }
967
+
896
968
  export type ReadHypotheticalBorrowCapacity = {
897
969
  user: Address;
898
970
  /** Current on-chain account snapshot (deposited positions only). */
@@ -1221,22 +1293,53 @@ export async function readHypotheticalBorrowCapacity(params: {
1221
1293
  getDefaultLtvConfig: () => Promise<unknown>;
1222
1294
  };
1223
1295
  };
1224
- oracle: { address: Address };
1296
+ oracle: {
1297
+ address: Address;
1298
+ /** Required when `walletPositionIds` is omitted (auto-discovery mode). */
1299
+ read?: {
1300
+ getConfiguredPositions?: () => Promise<readonly bigint[]>;
1301
+ };
1302
+ };
1225
1303
  positionsToken: { address: Address };
1226
1304
  client: { multicall: (args: any) => Promise<any> };
1227
1305
  user: Address;
1228
- walletPositionIds: readonly bigint[];
1306
+ /**
1307
+ * Position IDs to check in the user's wallet.
1308
+ *
1309
+ * When omitted, auto-discovers by reading ALL oracle-configured position IDs
1310
+ * and checking ERC1155.balanceOf for each (requires `oracle.read.getConfiguredPositions`).
1311
+ */
1312
+ walletPositionIds?: readonly bigint[];
1229
1313
  chunkSize?: number;
1230
1314
  }): Promise<ReadHypotheticalBorrowCapacity> {
1231
1315
  const chunkSize = params.chunkSize ?? 256;
1232
1316
 
1317
+ // Auto-discover wallet position IDs if not provided.
1318
+ let walletPositionIds = params.walletPositionIds;
1319
+ if (!walletPositionIds) {
1320
+ if (!params.oracle.read?.getConfiguredPositions) {
1321
+ throw new Error(
1322
+ "readHypotheticalBorrowCapacity: walletPositionIds not provided and " +
1323
+ "oracle.read.getConfiguredPositions not available for auto-discovery",
1324
+ );
1325
+ }
1326
+ const discovered = await readWalletPositionIds({
1327
+ oracle: { read: { getConfiguredPositions: params.oracle.read.getConfiguredPositions } },
1328
+ positionsToken: params.positionsToken,
1329
+ client: params.client,
1330
+ user: params.user,
1331
+ chunkSize,
1332
+ });
1333
+ walletPositionIds = discovered.positionIds;
1334
+ }
1335
+
1233
1336
  const [current, defaultLtv] = await Promise.all([
1234
1337
  readAccountSnapshot({ core: params.core as any, user: params.user }),
1235
1338
  readDefaultLtvConfig({ core: params.core as any }),
1236
1339
  ]);
1237
1340
 
1238
1341
  // Fast path: no wallet ids.
1239
- if (params.walletPositionIds.length === 0) {
1342
+ if (walletPositionIds.length === 0) {
1240
1343
  const potentialMaxBorrow =
1241
1344
  current.collateralValue > current.debt ? current.collateralValue - current.debt : 0n;
1242
1345
  return {
@@ -1248,7 +1351,7 @@ export async function readHypotheticalBorrowCapacity(params: {
1248
1351
  }
1249
1352
 
1250
1353
  // 1) Wallet balances (ERC1155 balanceOf)
1251
- const balCalls = params.walletPositionIds.map((pid) => ({
1354
+ const balCalls = walletPositionIds.map((pid) => ({
1252
1355
  address: params.positionsToken.address,
1253
1356
  abi: erc1155Abi,
1254
1357
  functionName: "balanceOf" as const,
@@ -1271,7 +1374,7 @@ export async function readHypotheticalBorrowCapacity(params: {
1271
1374
  const bal = r.result as bigint;
1272
1375
  if (typeof bal !== "bigint") throw new Error("Unexpected ERC1155 balanceOf return type");
1273
1376
  if (bal > 0n) {
1274
- heldIds.push(params.walletPositionIds[i]!);
1377
+ heldIds.push(walletPositionIds[i]!);
1275
1378
  heldBals.push(bal);
1276
1379
  }
1277
1380
  }