以太坊交易MCP服务器

一个模型上下文协议(MCP)服务器,使AI代理能够与以太坊和Uniswap V2交互,查询余额、代币价格并模拟代币交换。

  • by cengfanman
  • Rust
  • 本地部署
  • 更新于
在 GitHub 查看 cengfanman/eth-trading-mcp-server

使用说明

以太坊交易MCP服务器

一个模型上下文协议(MCP)服务器,它使人工智能代理能够与以太坊和Uniswap V2进行交互,以查询余额、代币价格以及模拟代币交换。


设置

先决条件

  • Rust 1.70+(rustup install stable)
  • 一个以太坊RPC端点(Infura、Alchemy或公共节点)
  • 一个钱包私钥(用于交易构建上下文, “NOT for actual execution”翻译成中文是“不用于实际执行”)

安装

  1. 克隆仓库:
git clone <your-repo-url>
cd eth-trading-mcp-server
  1. 构建项目:
cargo build --release
  1. 运行测试:
cargo test

预期输出: 19 passed

配置

建议:使用 .env 文件 (自动加载):

# Copy the example file
cp .env.example .env

# Edit .env with your credentials
nano .env  # or vim, code, etc.

你的 .env 文件应包含:

# Required: Ethereum RPC endpoint
RPC_URL=https://eth-mainnet.g.alchemy.com/v3/YOUR_API_KEY

# Required: Private key (with 0x prefix)
# ⚠️ WARNING: This key is used ONLY for constructing transaction context
# Transactions are NEVER broadcast to the network
PRIVATE_KEY=0xYOUR_PRIVATE_KEY_HERE

# Optional: Chain ID (defaults to 1 for Ethereum mainnet)
CHAIN_ID=1

# Optional: Set log level
RUST_LOG=info

安全提示永远不要将你的 .env 文件泄露或暴露你的私钥。私钥仅用于通过(某种方式)构建交易模拟 eth_call 以及气体估算。 没有交易被签名或广播。

运行服务器

cargo run --release

服务器在stdin/stdout上监听MCP JSON-RPC请求。


特点/特性

三种MCP工具

  1. get_balance

    • 查询任意钱包地址的ETH和ERC20代币余额
    • 自动小数格式化(以太坊(ETH)使用18位小数,美元稳定币(USDC)使用6位,等等)
    • 返回原始值(wei)和人类可读的格式化金额
  2. get_token_price

    • 从Uniswap V2流动性池获取实时代币价格
    • 以ETH或USD(通过USDC)计算价格
    • 使用实际储备中的恒定乘积公式(x * y = k)
  3. swap_tokens

    • 模拟代币交换并进行全面验证
    • 查询Uniswap以获取预估输出
    • 计算滑差保护(默认值为0.5%)
    • 估算燃气费用(燃气限额 × 燃气价格)
    • 通过(某种方式)模拟交易 eth_call (检查余额、审批等)
    • 没有实际的链上执行 - 仅用于估算

设计决策

为什么选择Uniswap V2?

  • 简洁V2使用恒定乘积公式(x * y = k),易于理解和实现
  • 成熟协议经过实战检验,广泛应用,全面流动性
  • V3 复杂性V3需要集中流动性计算、刻度(tick)计算以及复杂的路由算法

价格计算

代币价格是通过使用常数乘积公式,从Uniswap V2的储备金中推导出来的:

price = reserve_out / reserve_in

这提供了 真实的链上数据 但由于以下原因,可能会与集中交易的价格存在差异:

  • 流动性深度
  • 滑移影响
  • MEV机器人活动

交换模拟过程

该 swap_tokens 该工具遵循以下5步流程:

  1. ✅ 查询Uniswap - 呼叫 getAmountsOut() 获取估算输出
  2. ✅ 计算滑差 - 应用滑移容差(例如,0.5%)
  3. ✅(正确) 估算气体(或:预估气体量) - 获取实时燃气价格并计算费用
  4. ✅ 编码交易 - 构建 swapExactTokensForTokens `calldata` 在中文中可以翻译为“调用数据”或“函数调用数据”,具体取决于上下文。它通常用于描述在函数或方法调用过程中传递的数据。
  5. ✅(勾选标记,通常表示正确、完成或确认) 模拟执行 - 使用 eth_call 进行验证(检查余额,审批)

关键点如果您的代币余额不足,第五步可能会失败,但步骤1至4总是会成功(它们证明了Uniswap集成正常工作)。

路由策略

  • 直接交换如果一个代币是WETH → 单跳
  • **通过WETH(以太坊封装的以太币)**如果两个代币都不是WETH → 则需两跳(TOKEN → WETH → TOKEN)
  • 没有复杂的路由未实现通过WETH以外的多跳路径

十进制精度

用途 rust_decimal::Decimal 为所有财务计算采用(整数运算以)避免浮点数误差:

  • 以太坊(ETH):18位小数(1 ETH = 1,000,000,000,000,000,000 伟)
  • USDC:6位小数(1 USDC = 1,000,000个单位)
  • 原始(U256)和格式化(十进制)之间的自动转换

演示

本节将使用真实的以太坊主网数据来演示所有功能。

1. 展示您的钱包地址

./target/release/show-address

输出:

Your wallet address: 0x39cfc46991e17ada6429f4628cfb0c4c4a6886bf

这显示了由您的私钥派生出的以太坊地址。


2. 检查你自己的以太坊(ETH)余额

./target/release/eth-mcp-server 2>/dev/null <<'EOF'
{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"get_balance","arguments":{"wallet":"0x39cfc46991e17ada6429f4628cfb0c4c4a6886bf"}}}
EOF

预期输出:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "wallet": "0x39cfc46991e17ada6429f4628cfb0c4c4a6886bf",
    "native": {
      "raw": "0",
      "formatted": "0",
      "decimals": 18,
      "symbol": "ETH"
    },
    "block_number": 23604361
  }
}

我的钱包是空的(0 ETH)。我们来检查一个有资金的钱包吧。。。


3. 检查Vitalik的以太币(ETH)余额

./target/release/eth-mcp-server 2>/dev/null <<'EOF'
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"get_balance","arguments":{"wallet":"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"}}}
EOF

预期输出:

{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "wallet": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
    "native": {
      "raw": "788335827583562397",
      "formatted": "0.788335827583562397",
      "decimals": 18,
      "symbol": "ETH"
    },
    "block_number": 23604361
  }
}

Vitalik 拥有 0.788 以太币!这 以太坊主网的实时数据。


4. 检查Vitalik的USDC余额

./target/release/eth-mcp-server 2>/dev/null <<'EOF'
{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get_balance","arguments":{"wallet":"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045","token":"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"}}}
EOF

预期输出:

{
  "jsonrpc": "2.0",
  "id": 3,
  "result": {
    "wallet": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
    "erc20": {
      "raw": "17058407780",
      "formatted": "17058.40778",
      "decimals": 6,
      "symbol": "USDC"
    },
    "block_number": 23604361
  }
}

Vitalik 拥有 17,058 美元稳定币(USDC)!请注意,USDC 使用的是 6 位小数,而不是 18 位。


5. 获取USDC价格(以ETH计价)

./target/release/eth-mcp-server 2>/dev/null <<'EOF'
{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"get_token_price","arguments":{"token":"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48","base":"eth"}}}
EOF

预期输出:

{
  "jsonrpc": "2.0",
  "id": 4,
  "result": {
    "token": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
    "base": "WETH",
    "price": "0.0002608076152110160880795511",
    "pair": "0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc",
    "source": "Uniswap V2",
    "block_number": 23604361
  }
}

翻译:解释:

  • 1 美元稳定币(USDC)= 0.000261 以太坊(ETH)
  • 反过来:1 以太坊(ETH)= 3,835 美元稳定币(USDC)
  • 来自Uniswap V2 USDC/WETH池的价格
  • 流动性储备的实时数据

6. 验证Uniswap V2集成(测试交换)

在测试之前 swap_tokens让我们来证明我们的Uniswap集成是正确的:

./target/release/test-swap

输出:

🚀 Testing Real Uniswap V2 Swap Calculation

Input:
  From: USDC (0xA0b8...B48)
  To:   WETH (0xC02a...Cc2)
  Amount: 1000 USDC

Path: [USDC, WETH] (direct swap)

📡 Calling Uniswap V2 Router.getAmountsOut()...
✅ Response from Uniswap:
  Estimated output: 0.256880914571150306 WETH
  Raw value: 256880914571150306

📊 With 0.5% slippage protection:
  Minimum output: 0.255596509998294554 WETH
  Raw value: 255596509998294554

📝 Transaction data encoded:
  Function: swapExactTokensForTokens
  Calldata length: 260 bytes
  Calldata (first 68 bytes): 0x38ed1739000000000000000000000000000000000000000000000000000000003b9aca00

⛽ Estimating gas cost...
  Gas limit: 150000 (estimated, actual may vary)
  Note: Precise gas estimation requires token balance & approval
  Gas price: 0.102462498 gwei
  Estimated fee: 0.0000153693747 ETH

✅ All Uniswap V2 integration working correctly!
✅ Using REAL mainnet data from Infura
✅ Transaction construction complete
✅ Gas estimation complete

这证明了:

  • ✅ 通过RPC实时连接到以太坊主网
  • ✅ 实际调用Uniswap V2 Router合约的函数
  • ✅ 准确的价格计算(1000 USDC → 0.257 WETH)
  • ✅ 正确的滑差计算(0.5%)
  • ✅ 正确的交易编码(260字节的调用数据)
  • ✅ 实时获取gas价格并估算费用

重要的`test-swap` 不需要代币余额,因为它仅用于查询价格和构建交易,而不模拟执行。


7. 模拟代币交换(swap_tokens)

现在让我们来尝试完整的(版本/过程/内容) swap_tokens MCP工具:

./target/release/eth-mcp-server 2>&1 <<'EOF'
{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"swap_tokens","arguments":{"from_token":"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48","to_token":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","amount":"1000","slippage_bps":50}}}
EOF

预期输出:

2025-10-18T11:53:29.104372Z  INFO eth_mcp_server::rpc: Received request: method=tools/call
2025-10-18T11:53:29.104401Z  INFO eth_mcp_server::rpc: Tool call: swap_tokens
2025-10-18T11:53:29.104425Z  INFO eth_mcp_server::tools::swap: swap_tokens called: 1000 0xA0b8...B48 -> 0xC02a...Cc2
2025-10-18T11:53:30.751017Z ERROR eth_mcp_server::rpc: Request error: Swap simulation failed (likely insufficient balance or approval). Gas estimate: 150000 units @ 0.108172169 gwei = 0.00001622582535 ETH. Error: Swap simulation failed

{"jsonrpc":"2.0","id":5,"error":{"code":-32603,"message":"Swap simulation failed (likely insufficient balance or approval). Gas estimate: 150000 units @ 0.108172169 gwei = 0.00001622582535 ETH. Error: Swap simulation failed"}}

发生了什么事?

交换失败了,但是 这是正确的行为以下是完整过程:

  1. ✅ 步骤1:查询Uniswap - 成功获取价格(1000 USDC → 0.257 WETH)
  2. ✅ 步骤2:计算滑脱量 - 最小输出量(考虑0.5%的滑点)= 0.256 WETH
  3. ✅ 第三步:估算气体 - 气体限制:150,000 单位 @ 0.108 吉瓦(gwei)= 0.000016 以太币(ETH)
  4. ✅ 步骤4:对交易进行编码 - 为Uniswap路由器构建了正确的调用数据
  5. ❌(表示错误或否定的符号,无具体中文对应词汇,可理解为“错误”或“否”) 第五步:模拟执行 - 失败,因为我的钱包(0x39cf...6bf)中没有USDC

关键观察点尽管交易失败了,但错误信息中包含了 完整的气体估算:

  • 气体上限:150,000单位
  • 燃气价格:0.108172169 费沃(实时数据来自主网)
  • 预估费用:0.00001622582535 以太币(约 0.06 美元)

为什么需要两个工具?

工具 test-swap swap_tokens
执行的步骤 1-4 1-5
检查余额 ❌ 否 ✅ 是
需要代币 ❌ 否 ✅ 是
目的 验证集成正常工作 全面验证
结果 ✅ 总是成功 ⚠️ 无平衡则失败

它们共同证明:

  • test-swap:Uniswap 集成正确
  • swap_tokens:完整的验证逻辑已实现(包括余额检查)

API 参考

MCP协议方法

初始化

请求:

{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}}

回应:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "protocolVersion": "2024-11-05",
    "capabilities": {"tools": {}},
    "serverInfo": {"name": "eth-trading-mcp-server", "version": "0.1.0"}
  }
}

列出工具

请求:

{"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}

回答:

{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "tools": [
      {
        "name": "get_balance",
        "description": "Query ETH and ERC20 token balances for a wallet address",
        "input_schema": {
          "type": "object",
          "properties": {
            "wallet": {"type": "string", "description": "Ethereum wallet address (0x...)"},
            "token": {"type": "string", "description": "Optional ERC20 token contract address"}
          },
          "required": ["wallet"]
        }
      },
      {
        "name": "get_token_price",
        "description": "Get real-time token price from Uniswap V2",
        "input_schema": {
          "type": "object",
          "properties": {
            "token": {"type": "string", "description": "ERC20 token contract address"},
            "base": {"type": "string", "description": "Base currency: 'eth' or 'usd'"}
          },
          "required": ["token"]
        }
      },
      {
        "name": "swap_tokens",
        "description": "Simulate token swap with gas estimation",
        "input_schema": {
          "type": "object",
          "properties": {
            "from_token": {"type": "string", "description": "Source token address"},
            "to_token": {"type": "string", "description": "Destination token address"},
            "amount": {"type": "string", "description": "Amount to swap (human-readable)"},
            "slippage_bps": {"type": "integer", "description": "Slippage in basis points (default: 50 = 0.5%)"}
          },
          "required": ["from_token", "to_token", "amount"]
        }
      }
    ]
  }
}

工具:获取余额

请求:

{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "tools/call",
  "params": {
    "name": "get_balance",
    "arguments": {
      "wallet": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
      "token": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
    }
  }
}

回答:

{
  "jsonrpc": "2.0",
  "id": 3,
  "result": {
    "wallet": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
    "erc20": {
      "raw": "17058407780",
      "formatted": "17058.40778",
      "decimals": 6,
      "symbol": "USDC"
    },
    "block_number": 23604361
  }
}

工具:获取代币价格

请求:

{
  "jsonrpc": "2.0",
  "id": 4,
  "method": "tools/call",
  "params": {
    "name": "get_token_price",
    "arguments": {
      "token": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
      "base": "eth"
    }
  }
}

回答:

{
  "jsonrpc": "2.0",
  "id": 4,
  "result": {
    "token": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
    "base": "WETH",
    "price": "0.0002608076152110160880795511",
    "pair": "0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc",
    "source": "Uniswap V2",
    "block_number": 23604361
  }
}

工具:交换令牌(或“交换代币”)

请求:

{
  "jsonrpc": "2.0",
  "id": 5,
  "method": "tools/call",
  "params": {
    "name": "swap_tokens",
    "arguments": {
      "from_token": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
      "to_token": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
      "amount": "1000",
      "slippage_bps": 50
    }
  }
}

响应(成功):

{
  "jsonrpc": "2.0",
  "id": 5,
  "result": {
    "from_token": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
    "to_token": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
    "amount_in": "1000",
    "path": [
      "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
      "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
    ],
    "estimated_out": {
      "raw": "256880914571150306",
      "formatted": "0.256880914571150306"
    },
    "amount_out_min": {
      "raw": "255596509998294554",
      "formatted": "0.255596509998294554"
    },
    "gas_limit": "150000",
    "gas_price": "108172169",
    "estimated_fee_native": "0.00001622582535",
    "slippage_bps": 50
  }
}

响应(余额不足):

{
  "jsonrpc": "2.0",
  "id": 5,
  "error": {
    "code": -32603,
    "message": "Swap simulation failed (likely insufficient balance or approval). Gas estimate: 150000 units @ 0.108172169 gwei = 0.00001622582535 ETH. Error: Swap simulation failed"
  }
}

测试

单元测试

cargo test

预期输出: 19 passed

测试内容包括:

  • 十进制转换(U256 ↔ 十进制)
  • Uniswap V2的价格计算
  • 交换路径路由逻辑
  • 解析具有不同小数位数的金额

集成测试

集成测试连接到真实的以太坊主网:

# Requires RPC_URL in .env
cargo test --test it_mainnet -- --ignored --nocapture

这些测试验证了:

  • 真实的RPC连接
  • Uniswap V2合约调用
  • 实时余额查询
  • 价格数据准确性

验证工具

这个(或“该”) test-swap 二进制证明:Uniswap 集成无需依赖代币余额即可正常工作:

./target/release/test-swap

这非常适合进行演示和调试。


建筑学

项目结构

eth-trading-mcp-server/
│
├── Cargo.toml                # Project dependencies and metadata
├── .env.example              # Environment variable template
├── README.md                 # This file
│
├── src/                      # Main source code
│   ├── main.rs              # MCP server entry point (stdin/stdout)
│   ├── lib.rs               # Library exports for bins and tests
│   ├── config.rs            # Environment configuration (RPC_URL, etc.)
│   ├── types.rs             # MCP protocol types (Request/Response DTOs)
│   ├── rpc.rs               # MCP JSON-RPC handler (routes to tools)
│   │
│   ├── eth/                 # Ethereum RPC interactions
│   │   ├── mod.rs           # Module exports
│   │   ├── provider.rs      # Provider/signer creation
│   │   └── erc20.rs         # ERC20 ABI calls (balanceOf, decimals, symbol)
│   │
│   ├── uniswap/             # Uniswap V2 integration
│   │   ├── mod.rs           # Module exports
│   │   └── v2.rs            # Router/pair interactions, swap encoding
│   │
│   ├── tools/               # MCP tool implementations
│   │   ├── mod.rs           # Module exports
│   │   ├── balance.rs       # get_balance (ETH + ERC20)
│   │   ├── price.rs         # get_token_price (from Uniswap reserves)
│   │   └── swap.rs          # swap_tokens (5-step validation)
│   │
│   ├── utils/               # Utility functions
│   │   ├── mod.rs           # Module exports
│   │   └── decimal.rs       # U256 ↔ Decimal conversion
│   │
│   └── bin/                 # Verification utilities
│       ├── show_address.rs  # Display wallet address from private key
│       └── test_swap.rs     # Verify Uniswap integration (no balance needed)
│
└── tests/                   # Test suites
    ├── unit_decimal.rs      # Decimal conversion tests
    ├── unit_v2.rs           # Uniswap V2 logic tests
    └── it_mainnet.rs        # Integration tests (requires RPC)

模块职责

模块 目的 关键功能
main.rs MCP服务器入口点 stdin/stdout JSON-RPC循环
config.rs 环境配置 加载RPC_URL、PRIVATE_KEY、硬编码地址
types.rs 数据结构 MCP请求, MCP响应, 工具数据传输对象(DTOs)
rpc.rs 协议处理程序 将路由方法映射到工具
eth/provider.rs RPC 连接 创建提供者/签名者,获取区块/余额
eth/erc20.rs 代币查询 通过 eth_call 获取 balanceOf、decimals、symbol
uniswap/v2.rs DEX集成 getAmountsOut, 编码交换, 模拟
tools/balance.rs get_balance 查询 ETH + ERC20 余额
tools/price.rs get_token_price 从Uniswap储备金计算得出
tools/swap.rs 交换代币 5步流程(查询 → 估算 → 模拟)
utils/decimal.rs 精确数学 U256 ↔ 带适当小数位的十进制数

关键技术

技术 版本 用途
Rust(编程语言) 2021版 系统编程语言
东京 1.42 支持并发RPC调用的异步运行时
ethers-rs(可译为“以太坊库-rs”,但通常根据上下文直接保留为“ethers-rs”,因为这是特定库或项目的名称) 2.0.14 以太坊RPC客户端和ABI编码
`rust_decimal` 翻译为中文是“Rust 语言的十进制库”或“Rust 十进制处理库”。这个名称通常指的是一个用于在 Rust 语言中进行十进制数运算的库或模块 1.37 随意精度的小数(无浮点误差)
serde(在中文语境下,通常直接保留原名,不进行翻译,但如果要解释其含义,可以理解为“序列化/反序列化库”或“数据序列化框架”,具体取决于上下文) 1.0 JSON 序列化/反序列化
serde_json(在中文语境中,可直译为“序列化/反序列化 JSON”,但通常保持原名以指代特定库或技术) 1.0 MCP协议JSON处理
追踪 0.1 结构化日志记录(日志输出到标准错误流)
dotenv(用于加载环境变量的库/工具) 0.15 自动加载 .env 文件
不管怎样 1.0 带上下文的错误处理
hex(十六进制) 0.4 十六进制编码/解码

数据流

User/AI Agent
    │
    ├─ JSON-RPC request (stdin)
    ↓
┌─────────────────────────────────────┐
│  main.rs (MCP Server)               │
│  - Parse JSON                       │
│  - Call rpc.rs handler              │
└────────────┬────────────────────────┘
             │
             ↓
┌─────────────────────────────────────┐
│  rpc.rs (Protocol Handler)          │
│  - Route to appropriate tool        │
│  - initialize / tools/list / call   │
└────────────┬────────────────────────┘
             │
      ┌──────┴──────┬──────────────┐
      ↓             ↓              ↓
┌──────────┐  ┌──────────┐  ┌──────────┐
│ balance  │  │  price   │  │   swap   │
│  tool    │  │   tool   │  │   tool   │
└────┬─────┘  └────┬─────┘  └────┬─────┘
     │             │              │
     └─────────────┴──────────────┘
                   ↓
     ┌──────────────────────────────┐
     │  eth/provider.rs              │
     │  - RPC calls via ethers-rs    │
     │  - eth_call, eth_getBalance   │
     └─────────────┬─────────────────┘
                   │
                   ↓
          Ethereum Mainnet
          (Infura/Alchemy)
                   │
                   ↓
     ┌──────────────────────────────┐
     │  Smart Contracts              │
     │  - ERC20 tokens               │
     │  - Uniswap V2 Router/Pairs    │
     └───────────────────────────────┘

为何选择这种架构?

  1. 模块化设计明确的职责分离(RPC ↔ 工具 ↔ 区块链)
  2. 可测试性每个模块都可以独立进行单元测试
  3. 可重用性工具间共享的通用功能(小数、提供商)
  4. 安全没有实际的交易签名——所有操作都使用 eth_call
  5. 可扩展性易于添加新工具或支持Uniswap V3

已知的限制

  • 仅限Uniswap V2V3集中流动性不支持
  • 价格准确性仅反映V2池的价格,可能与中心化交易所(CEX)价格存在差异
  • 简单路由仅限直接或单跳(通过WETH)路径
  • 未找到符号需要合约地址(例如。, 0xA0b8... 不是 "USDC")
  • 仅限主网设计用于以太坊主网(链ID为1)
  • 只读无交易广播——仅模拟

常见代币地址(以太坊主网)

WETH:  0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
USDC:  0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
USDT:  0xdAC17F958D2ee523a2206206994597C13D831ec7
DAI:   0x6B175474E89094C44Da98b954EedeAC495271d0F
WBTC:  0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599

Uniswap V2 Router: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
Uniswap V2 Factory: 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f

许可证

麻省理工学院(MIT)


做出贡献

欢迎投稿!请确保:

  • cargo build 编译成功
  • cargo test 通过所有测试
  • cargo fmt 并且 cargo clippy 产生清洁的输出

开发说明

这个项目是在人工智能工具(Claude Code)的协助下开发的,并展示了:

  • 使用Tokio进行Rust异步编程
  • 通过ethers-rs进行以太坊RPC交互
  • 金融数据的正确十进制处理
  • MCP协议合规性
  • 现实世界的DeFi集成(Uniswap V2)