微算力 · API 导航与自营网关
API 接入 EN
未登录登录
🦄

智能路由与价格影响分析工具

uniswap-swap-simulation · vv0.1.0
专业技能 安全 A 级 作者 wpank 公开目录收录
下载 1.8k 安装 734 收藏 4.2k 浏览 60.2k
下载 zip

简介

基于 viem 的 Uniswap 交换模拟工具,提供价格影响计算与最优路由分析,助力 DeFi 交易者优化滑点与 Gas 成本。

原始描述

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.

通过你的 Agent 添加

点「快速安装」复制一段指令,粘给你正在用的 Agent(Claude Code、Codex、OpenClaw 等),它会自己完成安装。

或者手动安装

Claude Codeunzip -o skill.zip -d ~/.claude/skills/uniswap-swap-simulation/
其它 Agent解压到它自己的 skills 目录,目录名用 uniswap-swap-simulation

包里是 SKILL.md 加作者附带的文件,解压后目录名就是技能名。 技能申请的权限由作者决定,装之前请确认它能读什么、能执行什么。

中文使用说明 来自收录源

核心用法

本 Skill 提供 Uniswap V3 交易的全流程模拟能力,涵盖四大核心场景:

1. 零成本模拟报价 通过 QuoterV2 合约的 quoteExactInputSingle 函数,无需实际执行交易即可获取输出金额、执行后价格、初始化的 tick 数量及预估 gas。这是构建交易前分析的基础工具。

2. 价格冲击量化 提供标准化的价格冲击计算公式,对比执行价格与市场中间价,帮助用户直观理解大额交易对流动性的消耗程度。

3. 滑点容忍度分层配置 基于资产特性给出科学的滑点建议:稳定币对 0.01%-0.05%,主流币对 0.1%-0.5%,高波动资产 1%-5%,并配套最小输出金额计算逻辑。

4. 多跳路由优化 支持通过 encodePacked 构建多跳路径(如 ETH→USDC→DAI),解决非直接交易对的流动性获取问题。

显著优点

  • 零风险模拟:链上报价机制确保分析结果与实际执行高度一致,无资金风险
  • 专业级精度:涵盖 sqrtPriceX96 等底层计算细节,满足开发者深度需求
  • MEV 防护意识:主动提示私有 RPC 使用、三明治攻击风险及 deadline 参数设置
  • gas 成本预估:提供单跳/多跳的标准化 gas 基准,含 15-20% 缓冲建议

潜在缺点与局限性

  • 版本锁定风险:示例代码基于 Uniswap V3,V4 已发布但文档未同步更新
  • 网络覆盖不全:未明确区分 Ethereum Mainnet、Base、Arbitrum 等网络的合约地址差异
  • 个人维护来源:开发者 wpank 为 GitHub 个人账号,长期维护承诺弱于组织级项目
  • 无实时数据:纯文档型 Skill,不接入实际链上数据,用户需自行搭建查询环境

适合人群

  • DeFi 开发者构建交易聚合器或分析工具
  • 量化交易员评估大额订单的执行策略
  • 智能合约安全审计人员理解 Uniswap 交互模式
  • 进阶用户学习 AMM 底层机制

常规风险

  1. 代码示例过时:区块链协议迭代迅速,建议核对官方文档确认接口有效性
  2. 合约地址混淆:跨链场景下误用主网地址可能导致资金损失
  3. 滑点设置不当:过低滑点导致交易频繁失败,过高则暴露于 MEV 提取
  4. 个人项目可持续性:建议生产环境使用前进行独立代码审计

技能正文


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.

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

正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。

标签

finance-accountingdevelopment-engineeringblockchaindefidata-analytics

同类 Skill