mirror of
https://github.com/oceanprotocol/ocean-subgraph.git
synced 2024-12-02 05:57:29 +01:00
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:
parent
32e5de4039
commit
6e2daf6ee0
34
examples/javascript/get-all-pools.md
Normal file
34
examples/javascript/get-all-pools.md
Normal 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
|
35
examples/python/get-orders.md
Normal file
35
examples/python/get-orders.md
Normal 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)
|
||||||
|
```
|
34
examples/python/global-statistics.md
Normal file
34
examples/python/global-statistics.md
Normal 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)
|
||||||
|
```
|
41
examples/python/list-datatokens.md
Normal file
41
examples/python/list-datatokens.md
Normal 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)
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user