feat(ur-sdk): add SwapRouter.encodeSwaps() with safety envelope#517
feat(ur-sdk): add SwapRouter.encodeSwaps() with safety envelope#517
Conversation
Design for new SwapRouter.encodeSwaps() that separates safety-critical encoding (permits, token transfers, slippage sweeps) from routing's swap commands. Enables routing to innovate on path shapes without SDK changes while the SDK owns the safety envelope. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Implements the new encodeSwaps() method that separates safety-critical encoding (permit, token pull, slippage sweep) from routing's swap commands. Supports EXACT_INPUT and EXACT_OUTPUT with ERC20 and native ETH on both sides. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Fix minAmountOut to use amount/(1+s) instead of amount*(1-s) to match SDK-standard slippage calculation - Add invariant for empty swapSteps - Add missing "with Permit2 permit" test case - Import ROUTER_AS_RECIPIENT from constants in tests Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Import PoolKey and PathKey from @uniswap/v4-sdk instead of redefining them locally (PathKey.fee narrowed from BigNumberish to number to match v4-sdk) - Derive ACTION_NAME_TO_ENUM programmatically from Actions enum instead of manual mapping, so it stays in sync automatically Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace manual switch statement with automatic param extraction using COMMAND_DEFINITION param names (which match SwapStep field names by convention). Derive CommandType from step.type string automatically. V4_SWAP retains special handling for V4Planner pre-encoding. Adding new swap step types now only requires the type definition — no switch case to maintain. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add undefined guard on COMMAND_DEFINITION param extraction to fail fast if a SwapStep field name diverges from the expected param name - Restrict STEP_TYPE_TO_COMMAND to swap-relevant commands only instead of mapping all CommandTypes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Semgrep found 1 Risk: Affected versions of rollup are vulnerable to Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting'). Manual Review Advice: A vulnerability from this advisory is reachable if you use Rollup to bundle JavaScript with Fix: Upgrade this library to at least version 2.79.2 at sdks/package-lock.json:27661. Reference(s): GHSA-gcx4-mw62-g8wm, CVE-2024-47068 Semgrep found 2 Risk: Affected versions of @babel/traverse and babel-traverse are vulnerable to Incomplete List of Disallowed Inputs / Incorrect Comparison. Compiling untrusted code with Babel using plugins that invoke the internal path.evaluate() or path.evaluateTruthy() methods (for example @babel/plugin-transform-runtime, @babel/preset-env with useBuiltIns, or any polyfill‐provider plugin) allows a maliciously crafted AST to execute arbitrary code on the build machine during compilation. Manual Review Advice: A vulnerability from this advisory is reachable if you use Babel to compile untrusted JavaScript Fix: There are no safe versions of this library available for upgrade. Library included at sdks/package-lock.json:26929. Reference(s): GHSA-67hx-6x53-jw92, CVE-2023-45133 Semgrep found 1 Risk: @openzeppelin/contracts versions >= 3.3.0 before 3.4.2, >= 4.0.0 before 4.3.1 are vulnerable to Improper Privilege Management. A vulnerability found in Manual Review Advice: A vulnerability from this advisory is reachable if you have users configured with the Fix: Upgrade this library to at least version 3.4.2 at sdks/package-lock.json:7439. Reference(s): GHSA-fg47-3c2x-m2wr, CVE-2021-39167 |
|
you pushed a package-lock.json |
PR Scope
Please title your PR according to the following types and scopes following conventional commits:
fix(SDK name):will trigger a patch versionchore(<type>):will not trigger any release and should be used for internal repo changes<type>(public):will trigger a patch version for non-code changes (e.g. README changes)feat(SDK name):will trigger a minor versionfeat(breaking):will trigger a major version for a breaking changeDescription
[Summary of the change, motivation, and context]
How Has This Been Tested?
[e.g. Manually, E2E tests, unit tests, Storybook]
Are there any breaking changes?
[e.g. Type definitions, API definitions]
If there are breaking changes, please ensure you bump the major version Bump the major version (by using the title
feat(breaking): ...), post a notice in #eng-sdks, and explicitly notify all Uniswap Labs consumers of the SDK.(Optional) Feedback Focus
[Specific parts of this PR you'd like feedback on, or that reviewers should pay closer attention to]
(Optional) Follow Ups
[Things that weren't addressed in this PR, ways you plan to build on this work, or other ways this work could be extended]
✨ Claude-Generated Content
Description
Adds a new
SwapRouter.encodeSwaps(spec, swapSteps)entry point that separates safety-critical logic (SDK-owned) from swap encoding (routing-owned). This allows routing services to describe swaps as structuredSwapStepdata while the SDK handles the safety envelope: Permit2 approval, token transfer into router, slippage protection via SWEEP/UNWRAP_WETH, and excess refund for EXACT_OUTPUT trades.Changes
New API
SwapRouter.encodeSwaps(spec, swapSteps)method insrc/swapRouter.tsSwapSpecificationtype for SDK-owned safety metadata (tokens, amounts, slippage, permit, deadline, UR version)SwapStepdiscriminated union for routing-owned swap commands (V2/V3/V4 swaps, WRAP_ETH, UNWRAP_WETH)Type Definitions (
src/types/encodeSwaps.ts)SwapSpecification: tradeType, inputToken, outputToken, amount, quote, slippageTolerance, optional recipient/permit/deadline/urVersionSwapStep: V2SwapExactIn, V2SwapExactOut, V3SwapExactIn, V3SwapExactOut, V4Swap, WrapEth, UnwrapWethPoolKeyandPathKeyfrom@uniswap/v4-sdkUtilities
encodeSwapStep()insrc/utils/encodeSwapStep.ts- data-driven encoder using COMMAND_DEFINITION for param extractionv4ActionToParams()insrc/utils/encodeV4Action.ts- maps V4Action to V4Planner params with UR version supportExports
src/index.tsHow Has This Been Tested?
Unit tests in
test/unit/encodeSwaps.test.tscovering:v4ActionToParamsfor SETTLE, TAKE, SWAP_EXACT_IN (V2.0 and V2.1 with maxHopSlippage)encodeSwapStepfor V2, V3, V4 swaps and WRAP_ETHSwapRouter.encodeSwapsintegration tests:Are there any breaking changes?
No - this is a new API that coexists with the existing
swapCallParameters()method. Consumers can migrate at their own pace.