From 69707559438c971e665ae6bfc7a956b6bac4b72a Mon Sep 17 00:00:00 2001 From: vkulinich Date: Mon, 16 Dec 2024 10:25:44 +0100 Subject: [PATCH 1/3] Omnipool volume from squid --- src/api/volume.ts | 61 +++++++++++++++++++++++++++ src/sections/pools/PoolsPage.utils.ts | 32 ++++++++------ src/utils/queryKeys.ts | 5 +++ 3 files changed, 85 insertions(+), 13 deletions(-) diff --git a/src/api/volume.ts b/src/api/volume.ts index 5318b4ae3..0b76cc005 100644 --- a/src/api/volume.ts +++ b/src/api/volume.ts @@ -325,3 +325,64 @@ export const useXYKSquidVolumes = (addresses: string[]) => { }, ) } + +const omnipoolAddress = + "0x6d6f646c6f6d6e69706f6f6c0000000000000000000000000000000000000000" + +export const useOmnipoolVolumes = (ids: string[]) => { + const { data: bestNumber } = useBestNumber() + + return useQuery( + QUERY_KEYS.omnipoolSquidVolumes(ids), + bestNumber + ? async () => { + const omnipoolIds = ids.map((id) => `${omnipoolAddress}-${id}`) + + const startBlockNumber = + bestNumber.parachainBlockNumber.toNumber() - VOLUME_BLOCK_COUNT + + const { omnipoolAssetHistoricalVolumesByPeriod } = await request<{ + omnipoolAssetHistoricalVolumesByPeriod: { + nodes: { + assetId: number + assetVolume: string + }[] + } + }>( + squidUrl, + gql` + query OmnipoolVolume( + $omnipoolAssetIds: [String!]! + $startBlockNumber: Int! + ) { + omnipoolAssetHistoricalVolumesByPeriod( + filter: { + omnipoolAssetIds: $omnipoolAssetIds + startBlockNumber: $startBlockNumber + } + ) { + nodes { + assetId + assetVolume + } + } + } + `, + { omnipoolAssetIds: omnipoolIds, startBlockNumber }, + ) + + const { nodes = [] } = omnipoolAssetHistoricalVolumesByPeriod + + return nodes.map((node) => ({ + assetId: node.assetId.toString(), + assetVolume: node.assetVolume.toString(), + })) + } + : undefinedNoop, + { + enabled: !!bestNumber && !!ids.length, + staleTime: millisecondsInHour, + //refetchInterval: millisecondsInMinute, + }, + ) +} diff --git a/src/sections/pools/PoolsPage.utils.ts b/src/sections/pools/PoolsPage.utils.ts index 72349f35e..c6437deb1 100644 --- a/src/sections/pools/PoolsPage.utils.ts +++ b/src/sections/pools/PoolsPage.utils.ts @@ -15,7 +15,7 @@ import { encodeAddress, blake2AsHex } from "@polkadot/util-crypto" import { HYDRADX_SS58_PREFIX, XykMath } from "@galacticcouncil/sdk" import { useAccountBalances } from "api/accountBalances" import { useOmnipoolPositionsData } from "sections/wallet/assets/hydraPositions/data/WalletAssetsHydraPositionsData.utils" -import { useVolume } from "api/volume" +import { useOmnipoolVolumes, useVolume } from "api/volume" import BN from "bignumber.js" import { useXYKConsts } from "api/xyk" import { useShareOfPools } from "api/pools" @@ -81,10 +81,12 @@ export const usePools = () => { stableCoinId ? [...assetsId, stableCoinId] : assetsId, ) - const volumes = useVolume("all") const fees = useFee("all") const tvls = useTVL("all") + const { data: volumes_, isLoading: isVolumeLoading } = + useOmnipoolVolumes(assetsId) + const isInitialLoading = spotPrices.isInitialLoading || omnipoolAssets.isLoading @@ -110,9 +112,17 @@ export const usePools = () => { BN_NAN, ).multipliedBy(apiSpotPrice ?? 1) - const volume = volumes.data?.find( - (volume) => volume?.asset_id?.toString() === asset.id, - )?.volume_usd + const volumeRaw = volumes_?.find( + (volume) => volume.assetId === asset.id, + )?.assetVolume + + const volume = + volumeRaw && spotPrice + ? BN(volumeRaw) + .shiftedBy(-meta.decimals) + .multipliedBy(spotPrice) + .toString() + : undefined const isFeeLoading = fees?.isLoading || isAllFarmsLoading @@ -147,12 +157,8 @@ export const usePools = () => { spotPrice: spotPrice?.isNaN() ? undefined : spotPrice?.toFixed(6), canAddLiquidity: tradability.canAddLiquidity, canRemoveLiquidity: tradability.canRemoveLiquidity, - volume: volume - ? BN(volume) - .multipliedBy(apiSpotPrice ?? 1) - .toFixed(3) - : undefined, - isVolumeLoading: volumes?.isLoading, + volume, + isVolumeLoading: isVolumeLoading, farms: farms.filter((farm) => farm.isActive && BN(farm.apr).gt(0)), allFarms: farms.filter((farm) => farm.isActive ? BN(farm.apr).gt(0) : true, @@ -182,8 +188,6 @@ export const usePools = () => { omnipoolAssets.data, spotPrices.data, tvls.data, - volumes.data, - volumes?.isLoading, native.id, fees.data, fees?.isLoading, @@ -194,6 +198,8 @@ export const usePools = () => { isAllFarmsLoading, stoppedFarmsBanner, setWarnings, + volumes_, + isVolumeLoading, ]) return { data, isLoading: isInitialLoading } diff --git a/src/utils/queryKeys.ts b/src/utils/queryKeys.ts index 276b2632e..805566369 100644 --- a/src/utils/queryKeys.ts +++ b/src/utils/queryKeys.ts @@ -166,6 +166,11 @@ export const QUERY_KEYS = { "xykSquidVolumes", addresses.join(","), ], + omnipoolSquidVolumes: (ids: string[]) => [ + QUERY_KEY_PREFIX, + "omnipoolSquidVolumes", + ids.join(","), + ], timestamp: (bestNumber: Maybe) => bestNumber != null ? ["timestamp", bestNumber] From 9e6884378769cae9c5282370db26436c1fa11c29 Mon Sep 17 00:00:00 2001 From: vkulinich Date: Mon, 16 Dec 2024 10:41:39 +0100 Subject: [PATCH 2/3] wip --- src/api/volume.ts | 85 ++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/src/api/volume.ts b/src/api/volume.ts index 0b76cc005..61f15229a 100644 --- a/src/api/volume.ts +++ b/src/api/volume.ts @@ -11,6 +11,7 @@ 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" export type TradeType = { name: @@ -330,57 +331,57 @@ const omnipoolAddress = "0x6d6f646c6f6d6e69706f6f6c0000000000000000000000000000000000000000" export const useOmnipoolVolumes = (ids: string[]) => { - const { data: bestNumber } = useBestNumber() + const { api, isLoaded } = useRpcProvider() return useQuery( QUERY_KEYS.omnipoolSquidVolumes(ids), - bestNumber - ? async () => { - const omnipoolIds = ids.map((id) => `${omnipoolAddress}-${id}`) - const startBlockNumber = - bestNumber.parachainBlockNumber.toNumber() - VOLUME_BLOCK_COUNT + async () => { + const blockNumber = (await api.derive.chain.bestNumber()).toNumber() + const omnipoolIds = ids.map((id) => `${omnipoolAddress}-${id}`) - const { omnipoolAssetHistoricalVolumesByPeriod } = await request<{ - omnipoolAssetHistoricalVolumesByPeriod: { - nodes: { - assetId: number - assetVolume: string - }[] - } - }>( - squidUrl, - gql` - query OmnipoolVolume( - $omnipoolAssetIds: [String!]! - $startBlockNumber: Int! - ) { - omnipoolAssetHistoricalVolumesByPeriod( - filter: { - omnipoolAssetIds: $omnipoolAssetIds - startBlockNumber: $startBlockNumber - } - ) { - nodes { - assetId - assetVolume - } - } + const startBlockNumber = blockNumber - VOLUME_BLOCK_COUNT + + const { omnipoolAssetHistoricalVolumesByPeriod } = await request<{ + omnipoolAssetHistoricalVolumesByPeriod: { + nodes: { + assetId: number + assetVolume: string + }[] + } + }>( + squidUrl, + gql` + query OmnipoolVolume( + $omnipoolAssetIds: [String!]! + $startBlockNumber: Int! + ) { + omnipoolAssetHistoricalVolumesByPeriod( + filter: { + omnipoolAssetIds: $omnipoolAssetIds + startBlockNumber: $startBlockNumber } - `, - { omnipoolAssetIds: omnipoolIds, startBlockNumber }, - ) + ) { + nodes { + assetId + assetVolume + } + } + } + `, + { omnipoolAssetIds: omnipoolIds, startBlockNumber }, + ) - const { nodes = [] } = omnipoolAssetHistoricalVolumesByPeriod + const { nodes = [] } = omnipoolAssetHistoricalVolumesByPeriod + + return nodes.map((node) => ({ + assetId: node.assetId.toString(), + assetVolume: node.assetVolume.toString(), + })) + }, - return nodes.map((node) => ({ - assetId: node.assetId.toString(), - assetVolume: node.assetVolume.toString(), - })) - } - : undefinedNoop, { - enabled: !!bestNumber && !!ids.length, + enabled: isLoaded && !!ids.length, staleTime: millisecondsInHour, //refetchInterval: millisecondsInMinute, }, From a62b6af81be71aba7c44d2602f0091f9a739c829 Mon Sep 17 00:00:00 2001 From: vkulinich Date: Tue, 17 Dec 2024 10:27:37 +0100 Subject: [PATCH 3/3] push fix --- src/api/volume.ts | 114 ++++++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 54 deletions(-) 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