-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENG-5252 feat(portal): add raw graphql query to quests route for nft …
…points (#998) ## Affected Packages Apps - [ ] data populator - [x] portal - [ ] template Packages - [ ] 1ui - [ ] api - [ ] graphql - [ ] protocol - [ ] sdk Tools - [ ] tools ## Overview Swaps out the Phosphor and RPC calls used for NFT Relic points to use the new GraphQL ponder endpoint. ## Screen Captures If applicable, add screenshots or screen captures of your changes. ## Declaration - [x] I hereby declare that I have abided by the rules and regulations as outlined in the [CONTRIBUTING.md](https://github.com/0xIntuition/intuition-ts/blob/main/CONTRIBUTING.md)
- Loading branch information
1 parent
12816db
commit 8d810a5
Showing
8 changed files
with
146 additions
and
134 deletions.
There are no files selected for viewing
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 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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
interface GraphQLResponse<T> { | ||
data?: T | ||
errors?: Array<{ message: string }> | ||
} | ||
|
||
const GetMintCountDocument = { | ||
query: ` | ||
query GetMintCountUntilDate($address: String!, $cutoff_timestamp: Int!) { | ||
voucherRedeemedEvents( | ||
where: { redeemer: $address, timestamp_lte: $cutoff_timestamp } | ||
) { | ||
totalCount | ||
} | ||
} | ||
`, | ||
} as const | ||
|
||
interface GetMintCountQuery { | ||
voucherRedeemedEvents: { | ||
totalCount: number | ||
} | ||
} | ||
|
||
const GetRelicHoldingsDocument = { | ||
query: ` | ||
query GetRelicHoldings($address: String!) { | ||
account(address: $address) { | ||
tokens { | ||
totalCount | ||
} | ||
voucherRedeemedEvents { | ||
totalCount | ||
} | ||
} | ||
} | ||
`, | ||
} as const | ||
|
||
interface GetRelicHoldingsQuery { | ||
account: { | ||
tokens: { | ||
totalCount: number | ||
} | ||
voucherRedeemedEvents: { | ||
totalCount: number | ||
} | ||
} | ||
} | ||
|
||
async function fetchGraphQL<T, V>( | ||
document: { query: string }, | ||
variables: V, | ||
): Promise<GraphQLResponse<T>> { | ||
const endpoint = process.env.RELIC_GRAPHQL_ENDPOINT | ||
if (!endpoint) { | ||
throw new Error('RELIC_GRAPHQL_ENDPOINT not configured') | ||
} | ||
|
||
const response = await fetch(endpoint, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
query: document.query, | ||
variables, | ||
}), | ||
}) | ||
|
||
if (!response.ok) { | ||
throw new Error(`GraphQL request failed: ${response.statusText}`) | ||
} | ||
|
||
return response.json() | ||
} | ||
|
||
export function useRelicCounts(address: string) { | ||
const cutoffTimestamp = 1735516799 | ||
|
||
const { data: mintCountData } = useQuery({ | ||
queryKey: ['relicMintCount', address, cutoffTimestamp], | ||
queryFn: () => | ||
fetchGraphQL< | ||
GetMintCountQuery, | ||
{ address: string; cutoff_timestamp: number } | ||
>(GetMintCountDocument, { | ||
address, | ||
cutoff_timestamp: cutoffTimestamp, | ||
}), | ||
}) | ||
|
||
const { data: holdingsData } = useQuery({ | ||
queryKey: ['relicHoldCount', address], | ||
queryFn: () => | ||
fetchGraphQL<GetRelicHoldingsQuery, { address: string }>( | ||
GetRelicHoldingsDocument, | ||
{ | ||
address, | ||
}, | ||
), | ||
}) | ||
|
||
const mintCount = mintCountData?.data?.voucherRedeemedEvents?.totalCount ?? 0 | ||
const holdCount = holdingsData?.data?.account?.tokens?.totalCount ?? 0 | ||
|
||
return { | ||
mintCount, | ||
holdCount, | ||
nftMintPoints: mintCount * 2000000, | ||
nftHoldPoints: holdCount * 250000, | ||
totalNftPoints: mintCount * 2000000 + holdCount * 250000, | ||
} | ||
} |
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 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
Oops, something went wrong.