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
This commit is contained in:
Akshay 2021-11-02 12:48:34 +01:00 committed by GitHub
parent 32e5de4039
commit 6e2daf6ee0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,34 @@
## Ocean-subgraph javascript example
Query to get all datatoken pools
```javascript
const axios = require('axios');
const BASE_URL = 'https://subgraph.rinkeby.oceanprotocol.com';
const SUBGRAPHS_QUERY_ROUTE = '/subgraphs/name/oceanprotocol/ocean-subgraph';
const url = BASE_URL + SUBGRAPHS_QUERY_ROUTE;
async function getAllPoolDatatokenAddresses(url) {
const requestBody = { query: '{ pools(first:1000, orderBy: oceanReserve, orderDirection: desc) { id valueLocked name consumePrice totalShares symbol cap datatokenAddress tokens { id balance name symbol } } }' };
const response = await axios.post(url, requestBody).catch((error) => {
const { status, statusText, data } = error.response;
console.error('Error getting data from subgraph: ', status, statusText, data);
throw new Error(statusText);
});
const { data: { data: { pools }, }, } = response;
return pools.filter((pool) => pool.datatokenAddress).map((pool) => pool.datatokenAddress);
}
getAllPoolDatatokenAddresses(url).then(result => {
console.log(result)
});
```
### Credits
- German Navarro - Github user: gmanavarro - Discord user: Naga#2072
- Axel Diaz - Github user: axeldiaz10 - Discord user: axeldiaz10#0085
- Juan Arrillaga - Github user: jarrillaga - Discord user: juanarri#3482

View File

@ -0,0 +1,35 @@
## Ocean-subgraph python example
Query to fetch all datatoken `Transfer` events
```python
import requests
import json
datatoken_address = "0x00b43f5905ff736526294ac6de52d1dcd35821c9"
base_url = "https://subgraph.rinkeby.oceanprotocol.com"
route = "/subgraphs/name/oceanprotocol/ocean-subgraph"
url = base_url + route
query = """{{
tokenOrders(where:{{datatokenId:"{0}"}}, first: 5, orderBy: timestamp, orderDirection: desc) {{
id
consumer {{id}}
payer {{id}}
serviceId
marketFeeCollector {{id}}
marketFee
tx
block
}}
}}""".format(datatoken_address)
headers = {"Content-Type": "application/json"}
payload = json.dumps({"query": query})
response = requests.request("POST", url, headers=headers, data=payload)
result = json.loads(response.text)
print(result)
```

View File

@ -0,0 +1,34 @@
## Ocean-subgraph python example
Query global statistics like `totalValueLocked`, `totalOceanLiquidity`, `orderCount`, etc.
```python
import requests
import json
base_url = "https://subgraph.rinkeby.oceanprotocol.com"
route = "/subgraphs/name/oceanprotocol/ocean-subgraph"
url = base_url + route
query = """
{
globals {
id
totalValueLocked
totalOceanLiquidity
totalSwapVolume
totalOrderVolume
orderCount
poolCount
}
}"""
headers = {"Content-Type": "application/json"}
payload = json.dumps({"query": query})
response = requests.request("POST", url, headers=headers, data=payload)
result = json.loads(response.text)
print(result)
```

View File

@ -0,0 +1,41 @@
## Ocean-subgraph python example
Query to fetch all datatoken "Transfer" events
```python
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)
```