diff --git a/website/pages/en/subgraphs/cookbook/_meta.js b/website/pages/en/subgraphs/cookbook/_meta.js index f2f275dbd8af..6b9e6ccbece5 100644 --- a/website/pages/en/subgraphs/cookbook/_meta.js +++ b/website/pages/en/subgraphs/cookbook/_meta.js @@ -15,4 +15,5 @@ export default { 'grafting-hotfix': 'Subgraph Best Practice 6: Grafting and Hotfixing', enums: '', 'secure-api-keys-nextjs': '', + polymarket: 'Query Polymarket Data', } diff --git a/website/pages/en/subgraphs/cookbook/polymarket.mdx b/website/pages/en/subgraphs/cookbook/polymarket.mdx new file mode 100644 index 000000000000..982bae6c05bc --- /dev/null +++ b/website/pages/en/subgraphs/cookbook/polymarket.mdx @@ -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 + +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/). diff --git a/website/public/img/Polymarket-endpoint.png b/website/public/img/Polymarket-endpoint.png new file mode 100644 index 000000000000..ae5cc0dd56c0 Binary files /dev/null and b/website/public/img/Polymarket-endpoint.png differ diff --git a/website/public/img/Polymarket-playground.png b/website/public/img/Polymarket-playground.png new file mode 100644 index 000000000000..e6140401aef2 Binary files /dev/null and b/website/public/img/Polymarket-playground.png differ