1
1
import { useCallback } from 'react'
2
2
import { useWallet } from 'use-wallet'
3
3
import { Contract } from 'web3-eth-contract'
4
+ import { ethers } from 'ethers'
5
+ import { useDispatch } from 'react-redux'
6
+ import { updateUserAllowance , fetchFarmUserDataAsync } from 'state/actions'
7
+ import { getSushiContract , approve , getMasterChefContract , getSousChefContract } from 'sushi/utils'
8
+ import { getLotteryContract } from 'sushi/lotteryUtils'
4
9
import useSushi from './useSushi'
5
- import { getSushiContract , approve , getMasterChefContract , getSousChefContract } from '../sushi/utils'
6
- import { getLotteryContract } from '../sushi/lotteryUtils'
7
10
8
- const useApprove = ( lpContract : Contract ) => {
11
+ // Approve a Farm
12
+ export const useApprove = ( lpContract : Contract , pid : number ) => {
13
+ const dispatch = useDispatch ( )
9
14
const { account } : { account : string } = useWallet ( )
10
15
const sushi = useSushi ( )
11
16
const masterChefContract = getMasterChefContract ( sushi )
12
17
13
18
const handleApprove = useCallback ( async ( ) => {
14
19
try {
15
20
const tx = await approve ( lpContract , masterChefContract , account )
21
+ dispatch ( fetchFarmUserDataAsync ( pid , account ) )
16
22
return tx
17
23
} catch ( e ) {
18
24
return false
19
25
}
20
- } , [ account , lpContract , masterChefContract ] )
26
+ } , [ account , dispatch , lpContract , masterChefContract , pid ] )
21
27
22
28
return { onApprove : handleApprove }
23
29
}
24
30
31
+ // Approve a Pool
25
32
export const useSousApprove = ( lpContract : Contract , sousId ) => {
33
+ const dispatch = useDispatch ( )
26
34
const { account } : { account : string } = useWallet ( )
27
35
const sushi = useSushi ( )
28
36
const sousChefContract = getSousChefContract ( sushi , sousId )
29
37
30
38
const handleApprove = useCallback ( async ( ) => {
31
39
try {
32
40
const tx = await approve ( lpContract , sousChefContract , account )
41
+ dispatch ( updateUserAllowance ( sousId , account ) )
33
42
return tx
34
43
} catch ( e ) {
35
44
return false
36
45
}
37
- } , [ account , lpContract , sousChefContract ] )
46
+ } , [ account , dispatch , lpContract , sousChefContract , sousId ] )
38
47
39
48
return { onApprove : handleApprove }
40
49
}
41
50
51
+ // Approve the lottery
42
52
export const useLotteryApprove = ( ) => {
43
53
const { account } : { account : string } = useWallet ( )
44
54
const sushi = useSushi ( )
@@ -57,4 +67,19 @@ export const useLotteryApprove = () => {
57
67
return { onApprove : handleApprove }
58
68
}
59
69
60
- export default useApprove
70
+ // Approve an IFO
71
+ export const useIfoApprove = ( tokenContract : Contract , spenderAddress : string ) => {
72
+ const { account } = useWallet ( )
73
+ const onApprove = useCallback ( async ( ) => {
74
+ try {
75
+ const tx = await tokenContract . methods
76
+ . approve ( spenderAddress , ethers . constants . MaxUint256 )
77
+ . send ( { from : account } )
78
+ return tx
79
+ } catch {
80
+ return false
81
+ }
82
+ } , [ account , spenderAddress , tokenContract ] )
83
+
84
+ return onApprove
85
+ }
0 commit comments