qie-forge/packages/core/test/amm.vault.live.test.ts
caelum0x 3629870339 feat: close TS SDK parity gap — add vault.ts + amm.ts to @qie-forge/core, wire CLI vault-position/amm-quote
Python and Rust SDKs both shipped VaultClient + AMM quote/route modules on
2026-10-27, mirroring @qie-forge/core's intended shape, but the actual
TypeScript core SDK never got them — leaving the one SDK the frontend and
CLI both depend on without any vault-position lookup or swap-quoting API.

- packages/core/src/vault.ts: read-only VaultClient (positions/healthFactor/
  currentDebt/ltvBps/liquidationThresholdBps), field-for-field match of
  vault.py/vault.rs.
- packages/core/src/amm.ts: getAmountOut/getAmountIn/quote pure math,
  on-chain getReserves/getAmountsOut, and an unsigned
  buildSwapExactTokensForTokensTx builder — mirrors amm.py/amm.rs.
- CLI: new `vault-position` and `amm-quote` commands built directly on
  these, plus `--network demo` support (the only network with a real
  deployment today).

Verified live against the real self-hosted demo chain backing
https://qie-forge.vercel.app (addresses pulled via `vercel env pull`):
- `amm-quote` for 1 WQIE -> TUSD returned 139579, exactly matching the live
  production app's own `/api/quote` (on-chain router.getAmountsOut()) for
  the same input.
- `vault-position` against the real deployed vault returned real
  ltvBps=7500/liquidationThresholdBps=8000 and the correct "no position"
  shape (healthFactor = uint256 max) for an address with none.
- 20/20 new+existing core unit tests, 28/28 CLI tests, 3/3 new live
  integration tests (QIE_FORGE_RUN_LIVE_TESTS=1), full workspace
  build+lint clean, apps/web Next.js build unaffected.

Found but out of scope (flagged, not fixed): packages/core/test/
oracle.live.test.ts's two mainnet assertions reference a non-existent
`result.rawPrice` field (the real field is `answer`) and have been
silently failing since the very first commit — a distinct, pre-existing
bug, left for a future pass per this repo's one-gap-per-pass discipline.
2026-10-29 11:13:42 +03:00

59 lines
2.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { ChainClient } from "../src/chain.js";
import { getAmountsOut, getReserves } from "../src/amm.js";
import { VaultClient } from "../src/vault.js";
/**
* Live against the real self-hosted demo devnet backing
* https://qie-forge.vercel.app (the same persistent anvil instance used by
* every other live-verification pass in this repo's history). Gated the
* same way chain.live.test.ts is — QIE_FORGE_RUN_LIVE_TESTS=1 — so a plain
* `npm test` stays offline and deterministic.
*
* Addresses pulled from the real Vercel production env
* (`vercel env pull`) on 2026-10-29: the live production app's own
* FACTORY/ROUTER/VAULT/WQIE/TUSD addresses, not placeholders.
*/
const LIVE = process.env.QIE_FORGE_RUN_LIVE_TESTS === "1";
const describeLive = LIVE ? describe : describe.skip;
const DEMO_RPC_URL = "http://130.61.180.42:8647";
const FACTORY = "0x02b0b4efd909240fcb2eb5fae060dc60d112e3a4";
const VAULT = "0xd49a0e9a4cd5979ae36840f542d2d7f02c4817be";
const WQIE = "0x6C2d83262fF84cBaDb3e416D527403135D757892";
const TUSD = "0xFD6F7A6a5c21A3f503EBaE7a473639974379c351";
describeLive("@qie-forge/core amm.ts + vault.ts (live, real self-hosted demo chain)", () => {
it("getReserves reads the real WQIE/TUSD pair reserves", async () => {
const chain = new ChainClient({ network: "demo", rpcUrl: DEMO_RPC_URL });
const [reserveWqie, reserveTusd] = await getReserves(chain, FACTORY, WQIE, TUSD);
expect(reserveWqie).toBeGreaterThan(0n);
expect(reserveTusd).toBeGreaterThan(0n);
}, 20_000);
it("getAmountsOut quotes a real swap matching the live router's own pricing", async () => {
const chain = new ChainClient({ network: "demo", rpcUrl: DEMO_RPC_URL });
const amounts = await getAmountsOut(chain, FACTORY, 1_000_000_000_000_000_000n, [WQIE, TUSD]);
expect(amounts).toHaveLength(2);
expect(amounts[0]).toBe(1_000_000_000_000_000_000n);
expect(amounts[1]).toBeGreaterThan(0n);
}, 20_000);
it("VaultClient.getPosition reads the real live vault's own ltv/liquidation config", async () => {
const chain = new ChainClient({ network: "demo", rpcUrl: DEMO_RPC_URL });
const vault = new VaultClient(chain, VAULT);
const [ltvBps, liquidationThresholdBps] = await Promise.all([
vault.getLtvBps(),
vault.getLiquidationThresholdBps(),
]);
expect(ltvBps).toBeGreaterThan(0n);
expect(liquidationThresholdBps).toBeGreaterThanOrEqual(ltvBps);
// An address with no open position on this real vault — healthFactor
// must report "infinite" (uint256 max) rather than reverting.
const position = await vault.getPosition("0x0000000000000000000000000000000000000001");
expect(position.debtPrincipal).toBe(0n);
expect(position.healthFactor).toBe(2n ** 256n - 1n);
}, 20_000);
});