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

61 lines
2.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { getAmountIn, getAmountOut, quote, buildSwapExactTokensForTokensTx, AmmQuoteError } from "../src/amm.js";
const WAD = 10n ** 18n;
const wad = (n: bigint) => n * WAD;
describe("AMM quote math (mirrors QieForgeLibrary.sol / amm.py / amm.rs)", () => {
it("getAmountOut matches the hand-calculated 30bps-fee formula", () => {
const amountIn = wad(100n);
const reserveIn = wad(10_000n);
const reserveOut = wad(10_000n);
const amountInWithFee = amountIn * 9970n;
const expected = (amountInWithFee * reserveOut) / (reserveIn * 10_000n + amountInWithFee);
expect(getAmountOut(amountIn, reserveIn, reserveOut)).toBe(expected);
expect(expected).toBeLessThan(amountIn);
});
it("getAmountOut and getAmountIn round-trip within rounding error", () => {
const reserveIn = wad(50_000n);
const reserveOut = wad(20_000n);
const amountIn = wad(1_000n);
const amountOut = getAmountOut(amountIn, reserveIn, reserveOut);
const recoveredIn = getAmountIn(amountOut, reserveIn, reserveOut);
const diff = recoveredIn > amountIn ? recoveredIn - amountIn : amountIn - recoveredIn;
expect(diff).toBeLessThanOrEqual(2n);
});
it("quote is proportional with no fee applied", () => {
expect(quote(100n, 1000n, 4000n)).toBe(400n);
});
it("rejects non-positive inputs", () => {
expect(() => getAmountOut(0n, 1000n, 1000n)).toThrowError(AmmQuoteError);
expect(() => getAmountIn(1000n, 1000n, 500n)).toThrowError(AmmQuoteError);
expect(() => quote(0n, 1000n, 1000n)).toThrowError(AmmQuoteError);
});
it("buildSwapExactTokensForTokensTx encodes a real swapExactTokensForTokens call", () => {
const router = "0x0000000000000000000000000000000000000001";
const tokenA = "0x0000000000000000000000000000000000000002";
const tokenB = "0x0000000000000000000000000000000000000003";
const to = "0x0000000000000000000000000000000000000004";
const { to: callTarget, data } = buildSwapExactTokensForTokensTx(
router,
wad(1n),
0n,
[tokenA, tokenB],
to,
9_999_999_999n,
);
expect(callTarget.toLowerCase()).toBe(router.toLowerCase());
expect(data.startsWith("0x38ed1739")).toBe(true); // swapExactTokensForTokens selector
});
it("rejects an invalid address before encoding anything", () => {
expect(() =>
buildSwapExactTokensForTokensTx("not-an-address", 1n, 0n, ["0x1", "0x2"], "0x3", 1n),
).toThrow(/valid EVM address/);
});
});