Skip to content

Commit b5ab142

Browse files
authored
Pools user redux (pancakeswap#206)
* feat: Add user data to redux * feat: Add a refresh context * chore: Clean hooks
1 parent 388d696 commit b5ab142

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+439
-419
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"no-console": ["warn", { "allow": ["info", "warn", "error"] }],
1717
"no-plusplus": 0,
1818
"prefer-destructuring": ["warn", { "object": true, "array": false }],
19+
"no-underscore-dangle": 0,
1920
// Start temporary rules
2021
// These rules are here just to keep the lint error to 0 during the migration to the new rule set
2122
// They need to be removed and fixed as soon as possible

src/App.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect } from 'react'
22
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
33
import { useWallet } from 'use-wallet'
44
import { ResetCSS, Footer } from '@pancakeswap-libs/uikit'
5-
import useStateInit from 'state/hooks'
5+
import { useFetchPublicData } from 'state/hooks'
66
import GlobalStyle from './style/Global'
77
import Menu from './components/Menu'
88
import Farms from './views/Farms'
@@ -24,7 +24,7 @@ const App: React.FC = () => {
2424
}
2525
}, [account, connect])
2626

27-
useStateInit()
27+
useFetchPublicData()
2828

2929
return (
3030
<Router>

src/Providers.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import SushiProvider from 'contexts/SushiProvider'
77
import { LanguageContextProvider } from 'contexts/Localisation/languageContext'
88
import { ThemeContextProvider } from 'contexts/ThemeContext'
99
import { BlockContextProvider } from 'contexts/BlockContext'
10+
import { RefreshContextProvider } from 'contexts/RefreshContext'
1011
import store from 'state'
1112

1213
const Providers: React.FC = ({ children }) => {
@@ -24,7 +25,9 @@ const Providers: React.FC = ({ children }) => {
2425
>
2526
<BlockContextProvider>
2627
<SushiProvider>
27-
<ModalProvider>{children}</ModalProvider>
28+
<RefreshContextProvider>
29+
<ModalProvider>{children}</ModalProvider>
30+
</RefreshContextProvider>
2831
</SushiProvider>
2932
</BlockContextProvider>
3033
</UseWalletProvider>

src/contexts/BlockContext.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const BlockContextProvider = ({ children }) => {
1515
previousBlock.current = blockNumber
1616
setBlock(blockNumber)
1717
}
18-
}, 5000)
18+
}, 6000)
1919

2020
return () => clearInterval(interval)
2121
}, [])

src/contexts/RefreshContext.tsx

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React, { useState, useEffect } from 'react'
2+
3+
const FAST_INTERVAL = 10000
4+
const SLOW_INTERVAL = 60000
5+
6+
const RefreshContext = React.createContext({ slow: 0, fast: 0 })
7+
8+
// This context maintain 2 counters that can be used as a dependencies on other hooks to force a periodic refresh
9+
const RefreshContextProvider = ({ children }) => {
10+
const [slow, setSlow] = useState(0)
11+
const [fast, setFast] = useState(0)
12+
13+
useEffect(() => {
14+
const interval = setInterval(async () => {
15+
setFast((prev) => prev + 1)
16+
}, FAST_INTERVAL)
17+
return () => clearInterval(interval)
18+
}, [])
19+
20+
useEffect(() => {
21+
const interval = setInterval(async () => {
22+
setSlow((prev) => prev + 1)
23+
}, SLOW_INTERVAL)
24+
return () => clearInterval(interval)
25+
}, [])
26+
27+
return <RefreshContext.Provider value={{ slow, fast }}>{children}</RefreshContext.Provider>
28+
}
29+
30+
export { RefreshContext, RefreshContextProvider }

src/hooks/rework/useAllowance.ts

-25
This file was deleted.

src/hooks/rework/useApprove.ts

-22
This file was deleted.

src/hooks/rework/useBnbBalance.ts

-32
This file was deleted.

src/hooks/useAllEarnings.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import multicall from 'utils/multicall'
44
import masterChefABI from 'sushi/lib/abi/masterchef.json'
55
import addresses from 'sushi/lib/constants/contracts'
66
import { farmsConfig } from 'sushi/lib/constants'
7-
import useBlock from './useBlock'
7+
import useRefresh from './useRefresh'
88

99
const useAllEarnings = () => {
1010
const [balances, setBalance] = useState([])
1111
const { account }: { account: string } = useWallet()
12-
const block = useBlock()
12+
const { fastRefresh } = useRefresh()
1313

1414
useEffect(() => {
1515
const fetchAllBalances = async () => {
1616
const calls = farmsConfig.map((farm) => ({
17-
address: addresses.masterChef[56],
17+
address: addresses.masterChef[process.env.REACT_APP_CHAIN_ID],
1818
name: 'pendingCake',
1919
params: [farm.pid, account],
2020
}))
@@ -27,7 +27,7 @@ const useAllEarnings = () => {
2727
if (account) {
2828
fetchAllBalances()
2929
}
30-
}, [account, block])
30+
}, [account, fastRefresh])
3131

3232
return balances
3333
}

src/hooks/useAllowance.ts

+20-21
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,49 @@ import { useWallet } from 'use-wallet'
44
import { Contract } from 'web3-eth-contract'
55
import useSushi from './useSushi'
66
import { getAllowance } from '../utils/erc20'
7-
import { getSushiContract, getSousChefContract } from '../sushi/utils'
7+
import { getSushiContract } from '../sushi/utils'
88
import { getLotteryContract } from '../sushi/lotteryUtils'
99

10-
export const useSousAllowance = (lpContract: Contract, sousId) => {
10+
// Retrieve lottery allowance
11+
export const useLotteryAllowance = () => {
1112
const [allowance, setAllowance] = useState(new BigNumber(0))
1213
const { account }: { account: string } = useWallet()
1314
const sushi = useSushi()
14-
const sousChefContract = getSousChefContract(sushi, sousId)
15+
const lotteryContract = getLotteryContract(sushi)
16+
const cakeContract = getSushiContract(sushi)
1517

1618
useEffect(() => {
1719
const fetchAllowance = async () => {
18-
const res = await getAllowance(lpContract, sousChefContract, account)
20+
const res = await getAllowance(cakeContract, lotteryContract, account)
1921
setAllowance(new BigNumber(res))
2022
}
2123

22-
if (account && sousChefContract && lpContract) {
24+
if (account && cakeContract && cakeContract) {
2325
fetchAllowance()
2426
}
2527
const refreshInterval = setInterval(fetchAllowance, 10000)
2628
return () => clearInterval(refreshInterval)
27-
}, [account, sousChefContract, lpContract])
29+
}, [account, cakeContract, lotteryContract])
2830

2931
return allowance
3032
}
3133

32-
export const useLotteryAllowance = () => {
33-
const [allowance, setAllowance] = useState(new BigNumber(0))
34+
// Retrieve IFO allowance
35+
export const useIfoAllowance = (tokenContract: Contract, spenderAddress: string, dependency?: any) => {
3436
const { account }: { account: string } = useWallet()
35-
const sushi = useSushi()
36-
const lotteryContract = getLotteryContract(sushi)
37-
const cakeContract = getSushiContract(sushi)
37+
const [allowance, setAllowance] = useState(null)
3838

3939
useEffect(() => {
40-
const fetchAllowance = async () => {
41-
const res = await getAllowance(cakeContract, lotteryContract, account)
42-
setAllowance(new BigNumber(res))
43-
}
44-
45-
if (account && cakeContract && cakeContract) {
46-
fetchAllowance()
40+
const fetch = async () => {
41+
try {
42+
const res = await tokenContract.methods.allowance(account, spenderAddress).call()
43+
setAllowance(new BigNumber(res))
44+
} catch (e) {
45+
setAllowance(null)
46+
}
4747
}
48-
const refreshInterval = setInterval(fetchAllowance, 10000)
49-
return () => clearInterval(refreshInterval)
50-
}, [account, cakeContract, lotteryContract])
48+
fetch()
49+
}, [account, spenderAddress, tokenContract, dependency])
5150

5251
return allowance
5352
}

src/hooks/useApprove.ts

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,54 @@
11
import { useCallback } from 'react'
22
import { useWallet } from 'use-wallet'
33
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'
49
import useSushi from './useSushi'
5-
import { getSushiContract, approve, getMasterChefContract, getSousChefContract } from '../sushi/utils'
6-
import { getLotteryContract } from '../sushi/lotteryUtils'
710

8-
const useApprove = (lpContract: Contract) => {
11+
// Approve a Farm
12+
export const useApprove = (lpContract: Contract, pid: number) => {
13+
const dispatch = useDispatch()
914
const { account }: { account: string } = useWallet()
1015
const sushi = useSushi()
1116
const masterChefContract = getMasterChefContract(sushi)
1217

1318
const handleApprove = useCallback(async () => {
1419
try {
1520
const tx = await approve(lpContract, masterChefContract, account)
21+
dispatch(fetchFarmUserDataAsync(pid, account))
1622
return tx
1723
} catch (e) {
1824
return false
1925
}
20-
}, [account, lpContract, masterChefContract])
26+
}, [account, dispatch, lpContract, masterChefContract, pid])
2127

2228
return { onApprove: handleApprove }
2329
}
2430

31+
// Approve a Pool
2532
export const useSousApprove = (lpContract: Contract, sousId) => {
33+
const dispatch = useDispatch()
2634
const { account }: { account: string } = useWallet()
2735
const sushi = useSushi()
2836
const sousChefContract = getSousChefContract(sushi, sousId)
2937

3038
const handleApprove = useCallback(async () => {
3139
try {
3240
const tx = await approve(lpContract, sousChefContract, account)
41+
dispatch(updateUserAllowance(sousId, account))
3342
return tx
3443
} catch (e) {
3544
return false
3645
}
37-
}, [account, lpContract, sousChefContract])
46+
}, [account, dispatch, lpContract, sousChefContract, sousId])
3847

3948
return { onApprove: handleApprove }
4049
}
4150

51+
// Approve the lottery
4252
export const useLotteryApprove = () => {
4353
const { account }: { account: string } = useWallet()
4454
const sushi = useSushi()
@@ -57,4 +67,19 @@ export const useLotteryApprove = () => {
5767
return { onApprove: handleApprove }
5868
}
5969

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+
}

src/hooks/useBlock.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { useContext } from 'react'
2-
32
import { BlockContext } from 'contexts/BlockContext'
43

54
const useBlock = () => {
65
const block: number = useContext(BlockContext)
7-
86
return block
97
}
108

src/hooks/rework/useContract.ts src/hooks/useContract.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect, useState } from 'react'
22
import { AbiItem } from 'web3-utils'
33
import { ContractOptions } from 'web3-eth-contract'
4-
import useWeb3 from 'hooks/rework/useWeb3'
4+
import useWeb3 from 'hooks/useWeb3'
55
import ifo from 'sushi/lib/abi/ifo.json'
66
import erc20 from 'sushi/lib/abi/erc20.json'
77
import rabbitmintingfarm from 'sushi/lib/abi/rabbitmintingfarm.json'

0 commit comments

Comments
 (0)