-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compatibility with Web3.js patching the ethereum provider
- Loading branch information
Showing
6 changed files
with
9,708 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": "> 2%, not dead, not ie > 0", | ||
"modules": false | ||
} | ||
], | ||
"@babel/preset-react" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script src="./index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import 'babel-polyfill' | ||
import React, { useRef, useState } from 'react' | ||
import ReactDOM from 'react-dom' | ||
import Web3 from 'web3' | ||
import { | ||
RejectedActivationError, | ||
UseWalletProvider, | ||
useWallet, | ||
} from 'use-wallet' | ||
|
||
function App() { | ||
const [lastError, setLastError] = useState('') | ||
const wallet = useWallet() | ||
const blockNumber = wallet.getBlockNumber() | ||
const { current: web3 } = useRef(new Web3(window.ethereum)) | ||
|
||
const activate = async connector => { | ||
setLastError('') | ||
|
||
try { | ||
await wallet.activate(connector) | ||
} catch (err) { | ||
if (err instanceof RejectedActivationError) { | ||
setLastError('Connection error: the user rejected the activation') | ||
} | ||
setLastError(err.message) | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<h1>use-wallet</h1> | ||
|
||
{(() => { | ||
if (lastError) { | ||
return ( | ||
<p> | ||
<span>{lastError}</span> | ||
<button onClick={() => setLastError('')}>retry</button> | ||
</p> | ||
) | ||
} | ||
|
||
if (wallet.activating) { | ||
return ( | ||
<p> | ||
<span>Connecting to {wallet.activating}…</span> | ||
<button onClick={() => wallet.deactivate()}>cancel</button> | ||
</p> | ||
) | ||
} | ||
|
||
if (wallet.connected) { | ||
return ( | ||
<p> | ||
<span>Connected.</span> | ||
<button onClick={() => wallet.deactivate()}>disconnect</button> | ||
</p> | ||
) | ||
} | ||
|
||
return ( | ||
<p> | ||
<span>Connect:</span> | ||
<button onClick={() => activate('injected')}>injected</button> | ||
<button onClick={() => activate('frame')}>frame</button> | ||
<button onClick={() => activate('portis')}>portis</button> | ||
<button onClick={() => activate('fortmatic')}>fortmatic</button> | ||
</p> | ||
) | ||
})()} | ||
|
||
{wallet.connected && ( | ||
<> | ||
<p> | ||
<span>Account:</span> | ||
<span>{wallet.account}</span> | ||
</p> | ||
</> | ||
)} | ||
|
||
{wallet.account && ( | ||
<p> | ||
<span>Balance:</span> | ||
<span>{wallet.balance === '-1' ? '…' : `${wallet.balance} ETH`}</span> | ||
</p> | ||
)} | ||
|
||
{wallet.connected && ( | ||
<p> | ||
<span>Block:</span> <span>{blockNumber || '…'}</span> | ||
</p> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
ReactDOM.render( | ||
<UseWalletProvider | ||
chainId={1} | ||
connectors={{ | ||
fortmatic: { apiKey: '' }, | ||
portis: { dAppId: '' }, | ||
}} | ||
> | ||
<App /> | ||
<style> | ||
{` | ||
body { | ||
width: 40rem; | ||
margin: 0 auto; | ||
font-family: sans-serif; | ||
font-size: 18px; | ||
line-height: 1.5; | ||
} | ||
button { | ||
width: 6rem; | ||
} | ||
h1 { | ||
font-weight: 400; | ||
} | ||
p { | ||
display: grid; | ||
justify-content: space-between; | ||
align-items: center; | ||
grid-auto-flow: column; | ||
gap: 1rem; | ||
align-items: center; | ||
margin: 2rem 0; | ||
} | ||
button { | ||
width: 7rem; | ||
height: 3rem; | ||
cursor: pointer; | ||
font-size: 1rem; | ||
padding: 0; | ||
} | ||
`} | ||
</style> | ||
</UseWalletProvider>, | ||
document.querySelector('#app') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"scripts": { | ||
"start": "parcel serve index.html", | ||
"build": "parcel build index.html" | ||
}, | ||
"dependencies": { | ||
"babel-polyfill": "^6.26.0", | ||
"jsbi": "^3.1.1", | ||
"react": "^16.12.0", | ||
"react-dom": "^16.12.0", | ||
"use-wallet": "link:../..", | ||
"web3": "^1.2.6" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.8.3", | ||
"parcel": "^1.12.4" | ||
} | ||
} |
Oops, something went wrong.