运行时依赖
无特殊依赖
安装命令
点击复制官方npx clawhub@latest install uniswap-swap-simulation
镜像加速npx clawhub@latest install uniswap-swap-simulation --registry https://cn.longxiaskill.com 镜像可用
技能文档
Uniswap 交换模拟概述 本技能涵盖模拟 Uniswap 交换、计算价格影响和分析路由决策。
关键概念 价格影响(Price Impact):交换对池价格的变化。更大的交换具有更高的影响。 滑点(Slippage):预期价格和执行价格之间的差异,包括提交和执行之间的价格变化。 路由(Routing):找到跨池和协议的最佳执行路径。
模拟交换 使用 Quoter 合约模拟交换而不执行:
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,
},
],
});
// 返回:[amountOut, sqrtPriceX96After, initializedTicksCrossed, gasEstimate]
价格影响计算
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);
}
滑点容忍度 稳定币对:0.01% - 0.05% 主要对(ETH/USDC):0.1% - 0.5% 波动对:0.5% - 1.0% 低流动性:1% - 5% 计算最小输出量:
const minAmountOut = (amountOut * (10000n - BigInt(slippageBps))) / 10000n;
多跳路由 对于没有直接池的代币,通过中间代币路由:
// ETH -> USDC -> DAI (两跳)
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 估算 典型的 Gas 成本按交换复杂度: 单跳:~130,000 Gas 两跳:~200,000 Gas 三跳:~270,000 Gas 始终在 Gas 估算中添加 15-20% 的缓冲区。
MEV 考虑 构建交换工具时: 推荐使用私有 RPC(Flashbots Protect)进行大额交换 警告用户关于高影响交换的三明治攻击风险 建议使用截止日期参数来限制暴露