Repository navigation
fix(swap): quote on-chain via the V1 router and support HBAR swaps - #5
Merged
Merged
Conversation
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>
jmgomezl
force-pushed
the
fix/onchain-swap-quotes
branch
from
September 30, 2026 03:45
449754e to
1591301
Compare
|
eta on when this will be merged? |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
saucerswap_get_swap_quoteandsaucerswap_swap_tokensgot quotes fromGET /v1/swap/quote, which returns404 Cannot GET /v1/swap/quoteon bothapi.saucerswap.financeandtest-api.saucerswap.finance(checked 2026-09-29). SocoreActionalways returned{ success: false }and no swap was ever built.The swap also always called
swapExactTokensForTokenswith no payable amount, so HBAR input could never work. There was also no handling for the router allowance that HTS inputs need.Changes
getAmountsOut(uint256,address[])now runs as a read-only call through the mirror nodePOST /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 aresrc/utils/abi.ts,src/utils/router.tsandsrc/api/mirror.ts.getReserves/token0, found through the router'sfactory().getPair, so it includes the 0.3% LP fee per hop. If the reserves can't be read, it isnulland the quote still succeeds.swapExactETHForTokenswithsetPayableAmountswapExactTokensForETHswapExactTokensForTokensapprove_token_allowance_tool. If the lookup itself fails, the swap is still built and the network decides. The allowance is documented indocs/TOOLS.mdand the README.routerContractId. Before, they defaulted to the V2 router (defaultPoolVersion: "v2"), which has a different ABI with noswapExactTokensForTokens.coreActionstill returns{ transaction, extras: { estimatedOutput, minOutput, priceImpact, route } }, which hak-jev-plugin'sJevGatePolicydepends on.estimatedOutputandminOutputare now both decimal units of the output token. Before,minOutputwas in smallest units, but that path never ran.quote.rawis now{ routerContractId, path, amounts }.mirrorNodeUrl/SAUCERSWAP_MIRROR_NODE_URL. The default followsnetwork, then the client's ledger, then mainnet.toUint256is unsigned, so large amounts no longer overflow.formatUnitshandles 0 decimals.getSwapQuote, theswapQuoteendpoint,normalizeSwapQuoteandcalculatePriceImpact, none of which were exported from the package.integration-test.mjsquotes on-chain, anddry-run-swap.mjsuses network defaults for the V1 router.Testing
npm run typecheck,npm run lint(on tracked sources),npm test(49 passing) andnpm run buildall pass.tests/helpers/mirror.ts). They cover:getAmountsOut. The built transaction uses selector7ff36ab5(swapExactETHForTokens) with a 5 HBAR payable amount.SAUCERSWAP_NETWORK=testnet node scripts/integration-test.mjspasses.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/removeLiquiditycalls to the V2 router.🤖 Generated with Claude Code