Skip to main content

PyPI version

Automated test suite

Documentation Status

Web3-Ethereum-Defi

This project contains common Ethereum smart contracts and utilities, for trading, wallets,automated test suites and backend integrations for EVM based blockchains.

Pepe chooses Web3-Ethereum-DeFi and Python

Pepe chooses web3-ethereum-defi and Python.

Features

Features include

Precompiled ABI file distribution

The project provides a precompiled smart contract bundle, including ABI files, full source and debug maps, to make deploying test instances trivial.

This package primarly supports Python, Web3.p3 and Brownie developers. For other programming languages and frameworks, you can find precompiled Solidity smart contracts in abi folder.

These files are good to go with any framework:

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

Each JSON file has abi and bytecode keys you need to deploy a contract.

Just download and embed in your project. The compiled source code files are mixture of MIT and GPL v2 license.

Python usage

The Python support is available as web3-ethereum-defi Python package.

The package depends only on web3.py and not others, like Brownie. It grabs popular ABI files with their bytecode and compilation artifacts so that the contracts are easily deployable on any Ethereum tester interface. No Ganache is needed and everything can be executed on faster eth-tester enginer.

Unlike Brownie, which is a framework, web3-ethereum-defi is a library. It is designed to be included in any other Python application and you can only use bits of its that you need. There are no expectations on configuration files or folder structure.

[Read the full API documentation](High-quality API documentation](https://web3-ethereum-defi.readthedocs.io/)). For code examples please see below.

Prerequisites

Code examples

For more code examples, see the tutorials section in the documentation.

Deploy and transfer ERC-20 token between wallets

To use the package to deploy a simple ERC-20 token in pytest testing:

import pytest
from web3 import Web3, EthereumTesterProvider

from eth_defi.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

See full example.

For more information how to user Web3.py in testing, see Web3.py documentation.

Uniswap v2 trade example

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

from eth_defi.uniswap_v2.deployment 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)

See the full example.

Uniswap v2 price estimation example

# Create the trading pair and add initial liquidity
deploy_trading_pair(
    web3,
    deployer,
    uniswap_v2,
    weth,
    usdc,
    1_000 * 10**18,  # 1000 ETH liquidity
    1_700_000 * 10**18,  # 1.7M USDC liquidity
)

# Estimate the price of buying 1 ETH
usdc_per_eth = estimate_buy_price_decimals(
    uniswap_v2,
    weth.address,
    usdc.address,
    Decimal(1.0),
)
assert usdc_per_eth == pytest.approx(Decimal(1706.82216820632059904))

How to use the library in your Python project

Add web3-ethereum-defi as a development dependency:

Using Poetry:

# Data optional dependencies include pandas and gql, needed to fetch Uniswap v3 data
poetry add -D "web3-ethereum-defi[data]"

Documentation

Development and contributing

Version history

Social media

Notes

Currently there is no Brownie support. To support Brownie, one would need to figure out how to import an existing Hardhat based project (Sushiswap) to Brownie project format.

History

Originally created for Trading Strategy. Originally the package was known as eth-hentai.

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

web3-ethereum-defi-0.11.tar.gz (549.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

web3_ethereum_defi-0.11-py3-none-any.whl (685.5 kB view details)

Uploaded Python 3

File details

Details for the file web3-ethereum-defi-0.11.tar.gz.

File metadata

  • Download URL: web3-ethereum-defi-0.11.tar.gz
  • Upload date:
  • Size: 549.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.13 Darwin/21.5.0

File hashes

Hashes for web3-ethereum-defi-0.11.tar.gz
Algorithm Hash digest
SHA256 9b66900b846a01f1a64fb99b0b39c7bbd408d0985973a6488e947999eb69d41b
MD5 77568e76fb68eed8de2b1838840482c2
BLAKE2b-256 7b37041c91e08b52107c7890ad995e3ebba4f6ae28dfcddcad0fcf7c3d8070b0

See more details on using hashes here.

File details

Details for the file web3_ethereum_defi-0.11-py3-none-any.whl.

File metadata

  • Download URL: web3_ethereum_defi-0.11-py3-none-any.whl
  • Upload date:
  • Size: 685.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.13 Darwin/21.5.0

File hashes

Hashes for web3_ethereum_defi-0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 af521f60ff20b99f6f411eaa0918798a803427c7bfb917b6b42827d90d591937
MD5 401c19dab5413b77dbd411864d04ace1
BLAKE2b-256 8c04c79eff40a93b55e23d3c1374c97d935012b0357a9b2296c3d050da1d98d0

See more details on using hashes here.

Release history Release notifications | RSS feed

1.2

2 files

1.1

2 files

1.0.2

2 files

1.0.1

2 files

1.0

2 files

0.41

2 files

0.40

2 files

0.39

2 files

0.38

2 files

0.37

2 files

0.36

2 files

0.35

2 files

0.34

2 files

0.33

2 files

0.32

2 files

0.31

2 files

0.30

2 files

0.29.1

2 files

0.29

2 files

0.28.1

2 files

0.28

2 files

0.27

2 files

0.26.1

2 files

0.26

2 files

0.25.7

2 files

0.25.6

2 files

0.25.5

2 files

0.25.4

2 files

0.25.3

2 files

0.25.2

2 files

0.25.1

2 files

0.25

2 files

0.24.6

2 files

0.24.5

2 files

0.24.4

2 files

0.24.3

2 files

0.24.2

2 files

0.24.1

2 files

0.24

2 files

0.23.1

2 files

0.23

2 files

0.22.30

2 files

0.22.29

2 files

0.22.28

2 files

0.22.27

2 files

0.22.26

2 files

0.22.25

2 files

0.22.24

2 files

0.22.23

2 files

0.22.22

2 files

0.22.21

2 files

0.22.20

2 files

0.22.19

2 files

0.22.18

2 files

0.22.17

2 files

0.22.16

2 files

0.22.15

2 files

0.22.14

2 files

0.22.13

2 files

0.22.12

2 files

0.22.11

2 files

0.22.10

2 files

0.22.9

2 files

0.22.8

2 files

0.22.7

2 files

0.22.6

2 files

0.22.5

2 files

0.22.3

2 files

0.22.2

2 files

0.22.1

2 files

0.22

2 files

0.21.8

2 files

0.21.7

2 files

0.21.6

2 files

0.21.5

2 files

0.21.4

2 files

0.21.3

2 files

0.21.2

2 files

0.21.1

2 files

0.21

2 files

0.20.1

2 files

0.20

2 files

0.19.2

2 files

0.19.1

2 files

0.19

2 files

0.18.4

2 files

0.18.3

2 files

0.18.2

2 files

0.18.1

2 files

0.18

2 files

0.17

2 files

0.16.1

2 files

0.16

2 files

0.15.3

2 files

0.15.2

2 files

0.15.1

2 files

0.15

2 files

0.14.1

2 files

0.14

2 files

0.13.11

2 files

0.13.10

2 files

0.13.9

2 files

0.13.8

2 files

0.13.7

2 files

0.13.6

2 files

0.13.5

2 files

0.13.4

2 files

0.13.3

2 files

0.13.2

2 files

0.13.1

2 files

0.13

2 files

0.12

2 files

0.11.3

2 files

0.11.2

2 files

0.11.1

2 files

This release

0.11 This release

2 files

0.10

2 files

0.9

2 files

0.8

2 files

0.7.1

2 files

0.7

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page