Hummingbot 第8章:Gateway DEX 中间件
Gateway 是 Hummingbot 的 DEX(去中心化交易所)中间件组件,负责处理与区块链网络的交互,使 Hummingbot 能够连接各类 DeFi 协议。
Gateway 架构
什么是 Gateway
Gateway 是一个独立的 Node.js 服务,作为 Hummingbot Client 和区块链之间的桥梁:
┌──────────────┐ REST API ┌──────────────────┐ RPC ┌───────────┐
│ Hummingbot │ ◄──────────────────► │ Gateway 服务 │ ◄──────────────► │ 区块链 │
│ Client │ (localhost) │ (Node.js) │ │ 节点 │
└──────────────┘ └──────────────────┘ └───────────┘
Gateway 提供以下核心功能:
- 私钥管理:安全存储和管理区块链钱包私钥
- 交易构建:构建和签名区块链交易
- Gas 估算:自动估算交易 Gas 费用
- 合约交互:与智能合约进行交互
- 多链支持:同时连接多条区块链
Gateway 安装
Gateway 需要独立安装,可以通过 Docker 运行:
# 拉取 Gateway 镜像
docker pull hummingbot/hummingbot-gateway:latest
# 运行 Gateway 容器
docker run -d \
--name hummingbot-gateway \
-p 15888:15888 \
-v ~/hummingbot-gateway/conf:/usr/src/app/conf \
-v ~/hummingbot-gateway/logs:/usr/src/app/logs \
hummingbot/hummingbot-gateway:latest
# 验证 Gateway 是否启动
curl http://localhost:15888/api/v2/status
# {"status": "ok", "version": "2.0.0"}
源码安装
# 克隆仓库
git clone https://github.com/hummingbot/gateway.git
cd gateway
# 安装依赖
npm install
# 编译 TypeScript
npm run build
# 启动服务
npm start
DEX 连接配置
连接 EVM 链
通过 Hummingbot Client 连接 EVM 兼容链:
>>> gateway connect ethereum
Which blockchain do you want to connect to? ethereum
Enter your ethereum wallet address: 0x1234567890abcdef1234567890abcdef12345678
Enter your ethereum private key: <your-private-key>
Enter your ethereum node URL (Infura/Alchemy): https://mainnet.infura.io/v3/your-project-id
Configuration saved. You are now connected to ethereum.
支持的 EVM 链
| 区块链 | Gateway 链名 | 推荐 RPC 提供商 |
|---|---|---|
| Ethereum | ethereum | Infura / Alchemy |
| BNB Chain | bsc | Binance Public RPC |
| Polygon | polygon | Infura / QuickNode |
| Arbitrum | arbitrum | Infura / Alchemy |
| Optimism | optimism | Infura / Alchemy |
| Avalanche | avalanche | Infura / Ava Labs |
| Base | base | Base Public RPC |
连接到 DEX
以 Uniswap V3 为例:
# conf_uniswap.yml
strategy: amm_arb
connector: uniswap_v3_ethereum
trading_pair: WETH-USDC
# Gateway 连接配置
gateway:
host: 127.0.0.1
port: 15888
api_key: null
# DEX 参数
slippage_buffer: 0.5
gas_limit: 200000
max_fee_per_gas: 100 # gwei
max_priority_fee_per_gas: 2 # gwei
EVM 链配置
RPC 节点配置
Gateway 的 RPC 节点配置位于 conf/gateway.yml:
# conf/gateway.yml
ethereum:
rpcUrl: https://mainnet.infura.io/v3/your-project-id
chainId: 1
gasPrice: 50
gasLimit: 200000
bsc:
rpcUrl: https://bsc-dataseed.binance.org
chainId: 56
gasPrice: 5
gasLimit: 200000
polygon:
rpcUrl: https://polygon-rpc.com
chainId: 137
gasPrice: 50
gasLimit: 500000
多 RPC 配置
为提高稳定性,可以为每条链配置多个 RPC 节点:
ethereum:
rpcUrls:
- https://mainnet.infura.io/v3/project-id-1
- https://eth-mainnet.alchemyapi.io/v2/project-id-2
- https://rpc.ankr.com/eth
rpcUrlRotation: true # 自动轮换
chainId: 1
Solana 链配置
连接 Solana
>>> gateway connect solana
Which blockchain do you want to connect to? solana
Enter your solana wallet address: <your-solana-address>
Enter your solana private key: <base58-private-key>
Enter your solana RPC URL: https://api.mainnet-beta.solana.com
Configuration saved. You are now connected to solana.
Solana 配置要点
# gateway.yml 中的 Solana 配置
solana:
rpcUrl: https://api.mainnet-beta.solana.com
commitment: confirmed # 交易确认级别
maxTransactionVersion: 0 # 兼容版本化交易
computeUnitPrice: 1000 # microLamports
Gas 管理
Gas 策略配置
# Gas 管理配置
gas:
# 自动估算 Gas
estimation: true
estimationStrategy: eth_gasPrice # 或: oracle
# Gas 价格限制
maxGasPrice: 200 # gwei
priorityFee: 2 # gwei (EIP-1559)
# Gas 限制
gasLimitMultiplier: 1.2 # 在估算基础上增加 20%
# 加速交易
replaceTransactionOnGasIncrease: true
gasIncreasePercent: 20
Gas 监控
# 查看当前 Gas 状态
>>> gateway gas
# 输出示例:
# Ethereum Gas Status:
# Current Base Fee: 25 gwei
# Priority Fee: 2 gwei
# Estimated Gas Limit: 200000
# Estimated Cost: $12.50
# 根据 Gas 调整策略
>>> gateway gas --strategy conservative
# Gas strategy set to conservative (max 50 gwei)
安全注意事项
私钥管理
# Gateway 私钥加密配置
security:
# 私钥加密存储
encryptedPrivateKeys: true
encryptionMethod: aes-256-gcm
# Gateway API 认证
authentication:
enabled: true
username: "gateway_admin"
password: "strong-password-here"
# IP 白名单
ipWhitelist:
- "127.0.0.1"
- "192.168.1.0/24"
安全最佳实践
| 建议 | 说明 |
|---|---|
| 使用单独钱包 | Gateway 使用专用交易钱包,不要存放大量资产 |
| 限制 API 暴露 | Gateway 默认绑定 127.0.0.1,不要暴露到公网 |
| 定期轮换密钥 | 定期更换区块链私钥和 Gateway 访问密码 |
| 监控异常交易 | 设置金额上限,防止私钥泄露后的资产损失 |
| 使用硬件钱包 | 大额资金建议使用硬件钱包签名 |
故障排除
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 交易卡住 | Gas 设置过低 | 提高 Gas 价格或等待网络空闲 |
| RPC 连接失败 | 节点不可用 | 切换到备用 RPC 节点 |
| 交易失败 | Slippage 过小 | 增大滑点缓冲百分比 |
| Nonce 错误 | 交易顺序混乱 | 手动重置 Nonce 或重启 Gateway |