← 返回
数据分析 中文

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.
模拟分析Uniswap交易(含价格影响、滑点、最优路径及Gas估算)。适用于询问交易执行、路由、价格影响或MEV的情况。
wpank
数据分析 clawhub v0.1.0 1 版本 99822.9 Key: 无需
★ 0
Stars
📥 1,127
下载
💾 16
安装
1
版本
#latest

概述

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

版本历史

共 1 个版本

  • v0.1.0 当前
    2026-03-29 02:33 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

data-analysis

A股量化 AkShare

mbpz
A股量化数据分析工具,基于AkShare库获取A股行情、财务数据、板块信息等。用于回答关于A股股票查询、行情数据、财务分析、选股等问题。
★ 165 📥 60,001
data-analysis

Data Analysis

ivangdavila
{"answer":"数据分析与可视化。查询数据库、生成报告、自动化电子表格,将原始数据转化为清晰可行的见解。适用于:(1) 您……"}
★ 198 📥 65,112
developer-tools

Code Review

wpank
涵盖安全、性能、可维护性、正确性和测试的系统化代码审查模式,包含严重等级、结构化反馈指南、审查流程及需避免的反模式。适用于审查 PR、建立审查标准或提升审查质量。
★ 31 📥 17,101