ocean-subgraph/examples/python/list-datatokens.md
Akshay 6e2daf6ee0
Feature/examples (#261)
* Feature: Ocean-subgraph javascript and python examples

* Feature: Ocean-subgraph example add credit

* Rename js example file

* Feature: Add ocean-subgraph python example
2021-11-02 12:48:34 +01:00

1.1 KiB

Ocean-subgraph python example

Query to fetch all datatoken "Transfer" events

import requests
import json

datatoken_address = "0xd0a8540db74bab9ef5847a2424ee3fc26b4518a5"
base_url = "https://subgraph.rinkeby.oceanprotocol.com"
route = "/subgraphs/name/oceanprotocol/ocean-subgraph"
url = base_url + route

query_string = """{{tokenTransactions(skip: {0}, first: {1},
                where:{{event:"Transfer",datatokenAddress:"{2}"}},
                orderBy: timestamp, orderDirection: asc) {{
                id,
                event
                timestamp
                block
        }}}}"""

headers = {"Content-Type": "application/json"}

skip = 0
limit = 1000
result = []
while True:
    query = query_string.format(skip, limit, datatoken_address)
    payload = json.dumps({"query": query})
    response = requests.request("POST", url, headers=headers, data=payload)
    if response.status_code != 200:
        break
    data = json.loads(response.text)
    if (len(data["data"]["tokenTransactions"])) == 0:
        break
    result.append(data["data"])
    skip = skip + limit

print(result)