Skip to content

Commit 8556717

Browse files
committed
Connect to the network
1 parent 680d0da commit 8556717

File tree

5 files changed

+56
-109
lines changed

5 files changed

+56
-109
lines changed

.prettierrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"semi": false
4+
}

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "swr-hello-world",
2+
"name": "swr-eth",
33
"version": "1.0.0",
4-
"description": "Alphabet flash cards",
4+
"description": "DApp Data Fetching strategy",
55
"main": "dist/index.html",
66
"scripts": {
77
"start": "parcel src/index.html",
@@ -30,7 +30,6 @@
3030
"@ethersproject/contracts": "^5.0.0-beta.152",
3131
"@ethersproject/providers": "^5.0.0-beta.166",
3232
"@ethersproject/units": "^5.0.0-beta.133",
33-
"@makerdao/multicall": "^0.11.0",
3433
"@web3-react/core": "^6.0.9",
3534
"@web3-react/injected-connector": "^6.0.7",
3635
"@web3-react/network-connector": "^6.0.9",

src/.prettierrc

-4
This file was deleted.

src/components/App.tsx

+45-62
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,51 @@
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+
})
1316

1417
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
1921
}
2022

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

6645
export const App = () => {
67-
return <Web3ReactProvider getLibrary={getLibrary} children={<Wallet />} />;
68-
};
46+
return (
47+
<Web3ReactProvider getLibrary={getLibrary}>
48+
<Wallet />
49+
</Web3ReactProvider>
50+
)
51+
}

src/components/Wallet.tsx

+5-40
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,21 @@
1-
import React, { Suspense } from "react"
2-
import { SWRConfig } from "swr"
3-
import { useWeb3React } from "@web3-react/core"
4-
import { Web3Provider } from "@ethersproject/providers"
1+
import React from 'react'
2+
import { useWeb3React } from '@web3-react/core'
3+
import { Web3Provider } from '@ethersproject/providers'
54

6-
import { useEagerConnect } from "../hooks/useEagerConnect"
7-
import { useInactiveListener } from "../hooks/useInactiveListener"
8-
import { DisplayInfo } from "./DisplayInfo"
9-
import { web3Fetcher } from "../utils"
10-
import { injected } from "../connectors"
11-
import { ABIs } from "../abi"
5+
import { injected } from '../connectors'
126

137
export const Wallet = () => {
14-
const context = useWeb3React<Web3Provider>()
158
const {
16-
connector,
17-
library,
189
chainId,
1910
account,
2011
activate,
21-
deactivate,
2212
active,
23-
error,
24-
} = context
13+
} = useWeb3React<Web3Provider>()
2514

2615
const onClick = () => {
2716
activate(injected)
2817
}
2918

30-
// handle logic to recognize the connector currently being activated
31-
const [activatingConnector, setActivatingConnector] = React.useState()
32-
React.useEffect(() => {
33-
console.log("Wallet running")
34-
if (activatingConnector && activatingConnector === connector) {
35-
setActivatingConnector(undefined)
36-
}
37-
}, [activatingConnector, connector])
38-
39-
// mount only once or face issues :P
40-
const triedEager = useEagerConnect()
41-
useInactiveListener(!triedEager || !!activatingConnector)
42-
console.log({ library, active, triedEager, activatingConnector })
4319
return (
4420
<div>
4521
<div>ChainId: {chainId}</div>
@@ -51,17 +27,6 @@ export const Wallet = () => {
5127
Connect
5228
</button>
5329
)}
54-
{library && (
55-
<SWRConfig
56-
value={{
57-
fetcher: web3Fetcher(library, ABIs),
58-
suspense: true,
59-
focusThrottleInterval: 5000,
60-
}}
61-
>
62-
<DisplayInfo chainId={chainId} />
63-
</SWRConfig>
64-
)}
6530
</div>
6631
)
6732
}

0 commit comments

Comments
 (0)