Last active
September 30, 2021 16:40
-
-
Save chuckbergeron/e29653f7143c660171d79faa3feb3b33 to your computer and use it in GitHub Desktop.
Code to query historical prizes from the graph protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This would be called with some code like this: | |
import { poolPrizesQuery } from 'queries/poolPrizesQuery' | |
const variables = { | |
poolAddress: prizePool.address.toLowerCase(), | |
first: pageSize, | |
skip: (page - 1) * pageSize | |
} | |
const graphQLClient = new GraphQLClient(url) | |
const query = poolPrizesQuery() | |
const data = await graphQLClient.request(query, variables) | |
// PoolPrizesQuery.js | |
import gql from 'graphql-tag' | |
export const poolPrizesQuery = (number = -1) => { | |
const blockFilter = number > 0 ? `, block: { number: ${number} }` : '' | |
return gql` | |
query poolPrizesQuery($poolAddress: String!, $skip: Int, $first: Int) { | |
prizePool(id: $poolAddress ${blockFilter}) { | |
id | |
prizes(skip: $skip, first: $first, orderBy: awardedTimestamp, orderDirection: desc) { | |
...prizeFragment | |
} | |
} | |
} | |
${prizeFragment} | |
` | |
} | |
// PrizeFragment.js | |
import gql from 'graphql-tag' | |
import { awardedControlledTokenFragment } from 'lib/fragments/awardedControlledTokenFragment' | |
import { awardedExternalErc20TokenFragment } from 'lib/fragments/awardedExternalErc20TokenFragment' | |
import { awardedExternalErc721NftFragment } from 'lib/fragments/awardedExternalErc721NftFragment' | |
export const prizeFragment = gql` | |
fragment prizeFragment on Prize { | |
id | |
awardedTimestamp | |
awardedBlock | |
totalTicketSupply | |
prizePeriodStartedTimestamp | |
lockBlock | |
awardedControlledTokens { | |
...awardedControlledTokenFragment | |
} | |
awardedExternalErc20Tokens { | |
...awardedExternalErc20TokenFragment | |
} | |
awardedExternalErc721Nfts { | |
...awardedExternalErc721NftFragment | |
} | |
} | |
${awardedControlledTokenFragment} | |
${awardedExternalErc20TokenFragment} | |
${awardedExternalErc721NftFragment} | |
` | |
// AwardedExternalErc721NftFragment.js | |
import gql from 'graphql-tag' | |
export const awardedExternalErc721NftFragment = gql` | |
fragment awardedExternalErc721NftFragment on AwardedExternalErc721Nft { | |
id | |
address | |
tokenIds | |
winner | |
} | |
` | |
// AwardedExternalErc20NftFragment.js | |
import gql from 'graphql-tag' | |
export const awardedExternalErc20TokenFragment = gql` | |
fragment awardedExternalErc20TokenFragment on AwardedExternalErc20Token { | |
id | |
winner | |
address | |
balanceAwarded | |
name | |
symbol | |
decimals | |
} | |
` | |
// AwardedControlledTokenFragment.js | |
import gql from 'graphql-tag' | |
export const awardedControlledTokenFragment = gql` | |
fragment awardedControlledTokenFragment on AwardedControlledToken { | |
id | |
amount | |
token | |
winner | |
prize { | |
id | |
prizePool { | |
underlyingCollateralDecimals | |
} | |
} | |
} | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment