Skip to content

DeFiPy: Python SDK for Agentic DeFi

DeFiPy is a unified Python SDK for agentic DeFi, covering analytics, simulation, and LLM-native tool schemas for autonomous agents. Built with modularity in mind, it exposes DeFi primitives as MCP tools and lets you isolate and extend your analytics by protocol using:

New here? The Quick Start walks through the essential Core Primitives — pool setup, Join, Swap — protocol by protocol. For onchain event access, use LiveProvider, which demonstrates the State Twin concept. The fork-and-evaluate worked example is the canonical v2.1 demonstration of the pattern against live mainnet state.

📄 Research Paper: arXiv  ·  📚 Legacy Docs: defipy.readthedocs.io

Terminal window
pip install defipy

Core install is pure math — no chain reads, no LLM dependencies. For MCP server, chain reads, or Foundry workflows, see Installation.

from defipy import AnalyzePosition
from defipy.twin import MockProvider, StateTwinBuilder
# Build a synthetic ETH/DAI Uniswap V2 pool
provider = MockProvider()
builder = StateTwinBuilder()
lp = builder.build(provider.snapshot("eth_dai_v2"))
# Ask the primitive: why is this LP position gaining or losing money?
result = AnalyzePosition().apply(
lp,
lp_init_amt = 1.0,
entry_x_amt = 1000,
entry_y_amt = 100000,
)
print(f"Diagnosis: {result.diagnosis}")
print(f"Net PnL: {result.net_pnl:.4f}")
print(f"IL %: {result.il_percentage:.4f}")
print(f"Current val: {result.current_value:.4f}")

MockProvider ships canonical synthetic pools; StateTwinBuilder turns a snapshot into a usable exchange object; any primitive runs against it. The same pattern works against live chain state via LiveProvider — the primitives don’t care where the pool state came from:

from defipy.twin import LiveProvider, StateTwinBuilder
provider = LiveProvider("https://eth-mainnet.g.alchemy.com/v2/<key>")
lp = StateTwinBuilder().build(
provider.snapshot("uniswap_v2:0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc")
)
# lp now reflects real WETH/USDC V2 chain state — same primitive interface

LiveProvider requires the [chain] install extra; see LiveProvider for V2 + V3 examples and the full surface.

Most Python DeFi libraries wrap chain queries from The Graph. DeFiPy ships the math — hand-derived invariants for four AMM families, exposed as composable typed primitives that an LLM can call directly via Model Context Protocol.

Exact math, not approximations

No textbook IL formulas. Closed-form expressions derived from each protocol’s invariant — xy = k for V2, L = √(xy) with concentrated ticks for V3, weighted geometric mean for Balancer, the ε ↔ δ relation for Stableswap.

Substrate, not agent

DeFiPy is the foundation. It has zero LLM dependencies and zero web3 dependencies in the core install. Build whatever agent you want on top — DeFiMind, LangChain, custom MCP server, your own framework.

Composable typed primitives

Every primitive follows the same interface: stateless construction, computation at .apply(), typed dataclass return. Same call shape from a Jupyter notebook or from Claude calling it as a tool.

Plan, don't execute

No signing keys. Ever. Primitives like OptimalDepositSplit and EvaluateRebalance return structured plans the caller can inspect, serialize, approve, or hand to whatever execution runtime they trust.

Terminal window
pip install defipy

Pure analytics — AMM math, primitives, State Twin, all 21 typed analytics functions. Zero web3 dependencies, zero LLM dependencies.

DeFiPy unifies four protocol-specific Python libraries under one consistent surface:


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