Skip to content

fix(swap): quote on-chain via the V1 router and support HBAR swaps - #5

Merged
jmgomezl merged 1 commit into
mainfrom
fix/onchain-swap-quotes
Nov 16, 2026
Merged

jmgomezl merged 1 commit into
mainfrom
fix/onchain-swap-quotes

Conversation

@jmgomezl

Copy link
Copy Markdown
Collaborator

Problem

saucerswap_get_swap_quote and saucerswap_swap_tokens got quotes from GET /v1/swap/quote, which returns 404 Cannot GET /v1/swap/quote on both api.saucerswap.finance and test-api.saucerswap.finance (checked 2026-09-29). So coreAction always returned { success: false } and no swap was ever built.

The swap also always called swapExactTokensForTokens with no payable amount, so HBAR input could never work. There was also no handling for the router allowance that HTS inputs need.

Changes

  • On-chain quotes. The V1 router's getAmountsOut(uint256,address[]) now runs as a read-only call through the mirror node POST /api/v1/contracts/call. This is free and needs no API key. The quoter tries the direct pair and a two-hop route through WHBAR, then keeps the better output. The new files are src/utils/abi.ts, src/utils/router.ts and src/api/mirror.ts.
  • Price impact. It is computed against the spot price from each pair's getReserves/token0, found through the router's factory().getPair, so it includes the 0.3% LP fee per hop. If the reserves can't be read, it is null and the quote still succeeds.
  • HBAR on either side.
    • HBAR input: swapExactETHForTokens with setPayableAmount
    • HBAR output: swapExactTokensForETH
    • HTS to HTS: swapExactTokensForTokens
  • Router allowance for HTS input. Before building the transaction, the tool reads the operator's allowance for the router from the mirror node. If it is too low, the tool returns an error naming the token, spender and amount, and points to HAK's approve_token_allowance_tool. If the lookup itself fails, the swap is still built and the network decides. The allowance is documented in docs/TOOLS.md and the README.
  • V1 router only. Swaps now always use routerContractId. Before, they defaulted to the V2 router (defaultPoolVersion: "v2"), which has a different ABI with no swapExactTokensForTokens.
  • Payload shape unchanged. coreAction still returns { transaction, extras: { estimatedOutput, minOutput, priceImpact, route } }, which hak-jev-plugin's JevGatePolicy depends on. estimatedOutput and minOutput are now both decimal units of the output token. Before, minOutput was in smallest units, but that path never ran.
  • Quote tool output. Amounts are in decimal units, and quote.raw is now { routerContractId, path, amounts }.
  • New setting. mirrorNodeUrl / SAUCERSWAP_MIRROR_NODE_URL. The default follows network, then the client's ledger, then mainnet.
  • Smaller fixes.
    • toUint256 is unsigned, so large amounts no longer overflow.
    • formatUnits handles 0 decimals.
    • Slippage above 100% is clamped.
  • Removed. The dead getSwapQuote, the swapQuote endpoint, normalizeSwapQuote and calculatePriceImpact, none of which were exported from the package.
  • Scripts. integration-test.mjs quotes on-chain, and dry-run-swap.mjs uses network defaults for the V1 router.
  • Version. Bumped to 2.2.0 (not published).

Testing

  • npm run typecheck, npm run lint (on tracked sources), npm test (49 passing) and npm run build all pass.
  • The new unit tests use an in-memory constant-product mirror fake (tests/helpers/mirror.ts). They cover:
    • ABI encoding and decoding
    • direct and WHBAR two-hop routing
    • price impact
    • each router function and its payable amount
    • the allowance check
    • the payload shape
    • error paths
  • Live on testnet (quote and transaction build only, nothing submitted):
    • 5 HBAR to SAUCE quotes 274.679694 SAUCE with 0.306% price impact, matching a direct getAmountsOut. The built transaction uses selector 7ff36ab5 (swapExactETHForTokens) with a 5 HBAR payable amount.
    • SAUCE to HBAR quotes correctly. With no allowance, the tool returns the approval instructions.
    • SAUCERSWAP_NETWORK=testnet node scripts/integration-test.mjs passes.

Not covered: an end-to-end swap submitted and settled on-chain.

Follow-up (not in this PR)

The liquidity tools have the same V2 router problem: by default they send V1-style addLiquidity/removeLiquidity calls to the V2 router.

🤖 Generated with Claude Code

SaucerSwap's REST API has no quote endpoint: GET /v1/swap/quote returns
404 on mainnet and testnet, so the quote and swap tools always failed
and no swap transaction was ever built.

- Quote with the V1 router getAmountsOut through the mirror node
  POST /api/v1/contracts/call (free, no API key). Try the direct pair
  and a two-hop route through WHBAR, keep the best output.
- Derive price impact from each pair's getReserves/token0.
- HBAR input uses swapExactETHForTokens with a payable amount; HBAR
  output uses swapExactTokensForETH.
- Check the router token allowance for HTS input on the mirror node and
  return approval instructions instead of a transaction that reverts.
- Swaps always use the V1 router; the V2 router has a different ABI.
- Report estimatedOutput and minOutput in decimal units. The core
  payload shape { transaction, extras } is unchanged.
- New mirrorNodeUrl / SAUCERSWAP_MIRROR_NODE_URL setting.
- toUint256 is unsigned; formatUnits handles 0 decimals.
- Bump to 2.2.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Juan Gomez <juanmag.lpez@gmail.com>
@nubeasado

Copy link
Copy Markdown

eta on when this will be merged?

Copy link
Copy Markdown
Collaborator Author

@nubeasado Sorry for the slow reply. The PR is still awaiting maintainer review and merge, so I don't have a confirmed ETA. The current head is mergeable, and the typecheck/lint/test/build check is passing. The V1 liquidity fix in #6 is stacked on this PR and needs this one merged first.

@jmgomezl
jmgomezl merged commit 1591301 into main Nov 16, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants