get vesting pledge info for mixnode

This commit is contained in:
fmtabbara
2022-03-04 13:00:50 +00:00
parent e6040b0ecc
commit b14f6ab159
+17 -10
View File
@@ -1,45 +1,52 @@
import { useCallback, useContext, useEffect, useState } from 'react'
import { ClientContext } from '../context/main'
import { checkGatewayOwnership, checkMixnodeOwnership } from '../requests'
import { EnumNodeType, TNodeOwnership } from '../types'
import { checkGatewayOwnership, checkMixnodeOwnership, getPledgeInfo } from '../requests'
import { EnumNodeType, TNodeOwnership, PledgeData } from '../types'
const initial = {
hasOwnership: false,
nodeType: undefined,
vestingPledge: undefined,
}
export const useCheckOwnership = () => {
const { clientDetails } = useContext(ClientContext)
const [ownership, setOwnership] = useState<TNodeOwnership>(initial)
const [isLoading, setIsLoading] = useState(false)
const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState<string>()
const checkOwnership = useCallback(async () => {
const status = {} as TNodeOwnership
setIsLoading(true)
try {
const ownsMixnode = await checkMixnodeOwnership()
const ownsGateway = await checkGatewayOwnership()
const [ownsMixnode, ownsGateway] = await Promise.all([checkMixnodeOwnership(), checkGatewayOwnership()])
if (ownsMixnode) {
status.hasOwnership = true
status.nodeType = EnumNodeType.mixnode
status.vestingPledge = await getPledgeInfo({
address: clientDetails?.client_address!,
type: EnumNodeType.mixnode,
})
}
if (ownsGateway) {
status.hasOwnership = true
status.nodeType = EnumNodeType.gateway
status.vestingPledge = await getPledgeInfo({
address: clientDetails?.client_address!,
type: EnumNodeType.gateway,
})
}
setOwnership(status)
} catch (e) {
setError(e as string)
setIsLoading(false)
setOwnership(initial)
} finally {
setIsLoading(false)
}
}, [])
}, [clientDetails])
useEffect(() => {
checkOwnership()