Skip to main content

Python package to query DeFi data from several The Graph subgraphs

Project description

banner

DeFi Crawler Python library

keyko.io


Table of Contents


Features

This library allows to get DeFi data from several protocols in a structured and standard way. This data comes from different subgraphs displayed in The Graph protocol

Prerequisites

Python 3.6

Quick-start

pip install deficrawler

Usage

To get data from a protocol, it's needed to create an instance of the protocol with the name, version and blockchain where the protocols exists, the supported protocols can be found in supported protocols section. And the entities for the protocols with the field types in the entities folder

To get data from Lending protocols

# Instanciate the protocol with the name, version and chain
from deficrawler import Lending

aave = Lending(protocol="Aave", chain="Ethereum", version=2)
aave_polygon = Lending(protocol="Aave", chain="Polygon", version=2)
compound = Lending(protocol="Compound", chain="Ethereum", version=2)
cream = Lending(protocol="Cream", chain="Ethereum", version=2)
cream_bsc = Lending(protocol="Cream", chain="bsc", version=2)

# Not all the protocols has the same available events to get data, to know which entities are supported for each protocol:
aave.supported_entities()
uniswap.suported_entities()

## For each different entity, data can be retrieved in a specific time range.
compound.get_data_from_date_range(start_date, end_date, "borrow")

# To get the all the users of a protocol
cream_bsc.get_all_users()

# And the user positions 
cream_bsc.get_all_users(user)

To get data from Dex protocols

# Instanciate the protocol with the name, version and chain
from deficrawler import Dex

uniswap = Dex(protocol="Uniswap", chain="Ethereum", version=2)
balancer = Dex(protocol="Balancer", chain="Ethereum", version=1)
bancor = Dex("Bancor", chain="Ethereum", version=1)
shushi = Dex("SushiSwap", chain="Ethereum", version=1)

# Not all the protocols has the same available events to get data, to know which entities are supported for each protocol:
uniswap.supported_entities()
balancer.suported_entities()

## For each different entity, data can be retrieved in a specific time range.
uniswap.get_data_from_date_range(start_date_amm, end_date_amm, "swap")

# To get the all the pools of a protocol
uniswap.get_all_pools()

To get prices from the oracles it's needed to instanciate an oracle object and call the functions.

# Instanciate the oracle with the name, version and chain
from deficrawler import Oracle

chainlink = Oracle(protocol="chainlink", version=1, chain="Ethereum")

#Get all the available pairs to get the data
chainlink.get_all_pairs() 

#Get the price for a specific pair in a time range
chainlink.get_price_from_date_range(from_date=start_date, to_date=end_date, pair="ETH/USD")

Supported-protocols

Protocols

Name Type Version Chain
Aave Lending 2 Ethereum
Aave Lending 2 Polygon
Compound Lending 2 Ethereum
Cream Lending 2 Ethereum
Cream Lending 2 BSC
Uniswap Dexes 2 Ethereum
Balancer Dexes 2 Ethereum
Bancor Dexes 1 Ethereum
SushiSwap Dexes 1 Ethereum
SushiSwap Dexes 1 BSC
SushiSwap Dexes 1 Polygon
Ubeswap Dexes 1 Celo

Oracles

Name Type Version Chain
Chainlink Oracle 1 Ethereum

Testing

Tests are creatd using Pytest library. To run the tests

pytest -v 

Examples

To run the examples run

python3 docs/examples.py 

Contributions

If you want to add a new protocol to be supported or a new entity to retrive data, create a PR with the new configuration of the protocol and the unit/integration tests of this new feature.

To add a new protocol, create a new json config file in the config folder, with the struture protocolname-version.json. Inside this configuration file must be the following sections:

Protocol

This section specifies the global configuration of the protocol. The required fields are

  • Name: Protocol name.
  • Type: Protocol type (Borrowing lending)
  • Version: Version of the protcol
  • Enpoint: Subgrapn endpoint to send the http request to get the data. If the protocols exists on more than one chain, several enpoints can be provided

Example

  "protocol": {
    "name": "AAVE",
    "type": "Lending",
    "version": 2,
    "endpoint": {
      "ethereum": "https://api.thegraph.com/subgraphs/name/aave/protocol-v2",
      "polygon": "https://api.thegraph.com/subgraphs/name/aave/aave-v2-matic"
    }
  }

After this section the supported entities should be specified. Each entity has three different sections inside of them.

  • Entity: The entity contains the fields to create a common entity from a shared type of event of different protocols (borrow, swap, deposit...). The entity starts with the name that will be applied to this specific event across all the protocols. Then should be a field for each attribute that will be contained in the entity, and each field shoud have an array, with the path to find this element in the corresponding subpgraph, these fields will be used to create the query. As example for borrow entity in Aave:
    "borrow": {
      "attributes": {
        "tx_id":[
          "id"
        ],
        "user": [
          "user",
          "id"
        ],
        "token": [
          "reserve",
          "symbol"
        ],
        "amount": [
          "amount"
        ],
        "timestamp": [
          "timestamp"
        ]
      }
    
  • Query:: Each query in each protocol can be named different, to allow create the query dynamically, the query name should be specified and the field to order by if will be more that one batch to retrive. In this section also can be specified, additional fields to get data from the subgraph that will be needed to apply transformations, but will not be part of the output itself. As example for Aave we need to get the decimals for the token to divide the amount, but the decimals are not part of the entity. As example
        "query": {
          "extra_fields": {
            "decimals": [
              "reserve",
              "decimals"
            ]
          },
          "name": "borrows",
          "params": {
            "orderBy": "timestamp"
          }
        }
    
  • Tranformations: This section specified the transformations that should be applied to the fields (if any transformation should be applied). Here we can specify the field of the common entity (the output) that needs to be transform, and the function that will be applied. This funtion will be call dinamically in the transformation phase. As example:
        "transformations": {
          "amount": "decimals",
          "tx_id":"tx_id_colon"
        }
    

License

Copyright 2020 Keyko GmbH.

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 of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

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

deficrawler-0.1.1.tar.gz (22.8 kB view details)

Uploaded Source

Built Distribution

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

deficrawler-0.1.1-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file deficrawler-0.1.1.tar.gz.

File metadata

  • Download URL: deficrawler-0.1.1.tar.gz
  • Upload date:
  • Size: 22.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for deficrawler-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4e7920d3c69cf4b4c3e35a8b5f8ee9f884cd1381245f591b43055b66a38762fd
MD5 f48a2b38c73c9781d282e24e7d8d5764
BLAKE2b-256 0d01720087f62a92edb4e87aace93662c7bea6b103400e669afa0f7fbb7daeb8

See more details on using hashes here.

File details

Details for the file deficrawler-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: deficrawler-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for deficrawler-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7cb58d0311aadb1d3661a4e6a1801d5b4accffade164b8cea13713f35d832c3c
MD5 fe14cf8c1aa8b58175d6b8d1fc2ce04b
BLAKE2b-256 040d371bfe6dd4b8883f514cb4ad7a8e3fa1f6981da2f8ad60abfa843ca5d485

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