|
| 1 | +--- |
| 2 | +title: Querying Blockchain Data from Polymarket with Subgraphs on The Graph |
| 3 | +--- |
| 4 | + |
| 5 | +Polymarket’s onchain data can be easily queried using GraphQL via subgraphs on The Graph Network. Subgraphs are decentralized APIs powered by The Graph, a protocol for indexing & querying data from blockchains. |
| 6 | + |
| 7 | +## The Polymarket Subgraph |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +## How to use the Visual Query Editor |
| 15 | + |
| 16 | +You can use the GraphiQL Explorer to compose your GraphQL queries by clicking on the fields you want. |
| 17 | + |
| 18 | +### Example Query: Get the top 5 highest payouts from Polymarket |
| 19 | + |
| 20 | +``` |
| 21 | +{ |
| 22 | + redemptions(orderBy: payout, orderDirection: desc, first: 5) { |
| 23 | + payout |
| 24 | + redeemer |
| 25 | + id |
| 26 | + timestamp |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +### Example output |
| 32 | + |
| 33 | +``` |
| 34 | +{ |
| 35 | + "data": { |
| 36 | + "redemptions": [ |
| 37 | + { |
| 38 | + "id": "0x8fbb68b7c0cbe9aca6024d063a843a23d046b5522270fd25c6a81c511cf517d1_0x3b", |
| 39 | + "payout": "6274509531681", |
| 40 | + "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c", |
| 41 | + "timestamp": "1722929672" |
| 42 | + }, |
| 43 | + { |
| 44 | + "id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0x7", |
| 45 | + "payout": "2246253575996", |
| 46 | + "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c", |
| 47 | + "timestamp": "1726701528" |
| 48 | + }, |
| 49 | + { |
| 50 | + "id": "0x983b71c64b5075fc1179f4e03849af9c727be60de71c9e86e37ad0b3e43f9db9_0x26", |
| 51 | + "payout": "2135448291991", |
| 52 | + "redeemer": "0x5a181dcf3eb53a09fb32b20a5a9312fb8d26f689", |
| 53 | + "timestamp": "1704932625" |
| 54 | + }, |
| 55 | + { |
| 56 | + "id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0xa", |
| 57 | + "payout": "1917395333835", |
| 58 | + "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c", |
| 59 | + "timestamp": "1726701528" |
| 60 | + }, |
| 61 | + { |
| 62 | + "id": "0xfe82e117201f5169abc822281ccf0469e6b3740fcb4e799d1b599f83b8f11656_0x30", |
| 63 | + "payout": "1862505580000", |
| 64 | + "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c", |
| 65 | + "timestamp": "1722929866" |
| 66 | + } |
| 67 | + ] |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## Polymarket's GraphQL Schema |
| 73 | + |
| 74 | +The schema for this subgraph is defined [here in Polymarket’s GitHub](https://github.com/Polymarket/polymarket-subgraph/blob/main/polymarket-subgraph/schema.graphql). |
| 75 | + |
| 76 | +### Polymarket Subgraph Endpoint |
| 77 | + |
| 78 | +https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp |
| 79 | + |
| 80 | +The Polymarket Subgraph endpoint is available on [Graph Explorer](https://thegraph.com/explorer). |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +## How to FGet your own API Key |
| 85 | + |
| 86 | +1. Go to [https://thegraph.com/studio](http://thegraph.com/studio) and connect your wallet |
| 87 | +2. Go to https://thegraph.com/studio/apikeys/ to create an API key |
| 88 | + |
| 89 | +You can use this API key on any subgraph in [Graph Explorer](https://thegraph.com/explorer), and it’s not limited to just Polymarket. |
| 90 | + |
| 91 | +100k queries per month are free which is perfect for your side project! |
| 92 | + |
| 93 | +## Additional Polymarket Subgraphs |
| 94 | + |
| 95 | +- [Polymarket](https://thegraph.com/explorer/subgraphs/81Dm16JjuFSrqz813HysXoUPvzTwE7fsfPk2RTf66nyC?view=Query&chain=arbitrum-one) |
| 96 | +- [Polymarket Activity Polygon](https://thegraph.com/explorer/subgraphs/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp?view=Query&chain=arbitrum-one) |
| 97 | +- [Polymarket Profit & Loss](https://thegraph.com/explorer/subgraphs/6c58N5U4MtQE2Y8njfVrrAfRykzfqajMGeTMEvMmskVz?view=Query&chain=arbitrum-one) |
| 98 | +- [Polymarket Open Interest](https://thegraph.com/explorer/subgraphs/ELaW6RtkbmYNmMMU6hEPsghG9Ko3EXSmiRkH855M4qfF?view=Query&chain=arbitrum-one) |
| 99 | + |
| 100 | +## How to Query with the API |
| 101 | + |
| 102 | +You can pass any GraphQL query to the Polymarket endpoint and receive data in json format. |
| 103 | + |
| 104 | +This following code example will return the exact same output as above. |
| 105 | + |
| 106 | +### Sample Code from node.js |
| 107 | + |
| 108 | +``` |
| 109 | +const axios = require('axios'); |
| 110 | +
|
| 111 | +const graphqlQuery = `{ |
| 112 | + positions(first: 5) { |
| 113 | + condition |
| 114 | + outcomeIndex |
| 115 | + } |
| 116 | +}; |
| 117 | +
|
| 118 | +const queryUrl = 'https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp' |
| 119 | +
|
| 120 | +const graphQLRequest = { |
| 121 | + method: 'post', |
| 122 | + url: queryUrl, |
| 123 | + data: { |
| 124 | + query: graphqlQuery, |
| 125 | + }, |
| 126 | +}; |
| 127 | +
|
| 128 | +// Send the GraphQL query |
| 129 | +axios(graphQLRequest) |
| 130 | + .then((response) => { |
| 131 | + // Handle the response here |
| 132 | + const data = response.data.data |
| 133 | + console.log(data) |
| 134 | +
|
| 135 | + }) |
| 136 | + .catch((error) => { |
| 137 | + // Handle any errors |
| 138 | + console.error(error); |
| 139 | + }); |
| 140 | +``` |
| 141 | + |
| 142 | +## About The Graph |
| 143 | + |
| 144 | +The Graph is a decentralized protocol that enables seamless querying and indexing of blockchain data. It simplifies the complex process of querying blockchain data through the use of subgraphs (open APIs). |
| 145 | + |
| 146 | +Query data easier and faster with The Graph. |
| 147 | + |
| 148 | +Benefits of Using The Graph |
| 149 | + |
| 150 | +- All users get 100,000 free queries per month |
| 151 | +- Anyone can create or query subgraphs on The Graph |
| 152 | +- Over 100 Indexers around the world for resilience and redundancy |
| 153 | +- 99.9% less expensive than running a self-hosted indexing node |
| 154 | +- Unlocking more queries is a seamless experience, you can pay with crypto or a credit card |
| 155 | + |
| 156 | +### Additional resources |
| 157 | + |
| 158 | +For more information about querying data from your subgraph, read more [here](/subgraphs/querying/introduction/). |
| 159 | + |
| 160 | +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/). |
| 161 | + |
| 162 | + |
0 commit comments