Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Polymarket Cookbook #858

Merged
merged 7 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions website/pages/en/subgraphs/cookbook/_meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export default {
'grafting-hotfix': 'Subgraph Best Practice 6: Grafting and Hotfixing',
enums: '',
'secure-api-keys-nextjs': '',
polymarket: 'Query Polymarket Data',
}
147 changes: 147 additions & 0 deletions website/pages/en/subgraphs/cookbook/polymarket.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
---
title: Querying Blockchain Data from Polymarket with Subgraphs on The Graph
---

Query Polymarket’s onchain data using GraphQL via subgraphs on The Graph Network. Subgraphs are decentralized APIs powered by The Graph, a protocol for indexing & querying data from blockchains.

## Polymarket Subgraph on Graph Explorer

You can see an interactive query playground on the [Polymarket subgraph’s page on The Graph Explorer](https://thegraph.com/explorer/subgraphs/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp?view=Query&chain=arbitrum-one), where you can test any query.

![Polymarket Playground](/img/Polymarket-playground.png)

## How to use the Visual Query Editor
MichaelMacaulay marked this conversation as resolved.
Show resolved Hide resolved

The visual query editor helps you test sample queries from your subgraph.

You can use the GraphiQL Explorer to compose your GraphQL queries by clicking on the fields you want.

### Example Query: Get the top 5 highest payouts from Polymarket

```
{
redemptions(orderBy: payout, orderDirection: desc, first: 5) {
payout
redeemer
id
timestamp
}
}
```

### Example output

```
{
"data": {
"redemptions": [
{
"id": "0x8fbb68b7c0cbe9aca6024d063a843a23d046b5522270fd25c6a81c511cf517d1_0x3b",
"payout": "6274509531681",
"redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",
"timestamp": "1722929672"
},
{
"id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0x7",
"payout": "2246253575996",
"redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",
"timestamp": "1726701528"
},
{
"id": "0x983b71c64b5075fc1179f4e03849af9c727be60de71c9e86e37ad0b3e43f9db9_0x26",
"payout": "2135448291991",
"redeemer": "0x5a181dcf3eb53a09fb32b20a5a9312fb8d26f689",
"timestamp": "1704932625"
},
{
"id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0xa",
"payout": "1917395333835",
"redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",
"timestamp": "1726701528"
},
{
"id": "0xfe82e117201f5169abc822281ccf0469e6b3740fcb4e799d1b599f83b8f11656_0x30",
"payout": "1862505580000",
"redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",
"timestamp": "1722929866"
}
]
}
}
```

## Polymarket's GraphQL Schema

The schema for this subgraph is defined [here in Polymarket’s GitHub](https://github.com/Polymarket/polymarket-subgraph/blob/main/polymarket-subgraph/schema.graphql).

### Polymarket Subgraph Endpoint

https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp

The Polymarket Subgraph endpoint is available on [Graph Explorer](https://thegraph.com/explorer).

![Polymarket Endpoint](/img/Polymarket-endpoint.png)

## How to Get your own API Key

1. Go to [https://thegraph.com/studio](http://thegraph.com/studio) and connect your wallet
2. Go to https://thegraph.com/studio/apikeys/ to create an API key

You can use this API key on any subgraph in [Graph Explorer](https://thegraph.com/explorer), and it’s not limited to just Polymarket.

100k queries per month are free which is perfect for your side project!

## Additional Polymarket Subgraphs

- [Polymarket](https://thegraph.com/explorer/subgraphs/81Dm16JjuFSrqz813HysXoUPvzTwE7fsfPk2RTf66nyC?view=Query&chain=arbitrum-one)
- [Polymarket Activity Polygon](https://thegraph.com/explorer/subgraphs/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp?view=Query&chain=arbitrum-one)
- [Polymarket Profit & Loss](https://thegraph.com/explorer/subgraphs/6c58N5U4MtQE2Y8njfVrrAfRykzfqajMGeTMEvMmskVz?view=Query&chain=arbitrum-one)
- [Polymarket Open Interest](https://thegraph.com/explorer/subgraphs/ELaW6RtkbmYNmMMU6hEPsghG9Ko3EXSmiRkH855M4qfF?view=Query&chain=arbitrum-one)

## How to Query with the API

You can pass any GraphQL query to the Polymarket endpoint and receive data in json format.

This following code example will return the exact same output as above.

### Sample Code from node.js

```
const axios = require('axios');

const graphqlQuery = `{
positions(first: 5) {
condition
outcomeIndex
}
};

const queryUrl = 'https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp'

const graphQLRequest = {
method: 'post',
url: queryUrl,
data: {
query: graphqlQuery,
},
};

// Send the GraphQL query
axios(graphQLRequest)
.then((response) => {
// Handle the response here
const data = response.data.data
console.log(data)

})
.catch((error) => {
// Handle any errors
console.error(error);
});
```

### Additional resources

For more information about querying data from your subgraph, read more [here](/subgraphs/querying/introduction/).

To explore all the ways you can optimize & customize your subgraph for a better performance, read more about [creating a subgraph here](/developing/creating-a-subgraph/).
Binary file added website/public/img/Polymarket-endpoint.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/img/Polymarket-playground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading