diff --git a/src/api/volume.ts b/src/api/volume.ts index 61f15229a..3984d7776 100644 --- a/src/api/volume.ts +++ b/src/api/volume.ts @@ -9,7 +9,6 @@ import { PROVIDERS, useActiveProvider } from "./provider" import { u8aToHex } from "@polkadot/util" import { decodeAddress, encodeAddress } from "@polkadot/util-crypto" import { HYDRA_ADDRESS_PREFIX } from "utils/api" -import { useBestNumber } from "./chain" import { millisecondsInHour, millisecondsInMinute } from "date-fns/constants" import { useRpcProvider } from "providers/rpcProvider" @@ -264,63 +263,68 @@ const squidUrl = const VOLUME_BLOCK_COUNT = 7200 //24 hours export const useXYKSquidVolumes = (addresses: string[]) => { - const { data: bestNumber } = useBestNumber() + const { api, isLoaded } = useRpcProvider() return useQuery( QUERY_KEYS.xykSquidVolumes(addresses), - bestNumber - ? async () => { - const hexAddresses = addresses.map((address) => - u8aToHex(decodeAddress(address)), - ) - const startBlockNumber = - bestNumber.parachainBlockNumber.toNumber() - VOLUME_BLOCK_COUNT - - const { xykPoolHistoricalVolumesByPeriod } = await request<{ - xykPoolHistoricalVolumesByPeriod: { - nodes: { - poolId: string - assetAId: number - assetAVolume: string - assetBId: number - assetBVolume: string - }[] - } - }>( - squidUrl, - gql` - query XykVolume($poolIds: [String!]!, $startBlockNumber: Int!) { - xykPoolHistoricalVolumesByPeriod( - filter: { - poolIds: $poolIds - startBlockNumber: $startBlockNumber - } - ) { - nodes { - poolId - assetAId - assetAVolume - assetBId - assetBVolume - } - } - } - `, - { poolIds: hexAddresses, startBlockNumber }, - ) - const { nodes = [] } = xykPoolHistoricalVolumesByPeriod + async () => { + const hexAddresses = addresses.map((address) => + u8aToHex(decodeAddress(address)), + ) + + const endBlockNumber = (await api.derive.chain.bestNumber()).toNumber() + const startBlockNumber = endBlockNumber - VOLUME_BLOCK_COUNT - return nodes.map((node) => ({ - poolId: encodeAddress(node.poolId, HYDRA_ADDRESS_PREFIX), - assetId: node.assetAId.toString(), - assetIdB: node.assetBId.toString(), - volume: node.assetAVolume, - })) + const { xykPoolHistoricalVolumesByPeriod } = await request<{ + xykPoolHistoricalVolumesByPeriod: { + nodes: { + poolId: string + assetAId: number + assetAVolume: string + assetBId: number + assetBVolume: string + }[] } - : undefinedNoop, + }>( + squidUrl, + gql` + query XykVolume( + $poolIds: [String!]! + $startBlockNumber: Int! + $endBlockNumber: Int! + ) { + xykPoolHistoricalVolumesByPeriod( + filter: { + poolIds: $poolIds + startBlockNumber: $startBlockNumber + endBlockNumber: $endBlockNumber + } + ) { + nodes { + poolId + assetAId + assetAVolume + assetBId + assetBVolume + } + } + } + `, + { poolIds: hexAddresses, startBlockNumber, endBlockNumber }, + ) + + const { nodes = [] } = xykPoolHistoricalVolumesByPeriod + + return nodes.map((node) => ({ + poolId: encodeAddress(node.poolId, HYDRA_ADDRESS_PREFIX), + assetId: node.assetAId.toString(), + assetIdB: node.assetBId.toString(), + volume: node.assetAVolume, + })) + }, { - enabled: !!bestNumber && !!addresses.length, + enabled: isLoaded && !!addresses.length, staleTime: millisecondsInHour, refetchInterval: millisecondsInMinute, }, @@ -337,10 +341,10 @@ export const useOmnipoolVolumes = (ids: string[]) => { QUERY_KEYS.omnipoolSquidVolumes(ids), async () => { - const blockNumber = (await api.derive.chain.bestNumber()).toNumber() + const endBlockNumber = (await api.derive.chain.bestNumber()).toNumber() const omnipoolIds = ids.map((id) => `${omnipoolAddress}-${id}`) - const startBlockNumber = blockNumber - VOLUME_BLOCK_COUNT + const startBlockNumber = endBlockNumber - VOLUME_BLOCK_COUNT const { omnipoolAssetHistoricalVolumesByPeriod } = await request<{ omnipoolAssetHistoricalVolumesByPeriod: { @@ -355,11 +359,13 @@ export const useOmnipoolVolumes = (ids: string[]) => { query OmnipoolVolume( $omnipoolAssetIds: [String!]! $startBlockNumber: Int! + $endBlockNumber: Int! ) { omnipoolAssetHistoricalVolumesByPeriod( filter: { omnipoolAssetIds: $omnipoolAssetIds startBlockNumber: $startBlockNumber + endBlockNumber: $endBlockNumber } ) { nodes { @@ -369,7 +375,7 @@ export const useOmnipoolVolumes = (ids: string[]) => { } } `, - { omnipoolAssetIds: omnipoolIds, startBlockNumber }, + { omnipoolAssetIds: omnipoolIds, startBlockNumber, endBlockNumber }, ) const { nodes = [] } = omnipoolAssetHistoricalVolumesByPeriod