This is package for creating and visualizing graphs in DeFi protocols.
Install using pip or uv
pip install defigraph
# or using uv
uv add defigraph
- Edge - describes a path between tokens
- Vertex - describes a token
- Graph - describes a defi market
- Pool - describes trading token pairs
Edges store two main state:
self.weight
- a weight between two tokens- typeof
float
{0,1}
Depends on direction of edge- Calculated as
-Math.log(self.pool.token_price_{0,1})
- typeof
self.pool
- an instance of a token pool- typeof
Pool
- typeof
self._tuple
- Allows indexing/iteration of the edge object- typeof
iter
- Array of
[Vertex1, Vertex2, (self.weight, Pool)]
- typeof
A pool object describing tokens:
self.address
- the address of the pool- typeof
Hex
checksum address
- typeof
self.token0
- a token described as a vertex- typeof
Vertex
- typeof
self.token1
- a token described as a vertex- typeof
Vertex
- typeof
self.token0_price
- the price of token0- typeof
float
- typeof
self.token1_price
- the price of token1- typeof `float
self.fee
- describes the pool fee e.g Uniswap (100 | 500 | 1000 | 3000)- typeof
int
- typeof
An adjacency list graph object desribing a defi market
self.vertices
- contains a list of all vertices- typeof
List[Vertex]
- typeof
self.adjascency_list
- a mapping describing edges in the graph- typeof
Dict[Vertex, List[Edge]]
- example: {Vertex: [Edge1, Edge2, Edge3, Edge4]}
- typeof
A node on the graph describing a token
self.name
- name of the token- typeof
string
- typeof
self.decimals
- number of decimals for token- typeof
int
- typeof
self.address
- address of token- typeof
Hex
checksum address
- typeof
Run tests:
pytest
See example in docs/notebooks/graph.ipynb