{
  "markdown": "[![PyPI version](https://badge.fury.io/py/web3-ethereum-defi.svg)](https://badge.fury.io/py/web3-ethereum-defi)\n\n[![Automated test suite](https://github.com/tradingstrategy-ai/web3-ethereum-defi/actions/workflows/test.yml/badge.svg)](https://github.com/tradingstrategy-ai/web3-ethereum-defi/actions/workflows/test.yml)\n\n[![GMX Tests](https://github.com/tradingstrategy-ai/web3-ethereum-defi/actions/workflows/test-gmx.yml/badge.svg)](https://github.com/tradingstrategy-ai/web3-ethereum-defi/actions/workflows/test-gmx.yml)\n\n\n# Web3-Ethereum-Defi\n\nWeb-Ethereum-DeFi (`eth_defi`) allows you to integrate [EVM-compatible](https://tradingstrategy.ai/glossary/evm-compatible) Web3 and DeFi protocols into your Python application.\n\n- [Use cases](#use-cases)\n- [Supported protocols, chains and integrations](#supported-protocols-chains-and-integrations)\n- [Prerequisites](#prerequisites)\n- [Install](#install)\n- [Example code](#example-code)\n   * [Uniswap swap example](#uniswap-swap-example)\n- [How to use the library in your Python project](#how-to-use-the-library-in-your-python-project)\n- [Documentation](#documentation)\n- [Development and contributing](#development-and-contributing)\n- [Version history](#version-history)\n- [Support ](#support)\n- [Social media](#social-media)\n- [License ](#license)\n\n# Use cases\n\nUse cases for this package include\n\n- Trading and bots\n- Data research, extraction, transformation and loading\n- Portfolio management and accounting\n- System integrations and backends\n- AI agent interaction for EVM chains\n\n# Supported protocols, chains and integrations\n\n![Supported protocols include Uniswap, Aave, others](https://github.com/tradingstrategy-ai/web3-ethereum-defi/blob/master/docs/source/_static/logo-wall-white.png)\n\n| Protocol         | Actions                                                       | Tutorial and API links                                                                                                                                                          |\n|:-----------------|:--------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Uniswap          | Token swaps, data research                                    | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/make-uniswap-v3-swap-in-python.html)                                                                             |\n| Gnosis Safe      | Safe deployment customisation and modules                     | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/safe/index.html)                                                                                                            |\n| Circle USDC      | USDC interactions                                             | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/usdc/index.html)                                                                                                            |\n| Circle CCTP V2   | Cross-chain USDC transfers (burn-and-mint)                    | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/cctp/index.html)                                                                                                            |\n| LI.FI            | Cross-chain bridge, gas feeding                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/lifi/index.html) [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/lifi-feed-crosschain.html)               |\n| ChainLink        | Read oracle prices, set up oracles                            | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/chainlink-native-token.html)                                                                                     |\n| CoW Swap         | Swaps, vault integration                                      | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/lagoon-cowswap.html)                                                                                             |\n| PancakeSwap      | Token swaps, data research                                    | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/pancakeswap-live-minimal.html)                                                                                   |\n| GMX              | Leveraged trading, spot trading                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/gmx/index.html) [Tutorial](https://github.com/tradingstrategy-ai/gmx-ccxt-freqtrade)                            |\n| Derive           | Perpetuals, options trading                                   | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/derive/index.html)                                                                                                          |\n| gTrade           | Leveraged trading, vaults                                     | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/gains/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/gains-network) |\n| Ostium           | Leveraged trading, vaults                                     | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/gains/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/gains-network) |\n| LFG              | Token swaps, data research                                    | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/uniswap_v2/index.html)                                                                                                      |\n| Aave             | Credit, borrow, read rates                                    | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/aave-v3-interest-analysis.html)                                                                                  |\n| BENQI            | Credit, borrow, read rates                                    | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/aave_v2/index.html)                                                                                                         |\n| Lendle           | Credit, borrow, read rates                                    | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/aave_v2/index.html)                                                                                                         |\n| Sky (MakerDAO)   | Savings vaults                                                | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/sky/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/sky) |\n| Enzyme           | Deposit to vaults, deploy, read vault data                    | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/enzyme-read-vaults.html)                                                                                         |\n| Lagoon           | Deposit to vaults, deploy, read vault data                    | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/lagoon/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/lagoon-finance) |\n| Velvet           | Deposit to vaults, deploy, read vault data                    | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/velvet/index.html)                                                                                                          |\n| Morpho           | Read vault data                                               | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/erc-4626-scan-prices.html)                                                                                       |\n| Euler            | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/euler/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/euler) |\n| Ethena           | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/ethena/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/ethena) |\n| IPOR             | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/ipor/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/ipor) |\n| 1delta           | Open/close leveraged long/short positions                     | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/one_delta/index.html)                                                                                                       |\n| Yearn            | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/yearn/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/yearn) |\n| NashPoint        | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/nashpoint/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/nashpoint) |\n| Untangle Finance | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/untangle/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/untangle-finance) |\n| Plutus           | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/plutus/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/plutus) |\n| D2 Finance       | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/d2_finance/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/d2-finance) |\n| Umami Finance    | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/umami/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/umami) |\n| Harvest Finance  | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/harvest/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/harvest-finance) |\n| USDAi            | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/usdai/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/usdai) |\n| AUTO Finance     | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/auto_finance/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/auto-finance) |\n| Goat Protocol    | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/goat/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/goat-protocol) |\n| Cap              | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/cap/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/cap) |\n| Centrifuge       | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/centrifuge/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/centrifuge) |\n| C-Sigma          | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/csigma/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/csigma-finance) |\n| Deltr            | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/deltr/index.html)                                                                                                        |\n| Foxify           | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/foxify/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/foxify) |\n| Liquid Royalty   | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/liquid_royalty/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/liquid-royalty) |\n| LLamma           | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/llamma/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/llamma) |\n| Maple            | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/maple/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/maple) |\n| Silo             | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/silo/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/silo-finance) |\n| Spark            | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/spark/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/spark) |\n| Summer           | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/summer/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/summer-fi) |\n| Superform        | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/superform/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/superform) |\n| Teller           | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/teller/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/teller) |\n| Term Finance     | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/term_finance/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/term-finance) |\n| TrueFi           | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/truefi/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/truefi) |\n| Upshift          | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/upshift/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/upshift) |\n| USDD             | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/usdd/index.html)                                                                                                         |\n| Aarna            | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/aarna/index.html)                                                                                                        |\n| Accountable      | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/accountable/index.html)                                                                                                  |\n| Altura           | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/altura/index.html)                                                                                                       |\n| Avant            | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/avant/index.html)                                                                                                        |\n| Brink            | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/brink/index.html)                                                                                                        |\n| Curvance         | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/curvance/index.html)                                                                                                     |\n| Dolomite         | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/dolomite/index.html)                                                                                                     |\n| Eth Strategy     | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/eth_strategy/index.html)                                                                                                 |\n| Fluid            | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/fluid/index.html)                                                                                                        |\n| Frax             | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/frax/index.html)                                                                                                         |\n| Gearbox          | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/gearbox/index.html)                                                                                                      |\n| Hyperlend        | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/hyperlend/index.html)                                                                                                    |\n| HypurrFi         | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/hypurrfi/index.html)                                                                                                     |\n| InfiniFi         | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/infinifi/index.html)                                                                                                     |\n| Mainstreet       | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/mainstreet/index.html)                                                                                                   |\n| Renalta          | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/renalta/index.html)                                                                                                      |\n| Resolv           | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/resolv/index.html)                                                                                                       |\n| Royco            | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/royco/index.html)                                                                                                        |\n| Sentiment        | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/sentiment/index.html)                                                                                                    |\n| Singularity      | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/singularity/index.html)                                                                                                  |\n| Spectra          | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/spectra/index.html)                                                                                                      |\n| USDx Money       | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/usdx_money/index.html)                                                                                                   |\n| YieldFi          | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/yieldfi/index.html)                                                                                                      |\n| YieldNest        | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/yieldnest/index.html)                                                                                                    |\n| YO               | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/yo/index.html)                                                                                                           |\n| Yuzu Money       | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/yuzu_money/index.html)                                                                                                   |\n| ZeroLend         | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/zerolend/index.html)                                                                                                     |\n| BaseVol          | Read vault data                                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/vaults/basevol/index.html) [Listing](https://tradingstrategy.ai/vaults/protocols/basevol) |\n| Hypersync        | Read historical data fast                                     | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/hypersync/index.html)                                                                                                       |\n| Token Risk       | Glider Token Risk API by Hexens                               | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/token_analysis/_autosummary_token_analysis/eth_defi.token_analysis.tokenrisk.html#module-eth_defi.token_analysis.tokenrisk) |\n| TokenSniffer     | Read token risk core and metricws                             | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/token_analysis/_autosummary_token_analysis/eth_defi.token_analysis.tokensniffer.html)                                       |\n| Foundry          | Compile, deploy and verify smart contracts                    | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/foundry/_autosummary_forge/eth_defi.foundry.forge.html)                                                                     |\n| Etherscan        | Deploy and verify smart contracts                             | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/etherscan/index.html)                                                                                                       |\n| MEVBlocker       | Frontrun protection                                           | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/mev-blocker.html)                                                                                                |\n| Ethereum mainnet | Frontrun protection, token mapping                            | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/mev-blocker.html)                                                                                                |                                                                                                                                                                                                                                                        |\n| Base             | Frontrun protection, token mapping                            | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/mev-blocker.html)                                                                                                |\n| Arbitrum         | Frontrun protection, token mapping                            | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/mev-blocker.html)                                                                                                |\n| Avalanche        | Token mapping                                                 | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html#module-eth_defi.token)                                                                |                                                                                                                                                                        |\n| BNB chain        | Token mapping                                                 | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html#module-eth_defi.token)                                                                |\n| Polygon          | Token mapping                                                 | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html#module-eth_defi.token)                                                                |\n| BNB Chain        | Token mapping                                                 | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html#module-eth_defi.token)                                                                |                                                                                                                                                                           |\n| Berachain        | Token mapping                                                 | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html#module-eth_defi.token)                                                                |\n| Avalanche        | Token mapping                                                 | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html#module-eth_defi.token)                                                                |\n| Hyperliquid      | Token mapping                                                 | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html#module-eth_defi.token)                                                                |\n| Mode             | Token mapping                                                 | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html#module-eth_defi.token)                                                                |\n| Unichain         | Token mapping                                                 | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html#module-eth_defi.token)                                                                |\n| ZKSync           | Token mapping                                                 | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html#module-eth_defi.token)                                                                |\n| Soneium          | Token mapping                                                 | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html#module-eth_defi.token)                                                                |\n| Google GCloud    | Support hardware security module wallets                      | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.gcloud_hsm_wallet.html)                                                                          |\n| Hot wallet       | Secure hot wallet handling                                    | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.hotwallet.html)                                                                                  |\n| Multicall3       | Chunked and historical data reading                           | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/multicall3-with-python.html)                                                                                     |\n| Gas              | Ethereum gas management                                       | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.gas.html)                                                                                        |\n| EIP-4626         | Vault analysis                                                | [Tutorial](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/erc-4626-best-vaults.html)                                                                                       |\n| EIP-726          | Message signing and decoding                                  | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.eip_712.html#module-eth_defi.eip_712)                                                            |\n| ERC-20           | High performance reading, data mappings                       | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.token.html)                                                                                      |\n| ABI              | High performance smart contract ABI management                | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.abi.html)                                                                                        |\n| Transactions     | Stack traces and symbolic revert reasons                      | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/core/_autosummary/eth_defi.gas.html)                                                                                        |\n| Anvil            | Mainnet works and local unit testing                          | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/provider/_autosummary_provider/eth_defi.provider.anvil.html)                                                                |\n| LlamaNodes       | Special RPC support                                           | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/provider/_autosummary_provider/eth_defi.provider.llamanodes.html#module-eth_defi.provider.llamanodes)                       |\n| Ankr             | Special RPC support                                           | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/provider/_autosummary_provider/eth_defi.provider.ankr.html)                                                                 |\n| dRPC             | Special RPC support                                           | [API](https://web3-ethereum-defi.tradingstrategy.ai/api/event_reader/_autosummary_enzyne/eth_defi.event_reader.fast_json_rpc.get_last_headers.html?highlight=get_last_headers)      |\n\n\n👉 [Read the full API documentation](https://web3-ethereum-defi.tradingstrategy.ai/).\n\nThis is a MIT-licensed open source project. Those who sponsor and contribute get integrations.\n\n# Prerequisites\n\nTo use this package you need to\n\n* Have Python 3.10 or newer. Python 3.14 is supported when installing the HyperSync extra through the temporary upstream package `hypersync-temp`.\n* macOS, Linux or Windows Subsystem for Linux (WSL) needed, Microsoft Windows is not officially supported\n  * For WSL, [make sure you have gcc and other tools installed](https://stackoverflow.com/questions/62215963/how-to-install-gcc-and-gdb-for-wslwindows-subsytem-for-linux/63548362#63548362)\n* [Be proficient in Python programming](https://wiki.python.org/moin/BeginnersGuide)\n* [Understand of Web3.py library](https://web3py.readthedocs.io/en/stable/) \n* [Understand Pytest basics](https://docs.pytest.org/)\n \n\n# Install\n\nWith `uv`:\n\n```shell\nuv pip install \"web3-ethereum-defi\" --all-extras\n```\n\nWith `pip`:\n\n```shell\npip install \"web3-ethereum-defi[data,test,docs,hypersync,ccxt,cloudflare_r2,duckdb]\"\n```\n\nWith `poetry`:\n\n```shell\npoetry add --extras \"data test docs hypersync ccxt cloudflare_r2 duckdb\" web3-ethereum-defi\n```\n\nFrom master Git branch:\n\n```shell\ngit clone git@github.com:tradingstrategy-ai/web3-ethereum-defi.git\ncd web3-ethereum-defi\npoetry shell\npoetry install --all-extras\n```\n\n# Example code\n\nSee [the tutorials section in the documentation](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/index.html)\nfor full code examples.\n\n## Uniswap swap example\n\n- This example shows how to make a trade on Uniswap v3.\n- The example is for Polygon, but works on other chains.\n- See [tutorials](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/index.html) for more Uniswap and other DEX examples\n\n```python\n\nimport datetime\nimport decimal\nimport os\nimport sys\nfrom decimal import Decimal\n\nfrom eth_account import Account\nfrom eth_account.signers.local import LocalAccount\nfrom eth_defi.compat import construct_sign_and_send_raw_middleware\n\nfrom eth_defi.provider.multi_provider import create_multi_provider_web3\nfrom eth_defi.revert_reason import fetch_transaction_revert_reason\nfrom eth_defi.token import fetch_erc20_details\nfrom eth_defi.confirmation import wait_transactions_to_complete\nfrom eth_defi.uniswap_v3.constants import UNISWAP_V3_DEPLOYMENTS\nfrom eth_defi.uniswap_v3.deployment import fetch_deployment\nfrom eth_defi.uniswap_v3.swap import swap_with_slippage_protection\n\n# The address of a token we are going to swap out\n#\n# Use https://tradingstrategy.ai/search to find your token\n#\n# For quote terminology see https://tradingstrategy.ai/glossary/quote-token\n#\nQUOTE_TOKEN_ADDRESS = \"0x3c499c542cef5e3811e1192ce70d8cc03d5c3359\"  # USDC (native)\n\n# The address of a token we are going to receive\n#\n# Use https://tradingstrategy.ai/search to find your token\n#\n# For base terminology see https://tradingstrategy.ai/glossary/base-token\nBASE_TOKEN_ADDRESS = \"0x7ceb23fd6bc0add59e62ac25578270cff1b9f619\"  # WETH\n\n\n# Connect to JSON-RPC node\nrpc_env_var_name = \"JSON_RPC_POLYGON\"\njson_rpc_url = os.environ.get(rpc_env_var_name)\nassert json_rpc_url, f\"You need to give {rpc_env_var_name} node URL. Check ethereumnodes.com for options\"\n\n# Create a Web3 provider with ability to retry failed requests\n# and supporting fallback JSON-RPC nodes. RPC connections\n# are extremely flaky and for production grade usage you need to use multiple\n# JSON-RPC nodes.\n# create_multi_provider_web3() will also take care of any chain-specific\n# RPC setup.\nweb3 = create_multi_provider_web3(json_rpc_url)\n\nprint(f\"Connected to blockchain, chain id is {web3.eth.chain_id}. the latest block is {web3.eth.block_number:,}\")\n\n# Grab Uniswap v3 smart contract addreses for Polygon.\n#\ndeployment_data = UNISWAP_V3_DEPLOYMENTS[\"polygon\"]\nuniswap_v3 = fetch_deployment(\n    web3,\n    factory_address=deployment_data[\"factory\"],\n    router_address=deployment_data[\"router\"],\n    position_manager_address=deployment_data[\"position_manager\"],\n    quoter_address=deployment_data[\"quoter\"],\n)\n\nprint(f\"Using Uniwap v3 compatible router at {uniswap_v3.swap_router.address}\")\n\n# Read and setup a local private key\nprivate_key = os.environ.get(\"PRIVATE_KEY\")\nassert private_key is not None, \"You must set PRIVATE_KEY environment variable\"\nassert private_key.startswith(\"0x\"), \"Private key must start with 0x hex prefix\"\naccount: LocalAccount = Account.from_key(private_key)\nmy_address = account.address\n\n# Enable eth_sendTransaction using this private key\nweb3.middleware_onion.add(construct_sign_and_send_raw_middleware(account))\n\n# Read on-chain ERC-20 token data (name, symbol, etc.)\nbase = fetch_erc20_details(web3, BASE_TOKEN_ADDRESS)\nquote = fetch_erc20_details(web3, QUOTE_TOKEN_ADDRESS)\n\n# Native token balance\n# See https://tradingstrategy.ai/glossary/native-token\ngas_balance = web3.eth.get_balance(account.address)\n\nprint(f\"Your address is {my_address}\")\nprint(f\"Your have {base.fetch_balance_of(my_address)} {base.symbol}\")\nprint(f\"Your have {quote.fetch_balance_of(my_address)} {quote.symbol}\")\nprint(f\"Your have {gas_balance / (10 ** 18)} for gas fees\")\n\nassert quote.fetch_balance_of(my_address) > 0, f\"Cannot perform swap, as you have zero {quote.symbol} needed to swap\"\n\n# Ask for transfer details\ndecimal_amount = input(f\"How many {quote.symbol} tokens you wish to swap to {base.symbol}? \")\n\n# Some input validation\ntry:\n    decimal_amount = Decimal(decimal_amount)\nexcept (ValueError, decimal.InvalidOperation) as e:\n    raise AssertionError(f\"Not a good decimal amount: {decimal_amount}\") from e\n\n# Fat-fingering check\nprint(f\"Confirm swap amount {decimal_amount} {quote.symbol} to {base.symbol}\")\nconfirm = input(\"Ok [y/n]?\")\nif not confirm.lower().startswith(\"y\"):\n    print(\"Aborted\")\n    sys.exit(1)\n\n# Convert a human-readable number to fixed decimal with 18 decimal places\nraw_amount = quote.convert_to_raw(decimal_amount)\n\n# Each DEX trade is two transactions\n# - ERC-20.approve()\n# - swap (various functions)\n# This is due to bad design of ERC-20 tokens,\n# more here https://twitter.com/moo9000/status/1619319039230197760\n\n# Uniswap router must be allowed to spent our quote token\n# and we do this by calling ERC20.approve() from our account\n# to the token contract.\napprove = quote.contract.functions.approve(uniswap_v3.swap_router.address, raw_amount)\ntx_1 = approve.build_transaction(\n    {\n        # approve() may take more than 500,000 gas on Arbitrum One\n        \"gas\": 850_000,\n        \"from\": my_address,\n    }\n)\n\n#\n# Uniswap v3 may have multiple pools per\n# trading pair differetiated by the fee tier. For example\n# WETH-USDC has pools of 0.05%, 0.30% and 1%\n# fees. Check for different options\n# in https://tradingstrategy.ai/search\n#\n# Here we use 5 BPS fee pool (5/10,000).\n#\n#\n# Build a swap transaction with slippage protection\n#\n# Slippage protection is very important, or you\n# get instantly overrun by MEV bots with\n# sandwitch attacks\n#\n# https://tradingstrategy.ai/glossary/mev\n#\n#\nbound_solidity_func = swap_with_slippage_protection(\n    uniswap_v3,\n    base_token=base,\n    quote_token=quote,\n    max_slippage=20,  # Allow 20 BPS slippage before tx reverts\n    amount_in=raw_amount,\n    recipient_address=my_address,\n    pool_fees=[500],   # 5 BPS pool WETH-USDC\n)\n\ntx_2 = bound_solidity_func.build_transaction(\n    {\n        # Uniswap swap should not take more than 1M gas units.\n        # We do not use automatic gas estimation, as it is unreliable\n        # and the number here is the maximum value only.\n        # Only way to know this number is by trial and error\n        # and experience.\n        \"gas\": 1_000_000,\n        \"from\": my_address,\n    }\n)\n\n# Sign and broadcast the transaction using our private key\ntx_hash_1 = web3.eth.send_transaction(tx_1)\ntx_hash_2 = web3.eth.send_transaction(tx_2)\n\n# This will raise an exception if we do not confirm within the timeout.\n# If the timeout occurs the script abort and you need to\n# manually check the transaction hash in a blockchain explorer\n# whether the transaction completed or not.\ntx_wait_minutes = 2.5\nprint(f\"Broadcasted transactions {tx_hash_1.hex()}, {tx_hash_2.hex()}, now waiting {tx_wait_minutes} minutes for it to be included in a new block\")\nprint(f\"View your transactions confirming at https://polygonscan/address/{my_address}\")\nreceipts = wait_transactions_to_complete(\n    web3,\n    [tx_hash_1, tx_hash_2],\n    max_timeout=datetime.timedelta(minutes=tx_wait_minutes),\n    confirmation_block_count=1,\n)\n\n# Check if any our transactions failed\n# and display the reason\nfor completed_tx_hash, receipt in receipts.items():\n    if receipt[\"status\"] == 0:\n        revert_reason = fetch_transaction_revert_reason(web3, completed_tx_hash)\n        raise AssertionError(f\"Our transaction {completed_tx_hash.hex()} failed because of: {revert_reason}\")\n\nprint(\"All ok!\")\nprint(f\"After swap, you have {base.fetch_balance_of(my_address)} {base.symbol}\")\nprint(f\"After swap, you have {quote.fetch_balance_of(my_address)} {quote.symbol}\")\nprint(f\"After swap, you have {gas_balance / (10 ** 18)} native token left\")\n```\n\n# How to use the library in your Python project\n\nAdd `web3-ethereum-defi` as a development dependency:\n\nUsing [Poetry](https://python-poetry.org/):\n\n```shell\n# Data optional dependencies include pandas and gql, needed to fetch Uniswap v3 data\npoetry add -D \"web3-ethereum-defi[data]\"\n```\n\n# Documentation\n\n- [Browse API documentation](https://web3-ethereum-defi.tradingstrategy.ai/).\n- [Browse tutorials](https://web3-ethereum-defi.tradingstrategy.ai/tutorials/index.html).\n\n# Development and contributing\n\n- [Read development instructions](https://web3-ethereum-defi.tradingstrategy.ai/development.html).\n\n# Version history\n\n- [Read changelog](https://github.com/tradingstrategy-ai/web3-ethereum-defi/blob/master/CHANGELOG.md).\n- [See releases](https://pypi.org/project/web3-ethereum-defi/#history).\n\n# Support \n\n- [Join Discord for any questions](https://tradingstrategy.ai/community).\n\n# Social media\n\n- [Watch tutorials on YouTube](https://www.youtube.com/@tradingstrategyprotocol)\n- [Follow on Twitter](https://twitter.com/TradingProtocol)\n- [Follow on Telegram](https://t.me/trading_protocol)\n- [Follow on LinkedIn](https://www.linkedin.com/company/trading-strategy/)\n\n# License \n\nMIT.\n\n[Created by Trading Strategy](https://tradingstrategy.ai).\n",
  "bytes": 42169,
  "sha": "549593b778e6f30359f9e62ab7f0c6897606184f77e5890e7adc3c471e30becc",
  "repo_slug": "tradingstrategy-ai/web3-ethereum-defi",
  "fonte": "repo",
  "truncated": false,
  "api": "https://api.agentalog.com/api/listings/skl_tradingstrategy_ai_web3_ethereum_defi_ge_54bafab1/readme"
}