跳转到主要内容

流行的用于测试的Solidity智能合约:ERC-20,Uniswap v2等。

项目描述

Automated test suite

Documentation Status

编写以太坊测试套件的模拟智能合约

此软件包包含用于自动化测试套件的常用以太坊智能合约。此软件包是为交易策略创建的,但也可用于其他任何项目。与较慢且较混乱的主网分叉测试策略相比,此项目旨在明确干净的部署和非常快速的测试执行。

支持的智能合约包括

  • ERC-20代币
  • SushiSwap:路由器、工厂、池(Uniswap v2、PancakeSwap、QuickSwap、Trader Joe和其他99%与SushiSwap兼容)
  • 高质量的API文档
  • 全面支持类型提示,以优化开发者体验
  • (更多集成即将推出)

目录

预编译ABI文件分发

本包主要支持Python、Web3.p3和Brownie开发者。对于其他编程语言和框架,您可以在abi文件夹中找到预编译的Solidity智能合约

这些文件可以与任何框架配合使用

  • Web3.js
  • Ethers.js
  • Hardhat
  • Truffle
  • Web3j

每个JSON文件都包含您需要部署合约的abibytecode键。

只需下载并将其嵌入到您的项目中。编译后的源代码文件是MIT和GPL v2许可证的混合体。

Python使用

Python支持作为smart_contract_test_fixtures Python包提供。

此包仅依赖于web3.py,而不是Brownie等其他包。它抓取流行的ABI文件及其字节码和编译工件,以便合约可以轻松地在任何以太坊测试器接口上部署。无需Ganache,所有操作都可以在更快的eth-tester引擎上执行。

先决条件

ERC-20代币示例

使用该包在pytest测试中部署简单的ERC-20令牌

import pytest
from web3 import Web3, EthereumTesterProvider

from smart_contracts_for_testing.token import create_token


@pytest.fixture
def tester_provider():
    return EthereumTesterProvider()


@pytest.fixture
def eth_tester(tester_provider):
    return tester_provider.ethereum_tester


@pytest.fixture
def web3(tester_provider):
    return Web3(tester_provider)


@pytest.fixture()
def deployer(web3) -> str:
    """Deploy account."""
    return web3.eth.accounts[0]


@pytest.fixture()
def user_1(web3) -> str:
    """User account."""
    return web3.eth.accounts[1]


@pytest.fixture()
def user_2(web3) -> str:
    """User account."""
    return web3.eth.accounts[2]


def test_deploy_token(web3: Web3, deployer: str):
    """Deploy mock ERC-20."""
    token = create_token(web3, deployer, "Hentai books token", "HENTAI", 100_000 * 10**18)
    assert token.functions.name().call() == "Hentai books token"
    assert token.functions.symbol().call() == "HENTAI"
    assert token.functions.totalSupply().call() == 100_000 * 10**18
    assert token.functions.decimals().call() == 18


def test_tranfer_tokens_between_users(web3: Web3, deployer: str, user_1: str, user_2: str):
    """Transfer tokens between users."""
    token = create_token(web3, deployer, "Telos EVM rocks", "TELOS", 100_000 * 10**18)

    # Move 10 tokens from deployer to user1
    token.functions.transfer(user_1, 10 * 10**18).transact({"from": deployer})
    assert token.functions.balanceOf(user_1).call() == 10 * 10**18

    # Move 10 tokens from deployer to user1
    token.functions.transfer(user_2, 6 * 10**18).transact({"from": user_1})
    assert token.functions.balanceOf(user_1).call() == 4 * 10**18
    assert token.functions.balanceOf(user_2).call() == 6 * 10**18

查看完整示例.

有关如何在测试中使用Web3.py的更多信息,请参阅Web3.py文档.

Uniswap交换示例

import pytest
from web3 import Web3
from web3.contract import Contract

from smart_contracts_for_testing.uniswap_v2 import UniswapV2Deployment, deploy_trading_pair, FOREVER_DEADLINE


def test_swap(web3: Web3, deployer: str, user_1: str, uniswap_v2: UniswapV2Deployment, weth: Contract, usdc: Contract):
    """User buys WETH on Uniswap v2 using mock USDC."""

    # Create the trading pair and add initial liquidity
    deploy_trading_pair(
        web3,
        deployer,
        uniswap_v2,
        weth,
        usdc,
        10 * 10**18,  # 10 ETH liquidity
        17_000 * 10**18,  # 17000 USDC liquidity
    )

    router = uniswap_v2.router

    # Give user_1 500 dollars to buy ETH and approve it on the router
    usdc_amount_to_pay = 500 * 10**18
    usdc.functions.transfer(user_1, usdc_amount_to_pay).transact({"from": deployer})
    usdc.functions.approve(router.address, usdc_amount_to_pay).transact({"from": user_1})

    # Perform a swap USDC->WETH
    path = [usdc.address, weth.address]  # Path tell how the swap is routed
    # https://docs.uniswap.org/protocol/V2/reference/smart-contracts/router-02#swapexacttokensfortokens
    router.functions.swapExactTokensForTokens(
        usdc_amount_to_pay,
        0,
        path,
        user_1,
        FOREVER_DEADLINE,
    ).transact({
        "from": user_1
    })

    # Check the user_1 received ~0.284 ethers
    assert weth.functions.balanceOf(user_1).call() / 1e18 == pytest.approx(0.28488156127668085)

查看完整示例.

如何在Python项目中使用此库

smart_contract_test_fixtures添加为开发依赖项

使用Poetry

poetry add -D smart_contract_test_fixtures

开发

此步骤将提取Sushiswap存储库中的编译后的智能合约。

要求

  • Node v14
  • npx
  • yarn
  • GNU Make
  • Unix shell

Make

构建ABI发行版

git submodule update --recursive --init
make all

有关更多信息,请参阅SushiSwap持续集成文件.

版本历史

查看变更日志.

Discord

加入Discord以提问.

注释

目前不支持Brownie。要支持Brownie,需要找出如何将基于Hardhat的项目(Sushiswap)导入Brownie项目格式。

许可

MIT

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

smart_contracts_for_testing-0.1.0.tar.gz (180.3 kB 查看哈希值)

上传时间

构建分发

smart_contracts_for_testing-0.1.0-py3-none-any.whl (226.9 kB 查看哈希值)

上传时间 Python 3

由以下支持