Skip to content

Commit

Permalink
feat: web3-react (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
dannythedawger authored Feb 1, 2023
1 parent 732b0c4 commit 81ec93e
Show file tree
Hide file tree
Showing 25 changed files with 11,521 additions and 8 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/lint-web3-react.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Lint web3-react

on:
push:
pull_request:
types:
- opened
- synchronize

jobs:
lint:
name: Run linters
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v3

- name: Set up node
uses: actions/setup-node@v2
with:
node-version: 16

- name: Install dependencies
working-directory: ./web3-react
run: yarn

- name: Run linters
working-directory: ./web3-react
run: yarn lint
31 changes: 23 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
**/.git
**/.svn
**/.hg
**/node_modules
**/.git
**/.svn
**/.hg
*.mdx
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
**/*/node_modules
**/*/.pnp
**/*/.pnp.js

# testing
**/*/coverage

# production
**/*/build

# misc
**/*/.DS_Store
**/*/.env.local
**/*/.env.development.local
**/*/.env.test.local
**/*/.env.production.local

**/*/npm-debug.log*
**/*/yarn-debug.log*
**/*/yarn-error.log*
7 changes: 7 additions & 0 deletions web3-react/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-env node */

require('@uniswap/eslint-config/load')

module.exports = {
extends: '@uniswap/eslint-config/react',
}
1 change: 1 addition & 0 deletions web3-react/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--frozen-lockfile true
24 changes: 24 additions & 0 deletions web3-react/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# web3-react

## Overview

This is an example of using the Uniswap `web3-react` package and includes running against mainnet and other L2s.

## Configuration

To configure the available chains, and pick an initial chain, edit the [configuration](./src/config.ts) file. The code should need no further modification to function.

## Setup

### Install dependencies

1. Run `yarn install` to install the project dependencies

### Get a mainnet RPC URL

1. Create an API key using any of the [Ethereum API providers](https://docs.ethers.io/v5/api/providers/) and grab the respective RPC URL, or use the default `https://mainnet.infura.io/v3/4bf032f2d38a4ed6bb975b80d6340847`
2. Set that as the value of the `rpc` inside the [config](./src/config.ts) for the chains you decide to connect the dApp to.

### Start the web interface

Run `yarn start` and navigate to [http://localhost:3000/](http://localhost:3000/)
51 changes: 51 additions & 0 deletions web3-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "web3-react",
"version": "0.1.0",
"private": true,
"dependencies": {
"@coinbase/wallet-sdk": "3.3.0",
"@walletconnect/ethereum-provider": "1.8",
"@web3-react/coinbase-wallet": "^8.0.34-beta.0",
"@web3-react/core": "8.0.35-beta.0",
"@web3-react/gnosis-safe": "^8.0.7-beta.0",
"@web3-react/metamask": "^8.0.29-beta.0",
"@web3-react/network": "^8.0.27-beta.0",
"@web3-react/types": "^8.0.20-beta.0",
"@web3-react/walletconnect": "^8.0.36-beta.0",
"buffer": "^6.0.3",
"ethers": "^5.7.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-error-boundary": "^3.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"lint": "yarn eslint ."
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/node": "^18.11.18",
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
"@uniswap/eslint-config": "^1.1.1",
"eslint": "^8.29.0",
"prettier": "^2.8.0",
"react-scripts": "^5.0.1",
"typescript": "^4.9.4"
},
"engines": {
"node": ">=16.0.0"
}
}
43 changes: 43 additions & 0 deletions web3-react/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
23 changes: 23 additions & 0 deletions web3-react/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Sets if the example should run locally or on chain
export enum Chain {
POLYGON,
MAINNET,
}

// Inputs that configure this example to run
interface ExampleConfig {
chain: Chain
rpc: {
polygon: string
mainnet: string
}
}

// Example Configuration
export const CurrentConfig: ExampleConfig = {
chain: Chain.MAINNET,
rpc: {
polygon: '',
mainnet: '',
},
}
22 changes: 22 additions & 0 deletions web3-react/src/example/Example.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.App {
text-align: center;
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.error {
color: red;
}

.connectors {
flex-direction: column;
align-items: center;
justify-content: center;
display: flex;
}
68 changes: 68 additions & 0 deletions web3-react/src/example/Example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import './Example.css'

import { useWeb3React } from '@web3-react/core'
import React, { useEffect, useState } from 'react'
import { ErrorBoundary, FallbackProps } from 'react-error-boundary'

import { ConnectionOptions } from '../libs/components/ConnectionOptions'
import { ConnectionType, switchNetwork } from '../libs/connections'
import { CHAIN_INFO, INPUT_CHAIN_URL } from '../libs/constants'

const FallbackComponent = ({ error }: FallbackProps) => {
return (
<div>
<h1>An error occurred: {error.message}</h1>
<p>Please reload the application</p>
</div>
)
}
// Listen for new blocks and update the wallet
const useOnBlockUpdated = (callback: (blockNumber: number) => void) => {
const { provider } = useWeb3React()
useEffect(() => {
if (!provider) {
return
}
const subscription = provider.on('block', callback)
return () => {
subscription.removeAllListeners()
}
})
}

const Example = () => {
const { chainId, account, isActive } = useWeb3React()
const [blockNumber, setBlockNumber] = useState<number>(0)
const [connectionType, setConnectionType] = useState<ConnectionType | null>(null)

// Listen for new blocks and update the wallet
useOnBlockUpdated((blockNumber: number) => {
setBlockNumber(blockNumber)
})

return (
<div className="App">
<ErrorBoundary FallbackComponent={FallbackComponent}>
{INPUT_CHAIN_URL === '' && <h2 className="error">Please set your RPC URL in config.ts</h2>}
<h3>{`Block Number: ${blockNumber + 1}`}</h3>
<ConnectionOptions
activeConnectionType={connectionType}
isConnectionActive={isActive}
onActivate={setConnectionType}
onDeactivate={setConnectionType}
/>
<h3>{`ChainId: ${chainId}`}</h3>
<h3>{`Connected Account: ${account}`}</h3>
{Object.keys(CHAIN_INFO).map((chainId) => (
<div key={chainId}>
<button onClick={() => switchNetwork(parseInt(chainId), connectionType)}>
{`Switch to ${CHAIN_INFO[chainId].label}`}
</button>
</div>
))}
</ErrorBoundary>
</div>
)
}

export default Example
13 changes: 13 additions & 0 deletions web3-react/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
24 changes: 24 additions & 0 deletions web3-react/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import './index.css'

import { Buffer } from 'buffer'
import React from 'react'
import ReactDOM from 'react-dom/client'

import Example from './example/Example'
import { Web3ContextProvider } from './libs/components/Web3ContextProvider'

if (window.ethereum) {
window.ethereum.autoRefreshOnNetworkChange = false
}

// Node polyfills required by WalletConnect are no longer bundled with webpack
window.Buffer = Buffer

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
root.render(
<React.StrictMode>
<Web3ContextProvider>
<Example />
</Web3ContextProvider>
</React.StrictMode>
)
27 changes: 27 additions & 0 deletions web3-react/src/libs/coinbase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CoinbaseWallet } from '@web3-react/coinbase-wallet'
import { initializeConnector } from '@web3-react/core'

import { Connection, ConnectionType, onConnectionError } from './connections'
import { INPUT_CHAIN_URL } from './constants'

export function buildCoinbaseWalletConnector() {
const [web3CoinbaseWallet, web3CoinbaseWalletHooks] = initializeConnector<CoinbaseWallet>(
(actions) =>
new CoinbaseWallet({
actions,
options: {
url: INPUT_CHAIN_URL,
appName: 'Uniswap Example',
reloadOnDisconnect: false,
},
onError: onConnectionError,
})
)
const coinbaseWalletConnection: Connection = {
connector: web3CoinbaseWallet,
hooks: web3CoinbaseWalletHooks,
type: ConnectionType.COINBASE_WALLET,
}

return coinbaseWalletConnection
}
Loading

0 comments on commit 81ec93e

Please sign in to comment.