Skip to content

Commit

Permalink
feat: change hook to read stake by hotkey from balance instead of chain
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKheops committed Dec 12, 2024
1 parent f431eaa commit df37902
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,83 +1,19 @@
import { useQueries, useQuery } from "@tanstack/react-query"
import { useMemo } from "react"

import { useScaleApi } from "@ui/hooks/sapi/useScaleApi"
import { useBalance } from "@ui/state"

type GetBittensorStakeByHotKey = {
address: string | null | undefined
hotkey: string | number | undefined | null
isEnabled?: boolean
}

type GetBittensorStakeByHotKeys = Omit<GetBittensorStakeByHotKey, "hotkey"> & {
hotkeys: (string | undefined)[]
}
type GetBittensorStakesByHotKeys = Omit<GetBittensorStakeByHotKey, "address" | "hotkey"> & {
addresses: (string | undefined)[]
hotkeys: (string[] | undefined)[]
}

export const useGetBittensorStakeByHotKey = ({
address,
hotkey,
isEnabled = true,
}: GetBittensorStakeByHotKey) => {
const { data: sapi } = useScaleApi("bittensor")
return useQuery({
queryKey: ["useGetBittensorStakeByHotKey", sapi?.id, address, hotkey],
queryFn: async () => {
return sapi?.getStorage<bigint>("SubtensorModule", "Stake", [hotkey, address])
},
enabled: isEnabled && !!sapi && !!address && !!hotkey,
})
}

export const useGetBittensorStakeByHotKeys = ({
address,
hotkeys,
isEnabled = true,
}: GetBittensorStakeByHotKeys) => {
const { data: sapi } = useScaleApi("bittensor")
return useQueries({
queries: hotkeys.map((hotkey) => ({
queryKey: ["useGetBittensorStakeByHotKey", sapi?.id, address, hotkey],
queryFn: () => sapi?.getStorage<bigint>("SubtensorModule", "Stake", [hotkey, address]),
enabled: isEnabled && !!sapi && !!address && !!hotkey,
})),
combine: (results) => {
return {
data: results.map((result) => result.data),
isPending: results.some((result) => result.isPending),
isLoading: results.some((result) => result.isLoading),
error: results.find((result) => result.isError),
}
},
})
}
export const useGetBittensorStakeByHotKey = ({ address, hotkey }: GetBittensorStakeByHotKey) => {
const balance = useBalance(hotkey ? address : null, "bittensor-substrate-native")

export const useGetBittensorStakesByHotKeys = ({
addresses,
hotkeys,
isEnabled = true,
}: GetBittensorStakesByHotKeys) => {
const { data: sapi } = useScaleApi("bittensor")
return useQueries({
queries: addresses
.map((address, index) => {
const hotkeysForAddress = hotkeys[index] ?? []
return hotkeysForAddress.map((hotkey) => ({
queryKey: ["useGetBittensorStakeByHotKey", sapi?.id, address, hotkey],
queryFn: () => sapi?.getStorage<bigint>("SubtensorModule", "Stake", [hotkey, address]),
enabled: isEnabled && !!sapi && !!address && !!hotkey,
}))
})
.flat(),
combine: (results) => {
return {
data: results.map((result) => result.data),
isPending: results.some((result) => result.isPending),
isLoading: results.some((result) => result.isLoading),
error: results.find((result) => result.isError),
}
},
})
return useMemo(() => {
if (!balance || !hotkey) return undefined
const value = balance?.subtensor.find((b) => (b.meta as { hotkey?: string })?.hotkey === hotkey)
return value?.amount.planck
}, [balance, hotkey])
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const useGetUnbondInfo = ({ sapi, chainId, address, unstakePoolId }: GetU
isEnabled: chainId !== "bittensor",
})

const { data: bittensorPlanks } = useGetBittensorStakeByHotKey({
const bittensorPlanks = useGetBittensorStakeByHotKey({
address,
hotkey: unstakePoolId,
})
Expand Down

0 comments on commit df37902

Please sign in to comment.