Skip to main content

Python SDK for DeFi Analytics, Simulation, and Agents

Project description

DeFiPy: Python SDK for DeFi Analytics and Agents

DeFiPy is the first unified Python SDK for DeFi analytics, simulation, and autonomous agents. Built with modularity in mind, DeFiPy lets you isolate and extend your analytics by protocol using:

For onchain event access and scripting, pair it with Web3Scout — a companion tool for decoding pool events and interfacing with Solidity contracts. Whether you’re building dashboards, simulations, or agent-based trading systems, DeFiPy + Web3Scout deliver a uniquely powerful toolset — unlike anything else in the ecosystem.

🔗 SPDX-Anchor: anchorregistry.ai/AR-2026-YdPXB5g

📝 Docs

Visit DeFiPy docs for full documentation

🔍 Install

DeFiPy requires Python 3.10 or later. Install via pip:

> pip install defipy

Book install (chapter 9 agents)

Chapter 9 of Hands-On AMMs with PythonBuilding Autonomous DeFi Agents — uses live chain integration via web3scout. To run those examples, install the [book] extra:

> pip install defipy[book]

This pulls in web3scout on top of the core install, enabling the chain event monitoring, ABI loading, and token-fetching utilities that chapter 9's agents require. Other chapters work with the core install alone.

Source install

To install from source:

> git clone https://github.com/defipy-devs/defipy
> cd defipy
> pip install .

System libraries for gmpy2

DeFiPy depends on gmpy2 for high-precision arithmetic in StableSwap math. On most platforms, pip will install gmpy2 from a prebuilt wheel and no further setup is needed. If the install fails, you may need the GMP, MPFR, and MPC system libraries installed before pip install:

macOS (Homebrew):

> brew install gmp mpfr libmpc

Linux (Debian / Ubuntu):

> sudo apt install libgmp-dev libmpfr-dev libmpc-dev

See the gmpy2 installation docs for other platforms.

🔍 Learning Resources

DeFiPy is accompanied by educational resources for developers and researchers interested in on-chain analytics and DeFi modeling.

📘 Textbook

DeFiPy: Python SDK for On-Chain Analytics

A comprehensive guide to DeFi analytics, AMM modeling, and simulation.

🔗 Buy on Amazon: https://www.amazon.com/dp/B0G3RV5QRB

🎓 Course

On-Chain Analytics Foundations

A practical course on transforming raw blockchain data into structured analytics pipelines using Python.

Topics include:

  • retrieving blockchain data via Ethereum RPC
  • decoding event logs
  • analyzing AMM swap events
  • building DeFi analytics pipelines

🔗 Course Page: https://defipy.thinkific.com/products/courses/foundations

🚀 Quick Example (Uniswap V3)


To setup a liquidity pool, you must first create the tokens in the pair using the ERC20 object. Next, create a liquidity pool (LP) factory using IFactory object. Once this is setup, an unlimited amount of LPs can be created; the procedures for such are as follows:

from defipy import *

# Step 1: Define tokens and parameters
eth = ERC20("ETH", "0x93")
tkn = ERC20("TKN", "0x111")
tick_spacing = 60
fee = 3000  # 0.3% fee tier

# Step 2: Set up exchange data for V3
exch_data = UniswapExchangeData(tkn0=eth, tkn1=tkn, symbol="LP", address="0x811", version='V3', tick_spacing=tick_spacing, fee=fee)

# Step 3: Initialize factory
factory = UniswapFactory("ETH pool factory", "0x2")

# Step 4: Deploy pool
lp = factory.deploy(exch_data)

# Step 5: Add initial liquidity within tick range
lwr_tick = UniV3Utils.getMinTick(tick_spacing)
upr_tick = UniV3Utils.getMaxTick(tick_spacing)
join = Join()
join.apply(lp, "user", 1000, 10000, lwr_tick, upr_tick)

# Step 6: Perform swap
swap = Swap()
out = swap.apply(lp, tkn, "user", 10)

# Check reserves and liquidity
lp.summary()

# OUTPUT:
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 999.0039930189599, TKN = 10010.0
Gross Liquidity: 3162.277660168379  

License

Licensed under the Apache License, Version 2.0.
See LICENSE and NOTICE for details.
Portions of this project may include code from third-party projects under compatible open-source licenses.

Project details


Download files

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

Source Distribution

defipy-1.2.0.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

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

defipy-1.2.0-py3-none-any.whl (39.4 kB view details)

Uploaded Python 3

File details

Details for the file defipy-1.2.0.tar.gz.

File metadata

  • Download URL: defipy-1.2.0.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for defipy-1.2.0.tar.gz
Algorithm Hash digest
SHA256 f7bbe536e07a8e42d531266bed38a5fb3515e68fbe7e89052ae76bd5f5513ff3
MD5 2f56ca8b54c24c6924cd505460077ce7
BLAKE2b-256 c26bfb330932637aba9d315f913bc760d8175fcc715406bd30720735cc881845

See more details on using hashes here.

File details

Details for the file defipy-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: defipy-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 39.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for defipy-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 232dc2d8d4a9178c5531322c46f38d5bbd6f4b33572ed12330d983f60f20307f
MD5 dd3aca0a5bede64fe5ac744e4ce7242e
BLAKE2b-256 349d458ef67518d7b9f01a8ce396068662d937ff67b335bc11283cbe5d8b165a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page