uniswap-swap-simulation
Simulate and analyze Uniswap swaps including price impact, slippage, optimal routing, and gas estimation. Use when the user asks about swap execution, routing, price impact, or MEV considerations.
- License 许可
- AGPL-3.0
- 最近更新
- 2026-11-15
- Stars
- ★ 81
- Forks
- 17
01 前言 Front Matter
- name
- uniswap-swap-simulation
- description
- Simulate and analyze Uniswap swaps including price impact, slippage, optimal routing, and gas estimation. Use when the user asks about swap execution, routing, price impact, or MEV considerations.
02 资源文件 5
- 参考文档_meta.json142 B
- 参考文档README.md986 B
- 参考文档skill-card.md2.1 KB
- 参考文档SOURCE.md264 B
- 参考文档TRUST.auto.yaml2.1 KB
03 SKILL.md 正文
Uniswap Swap Simulation
Overview
This skill covers simulating Uniswap swaps, calculating price impact, and analyzing routing decisions.
Key Concepts
- Price Impact: The change in pool price caused by a swap. Larger swaps have higher impact.
- Slippage: Difference between expected and executed price, including price movement between submission and execution.
- Routing: Finding the optimal path across pools and protocols for best execution.
Simulating a Swap
Use the Quoter contract to simulate swaps without executing:
import { createPublicClient, http, encodeFunctionData } from "viem";
// QuoterV2 for v3 pools
const quote = await client.readContract({
address: quoterV2Address,
abi: quoterV2Abi,
functionName: "quoteExactInputSingle",
args: [
{
tokenIn,
tokenOut,
amountIn,
fee,
sqrtPriceLimitX96: 0n,
},
],
});
// Returns: [amountOut, sqrtPriceX96After, initializedTicksCrossed, gasEstimate]
Price Impact Calculation
function calculatePriceImpact(
amountIn: bigint,
amountOut: bigint,
marketPrice: number, // token1/token0
decimals0: number,
decimals1: number,
): number {
const executionPrice =
Number(amountOut) / 10 ** decimals1 / (Number(amountIn) / 10 ** decimals0);
return Math.abs(1 - executionPrice / marketPrice);
}
Slippage Tolerance
- Stablecoin pairs: 0.01% - 0.05%
- Major pairs (ETH/USDC): 0.1% - 0.5%
- Volatile pairs: 0.5% - 1.0%
- Low liquidity: 1% - 5%
Calculate minimum amount out:
const minAmountOut = (amountOut * (10000n - BigInt(slippageBps))) / 10000n;
Multi-hop Routing
For tokens without direct pools, route through intermediary tokens:
// ETH -> USDC -> DAI (two hops)
const path = encodePacked(
["address", "uint24", "address", "uint24", "address"],
[WETH, 3000, USDC, 100, DAI],
);
const quote = await client.readContract({
address: quoterV2Address,
abi: quoterV2Abi,
functionName: "quoteExactInput",
args: [path, amountIn],
});
Gas Estimation
Typical gas costs by swap complexity:
- Single hop: ~130,000 gas
- Two hops: ~200,000 gas
- Three hops: ~270,000 gas
Always add a 15-20% buffer to gas estimates.
MEV Considerations
When building swap tools:
- Recommend private RPCs (Flashbots Protect) for large swaps
- Warn users about sandwich attack risk for high-impact swaps
- Suggest using deadline parameters to limit exposure
04 同仓库更多 Skills
recipe-performance
Diagnose a specific MetaMask user flow's real-app performance and Sentry critical-user-flow telemetry with deterministic mm-harness recipes. Use for CUF boundaries, cold/warm/background-resume behavior, latency regressions, lifecycle tags, duplicate or blocking network requests, long tasks, layout/paint cost, screen redraws, React rerenders, or browser trace analysis where tests alone cannot prove the real app.
mexc-spot-websocket
MEXC Spot WebSocket workflows for public streams, protobuf handling, depth maintenance, listen keys, and private streams without exposing API keys.
mexc-spot-rest
MEXC Spot REST workflows for public data, signed account requests, orders, wallet/admin actions, and secure signing without exposing API keys.
mexc-futures-websocket
MEXC Futures WebSocket workflows for public streams, private account/order/position streams, ping/pong, subscriptions, and depth maintenance.
0xklen-version-and-refresh-sanctions-lists
暂无简介
verify-tests-cover-changed-lines
Use when a PR adds code that the existing suite may not exercise. Measures patch coverage — the share of newly changed lines hit by tests — before approving.