Fix: use swapExactETHForTokens instead of swapETHForExactTokens - #5
Open
draevik wants to merge 1 commit into
Open
Fix: use swapExactETHForTokens instead of swapETHForExactTokens#5draevik wants to merge 1 commit into
draevik wants to merge 1 commit into
Conversation
The native-in swap branch called swapETHForExactTokens (an exact-OUTPUT function) while passing amountOutMin - a minimum floor, not an exact target. This silently capped output at the slippage floor instead of the actual market rate, without ever reverting. Extracted method selection into lib/swap-method.ts so it's unit-testable without a wallet/RPC connection, and added a regression test.
|
@draevik is attempting to deploy a commit to the Lightchain AI Team on Vercel. A member of the Team first needs to authorize it. |
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.
What this fixes
The native-in swap branch in
swap()(hooks/useWeb3Functions.ts) calledswapETHForExactTokens— an exact-output router function — while passing itamountOutMin, a slippage floor, not an exact target. The call still succeeds (the user's input amount is normally enough to cover the floor amount), so it never reverts — it just silently pins the received tokens to the worst-case slippage amount every time, refunding the unused ETH, instead of delivering the real market-rate output.Before (
hooks/useWeb3Functions.ts):After (matches
config.governor-style consistency — i.e. matches theswapExact...pattern already used correctly by the other two trade directions a few lines below):What this adds
Extracted the trade-type → router-function mapping into a small, dependency-free helper,
lib/swap-method.ts, so this logic is unit-testable without a wallet or RPC connection. Repo previously had zero test infrastructure (package.jsonhad notestscript), so this also addsvitestas a devDependency and atestscript.lib/swap-method.test.tscovers all three trade directions and explicitly asserts the native-in case returnsswapExactETHForTokens, notswapETHForExactTokens.Verification
pnpm testfails (2 of 4 assertions), then restored the fix and confirmed all 4 pass — the test genuinely catches this regression, not just a green checkmark by coincidence.pnpm teston the final diff: 4/4 passing.pnpm lintandpnpm build: both clean (aside from a pre-existing, unrelated@metamask/sdkReact Native resolution warning present onmaintoo).swapExact...pattern.Note on manual testnet verification
Wanted to confirm real quoted-vs-received amounts on-chain before/after this fix, but hit a blocker unrelated to this change: the app's testnet network isn't currently selectable in the UI at all (
config/index.tsonly listslcaiinchains, soconfig/wagmi.tsnever offers testnet as a connectable network). Separately, the router address the explorer associates with real swap activity on testnet doesn't match what's committed toconfig/index.tsforlcaiTestnet— worth a maintainer sanity check, but it's a config/deployment question, not something this PR should guess at. Flagging both as follow-ups rather than blocking this fix on them, since neither affects mainnet or the correctness of the change itself.