OpenPond
1Branch0Tags
GL
glucryptoMerge commit 'refs/tmp/prod-master'
typescript
import { z } from "zod"; import { store } from "opentool/store"; import { wallet } from "opentool/wallet"; import { fetchHyperliquidClearinghouseState, resolveHyperliquidChainConfig, fetchHyperliquidSpotClearinghouseState, } from "opentool/adapters/hyperliquid"; export const profile = { description: "Check Hyperliquid clearinghouse state for the configured Turnkey wallet (confirms user existence).", }; export const schema = z.object({ environment: z.enum(["mainnet", "testnet"]).default("testnet"), subAccounts: z .array(z.string().regex(/^0x[a-fA-F0-9]{40}$/, "subAccount must be a 0x address")) .optional() .default([]), }); export async function POST(req: Request): Promise<Response> { try { const body = await req.json().catch(() => ({})); const { environment, subAccounts } = schema.parse(body); const chainConfig = resolveHyperliquidChainConfig(environment); const context = await wallet({ chain: chainConfig.chain, }); const walletAddress = context.address; if (!walletAddress) { throw new Error("Wallet address unavailable."); } const addresses = [walletAddress, ...(subAccounts ?? [])].filter( (addr): addr is string => typeof addr === "string" && addr.trim().length > 0 ); const snapshots = await Promise.all( addresses.map(async (addr) => { const [clearinghouse, spotClearinghouse] = await Promise.all([ fetchHyperliquidClearinghouseState({ environment, walletAddress: addr as `0x${string}`, }), fetchHyperliquidSpotClearinghouseState({ environment, user: addr as `0x${string}`, }), ]); return { walletAddress: addr, clearinghouse, spotClearinghouse }; }) ); await store({ source: "hyperliquid", ref: `status-${Date.now()}`, status: "submitted", walletAddress, action: "status", network: environment === "mainnet" ? "hyperliquid" : "hyperliquid-testnet", metadata: { environment, snapshots, }, }); return Response.json({ ok: true, environment, walletAddress, snapshots, }); } catch (error) { console.error("[hyperliquid-status] failed", { message: error instanceof Error ? error.message : String(error), stack: error instanceof Error ? error.stack : undefined, }); return Response.json( { ok: false, error: error instanceof Error ? error.message : "Unknown error", }, { status: 500 } ); } }