integration_tests

package
v0.0.0-...-da40432 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2025 License: MIT Imports: 11 Imported by: 0

README

EVM Chain 测试

本目录包含 EVMChain 的集成测试。这些测试会启动一个 Ethereum 节点容器 (Ganache) 用于测试。

环境设置

首先,请复制 env.example 文件为 .env 文件,并填入所需的环境变量:

cp env.example .env

需要设置的环境变量有:

  • INFURA_RPC_URL - Infura 的以太坊主网 RPC URL (用于测试合约部署)
  • MAINNET_RPC_URL - 以太坊主网 RPC URL,用于 fork 主网测试 (可以是 Infura, Alchemy 等)
  • ETHERSCAN_API_KEY - Etherscan API 密钥,用于获取合约 ABI

运行测试

运行所有测试
go test -v ./... -count=1
运行特定测试

基本集成测试:

go test -v -run TestEVMChainIntegration

交易测试 (使用 fork 主网):

go test -v -run TestDoTradeWithForkMainnet

排查测试问题

如果在运行 fork 主网测试时遇到问题,可以尝试以下方法:

  1. 确保提供了有效的 RPC URL,并且该 RPC 提供商不限制您的请求。
  2. 增加 fork 测试的超时时间,因为它可能需要更长的时间来完成。
  3. 检查网络连接,确保可以访问主网 RPC 端点。
  4. 如果遇到内存问题,可以增加 Docker 容器的可用内存。

Ganache Fork 参数

Ganache fork 参数可以在 startGanacheForkMainnet 函数中进行调整。关键参数包括:

  • --fork - 主网 RPC URL
  • --fork.blockNumber - 要 fork 的区块号
  • --defaultBalanceEther - 测试账户的默认余额
  • --gasPrice - 默认 gas 价格
  • --gasLimit - 区块 gas 限制

注意事项

  1. fork 主网测试可能会使用较大的内存和磁盘空间。
  2. 确保您有足够的系统资源来运行测试。
  3. 测试可能需要几分钟才能完成,尤其是 fork 主网的测试。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEthClient

func GetEthClient(ctx context.Context, container testcontainers.Container) (*ethclient.Client, error)

GetEthClient returns an Ethereum client connected to the container

func HTTPEndpoint

func HTTPEndpoint(ctx context.Context, container testcontainers.Container) (string, error)

HTTPEndpoint returns the HTTP endpoint URL for the container

Types

type Account

type Account struct {
	Address    common.Address
	PrivateKey *ecdsa.PrivateKey
}

Account represents an Ethereum account with its address and private key

func GetPreFundedAccounts

func GetPreFundedAccounts(ctx context.Context, container testcontainers.Container) ([]Account, error)

GetPreFundedAccounts returns a set of pre-funded Ethereum accounts in Ganache

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL