Web3 Ethereum DeFi toolkit for smart contracts, Uniswap and PancakeSwap trades, Ethereum JSON-RPC utilities, wallets and automated test suites.

Overview

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

ERC-20 token example

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))

See full example.

How to use the library in your Python project

Add web3-ethereum-defi as a development dependency:

Using Poetry:

poetry add -D web3-ethereum-defi

Development and contributing

Read development instructions.

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

Comments
  • Initial version of Aave v3 integration (issue #5)

    Initial version of Aave v3 integration (issue #5)

    This PR includes Aave v3 constants, event reading from blockchain and a Jupyter notebook for analyzing the data, and event documentation in eth_defi/aave_v3/README.md.

    The Aave v3 ABI JSON files can be generated with make aavev3, which copies them from the @aave/core-v3 NPM package to the working directory (Node.js required).

    opened by kennu 8
  • Investigate Uniswap v3 and getting historical price data in good format

    Investigate Uniswap v3 and getting historical price data in good format

    • Github and other searches to find Python (and JavaScript) open source examples for getting data out from Uniswap v3
    • Data structure how to store this data in the (SQL) database for historical queries

    Question that needs to be answered:

    • Given (chain id, Uniswap v3 deployment address)
    • What is the historical price for buy/sell (timestamp, trading pair, token0 quantity in) -> token1 quantity out

    Can be a single pool price at milestone 1, later expanded to cover auto routing and multi-hop trades.

    opened by miohtama 6
  • Get revert reason of any tx and especially for failed trades

    Get revert reason of any tx and especially for failed trades

    Uniswap trade analyzer should be able to tell why the trade failed

    • Too much slippage
    • Some internal Uniswap error
    • (There should be no other reasons if the tokens are not scam tokens)

    As a bonus, trade analyzer should able to tell if the trade was reverted because of slippage. Though not sure how we can pick up this from the transaction receipt, I believe is going to be quite hard. The ”real” EVM nodes do not store the revert reason for very long time (few hundreds of blocks) and one might need to replay the transaction.

    https://snakecharmers.ethereum.org/web3py-revert-reason-parsing/

    This would be a research task of doing some little trades with Ganache and see what kind of data we can get out of JSON-RPC for the revert reason -if any. And then do the transaction replay trick.

    priority: P2 
    opened by miohtama 3
  • Slippage and MEV preventation in trades

    Slippage and MEV preventation in trades

    Creaet a function that calculates amountIn and amountOut values for buy/sell with a specific slippage.

    See this question for details:

    https://ethereum.stackexchange.com/questions/99404/pancakeswap-anti-front-run

    First for Uniswap v2, as its dog eats dog frontrun battle at BNB Chain.

    priority: P1 
    opened by miohtama 3
  • Read token tax from a token

    Read token tax from a token

    Some tokens with ponzinomis have a token tax. This is especially popular on wild west blockchains like BNB Chain and PancakeSwap.

    Token tax is used to

    • Create deflanatory tokens
    • Create malicious honey pots for trading bots ("buy only") - effectively a very high tokex tax like 90% on sell

    An example trading pair and token with token tax is ELEPHANT-BUSD.

    • Buy has 10% tax
    • Sell has 10% tax
    image

    honeypot.is is an independent service to check the token tax. It does this by (likely) running Ganache mainnet fork and simulates the transactions. There is no API to ask this information.

    Step 1: Read the token tax with Python

    To figure out the amount of different token taxes (buy, sell, transfer) one needs to run a simulated transaction in Ganache.

    Here is a pseudo-Python code to do it:

    from eth_account.signers.local import LocalAccount
    from eth_typing import HexAddress
    from web3 import Web3
    
    from eth_defi.uniswap_v2.deployment import UniswapV2Deployment
    from tradeexecutor.utils import dataclass
    
    
    @dataclass
    class TokenTaxInfo:
        """Different token taxes we figured out."""
    
        #: Token in the question
        base_token: HexAddress
    
        #: Which token we traded against it
        quote_token: HexAddress
    
        #: How much % we lost of the token on buy
        buy_tax: float
    
        #: How much % we lose the token when we transfer between addresses
        transfer_tax:float
    
        #: How much % we lose the token when we sold it
        sell_tax: float
    
    
    def estimate_token_taxes(
            uniswap: UniswapV2Deployment,
            base_token: HexAddress,
            quote_token: HexAddress,
            buy_account: LocalAccount,
            sell_account: LocalAccount
        ) -> TokenTaxInfo:
        """Estimates different token taxes for a token by running Ganache simulations for it.
    
        :param uniswap:
            Uniswap deployment on a Ganache mainnet fork.
            Set up prior calling this function.
            See `ganache.py` and `test_ganache.py` for more details.
    
        :param base_token:
            The token of which tax properties we are figuring out.
    
        :param quote_token:
            Address of the quote token used for the trading pair. E.g. `BUDS`, `WBNB`
            Based on this information we can derive Uniswap trading pair address.
    
        :param buy_account:
            The account that does initial buy to measure the buy tax.
            This account must be loaded with gas money (ETH/BNB) and `quote_token`
            for a purchase.
    
        :param sell_account:
            The account that receives the token transfer and does the sell to measure the sell tax.
            This account must be loaded with gas money for the sell.
    
        :return:
            ToxTaxInfo tells us what we figure out about taxes.
            This can be later recorded to a database.
        """
    
        web3: Web3 = uniswap.web3
    
        # Figure out base_token/quote_token trading pair
        # Buy base_token with buy_account
        # Measure the loss as "buy tax"
        # Transfer tokens to sell_account
        # Measure the loss as "transfer tax"
        # Sell tokens
        # Measure the loss as "sell tax"
    
    
    

    The TokenTaxInfo info can be then stored in a database or similar, like SQL, JSON file repo and so on. It can be later retrieved when you want to trade tokens.

    Step 2: Making taxed tokens easily tradeable with Python

    Create a buy/sell_with_tax(token_tax_info: TokenTaxInfo, max_slippage) function that considers slippage and token tax and correctly handles trading these kinds of tokens.

    • Normally buying ELELPHANT-BUSD would revert
    • Normally selling a token tax token would fail with execution reverted: TransferHelper: TRANSFER_FROM_FAILED because there is a mismatch between allowance() and swap amount

    Create four test based on BSC mainnet fork using the existing Ganache fixtures

    • Naive buy of ELEPHANT fails
    • Taxed buy of ELEPHANT success, we can calculate the taxed amount after the buy (update analyse_trade to return this value)
    • Naive sell of ELEPHANT fails
    • Taxed sell of ELEPHANT success, we can calculate the taxed amount after the sell
    • Comment how this can be also considered in three-way trade (BUSD-BNB-ELEPHANT)
    priority: P1 
    opened by miohtama 1
  • Block data storage

    Block data storage

    • Add a method to efficiently store block headers on disk to save a lot of time and API calls if we need to resume after a crash
    • Parquet append method used
    • The storage method is chain reorganisation safe
    opened by miohtama 0
  • Price oracle implementation

    Price oracle implementation

    • Feature: generic price oracle implementation with configurable price function
    • Feature: time weighted average price (TWAP) price function for price oracle
    • Feature: price oracle implementation for Uniswap v2 pools
    • Feature: fetch_pair_details to get info on Uniswap v2 pairs
    opened by miohtama 0
  • Create mybinder env

    Create mybinder env

    Badge logo:

    [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/tradingstrategy-ai/web3-ethereum-defi/master?labpath=docs/source/tutorials
    )
    
    opened by hieuh25 0
  • Handle chain reorg in event reader

    Handle chain reorg in event reader

    Current the event reader can't handle chain reorg situation, so it's likely to fail here: https://github.com/tradingstrategy-ai/web3-ethereum-defi/blob/df38b8a97176cc5b3dbc92783c0791c9603b0c5b/eth_defi/event_reader/reader.py#L148-L161

    bug enhancement priority: P2 size: M 
    opened by hieuh25 3
  • Aave deposit function

    Aave deposit function

    • [ ] Write a function that opens a loan position in Aave v3 by depositing any Aave v3 reserve token and receives aToken back
    • [ ] The function can live in eth_defi.aave_v3.loan module - The module should be modelled similar as e.g. Uniswap v2 swap_with_slippage_protection and deploy_trading_pair - Function name: deposit_in_aave - Inputs: HotWallet instance (assumed loaded with USDC), aave_deposit_address, token_address, amount - Outputs: tx_hash
    • [ ] The module must be sufficiently documented for autodoc according to the coding conventions
    • [ ] The code must be formatted to according to the coding conventions - there is is black that complains on open PRs if this is not the case
    • [ ] There must be integration test
      • Because there is no framework to set up Aave v3 smart contracts in Ethereum Tester environment, one must use Ganache mainnet fork for the test. Normally this is unpreferable, but creating Aave v3 test environment itself is large task.
      • There are some examples of using Ganache in tests in test_ganache and test_token_tax
      • The test can be called test_aave_deposit.py
      • A test case check that the 0) Hot wallet starts with USDC on Ethereum mainnet 1) deposit was correctly registered with the Aave reserves 2) you receive the corresponding Aave aUSDC token back in the wallet, with the correct amount
      • Because there is no automatic mechanism to fetch Aave reserves and addresses as a list, please use hardcoded and commented values as text fixtures for now for any Ethereum address, with links to their respective descriptions of what they are
    enhancement priority: P2 size: M 
    opened by miohtama 2
Owner
Trading Strategy
Algorithmic trading for decentralised markets
Trading Strategy
Defi PancakeSwap bot is programmed in Python to buy and sell tokens in seconds once the target is hit.

Defi PancakeSwap BOT A BOT that will make easy your life in Trading. Watch tutorial on Youtube Table of Contents About The Project Built With Getting

Zain Ullah 208 Jan 5, 2023
A Pancakeswap and Uniswap trading client (and bot) with limit orders, marker orders, stop-loss, custom gas strategies, a GUI and much more.

Pancakeswap and Uniswap trading client Adam A A Pancakeswap and Uniswap trading client (and bot) with market orders, limit orders, stop-loss, custom g

null 570 Mar 9, 2022
Pancakeswap Sniper Bot GUI Uniswap Matic 2022 (WINDOWS LINUX MAC) AUTO BUY TOKEN ON LAUNCH AFTER ADD LIQUIDITY

Pancakeswap Sniper Bot GUI Uniswap Matic 2022 (WINDOWS LINUX MAC) ⭐️ AUTO BUY TOKEN ON LAUNCH AFTER ADD LIQUIDITY ⭐️ ⭐️ First GUI SNIPER BOT for WINDO

Crypto Trader 1 Jan 5, 2022
Web3 Pancakeswap Sniper & honeypot detector Take Profit/StopLose bot written in python3, For ANDROID WIN MAC & LINUX

Pancakeswap BSC Sniper Bot web3 with honeypot detector (ANDROID WINDOWS MAC LINUX) First SNIPER BOT for ANDROID with honeypot detector Web3 Pancakeswa

HYDRA 1 Dec 23, 2021
Web3 Pancakeswap Sniper & honeypot detector Take Profit/StopLose bot written in python3, For ANDROID WIN MAC & LINUX

?? Pancakeswap BSC Sniper Bot web3 with honeypot detector (ANDROID WINDOWS MAC LINUX) ?? ⭐️ ⭐️ ⭐️ First SNIPER BOT for ANDROID & WINDOWS with honeypot

HYDRA 2 Dec 24, 2021
Web3 Pancakeswap Sniper & honeypot detector Take Profit/StopLose bot written in python3, For ANDROID WIN MAC & LINUX

Web3 Pancakeswap Sniper & honeypot detector Take Profit/StopLose bot written in python3, For ANDROID WIN MAC & LINUX

HYDRA 3 Dec 27, 2021
[Fullversion]Web3 Pancakeswap Sniper bot written in python3.

?? Pancakeswap BSC Sniper Bot ?? Web3 Pancakeswap Sniper && Take Profit/StopLose bot written in python3, Please note the license conditions! The secon

null 21 Dec 11, 2022
Powerful Ethereum Smart-Contract Toolkit

Heimdall Heimdall is an advanced and modular smart-contract toolkit which aims to make dealing with smart contracts on EVM based chains easier. Instal

Jonathan Becker 69 Dec 26, 2022
Microservice to extract structured information on EVM smart contracts.

Contract Serializer Microservice to extract structured information on EVM smart contract. Why? Modern NFT contracts may have different names for getPr

WeBill.io 8 Dec 19, 2022
PancakeTrade - Limit orders and more for PancakeSwap on Binance Smart Chain

PancakeTrade helps you create limit orders and more for your BEP-20 tokens that swap against BNB on PancakeSwap. The bot is controlled by Telegram so you can interact from anywhere.

Valentin Bersier 187 Dec 20, 2022
A python package that allows you to place automated trades using the TD Ameritrade API.

Template Repo Table of Contents Overview Setup Usage Support These Projects Overview Setup Setup - Requirements Install:* For this particular project,

Alex Reed 4 Jan 25, 2022
Get informed when your DeFI Earn CRO Validator is jailed or changes the commission rate.

CRO-DeFi-Warner Intro CRO-DeFi-Warner can be used to notify you when a validator changes the commission rate or gets jailed. It can also notify you wh

null 5 May 16, 2022
DeFi wallet on Chia Network.

DeFi wallet on Chia Network.

GobyWallet 21 Aug 12, 2022
The smart farm is an idea that designing Smart Farm by IoT

The smart farm is an idea that designing Smart Farm by IoT. Using Raspberry Pi 4 detect the data from different sensors(Raindrop sensor and DHT22 sensor), and push the data to Azure IoT central.

Jiage 1 Jan 11, 2022
This is a cryptocurrency trading bot that analyses Reddit sentiment and places trades on Binance based on reddit post and comment sentiment. If you like this project please consider donating via brave. Thanks.

This is a cryptocurrency trading bot that analyses Reddit sentiment and places trades on Binance based on reddit post and comment sentiment. The bot f

Andrei 157 Dec 15, 2022
Crypto trading bot that detects surges in the bitcoin price and executes trades.

The bot will be trading Bitcoin automatically if the price has increased by more than 3% in the last 10 minutes. We will have a stop loss of 5% and t

null 164 Oct 20, 2022
This is a crypto trading bot that scans the Binance Annoucements page for new coins, and places trades on Gateio

gateio-trading-bot-binance-announcements This Gateio x Binance cryptocurrency trading bot scans the Binance Announcements page and picks up on new coi

Andrei 1.2k Jan 1, 2023