-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Peter/fix vault dashboard volumes hook (#1557)
* chore: update monetary to latest 0.7.3 * fix: show all collateral currencies on locked collateral card
- Loading branch information
1 parent
d21ddf6
commit e9414ef
Showing
3 changed files
with
119 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { TickerToData } from '@interlay/interbtc-api'; | ||
import { useQuery, UseQueryResult } from 'react-query'; | ||
|
||
import cumulativeVaultCollateralVolumesFetcher, { | ||
CUMULATIVE_VAULT_COLLATERALVOLUMES_FETCHER | ||
} from '@/services/fetchers/cumulative-vault-collateral-volumes-fetcher'; | ||
import { VolumeDataPoint } from '@/services/fetchers/cumulative-volumes-fetcher'; | ||
|
||
import { useGetCollateralCurrencies } from './api/use-get-collateral-currencies'; | ||
|
||
const useAllCumulativeVaultCollateralVolumes = ( | ||
cutoffTimestamps: Array<Date> | ||
): UseQueryResult<TickerToData<Array<VolumeDataPoint>>, Error> => { | ||
const { data: collateralCurrencies } = useGetCollateralCurrencies(true); | ||
|
||
return useQuery<TickerToData<Array<VolumeDataPoint>>, Error>( | ||
[CUMULATIVE_VAULT_COLLATERALVOLUMES_FETCHER, cutoffTimestamps, collateralCurrencies], | ||
cumulativeVaultCollateralVolumesFetcher, | ||
{ | ||
enabled: !!collateralCurrencies | ||
} | ||
); | ||
}; | ||
|
||
export default useAllCumulativeVaultCollateralVolumes; |
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
80 changes: 80 additions & 0 deletions
80
src/services/fetchers/cumulative-vault-collateral-volumes-fetcher.ts
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,80 @@ | ||
import { CollateralCurrencyExt, CurrencyExt, newMonetaryAmount, TickerToData } from '@interlay/interbtc-api'; | ||
|
||
import { WRAPPED_TOKEN } from '@/config/relay-chains'; | ||
import graphqlFetcher, { GRAPHQL_FETCHER } from '@/services/fetchers/graphql-fetcher'; | ||
import { getCurrencyEqualityCondition } from '@/utils/helpers/currencies'; | ||
|
||
import { VolumeDataPoint } from './cumulative-volumes-fetcher'; | ||
|
||
const CUMULATIVE_VAULT_COLLATERALVOLUMES_FETCHER = 'cumulative-vault-collateral-volumes-fetcher'; | ||
|
||
const cumulativeVaultCollateralVolumesFetcher = async ( | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
{ queryKey }: any | ||
): Promise<TickerToData<Array<VolumeDataPoint>>> => { | ||
const [key, cutoffTimestamps, collateralCurrencies] = queryKey as CumulativeVaultCollateralVolumesFetcherParams; | ||
|
||
if (key !== CUMULATIVE_VAULT_COLLATERALVOLUMES_FETCHER) throw new Error('Invalid key!'); | ||
|
||
const queryFragment = (date: Date, collateralCurrency: CurrencyExt) => { | ||
const where = `{ | ||
tillTimestamp_lte: "${date.toISOString()}", | ||
type_eq: Collateral, | ||
${`collateralCurrency: { | ||
${getCurrencyEqualityCondition(collateralCurrency)}}`}, | ||
${`wrappedCurrency: { | ||
${getCurrencyEqualityCondition(WRAPPED_TOKEN)}}`}, | ||
}`; | ||
|
||
return ` | ||
ts${date.getTime()}_${ | ||
collateralCurrency?.ticker | ||
}: cumulativeVolumePerCurrencyPairs (where: ${where}, orderBy: tillTimestamp_DESC, limit: 1) { | ||
amount | ||
tillTimestamp | ||
} | ||
`; | ||
}; | ||
|
||
const query = ` | ||
{ | ||
${cutoffTimestamps.map((date) => collateralCurrencies.map((currency) => queryFragment(date, currency)))} | ||
} | ||
`; | ||
|
||
// TODO: should type properly (`Relay`) | ||
const volumesData = await graphqlFetcher<Array<any>>()({ | ||
queryKey: [GRAPHQL_FETCHER, query] | ||
}); | ||
|
||
const volumes = volumesData?.data || {}; | ||
|
||
return Object.entries(volumes).reduce((result, [key, [volumeData]]) => { | ||
const [rawTimestamp, ticker] = key.split('_'); | ||
const timestamp = rawTimestamp.slice(2); | ||
const currency = collateralCurrencies.find((collateralCurrency) => collateralCurrency.ticker === ticker); | ||
if (currency === undefined) { | ||
throw new Error('Ivalid query.'); | ||
} | ||
return { | ||
...result, | ||
[ticker]: [ | ||
...(result[ticker] || []), | ||
{ | ||
amount: newMonetaryAmount(volumeData.amount || 0, currency), | ||
tillTimestamp: cutoffTimestamps.find((cutoffTimestamp) => cutoffTimestamp.getTime().toString() === timestamp) | ||
} | ||
] | ||
}; | ||
}, {} as any); | ||
}; | ||
|
||
type CumulativeVaultCollateralVolumesFetcherParams = [ | ||
queryKey: string, | ||
cutoffTimestamp: Array<Date>, | ||
collateralCurrency: Array<CollateralCurrencyExt> | ||
]; | ||
|
||
export { CUMULATIVE_VAULT_COLLATERALVOLUMES_FETCHER }; | ||
|
||
export default cumulativeVaultCollateralVolumesFetcher; |
e9414ef
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.
Successfully deployed to the following URLs:
interbtc-ui-kintsugi-testnet – ./
interbtc-ui-kintsugi-testnet-interlay.vercel.app
interbtc-ui.vercel.app
interbtc-ui-kintsugi-testnet-git-master-interlay.vercel.app
kintnet.interlay.io
e9414ef
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.
Successfully deployed to the following URLs:
interbtc-ui-interlay-testnet – ./
interbtc-ui-interlay-testnet-interlay.vercel.app
interbtc-ui-interlay-testnet.vercel.app
interbtc-ui-interlay-testnet-git-master-interlay.vercel.app
testnet.interlay.io