-
Notifications
You must be signed in to change notification settings - Fork 3
refactor: migrate user positions to monarch api #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3913c4c
first migration
antoncoding a1f1162
feat: monarch first for user positions
antoncoding a61839c
feat: fix duplicated history
antoncoding a645859
fix: grouped apy in period wrong
antoncoding 5232cf1
chore: review feedbacks
antoncoding 73c67cf
feat: cache history
antoncoding b356ca5
fix: address review nits for history keys and APY guard
starksama e8b4f78
fix: clamp history page before slicing
starksama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,113 @@ | ||
| import { envioUserPositionForMarketQuery, envioUserPositionsPageQuery } from '@/graphql/envio-queries'; | ||
| import { ALL_SUPPORTED_NETWORKS, type SupportedNetworks } from '@/utils/networks'; | ||
| import { monarchGraphqlFetcher } from './fetchers'; | ||
|
|
||
| type PositionMarket = { | ||
| marketUniqueKey: string; | ||
| chainId: number; | ||
| }; | ||
|
|
||
| type MonarchUserPositionRow = { | ||
| marketId: string; | ||
| chainId: number; | ||
| supplyShares: string; | ||
| borrowShares: string; | ||
| collateral: string; | ||
| }; | ||
|
|
||
| type MonarchUserPositionsPageResponse = { | ||
| data?: { | ||
| Position?: MonarchUserPositionRow[]; | ||
| }; | ||
| }; | ||
|
|
||
| export type MonarchUserPositionState = { | ||
| supplyShares: string; | ||
| borrowShares: string; | ||
| collateral: string; | ||
| }; | ||
|
|
||
| const MONARCH_POSITION_MARKETS_PAGE_SIZE = 500; | ||
|
|
||
| const isNonZero = (value: string | null | undefined): boolean => { | ||
| return value !== null && value !== undefined && value !== '0'; | ||
| }; | ||
|
|
||
| export const fetchMonarchUserPositionMarketsForNetworks = async ( | ||
| userAddress: string, | ||
| networks: SupportedNetworks[], | ||
| ): Promise<PositionMarket[]> => { | ||
| if (networks.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| const requestedNetworks = new Set(networks); | ||
| const supportedNetworks = new Set(ALL_SUPPORTED_NETWORKS); | ||
| const positionMarkets = new Map<string, PositionMarket>(); | ||
| let offset = 0; | ||
|
|
||
| while (true) { | ||
| const response = await monarchGraphqlFetcher<MonarchUserPositionsPageResponse>(envioUserPositionsPageQuery, { | ||
| user: userAddress.toLowerCase(), | ||
| chainIds: networks, | ||
| limit: MONARCH_POSITION_MARKETS_PAGE_SIZE, | ||
| offset, | ||
| }); | ||
|
|
||
| const positions = response.data?.Position ?? []; | ||
|
|
||
| for (const position of positions) { | ||
| const chainId = position.chainId as SupportedNetworks; | ||
| if (!supportedNetworks.has(chainId) || !requestedNetworks.has(chainId)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!isNonZero(position.supplyShares) && !isNonZero(position.borrowShares) && !isNonZero(position.collateral)) { | ||
| continue; | ||
| } | ||
|
|
||
| const positionMarket = { | ||
| marketUniqueKey: position.marketId, | ||
| chainId, | ||
| }; | ||
|
|
||
| positionMarkets.set(`${positionMarket.marketUniqueKey.toLowerCase()}-${positionMarket.chainId}`, positionMarket); | ||
| } | ||
|
|
||
| if (positions.length < MONARCH_POSITION_MARKETS_PAGE_SIZE) { | ||
| break; | ||
| } | ||
|
|
||
| offset += positions.length; | ||
| } | ||
|
|
||
| return Array.from(positionMarkets.values()); | ||
| }; | ||
|
|
||
| export const fetchMonarchUserPositionStateForMarket = async ( | ||
| marketUniqueKey: string, | ||
| userAddress: string, | ||
| network: SupportedNetworks, | ||
| ): Promise<MonarchUserPositionState | null> => { | ||
| const response = await monarchGraphqlFetcher<MonarchUserPositionsPageResponse>(envioUserPositionForMarketQuery, { | ||
| user: userAddress.toLowerCase(), | ||
| chainId: network, | ||
| marketId: marketUniqueKey.toLowerCase(), | ||
| }); | ||
|
|
||
| const position = response.data?.Position?.[0]; | ||
|
|
||
| if (!position) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!isNonZero(position.supplyShares) && !isNonZero(position.borrowShares) && !isNonZero(position.collateral)) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| supplyShares: position.supplyShares, | ||
| borrowShares: position.borrowShares, | ||
| collateral: position.collateral, | ||
| }; | ||
| }; | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.