-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change hook to read stake by hotkey from balance instead of chain
- Loading branch information
Showing
2 changed files
with
10 additions
and
74 deletions.
There are no files selected for viewing
82 changes: 9 additions & 73 deletions
82
apps/extension/src/ui/domains/Staking/hooks/bittensor/useGetBittensorStakeByHotKey.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters