Skip to content

Commit

Permalink
chore: Use rainbowkit simply wallet of example
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Jun 8, 2023
1 parent c35e735 commit d039ffa
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 235 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You can find some package documentation below:

Support use in browser and node.js

* [nextjs](./examples/nextjs/README.md)
* [Next.js Example(TypeScript)](./examples/nextjs/README.md)
* [Nodejs](./examples/nodejs/README.md)

## Supported JS environments
Expand Down
9 changes: 9 additions & 0 deletions examples/nextjs/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const nextConfig = {
// your project has ESLint errors.
ignoreDuringBuilds: true,
},
webpack: (config) => {
config.resolve.fallback = {
fs: false,
net: false,
tls: false,
};

return config;
},
};

module.exports = nextConfig;
3 changes: 2 additions & 1 deletion examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@ethersproject/signing-key": "^5.7.0",
"@metamask/eth-sig-util": "^5.0.2",
"@next/font": "13.1.6",
"@rainbow-me/rainbowkit": "^1.0.1",
"@types/node": "^18.7.1",
"@types/react": "^18.0.17",
"@types/react-dom": "^18.0.6",
Expand All @@ -34,7 +35,7 @@
"react-dom": "18.2.0",
"typescript": "^4.7.4",
"viem": "^0.3.31",
"wagmi": "^1.0.5"
"wagmi": "^1.1.1"
},
"devDependencies": {
"cross-env": "^7.0.3",
Expand Down
76 changes: 2 additions & 74 deletions examples/nextjs/src/components/walletInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,81 +1,9 @@
import {
BSC_CHAIN_ID,
GREEN_CHAIN_ID,
coinbaseWalletConnector,
metaMaskWalletConnector,
trustWalletConnector,
} from '@/config';
import {
useAccount,
useBalance,
useConnect,
useDisconnect,
useNetwork,
useSwitchNetwork,
} from 'wagmi';
import { ConnectButton } from '@rainbow-me/rainbowkit';

export const WalletInfo = () => {
const { address, connector, isConnected } = useAccount();
const { connect: metaMaskConnect } = useConnect({
connector: metaMaskWalletConnector,
});
const { connect: trustWalletConnect } = useConnect({
connector: trustWalletConnector,
});
const { connect: coinbaseWalletConnect } = useConnect({
connector: coinbaseWalletConnector,
});

const { disconnect } = useDisconnect();
const { chain } = useNetwork();
const { switchNetwork: switchToGreenField } = useSwitchNetwork({
chainId: GREEN_CHAIN_ID,
});

const { switchNetwork: switchToBSC } = useSwitchNetwork({
chainId: BSC_CHAIN_ID,
});

const balance = useBalance({
address,
watch: true,
});

if (!isConnected) {
return (
<>
<button onClick={() => metaMaskConnect()}>Connect MetaMask</button>
<button onClick={() => trustWalletConnect()}>Connect trustWallet</button>
<button onClick={() => coinbaseWalletConnect()}>Connect coinbase wallet</button>
</>
);
}

return (
<div>
<div>
<h2>address : {address}</h2>
<h2>connector: {connector?.name} </h2>
<button onClick={() => disconnect()}>Disconnect</button>
</div>

<h2>change chain chainId {chain?.id}</h2>
<h3>balance: {balance.data?.formatted}</h3>
<button
onClick={() => {
switchToGreenField?.(GREEN_CHAIN_ID);
}}
>
switch to green field
</button>
<br />
<button
onClick={() => {
switchToBSC?.(BSC_CHAIN_ID);
}}
>
switch to bsc
</button>
<ConnectButton />
</div>
);
};
1 change: 1 addition & 0 deletions examples/nextjs/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const CROSS_CHAIN_CONTRACT_ADDRESS = env.CROSS_CHAIN_CONTRACT_ADDRESS;
const greenFieldChain: Chain = {
id: GREEN_CHAIN_ID,
network: 'greenfield',
iconUrl: 'https://github.com/wagmi-dev/wagmi/assets/5653652/44446c8c-5c72-4e89-b8eb-3042ef618eed',
rpcUrls: {
default: {
http: [GREENFIELD_RPC_URL],
Expand Down
36 changes: 27 additions & 9 deletions examples/nextjs/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
import {
coinbaseWalletConnector,
metaMaskWalletConnector,
publicClient,
trustWalletConnector,
webSocketPublicClient,
} from '@/config';
import { chains, publicClient, webSocketPublicClient } from '@/config';
import '@/styles/globals.css';
import {
connectorsForWallets,
getDefaultWallets,
RainbowKitProvider,
} from '@rainbow-me/rainbowkit';
import '@rainbow-me/rainbowkit/styles.css';
import { trustWallet } from '@rainbow-me/rainbowkit/wallets';
import type { AppProps } from 'next/app';
import { createConfig, WagmiConfig } from 'wagmi';

const projectId = '';
const { wallets } = getDefaultWallets({
projectId,
appName: 'greenfield js sdk demo',
chains,
});

const connectors = connectorsForWallets([
...wallets,
{
groupName: 'Recommended',
wallets: [trustWallet({ projectId, chains })],
},
]);

const wagmiConfig = createConfig({
autoConnect: true,
connectors: [metaMaskWalletConnector, trustWalletConnector, coinbaseWalletConnector],
connectors,
webSocketPublicClient,
publicClient,
});

export default function App({ Component, pageProps }: AppProps) {
return (
<WagmiConfig config={wagmiConfig}>
<Component {...pageProps} />
<RainbowKitProvider modalSize="compact" chains={chains}>
<Component {...pageProps} />
</RainbowKitProvider>
</WagmiConfig>
);
}
Loading

0 comments on commit d039ffa

Please sign in to comment.