|
1 |
| -import React, { useState } from "react"; |
2 |
| -import styled from "styled-components"; |
3 |
| -import { |
4 |
| - Web3ReactProvider, |
5 |
| - useWeb3React, |
6 |
| - UnsupportedChainIdError, |
7 |
| - createWeb3ReactRoot, |
8 |
| -} from "@web3-react/core"; |
9 |
| -import { Web3Provider } from "@ethersproject/providers"; |
10 |
| -import useSWR, { SWRConfig } from "swr"; |
11 |
| -import { createWatcher, aggregate } from "@makerdao/multicall"; |
12 |
| -import { Wallet } from "./Wallet"; |
| 1 | +import React from 'react' |
| 2 | +import { Web3ReactProvider } from '@web3-react/core' |
| 3 | +import { Web3Provider } from '@ethersproject/providers' |
| 4 | +import { useWeb3React } from '@web3-react/core' |
| 5 | +import { InjectedConnector } from '@web3-react/injected-connector' |
| 6 | + |
| 7 | +export const injectedConnector = new InjectedConnector({ |
| 8 | + supportedChainIds: [ |
| 9 | + 1, // Mainet |
| 10 | + 3, // Ropsten |
| 11 | + 4, // Rinkeby |
| 12 | + 5, // Goerli |
| 13 | + 42, // Kovan |
| 14 | + ], |
| 15 | +}) |
13 | 16 |
|
14 | 17 | function getLibrary(provider: any): Web3Provider {
|
15 |
| - console.log({ provider }); |
16 |
| - const library = new Web3Provider(provider); |
17 |
| - library.pollingInterval = 12000; |
18 |
| - return library; |
| 18 | + const library = new Web3Provider(provider) |
| 19 | + library.pollingInterval = 12000 |
| 20 | + return library |
19 | 21 | }
|
20 | 22 |
|
21 |
| -// Contract addresses used in this example |
22 |
| -const MKR_TOKEN = "0xaaf64bfcc32d0f15873a02163e7e500671a4ffcd"; |
23 |
| -const MKR_WHALE = "0xdb33dfd3d61308c33c63209845dad3e6bfb2c674"; |
24 |
| -const MKR_FISH = "0x2dfcedcb401557354d0cf174876ab17bfd6f4efd"; |
25 |
| - |
26 |
| -// const config = { preset: "rinkeby" }; |
27 |
| - |
28 |
| -const Container = styled.div` |
29 |
| - border: 1px solid #ccc; |
30 |
| - font-family: monospace; |
31 |
| - white-space: pre; |
32 |
| -`; |
33 |
| - |
34 |
| -// Create watcher |
35 |
| -/*const watcher = createWatcher( |
36 |
| - [ |
37 |
| - { |
38 |
| - target: MKR_TOKEN, |
39 |
| - call: ["balanceOf(address)(uint256)", MKR_WHALE], |
40 |
| - returns: [["BALANCE_OF_MKR_WHALE", (val) => val / 10 ** 18]], |
41 |
| - }, |
42 |
| - ], |
43 |
| - config |
44 |
| -); |
45 |
| -
|
46 |
| -// Subscribe to state updates |
47 |
| -watcher.subscribe((update) => { |
48 |
| - console.log(`Update: ${update.type} = ${update.value}`); |
49 |
| -}); |
50 |
| -
|
51 |
| -// Subscribe to batched state updates |
52 |
| -watcher.batch().subscribe((updates) => { |
53 |
| - // Handle batched updates here |
54 |
| - // Updates are returned as { type, value } objects, e.g: |
55 |
| - // { type: 'BALANCE_OF_MKR_WHALE', value: 70000 } |
56 |
| -}); |
57 |
| -
|
58 |
| -// Subscribe to new block number updates |
59 |
| -watcher.onNewBlock((blockNumber) => { |
60 |
| - console.log("New block:", blockNumber); |
61 |
| -});*/ |
62 |
| - |
63 |
| -// Start the watcher polling |
64 |
| -/*watcher.start();*/ |
| 23 | +export const Wallet = () => { |
| 24 | + const { chainId, account, activate, active } = useWeb3React<Web3Provider>() |
| 25 | + |
| 26 | + const onClick = () => { |
| 27 | + activate(injectedConnector) |
| 28 | + } |
| 29 | + |
| 30 | + return ( |
| 31 | + <div> |
| 32 | + <div>ChainId: {chainId}</div> |
| 33 | + <div>Account: {account}</div> |
| 34 | + {active ? ( |
| 35 | + <div>✅ </div> |
| 36 | + ) : ( |
| 37 | + <button type="button" onClick={onClick}> |
| 38 | + Connect |
| 39 | + </button> |
| 40 | + )} |
| 41 | + </div> |
| 42 | + ) |
| 43 | +} |
65 | 44 |
|
66 | 45 | export const App = () => {
|
67 |
| - return <Web3ReactProvider getLibrary={getLibrary} children={<Wallet />} />; |
68 |
| -}; |
| 46 | + return ( |
| 47 | + <Web3ReactProvider getLibrary={getLibrary}> |
| 48 | + <Wallet /> |
| 49 | + </Web3ReactProvider> |
| 50 | + ) |
| 51 | +} |
0 commit comments