-
Notifications
You must be signed in to change notification settings - Fork 70
Pull liquidation info from the new subgraph #1204
base: dev
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import React, { FC } from 'react'; | ||
| import { Trans } from 'react-i18next'; | ||
| import styled from 'styled-components'; | ||
|
|
||
| import Banner, { BannerType } from 'sections/shared/Layout/Banner'; | ||
| import { ExternalLink } from 'styles/common'; | ||
| import { formatShortDateWithTime } from 'utils/formatters/date'; | ||
| import { EXTERNAL_LINKS } from 'constants/links'; | ||
|
|
||
| type LiquidationBannerProps = { | ||
| ratio: number; | ||
| deadline: number; | ||
| }; | ||
|
|
||
| export const LiquidationBanner: FC<LiquidationBannerProps> = ({ ratio, deadline }) => { | ||
| return ( | ||
| <Banner | ||
| type={BannerType.WARNING} | ||
| message={ | ||
| <Trans | ||
| i18nKey={'user-menu.banner.liquidation-warning'} | ||
| values={{ | ||
| liquidationRatio: ratio, | ||
| liquidationDeadline: formatShortDateWithTime(deadline), | ||
| }} | ||
| components={[ | ||
| <Strong />, | ||
| <Strong />, | ||
| <StyledExternalLink href={EXTERNAL_LINKS.Synthetix.SIP148Liquidations} />, | ||
| ]} | ||
| /> | ||
| } | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| const Strong = styled.strong` | ||
| font-family: ${(props) => props.theme.fonts.condensedBold}; | ||
| `; | ||
|
|
||
| const StyledExternalLink = styled(ExternalLink)` | ||
| color: ${(props) => props.theme.colors.white}; | ||
| text-decoration: underline; | ||
| &:hover { | ||
| text-decoration: underline; | ||
| } | ||
| `; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export * from './useLiquidation'; | ||
| export * from './useLiquidationOptimismSubgraph'; | ||
| export * from './LiquidationBanner'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { wei } from '@synthetixio/wei'; | ||
| import useSynthetixQueries from '@synthetixio/queries'; | ||
| import Connector from 'containers/Connector'; | ||
| import { useLiquidationOptimismSubgraph } from './useLiquidationOptimismSubgraph'; | ||
|
|
||
| export function useLiquidation() { | ||
| const { useGetLiquidationDataQuery, useGetDebtDataQuery } = useSynthetixQueries(); | ||
| const { walletAddress } = Connector.useContainer(); | ||
|
|
||
| const liquidationData = useGetLiquidationDataQuery(walletAddress); | ||
| const debtData = useGetDebtDataQuery(walletAddress); | ||
|
|
||
| const issuanceRatio = debtData?.data?.targetCRatio ?? wei(0); | ||
| const cRatio = debtData?.data?.currentCRatio ?? wei(0); | ||
| const liquidationDeadlineForAccount = | ||
| liquidationData?.data?.liquidationDeadlineForAccount ?? wei(0); | ||
|
|
||
| const ratio = issuanceRatio.eq(0) ? 0 : 100 / Number(issuanceRatio); | ||
|
|
||
| const optimismDeadline = useLiquidationOptimismSubgraph(); | ||
| const deadline = | ||
| optimismDeadline > 0 | ||
| ? optimismDeadline | ||
| : Number(liquidationDeadlineForAccount.toString()) * 1000; | ||
|
|
||
| const hasWarning = | ||
| optimismDeadline > 0 || (!liquidationDeadlineForAccount.eq(0) && cRatio.gt(issuanceRatio)); | ||
|
|
||
| return { | ||
| hasWarning, | ||
| ratio, | ||
| deadline, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { useQuery, UseQueryOptions } from 'react-query'; | ||
| import Connector from 'containers/Connector'; | ||
|
|
||
| function getEndpoint(networkName: String) { | ||
| switch (networkName) { | ||
| case 'kovan-ovm': | ||
| return 'https://api.thegraph.com/subgraphs/name/noisekit/liquidator-optimism-kovan'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add this subgraph to https://github.com/synthetixio/synthetix-subgraph/ instead. We should probably also remove the old liquidation graph: https://github.com/Synthetixio/synthetix-subgraph/blob/main/subgraphs/liquidations.js If it's added to that repo we can then pull/refresh the subgraph main.json file in the queries lib, and it will generate typed hooks for us . It's a bit more work. But also good for you to get to know that repo. And convenient when upgrades to the liquidation contract happens, good to have one place to remember to update
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem with existing repo is that it is not really set up well for testing and other things. The graph generates a different structure now and fitting it with the old way does not make much sense to me. I would rather keep it either in a separate repo or have another monorepo for subgraphs.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also when we do a deploy from that repo it deployes the subgraphs to all supported network (which very sson does not include kovan). And the hooks will use the correct network url in the hooks automatically from context.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's the part I would like to avoid. We already depend too much on abstractions that cannot be untied right now, trying to run all queries without relying on any of the existing libs and context that includes all the contracts. That's kind of the point of this POC
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You mean the liquidation graph? I think the current liquidation graph in unused.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yea I guess to would be nice to decouple it a little. But having hooks generated make sense to me, since with graphql you can ask for what ever parameters you need either way. Also, I guess decoupling it from the queries lib isn't really related to having the subgraph definition live in synthetix-subgraphs? That repo is essentially a monorepo for all our subgraphs already? And it has some smart things in the deployment script. Using the "Grafting" feature, deploying to all supported synthetix networks, deploying the mainnet graph to the decentralised network. Not saying it's perfect put there's quite a few useful features there.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are few useful features, yes, but we need to talk about it properly. They are heavily outdated now and I don't want to limit myself to an old version of the graph tooling. At the same time I cannot afford upgrading it to the modern standard now either. The only practical solution was to have subgraph created separately. It is a lot of context to learn already and increasing it with all the extra tooling written with old graph in mind - would be prohibitively hard. BIG NOTE here: this pr is a another thing is that we cannot just do full automated switching of networks as we have differences between contracts sometimes, and those contracts are not compatible. But that's whole another topic. I do not believe that full automation in a way we have it now in synthetix-subgraph is fully usable and covers all cases anymore. Contracts changed a lot, the graph tooling changed too.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea know it's a draft. Spark some good convos either way. And I do agree with your points.. I guess what's annoying is that we have some quite tricky bugs related to synth supply and SNXHolder that are more important than the liquidation subgraph. So the main benefit of you starting to look into the synthetix-subgraphs repo would be to help debugging those.. With that said, maybe the easiest way to resolve those bugs (in handleTransferSynth) is to start with a fresh subgraph...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yep 100%, this is a learning exercise first not a full on feature work. Would be great if it can be helpful in a more feature-practical sense, but this was not a main goal
Haha, literally was my plan I mentioned today over standup ;) I planned to give a quick try and if it does not help - dig into existing one harder. Will need to learn a lot of smart contract context for it either way, might as well keep it simple at first to reduce the learning surface. |
||
| case 'mainnet-ovm': | ||
| return 'https://api.thegraph.com/subgraphs/name/noisekit/liquidator-optimism'; | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| const gql = (data: any) => data[0]; | ||
| const query = gql` | ||
| query stakerEntity($id: String!) { | ||
| stakerEntity(id: $id) { | ||
| id | ||
| timestamp | ||
| status | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| export async function fetchLiquidationInfo( | ||
| walletAddress: string | null, | ||
| networkName: string | undefined | ||
| ) { | ||
| if (!walletAddress) { | ||
| return null; | ||
| } | ||
| if (!networkName) { | ||
| return null; | ||
| } | ||
| const endpoint = getEndpoint(networkName); | ||
| if (!endpoint) { | ||
| return null; | ||
| } | ||
|
|
||
| const body = await fetch(endpoint, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json', | ||
| }, | ||
| body: JSON.stringify({ | ||
| query, | ||
| variables: { | ||
| id: walletAddress, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| const { data, errors } = await body.json(); | ||
| if (errors?.[0]) { | ||
| throw new Error(errors?.[0]?.message || 'Unknown server error'); | ||
| } | ||
| return data?.stakerEntity; | ||
| } | ||
|
|
||
| export function useLiquidationOptimismInfo(queryOptions?: UseQueryOptions) { | ||
| const { walletAddress, network } = Connector.useContainer(); | ||
| return useQuery( | ||
| [walletAddress, network?.name], | ||
| () => fetchLiquidationInfo(walletAddress, network?.name), | ||
| { | ||
| enabled: Boolean(walletAddress && network?.name), | ||
| ...queryOptions, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| export function useLiquidationOptimismSubgraph(queryOptions?: UseQueryOptions) { | ||
| const { data, isSuccess } = useLiquidationOptimismInfo(queryOptions); | ||
| if (!isSuccess) { | ||
| return 0; | ||
| } | ||
| // @ts-ignore I give up | ||
|
||
| return data?.status === 'FLAGGED' ? parseInt(data.timestamp, 10) * 1000 : 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
liquidationDeadlineForAccountiswei. I guess you can directly doliq.toNumber() * 1000Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a copy of previous code, but yeah makes sense to avoid this