DeFiPy: Python SDK for Agentic DeFi

Note

This site is in maintenance mode. The canonical, actively-maintained DeFiPy documentation now lives at defipy.org. The content here describes DeFiPy v2.0 and has been updated for v2.1 factual accuracy, but new features, examples, and integration walkthroughs land at defipy.org first. For the most current information, please refer to the canonical site.

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 our State Twin concept. The fork-and-evaluate worked example is the canonical v2.1 demonstration of the pattern against live mainnet state.

πŸ”— SPDX-Anchor: anchorregistry.ai/AR-2026-YdPXB5g

Where to start

πŸ€– Building an agent?

Wire DeFiPy’s primitives into Claude Desktop, Claude Code, or any MCP-compatible client. 10 curated tools ship with schemas; a reference MCP server closes the end-to-end loop.

Overview
πŸ“Š Running analytics?

21 stateless, typed primitives across 9 categories β€” position analysis, risk, pool health, price scenarios, portfolio aggregation. Every primitive returns a structured dataclass.

Overview
πŸ”§ Executing swaps & liquidity?

Cross-protocol execution primitives: Join, Swap, AddLiquidity, SwapDeposit, LPQuote. Unified interface across Uniswap V2, V3, Balancer, and Stableswap.

Overview
πŸ“ Learning the math?

Hand-derived AMM math across all four protocols, with Uniswap V3 the most-read derivation β€” sqrt-price, ticks, concentrated liquidity, and the impermanent loss formula. Runnable as a Jupyter notebook.

Uniswap V3

Quick install

pip install defipy

Core install is pure math β€” no chain reads, no LLM dependencies. For MCP server, chain reads, or Foundry workflows, see Installation. For the most up-to-date list of install extras, the canonical reference is defipy.org/installation.

Here’s what a primitive call looks like

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 (shipped in v2.1) β€” the primitives don’t care where the pool state came from. See LiveProvider on defipy.org for the V2 + V3 surface.

Learning resources

On-Chain Analytics Foundations

πŸŽ“ On-Chain Analytics Foundations

Practical course on transforming raw blockchain data into Python analytics pipelines. Take the course β†’