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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
170 changes: 119 additions & 51 deletions src/api/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ 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"

export type TradeType = {
name:
Expand Down Expand Up @@ -263,65 +263,133 @@ 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,
},
)
}

const omnipoolAddress =
"0x6d6f646c6f6d6e69706f6f6c0000000000000000000000000000000000000000"

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

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
}[]
}
}>(
squidUrl,
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,
//refetchInterval: millisecondsInMinute,
},
)
}
32 changes: 19 additions & 13 deletions src/sections/pools/PoolsPage.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -182,8 +188,6 @@ export const usePools = () => {
omnipoolAssets.data,
spotPrices.data,
tvls.data,
volumes.data,
volumes?.isLoading,
native.id,
fees.data,
fees?.isLoading,
Expand All @@ -194,6 +198,8 @@ export const usePools = () => {
isAllFarmsLoading,
stoppedFarmsBanner,
setWarnings,
volumes_,
isVolumeLoading,
])

return { data, isLoading: isInitialLoading }
Expand Down
5 changes: 5 additions & 0 deletions src/utils/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,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