Skip to content
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

Omnipool volume from squid #1962

Merged
merged 8 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions src/api/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { normalizeId, undefinedNoop } from "utils/helpers"
import { QUERY_KEYS } from "utils/queryKeys"
import BN from "bignumber.js"
import { BN_0 } from "utils/constants"
import { PROVIDERS, useActiveProvider } from "./provider"
import { PROVIDERS, useActiveProvider, useSquidUrl } from "./provider"
import { u8aToHex } from "@polkadot/util"
import { decodeAddress, encodeAddress } from "@polkadot/util-crypto"
import { HYDRA_ADDRESS_PREFIX } from "utils/api"
Expand Down Expand Up @@ -258,12 +258,11 @@ const getVolumeDaily = async (assetId?: string) => {
return data
}

const squidUrl =
"https://galacticcouncil.squids.live/hydration-pools:prod/api/graphql"
const VOLUME_BLOCK_COUNT = 7200 //24 hours

export const useXYKSquidVolumes = (addresses: string[]) => {
const { api, isLoaded } = useRpcProvider()
const url = useSquidUrl()

return useQuery(
QUERY_KEYS.xykSquidVolumes(addresses),
Expand All @@ -287,7 +286,7 @@ export const useXYKSquidVolumes = (addresses: string[]) => {
}[]
}
}>(
squidUrl,
url,
gql`
query XykVolume(
$poolIds: [String!]!
Expand Down Expand Up @@ -330,3 +329,66 @@ export const useXYKSquidVolumes = (addresses: string[]) => {
},
)
}

const omnipoolAddress =
"0x6d6f646c6f6d6e69706f6f6c0000000000000000000000000000000000000000"

export const useOmnipoolVolumes = (ids: string[]) => {
const { api, isLoaded } = useRpcProvider()
const url = useSquidUrl()

return useQuery(
QUERY_KEYS.omnipoolSquidVolumes(ids),

async () => {
const endBlockNumber = (await api.derive.chain.bestNumber()).toNumber()
const omnipoolIds = ids.map((id) => `${omnipoolAddress}-${id}`)

const startBlockNumber = endBlockNumber - VOLUME_BLOCK_COUNT

const { omnipoolAssetHistoricalVolumesByPeriod } = await request<{
omnipoolAssetHistoricalVolumesByPeriod: {
nodes: {
assetId: number
assetVolume: string
}[]
}
}>(
url,
gql`
query OmnipoolVolume(
$omnipoolAssetIds: [String!]!
$startBlockNumber: Int!
$endBlockNumber: Int!
) {
omnipoolAssetHistoricalVolumesByPeriod(
filter: {
omnipoolAssetIds: $omnipoolAssetIds
startBlockNumber: $startBlockNumber
endBlockNumber: $endBlockNumber
}
) {
nodes {
assetId
assetVolume
}
}
}
`,
{ omnipoolAssetIds: omnipoolIds, startBlockNumber, endBlockNumber },
)

const { nodes = [] } = omnipoolAssetHistoricalVolumesByPeriod

return nodes.map((node) => ({
assetId: node.assetId.toString(),
assetVolume: node.assetVolume.toString(),
}))
},

{
enabled: isLoaded && !!ids.length,
staleTime: millisecondsInHour,
},
)
}
32 changes: 19 additions & 13 deletions src/sections/pools/PoolsPage.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { pool_account_name } from "@galacticcouncil/math-stableswap"
import { encodeAddress, blake2AsHex } from "@polkadot/util-crypto"
import { HYDRADX_SS58_PREFIX, XykMath } from "@galacticcouncil/sdk"
import { useOmnipoolPositionsData } from "sections/wallet/assets/hydraPositions/data/WalletAssetsHydraPositionsData.utils"
import { useVolume } from "api/volume"
import { useOmnipoolVolumes } from "api/volume"
import BN from "bignumber.js"
import { useXYKConsts, useXYKSDKPools } from "api/xyk"
import { useShareOfPools } from "api/pools"
Expand Down Expand Up @@ -79,10 +79,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

Expand All @@ -108,9 +110,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

Expand Down Expand Up @@ -145,12 +155,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,
Expand Down Expand Up @@ -180,8 +186,6 @@ export const usePools = () => {
omnipoolAssets.data,
spotPrices.data,
tvls.data,
volumes.data,
volumes?.isLoading,
native.id,
fees.data,
fees?.isLoading,
Expand All @@ -192,6 +196,8 @@ export const usePools = () => {
isAllFarmsLoading,
stoppedFarmsBanner,
setWarnings,
volumes,
isVolumeLoading,
])

return { data, isLoading: isInitialLoading }
Expand Down
25 changes: 15 additions & 10 deletions src/sections/stats/StatsPage.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BN_0, BN_NAN } from "utils/constants"
import { useDisplayAssetStore } from "utils/displayAsset"
import { isNotNil } from "utils/helpers"
import { useFee, useTVL } from "api/stats"
import { useVolume } from "api/volume"
import { useOmnipoolVolumes } from "api/volume"
import { useLiquidityPositionData } from "utils/omnipool"
import { useAssets } from "providers/assets"
import { useAccountAssets } from "api/deposits"
Expand All @@ -28,7 +28,9 @@ export const useOmnipoolAssetDetails = (sortBy: "tvl" | "pol") => {
const { data: allFarms, isLoading: isAllFarmsLoading } =
useOmnipoolFarms(omnipoolAssetsIds)

const volumes = useVolume("all")
const { data: volumes = [], isLoading: isVolumeLoading } =
useOmnipoolVolumes(omnipoolAssetsIds)

const tvls = useTVL("all")
const fees = useFee("all")

Expand Down Expand Up @@ -99,11 +101,14 @@ export const useOmnipoolAssetDetails = (sortBy: "tvl" | "pol") => {
?.tvl_usd ?? BN_NAN,
)

const volume = BN(
volumes?.data?.find(
(volume) => volume?.asset_id === Number(omnipoolAssetId),
)?.volume_usd ?? BN_NAN,
)
const volumeRaw = volumes?.find(
(volume) => volume.assetId === meta.id,
)?.assetVolume

const volume =
volumeRaw && spotPrice
? BN(volumeRaw).shiftedBy(-meta.decimals).multipliedBy(spotPrice)
: BN_NAN
const isLoadingFee = fees?.isInitialLoading || isAllFarmsLoading

const { totalApr, farms = [] } = allFarms?.get(omnipoolAsset.id) ?? {}
Expand Down Expand Up @@ -134,7 +139,7 @@ export const useOmnipoolAssetDetails = (sortBy: "tvl" | "pol") => {
totalFee,
farms,
isLoadingFee,
isLoadingVolume: volumes.isInitialLoading,
isLoadingVolume: isVolumeLoading,
}
})
return rows
Expand All @@ -148,8 +153,8 @@ export const useOmnipoolAssetDetails = (sortBy: "tvl" | "pol") => {
positions,
spotPrices,
tvls.data,
volumes?.data,
volumes.isInitialLoading,
volumes,
isVolumeLoading,
allFarms,
isAllFarmsLoading,
])
Expand Down
5 changes: 5 additions & 0 deletions src/utils/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ export const QUERY_KEYS = {
"xykSquidVolumes",
addresses.join(","),
],
omnipoolSquidVolumes: (ids: string[]) => [
QUERY_KEY_PREFIX,
"omnipoolSquidVolumes",
ids.join(","),
],
timestamp: (bestNumber: Maybe<u32 | BigNumber>) =>
bestNumber != null
? ["timestamp", bestNumber]
Expand Down
Loading