Skip to content

Commit

Permalink
Merge pull request #985 from hotaplayer/wagmi
Browse files Browse the repository at this point in the history
feat(wagmi):add wagmi basics
  • Loading branch information
yingjingyang authored Aug 31, 2023
2 parents 1845f31 + d1ec263 commit 930d635
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 0 deletions.
23 changes: 23 additions & 0 deletions basic/59-wagmi-and-nextjs/wagmi-basic/.gitignore
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*
11 changes: 11 additions & 0 deletions basic/59-wagmi-and-nextjs/wagmi-basic/README.md
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

8 changes: 8 additions & 0 deletions basic/59-wagmi-and-nextjs/wagmi-basic/craco.config.js
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'),
},
},
};
8 changes: 8 additions & 0 deletions basic/59-wagmi-and-nextjs/wagmi-basic/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
42 changes: 42 additions & 0 deletions basic/59-wagmi-and-nextjs/wagmi-basic/package.json
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"
]
}
}
42 changes: 42 additions & 0 deletions basic/59-wagmi-and-nextjs/wagmi-basic/public/index.html
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 basic/59-wagmi-and-nextjs/wagmi-basic/public/manifest.json
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"
}
3 changes: 3 additions & 0 deletions basic/59-wagmi-and-nextjs/wagmi-basic/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
14 changes: 14 additions & 0 deletions basic/59-wagmi-and-nextjs/wagmi-basic/src/App.css
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;
}

82 changes: 82 additions & 0 deletions basic/59-wagmi-and-nextjs/wagmi-basic/src/App.js
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;
13 changes: 13 additions & 0 deletions basic/59-wagmi-and-nextjs/wagmi-basic/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;
}
12 changes: 12 additions & 0 deletions basic/59-wagmi-and-nextjs/wagmi-basic/src/index.js
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>
);

0 comments on commit 930d635

Please sign in to comment.