-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #985 from hotaplayer/wagmi
feat(wagmi):add wagmi basics
- Loading branch information
Showing
12 changed files
with
268 additions
and
0 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,23 @@ | ||
# 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* |
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 @@ | ||
|
||
# Run the demo | ||
``` | ||
yarn | ||
yarn start | ||
``` | ||
|
||
# Reference | ||
- https://wagmi.sh/react/getting-started | ||
- https://wagmi.sh/examples/connect-wallet | ||
|
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,8 @@ | ||
const path = require('path'); | ||
module.exports = { | ||
webpack: { | ||
alias: { | ||
'@': path.resolve(__dirname, 'src'), | ||
}, | ||
}, | ||
}; |
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,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"@/*": ["./src/*"] | ||
} | ||
} | ||
} |
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,42 @@ | ||
{ | ||
"name": "wagmi-basic", | ||
"version": "0.1.0", | ||
"private": true, | ||
"dependencies": { | ||
"@craco/craco": "^7.1.0", | ||
"@material-ui/core": "^4.12.4", | ||
"@testing-library/jest-dom": "^5.14.1", | ||
"@testing-library/react": "^13.0.0", | ||
"@testing-library/user-event": "^13.2.1", | ||
"@wagmi/chains": "^1.6.0", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"react-scripts": "5.0.1", | ||
"viem": "^1.5.3", | ||
"wagmi": "^1.3.9", | ||
"web-vitals": "^2.1.0" | ||
}, | ||
"scripts": { | ||
"start": "craco start", | ||
"build": "craco build", | ||
"test": "craco test" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"react-app", | ||
"react-app/jest" | ||
] | ||
}, | ||
"browserslist": { | ||
"production": [ | ||
">0.2%", | ||
"not dead", | ||
"not op_mini all" | ||
], | ||
"development": [ | ||
"last 1 chrome version", | ||
"last 1 firefox version", | ||
"last 1 safari version" | ||
] | ||
} | ||
} |
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,42 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<meta name="theme-color" content="#000000" /> | ||
<meta | ||
name="description" | ||
content="Wagmi Demo" | ||
/> | ||
|
||
<!-- | ||
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>Wagmi Demo</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> |
10 changes: 10 additions & 0 deletions
10
basic/59-wagmi-and-nextjs/wagmi-basic/public/manifest.json
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,10 @@ | ||
{ | ||
"short_name": "Wagmi Demo", | ||
"name": "Wagmi Demo", | ||
"icons": [ | ||
], | ||
"start_url": ".", | ||
"display": "standalone", | ||
"theme_color": "#000000", | ||
"background_color": "#ffffff" | ||
} |
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,3 @@ | ||
# https://www.robotstxt.org/robotstxt.html | ||
User-agent: * | ||
Disallow: |
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,14 @@ | ||
|
||
.profile { | ||
position: relative; | ||
width: 60%; | ||
top: 20vh; | ||
margin: auto; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-evenly; | ||
align-items: left; | ||
font-size: 20px; | ||
font-weight: 500; | ||
} | ||
|
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,82 @@ | ||
import Button from '@material-ui/core/Button'; | ||
import './App.css'; | ||
//wagmi dependencies | ||
import { WagmiConfig, createConfig, configureChains } from 'wagmi' | ||
import { useAccount, useConnect, useDisconnect } from 'wagmi' | ||
import { publicProvider } from 'wagmi/providers/public'; | ||
import { jsonRpcProvider } from 'wagmi/providers/jsonRpc'; | ||
import { MetaMaskConnector } from 'wagmi/connectors/metaMask'; | ||
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect' | ||
//view dependencies | ||
import { localhost, polygon } from '@wagmi/chains' | ||
|
||
/*Firstly we create the client calling to public JSON rpcs. | ||
You can refer to https://wagmi.sh/react/providers/configuring-chains | ||
Also keep in mind that publicClient is the http client connecting to public JSON-RPC endpoints while webSocketPublicClient | ||
is websocket client. | ||
*/ | ||
const { chains, publicClient, webSocketPublicClient ,} = configureChains( | ||
//more chains see https://www.npmjs.com/package/@wagmi/chains | ||
[polygon,localhost], | ||
//providers are tried one by one to connect to above chains. more to see https://wagmi.sh/react/providers/configuring-chains | ||
//publicProvider will get the url from the preconfigured chains | ||
[publicProvider(), jsonRpcProvider({rpc: (chain) => ({http: 'https://localhost:8545',}),})] | ||
) | ||
|
||
/*Create wagmi config | ||
*/ | ||
const config = createConfig({ | ||
connectors:[ | ||
new MetaMaskConnector({chains}), | ||
// new WalletConnectConnector({ | ||
// chains, | ||
// options: { | ||
// projectId: '...', | ||
// }, | ||
// }), | ||
//See https://wagmi.sh/examples/connect-wallet for more | ||
], | ||
publicClient, | ||
webSocketPublicClient | ||
}) | ||
|
||
function App() { | ||
return ( | ||
<WagmiConfig config={config}> | ||
<Wallet /> | ||
</WagmiConfig> | ||
) | ||
} | ||
|
||
const Wallet = ()=> { | ||
const { address, connector, isConnected } = useAccount(); | ||
const { connect, connectors } = useConnect(); | ||
const { disconnect } = useDisconnect() | ||
|
||
if (isConnected) | ||
return ( | ||
<div className='profile'> | ||
<div>Connector:{connector.name}</div> | ||
<div>Current address:{address}</div> | ||
<div> | ||
<Button variant="outlined" color="primary" onClick={() => disconnect()}> | ||
Disconnect | ||
</Button> | ||
</div> | ||
</div> | ||
) | ||
return (<div className='profile'> | ||
{connectors.map((c) => ( | ||
<Button style={{ | ||
marginBottom: '20px' | ||
}} | ||
variant="contained" color="primary" onClick={() => connect({connector:c})}> | ||
Connect {c.name} | ||
</Button> | ||
))} | ||
|
||
</div>) | ||
|
||
} | ||
|
||
export default 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,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; | ||
} |
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 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import './index.css'; | ||
import App from './App'; | ||
|
||
const root = ReactDOM.createRoot(document.getElementById('root')); | ||
root.render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode> | ||
); | ||
|