-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathStakePoolInfos.tsx
117 lines (101 loc) · 3.41 KB
/
StakePoolInfos.tsx
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {useTheme} from '@yoroi/theme'
import {Balance, Wallet} from '@yoroi/types'
import BigNumber from 'bignumber.js'
import React from 'react'
import {ActivityIndicator, StyleSheet, View} from 'react-native'
import {useQuery, useQueryClient, UseQueryOptions} from 'react-query'
import {useSelectedWallet} from '../../features/WalletManager/common/hooks/useSelectedWallet'
import {YoroiWallet} from '../../yoroi-wallets/cardano/types'
import {StakingInfo} from '../../yoroi-wallets/types/staking'
import {YoroiUnsignedTx} from '../../yoroi-wallets/types/yoroi'
import {Quantities} from '../../yoroi-wallets/utils/utils'
import {StakePoolInfo} from './StakePoolInfo'
export const StakePoolInfos = () => {
const {wallet} = useSelectedWallet()
const {stakePoolIds, isLoading} = useStakePoolIds(wallet)
const {isDark} = useTheme()
return stakePoolIds != null ? (
<View>
{stakePoolIds.map((stakePoolId) => (
<StakePoolInfo key={stakePoolId} stakePoolId={stakePoolId} />
))}
</View>
) : isLoading ? (
<View style={styles.activityIndicator}>
<ActivityIndicator size="large" color={isDark ? 'white' : 'black'} />
</View>
) : null
}
const styles = StyleSheet.create({
activityIndicator: {
paddingVertical: 32,
},
})
export const usePrefetchStakingInfo = (wallet: YoroiWallet) => {
const queryClient = useQueryClient()
return () =>
queryClient.prefetchQuery({
queryKey: [wallet.id, 'stakingInfo'],
queryFn: () => wallet.getStakingInfo(),
})
}
export const useStakingInfo = (
wallet: YoroiWallet,
options?: UseQueryOptions<StakingInfo, Error, StakingInfo, [string, 'stakingInfo']>,
) => {
const query = useQuery({
...options,
retry: false,
queryKey: [wallet.id, 'stakingInfo'],
queryFn: () => wallet.getStakingInfo(),
})
React.useEffect(() => {
const unsubscribe = wallet.subscribe(({type}) => type === 'utxos' && query.refetch())
return () => unsubscribe?.()
}, [query, wallet])
return {
stakingInfo: query.data,
...query,
}
}
const useStakePoolIds = (
wallet: YoroiWallet,
options?: UseQueryOptions<StakingInfo, Error, StakingInfo, [string, 'stakingInfo']>,
) => {
const {stakingInfo, ...query} = useStakingInfo(wallet, options)
return {
...query,
stakePoolIds: stakingInfo?.status === 'staked' ? [stakingInfo.poolId] : [],
}
}
export const useStakingTx = (
{wallet, meta, poolId}: {wallet: YoroiWallet; poolId?: string; meta: Wallet.Meta},
options: UseQueryOptions<YoroiUnsignedTx, Error, YoroiUnsignedTx, [string, 'stakingTx']>,
) => {
const query = useQuery({
...options,
retry: false,
queryKey: [wallet.id, 'stakingTx'],
queryFn: async () => {
if (poolId == null) throw new Error('invalid state')
const accountStates = await wallet.fetchAccountState()
const accountState = accountStates[wallet.rewardAddressHex]
if (!accountState) throw new Error('Account state not found')
const stakingUtxos = await wallet.getAllUtxosForKey()
const amountToDelegate = Quantities.sum([
...stakingUtxos.map((utxo) => utxo.amount as Balance.Quantity),
accountState.remainingAmount as Balance.Quantity,
])
return wallet.createDelegationTx({
poolId,
delegatedAmount: new BigNumber(amountToDelegate),
addressMode: meta.addressMode,
})
},
enabled: poolId != null,
})
return {
...query,
stakingTx: query.data,
}
}