1
1
import React , { useEffect , useMemo } from "react"
2
- import { buildContract , IERC20 , memoizedBuildContract } from "../utils"
3
- import ERC20ABI from "../abi/ERC20.abi.json"
2
+ import { buildContract , ethContractKey , IERC20 , memoizedBuildContract } from "../utils"
4
3
import useSWR , { keyInterface } from "swr"
5
4
import { useWeb3React } from "@web3-react/core"
6
5
import { Web3Provider } from "@ethersproject/providers"
7
6
import { formatUnits } from "@ethersproject/units"
8
7
import { BigNumber } from "@ethersproject/bignumber"
9
8
import { ABI , ABIs } from "../abi"
10
9
import { useWeb3Filter } from "../hooks/useWeb3Filter"
11
- import { Contract } from "@ethersproject/contracts"
12
10
13
- const balanceOfKey = ( address :string , account :string ) : keyInterface => {
14
- return [ ABI . ERC20 , address , "balanceOf" , account ]
15
- }
11
+
16
12
export const ERC20View = ( { name, address, decimals, symbol } : IERC20 ) => {
17
13
const { account, library } = useWeb3React < Web3Provider > ( )
18
- const { data : balance , isValidating, error, mutate } = useSWR < BigNumber > (
19
- // [ABI.ERC20, address, "balanceOf", account]
20
- balanceOfKey ( address , account )
21
- )
22
- console . log ( { balance} )
23
- // useWeb3Filter(
24
- // balanceOfKey(address, account),
25
- // "Transfer",
26
- // (from, to, amount, event) => {
27
- // console.log("Transfer|sent", { from, to, amount, event })
28
- // mutate(balance.sub(amount), false)
29
- // // mutate(undefined, true)
30
- // }
31
- // )
14
+ const key = ethContractKey ( [
15
+ address ,
16
+ "balanceOf" ,
17
+ account ] )
18
+ const { data : balance , error, mutate } = useSWR < BigNumber > ( key )
19
+
20
+ useWeb3Filter ( key , [
21
+ ( contract ) => [
22
+ contract . filters . Transfer ( account , null ) , // fromMe
23
+ async ( ) => {
24
+ mutate ( undefined , true )
25
+ } ,
26
+ ] ,
27
+ ( contract ) => [
28
+ contract . filters . Transfer ( null , account ) , //toMe
29
+ async ( ) => await mutate ( undefined , true ) ,
30
+ ] ,
31
+ ] )
32
32
33
33
// TODO LS export as useFilter.e.g useFilter(contract, [contract.filters.Transfer(account, null), mutate]) The web3fetcher could return the data and the contract built
34
- useEffect ( ( ) => {
34
+ /* useEffect(() => {
35
35
const contract = buildContract(address, ABIs.get(ABI.ERC20), library)
36
36
const fromMe = contract.filters.Transfer(account, null)
37
- console . log ( )
38
37
contract.once(fromMe, (from, to, amount, event) => {
39
38
console.log("Transfer|sent", { from, to, amount, event })
40
- mutate ( balance . sub ( amount ) , false )
41
- // mutate(undefined, true)
39
+ // mutate(balance.sub(amount), false) BUG because this is a closure the balance isn't update
40
+ // we could useRef https://stackoverflow.com/a/53641229 but it isn't very elegant
41
+ mutate(undefined, true)
42
42
})
43
43
44
44
const toMe = contract.filters.Transfer(null, account)
45
45
contract.once(toMe, (from, to, amount, event) => {
46
46
console.log("Transfer|received", { from, to, amount, event })
47
- mutate ( balance . add ( amount ) , false )
48
- // mutate(undefined, true)
47
+ // mutate(balance.add(amount), false) // BUG because this is a closure the balance isn't update
48
+ mutate(undefined, true)
49
49
})
50
50
console.log("rendering", {
51
51
fromMe: contract.listenerCount(fromMe),
@@ -56,7 +56,7 @@ export const ERC20View = ({ name, address, decimals, symbol }: IERC20) => {
56
56
contract.removeAllListeners(toMe)
57
57
contract.removeAllListeners(fromMe)
58
58
}
59
- } , [ ] )
59
+ }, [])*/
60
60
61
61
if ( error ) {
62
62
return < pre > { error } </ pre >
0 commit comments