Skip to content

Commit

Permalink
push fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vkulinich-cl committed Dec 17, 2024
1 parent 9e68843 commit a62b6af
Showing 1 changed file with 60 additions and 54 deletions.
114 changes: 60 additions & 54 deletions src/api/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
},
Expand All @@ -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: {
Expand All @@ -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 {
Expand All @@ -369,7 +375,7 @@ export const useOmnipoolVolumes = (ids: string[]) => {
}
}
`,
{ omnipoolAssetIds: omnipoolIds, startBlockNumber },
{ omnipoolAssetIds: omnipoolIds, startBlockNumber, endBlockNumber },
)

const { nodes = [] } = omnipoolAssetHistoricalVolumesByPeriod
Expand Down

0 comments on commit a62b6af

Please sign in to comment.