name: trading-evm
description: “在EVM链上交易代币 - 以太坊上的Uniswap V3, 1inch,Arbitrum, Optimism, Base, Polygon”
emoji: “⟠”
gates:
envs:
- EVM_PRIVATE_KEY
EVM DEX交易 - 完整API参考
使用Uniswap V3和1inch聚合器在以太坊,Arbitrum,Optimism,Base和Polygon上交易任何代币。
必需的环境变量
EVM_PRIVATE_KEY=0x... # 您的EVM钱包私钥
ALCHEMY_API_KEY=... # 可选:更好的RPC
ONEINCH_API_KEY=... # 可选:1inch API
支持的链
| 链 |
链ID |
DEXes |
MEV保护 |
| 以太坊 |
1 |
Uniswap V3, 1inch |
Flashbots Protect |
| Arbitrum |
42161 |
Uniswap V3, 1inch |
Sequencer |
| Optimism |
10 |
Uniswap V3, 1inch |
Sequencer |
| Base |
8453 |
Uniswap V3, 1inch |
Sequencer |
| Polygon |
137 |
Uniswap V3, 1inch |
标准 |
聊天命令
交换
/swap eth <amount> <from> to <to> # 在以太坊上交换
/swap arb <amount> <from> to <to> # 在Arbitrum上交换
/swap op <amount> <from> to <to> # 在Optimism上交换
/swap base <amount> <from> to <to> # 在Base上交换
/swap matic <amount> <from> to <to> # 在Polygon上交换
# 示例:
/swap eth 1 ETH to USDC # 在以太坊上将1 ETH换成USDC
/swap arb 100 USDC to ARB # 在Arbitrum上将100 USDC换成ARB
/swap base 0.5 ETH to DEGEN # 在Base上将0.5 ETH换成DEGEN
报价
/quote eth <amount> <from> to <to> # 获取报价但不执行
/quote arb 1 ETH to USDC # 在Arbitrum上报价
比较路线
/compare <chain> <amount> <from> to <to> # 比较Uniswap与1inch
/compare eth 1 ETH to USDC # 在以太坊上比较路线
余额
/balance eth # 检查ETH和代币余额
/balance arb # 检查Arbitrum余额
/balance base <token> # 检查Base上的特定代币
TypeScript API参考
Uniswap V3
import {
executeUniswapSwap,
getUniswapQuote,
resolveToken,
getTokenInfo,
getEvmBalance
} from 'clodds/evm/uniswap';
// 获取报价
const quote = await getUniswapQuote({
chain: 'ethereum',
tokenIn: 'ETH',
tokenOut: 'USDC',
amountIn: '1000000000000000000', // 1 ETH in wei
slippageTolerance: 0.5,
});
console.log(`预期输出:${quote.amountOut}`);
console.log(`价格影响:${quote.priceImpact}%`);
console.log(`路线:${quote.route.join(' → ')}`);
// 执行交换
const result = await executeUniswapSwap({
chain: 'ethereum',
tokenIn: 'ETH',
tokenOut: 'USDC',
amountIn: '1000000000000000000',
slippageTolerance: 0.5,
deadline: 300, // 5分钟
});
console.log(`TX:${result.transactionHash}`);
console.log(`输出量:${result.amountOut}`);
// 将代币符号解析为地址
const usdcAddress = resolveToken('USDC', 'ethereum');
// 获取代币信息
const tokenInfo = await getTokenInfo('ethereum', usdcAddress);
console.log(`${tokenInfo.symbol}: ${tokenInfo.decimals} decimals`);
// 检查余额
const balance = await getEvmBalance('ethereum', walletAddress, 'USDC');
1inch聚合器
import {
executeOneInchSwap,
getOneInchQuote,
getOneInchProtocols,
compareDexRoutes
} from 'clodds/evm/oneinch';
// 从1inch获取报价
const quote = await getOneInchQuote({
chain: 'ethereum',
fromToken: 'ETH',
toToken: 'USDC',
amount: '1000000000000000000',
});
console.log(`预期输出:${quote.toAmount}`);
console.log(`估计的gas:${quote.estimatedGas}`);
console.log(`使用的协议:${quote.protocols.join(', ')}`);
// 通过1inch执行交换
const result = await executeOneInchSwap({
chain: 'ethereum',
fromToken: 'ETH',
toToken: 'USDC',
amount: '1000000000000000000',
slippage: 0.5,
});
// 获取可用协议
const protocols = await getOneInchProtocols('ethereum');
// ['UNISWAP_V3', 'SUSHISWAP', 'CURVE', ...]
// 比较Uniswap和1inch之间的路线
const comparison = await compareDexRoutes({
chain: 'ethereum',
tokenIn: 'ETH',
tokenOut: 'USDC',
amountIn: '1000000000000000000',
});
console.log(`Uniswap:${comparison.uniswap.amountOut}`);
console.log(`1inch:${comparison.oneinch.amountOut}`);
console.log(`最佳:${comparison.best}`);
链特定示例
以太坊
// 在以太坊上交换ETH → USDC,使用MEV保护
const result = await executeUniswapSwap({
chain: 'ethereum',
tokenIn: 'ETH',
tokenOut: 'USDC',
amountIn: '1000000000000000000',
useMevProtection: true, // 使用Flashbots
});
Arbitrum
// 在Arbitrum上交换(更低的gas)
const result = await executeOneInchSwap({
chain: 'arbitrum',
fromToken: 'ETH',
toToken: 'ARB',
amount: '500000000000000000', // 0.5 ETH
slippage: 1,
});
Base
// 在Base上交换
const result = await executeUniswapSwap({
chain: 'base',
tokenIn: 'ETH',
tokenOut: 'DEGEN',
amountIn: '100000000000000000', // 0.1 ETH
slippageTolerance: 2, // 对于迷因币更高的滑点
});
Optimism
// 在Optimism上交换
const result = await executeOneInchSwap({
chain: 'optimism',
fromToken: 'USDC',
toToken: 'OP',
amount: '100000000', // 100 USDC (6 decimals)
slippage: 0.5,
});
Polygon
// 在Polygon上交换
const result = await executeUniswapSwap({
chain: 'polygon',
tokenIn: 'MATIC',
tokenOut: 'USDC',
amountIn: '10000000000000000000', // 10 MATIC
slippageTolerance: 0.5,
});
常见代币
以太坊
| 符号 |
地址 |
| ETH |
本地 |
| USDC |
0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 |
| USDT |
0xdAC17F958D2ee523a2206206994597C13D831ec7 |
| WETH |
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 |
| DAI |
0x6B175474E89094C44Da98b954EescdeCB5BE3830 |
Arbitrum
| 符号 |
地址 |
| ETH |
本地 |
| ARB |
0x912CE59144191C1204E64559FE8253a0e49E6548 |
| USDC |
0xaf88d065e77c8cC2239327C5EDb3A432268e5831 |
Base
| 符号 |
地址 |
| ETH |
本地 |
| USDC |
0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 |
| DEGEN |
0x4ed4E862860beD51a9570b96d89aF5E1B0Efefed |
Optimism
| 符号 |
地址 |
| ETH |
本地 |
| OP |
0x4200000000000000000000000000000000000042 |
| USDC |
0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85 |
MEV保护
以太坊(Flashbots)
// 自动使用Flashbots Protect RPC
const result = await executeUniswapSwap({
chain: 'ethereum',
useMevProtection: true, // 通过Flashbots发送
...
});
L2链
L2(Arbitrum, Optimism, Base)默认由序列器级别的MEV保护。
Gas估算
// 在交换前获取gas估算
const quote = await getUniswapQuote({ ... });
console.log(`估计的gas:${quote.gasEstimate}`);
console.log(`Gas价格:${quote.gasPrice} gwei`);
console.log(`总gas成本:${quote.gasCostUsd} USD`);
错误处理
import { EvmSwapError, InsufficientBalanceError, SlippageExceededError } from 'clodds/evm';
try {
await executeUniswapSwap({ ... });
} catch (error) {
if (error instanceof InsufficientBalanceError) {
console.log('余额不足');
} else if (error instanceof SlippageExceededError) {
console.log('价格变动,增加滑点');
}
}