A quantitative and computational guide to decentralized finance using Python.
DeFiPy: Python SDK for On-chain Analytics accompanies the open-source analytics framework DeFiPy and provides a structured, reproducible foundation for DeFi modeling, simulation, and risk analysis. It unifies the mathematical, programmatic, and architectural components of decentralized finance into an executable, open research framework.
This repository contains the Jupyter notebooks for the DeFiPy TextBook which is a comprehensive technical reference for the defipy ecosystem.
The book introduces:
- DeFi architecture – infrastructure, EVM/ABI/JSON-RPC connectivity
- AMM mathematics – constant-product, Balancer, and StableSwap invariants
- Analytics and risk modeling – impermanent loss, expected value, variance, stationarity
- Event-driven pipelines – Web3-based feed handling via
web3.pyandweb3scout - Agent-based simulation – supervisory and transition-matrix modeling of DeFi systems
DeFiPy is the root analytics package, composed of three major submodules:
| Module | Description |
|---|---|
uniswappy |
Uniswap-focused invariant and tick-math analytics |
balancerpy |
Balancer-style multi-asset pool invariants and weighting logic |
stableswappy |
Curve-style StableSwap invariant and slippage analysis |
DeFiPy integrates seamlessly with web3scout, which is a lightweight extension of web3.py that provides efficient on-chain data fetching and event monitoring for analytics pipelines.
defipy-book/
├─ notebooks/ # Code examples per chapter (lightweight versions)
├─ env/ # Environment and dependency files
│ ├─ requirements.txt
│ └─ environment.yml
├─ LICENSE # Apache 2.0 License
└─ README.md # This file
The full Jupyter notebook suite for all chapter examples lives in the defipy-book/notebooks repository.
All executable examples are provided as Jupyter notebooks (one per chapter) in the official defipy-book/notebooks repository.
Each notebook reproduces the code listings from the text and can be run locally or in Colab. A simple clone command:
git clone https://github.com/defipy-devs/defipy-book.git
cd defipy-book/notebooks
jupyter labTo run the on-chain examples in this repository, you’ll need a reliable Ethereum JSON-RPC endpoint. The easiest option is to create a free Infura account and generate a project endpoint. Infura provides HTTPS and WebSocket gateways to Ethereum (and other chains), and integrates seamlessly with Web3.py.
To set one up you can visit Infura; once you sign in:
- Create a new Web3 project
- Select Ethereum as the network
- Copy your HTTPS endpoint, which will look like:
https://mainnet.infura.io/v3/<YOUR_API_KEY>DeFiPy and its examples require Python 3.10 or later and the following core packages:
defipy
gmpy2
pandas
numpy
matplotlib
web3
jupyterlab
Ensure GMP, MPFR, and MPC system libraries are installed prior to gmpy2.
If you use this book or its code in your research or coursework, please cite:
Moore, I. (2025). DeFiPy: Python SDK for On-chain Analytics.
DeFiPy.org, Apache-2.0.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy at:
All examples, text, and notebooks are distributed on an “AS IS” basis, without warranties or conditions of any kind.
© 2025 defipy-devs / Ian Moore and contributors.
Building open, deterministic analytics for decentralized finance.