-
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-5262 feat(portal): update protocol points from legacy api to grap…
…hql (#1007) ## Affected Packages Apps - [ ] data populator - [x] portal - [ ] template Packages - [ ] 1ui - [ ] api - [ ] graphql - [ ] protocol - [ ] sdk Tools - [ ] tools ## Overview Overview of the changes in the PR. ## 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
b20a059
commit 604babf
Showing
9 changed files
with
177 additions
and
48 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,110 @@ | ||
interface GraphQLResponse<T> { | ||
data?: T | ||
errors?: Array<{ message: string }> | ||
} | ||
|
||
const GetFeeTransfersDocument = { | ||
query: ` | ||
query GetFeeTransfers($address: String!, $cutoff_timestamp: bigint) { | ||
before_cutoff: fee_transfers_aggregate( | ||
where: { block_timestamp: { _lte: $cutoff_timestamp }, sender_id: { _eq: $address } } | ||
) { | ||
aggregate { | ||
sum { | ||
amount | ||
} | ||
} | ||
} | ||
after_cutoff: fee_transfers_aggregate( | ||
where: { block_timestamp: { _gt: $cutoff_timestamp }, sender_id: { _eq: $address } } | ||
) { | ||
aggregate { | ||
sum { | ||
amount | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
} as const | ||
|
||
interface GetFeeTransfersQuery { | ||
before_cutoff: { | ||
aggregate: { | ||
sum: { | ||
amount: string | ||
} | ||
} | ||
} | ||
after_cutoff: { | ||
aggregate: { | ||
sum: { | ||
amount: string | ||
} | ||
} | ||
} | ||
} | ||
|
||
interface GetFeeTransfersQueryVariables { | ||
address: string | ||
cutoff_timestamp: number | ||
} | ||
|
||
async function fetchGraphQL<T, V>( | ||
document: { query: string }, | ||
variables: V, | ||
): Promise<GraphQLResponse<T>> { | ||
const endpoint = process.env.I7N_GRAPHQL_ENDPOINT | ||
if (!endpoint) { | ||
throw new Error('I7N_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 async function fetchProtocolFees(address: string) { | ||
const cutoffTimestamp = 1733356800 | ||
|
||
const feeData = await fetchGraphQL< | ||
GetFeeTransfersQuery, | ||
GetFeeTransfersQueryVariables | ||
>(GetFeeTransfersDocument, { | ||
address, | ||
cutoff_timestamp: cutoffTimestamp, | ||
}) | ||
|
||
const beforeCutoffAmount = | ||
feeData?.data?.before_cutoff?.aggregate?.sum?.amount ?? '0' | ||
const afterCutoffAmount = | ||
feeData?.data?.after_cutoff?.aggregate?.sum?.amount ?? '0' | ||
|
||
const beforeCutoffPoints = | ||
(BigInt(beforeCutoffAmount) * BigInt(10000000)) / BigInt(1e18) | ||
const afterCutoffPoints = | ||
(BigInt(afterCutoffAmount) * BigInt(1000000)) / BigInt(1e18) | ||
const totalPoints = beforeCutoffPoints + afterCutoffPoints | ||
|
||
return { | ||
beforeCutoffAmount, | ||
afterCutoffAmount, | ||
beforeCutoffPoints: beforeCutoffPoints.toString(), | ||
afterCutoffPoints: afterCutoffPoints.toString(), | ||
totalPoints: totalPoints.toString(), | ||
} | ||
} |
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
Oops, something went wrong.