forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
useMembership.ts
75 lines (68 loc) · 1.96 KB
/
useMembership.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { useWallet } from '@noahsaso/cosmodal'
import { DaoCoreV2Selectors } from '@dao-dao/state'
import { useCachedLoadable } from '@dao-dao/stateless'
interface UseMembershipOptions {
coreAddress: string
chainId?: string
blockHeight?: number
}
interface UseMembershipResponse {
loading: boolean
isMember: boolean | undefined
walletVotingWeight: number | undefined
totalVotingWeight: number | undefined
}
export const useMembership = ({
coreAddress,
chainId,
blockHeight,
}: UseMembershipOptions): UseMembershipResponse => {
const { address: walletAddress } = useWallet()
// Use loadable to prevent flickering loading states when wallet address
// changes and on initial load if wallet is connecting.
const _walletVotingWeight = useCachedLoadable(
walletAddress
? DaoCoreV2Selectors.votingPowerAtHeightSelector({
contractAddress: coreAddress,
chainId,
params: [
{
address: walletAddress,
height: blockHeight,
},
],
})
: undefined
)
const _totalVotingWeight = useCachedLoadable(
DaoCoreV2Selectors.totalPowerAtHeightSelector({
contractAddress: coreAddress,
chainId,
params: [
{
height: blockHeight,
},
],
})
)
const walletVotingWeight =
_walletVotingWeight.state === 'hasValue' &&
!isNaN(Number(_walletVotingWeight.contents.power))
? Number(_walletVotingWeight.contents.power)
: undefined
const totalVotingWeight =
_totalVotingWeight.state === 'hasValue' &&
!isNaN(Number(_totalVotingWeight.contents.power))
? Number(_totalVotingWeight.contents.power)
: undefined
const isMember =
walletVotingWeight !== undefined ? walletVotingWeight > 0 : undefined
return {
isMember,
walletVotingWeight,
totalVotingWeight,
loading:
_walletVotingWeight.state === 'loading' ||
_totalVotingWeight.state === 'loading',
}
}