Created
June 10, 2023 08:07
-
-
Save Patricie29/3278daaa89d1638ccc9c4fc672e13547 to your computer and use it in GitHub Desktop.
CryptoNewbie - implementation of Web3.0 wallet/configuration of chains
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
import { ConnectButton } from '@rainbow-me/rainbowkit'; | |
import Image from 'next/image'; | |
export const Connect = () => { | |
return ( | |
<ConnectButton.Custom> | |
{({ | |
account, | |
chain, | |
openAccountModal, | |
openChainModal, | |
openConnectModal, | |
authenticationStatus, | |
mounted, | |
}) => { | |
// Note: If your app doesn't use authentication, you | |
// can remove all 'authenticationStatus' checks | |
const ready = mounted && authenticationStatus !== 'loading'; | |
const connected = | |
ready && | |
account && | |
chain && | |
(!authenticationStatus || | |
authenticationStatus === 'authenticated'); | |
return ( | |
<div | |
{...(!ready && { | |
'aria-hidden': true, | |
'style': { | |
opacity: 0, | |
pointerEvents: 'none', | |
userSelect: 'none', | |
}, | |
})} | |
> | |
{(() => { | |
if (!connected) { | |
return ( | |
<button onClick={openConnectModal} type="button" className="block py-2 px-4 font-medium text-sm text-zinc-400 rounded-lg md:border md:border-transparent md:hover:border-solid hover:text-white mr-1 hover:md:shadow-lg"> | |
Connect Wallet | |
</button> | |
); | |
} | |
if (chain.unsupported) { | |
return ( | |
<button onClick={openChainModal} type="button"> | |
Wrong network | |
</button> | |
); | |
} | |
return ( | |
<div style={{ display: 'flex', gap: 12 }}> | |
<button | |
onClick={openChainModal} | |
style={{ display: 'flex', alignItems: 'center' }} | |
type="button" | |
> | |
{chain.hasIcon && ( | |
<div | |
style={{ | |
background: chain.iconBackground, | |
width: 12, | |
height: 12, | |
borderRadius: 999, | |
overflow: 'hidden', | |
marginRight: 4, | |
}} | |
> | |
{chain.iconUrl && ( | |
<Image | |
alt={chain.name ?? 'Chain icon'} | |
src={chain.iconUrl} | |
style={{ width: 12, height: 12 }} | |
/> | |
)} | |
</div> | |
)} | |
{chain.name} | |
</button> | |
<button onClick={openAccountModal} type="button"> | |
{account.displayName} | |
{account.displayBalance | |
? ` (${account.displayBalance})` | |
: ''} | |
</button> | |
</div> | |
); | |
})()} | |
</div> | |
); | |
}} | |
</ConnectButton.Custom > | |
); | |
}; |
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
'use client' | |
import { CoinmarketProvider } from '@/context/cryptoCtx' | |
import { MessagesProvider } from '@/context/messages' | |
import { QueryClientProvider, QueryClient } from '@tanstack/react-query' | |
import { ThemeProvider } from 'next-themes' | |
import React, { FC, ReactNode } from 'react' | |
import { | |
RainbowKitProvider, | |
getDefaultWallets, | |
connectorsForWallets, | |
darkTheme | |
} from '@rainbow-me/rainbowkit'; | |
import { | |
argentWallet, | |
trustWallet, | |
ledgerWallet, | |
} from '@rainbow-me/rainbowkit/wallets'; | |
import { configureChains, createConfig, WagmiConfig } from 'wagmi'; | |
import { mainnet, polygon, optimism, arbitrum, goerli, bsc, bscTestnet } from 'wagmi/chains'; | |
import { publicProvider } from 'wagmi/providers/public'; | |
// here we can add/remove available chains | |
const { chains, publicClient, webSocketPublicClient } = configureChains( | |
[ | |
mainnet, | |
polygon, | |
optimism, | |
bsc, | |
bscTestnet, | |
arbitrum, | |
...(process.env.NEXT_PUBLIC_ENABLE_TESTNETS === 'true' ? [goerli] : []), | |
], | |
[publicProvider()] | |
); | |
const projectId = 'CryptoNewbie'; | |
const { wallets } = getDefaultWallets({ | |
appName: 'CryptoNewbie', | |
projectId, | |
chains, | |
}); | |
const demoAppInfo = { | |
appName: 'CryptoNewbie', | |
}; | |
const connectors = connectorsForWallets([ | |
...wallets, | |
{ | |
groupName: 'Other', | |
wallets: [ | |
argentWallet({ projectId, chains }), | |
trustWallet({ projectId, chains }), | |
ledgerWallet({ projectId, chains }), | |
], | |
}, | |
]); | |
const wagmiConfig = createConfig({ | |
autoConnect: true, | |
connectors, | |
publicClient, | |
webSocketPublicClient, | |
}); | |
interface ProvidersProps { | |
children: ReactNode | |
} | |
const Providers: FC<ProvidersProps> = ({ children }) => { | |
const queryClient = new QueryClient() | |
const [mounted, setMounted] = React.useState(false); | |
React.useEffect(() => setMounted(true), []); | |
return <QueryClientProvider client={queryClient}> | |
<WagmiConfig config={wagmiConfig}> | |
<RainbowKitProvider chains={chains} appInfo={demoAppInfo} theme={darkTheme({ | |
accentColor: 'transparent', | |
accentColorForeground: 'white', | |
fontStack: 'system' | |
})}> | |
<ThemeProvider attribute='class' defaultTheme='system' enableSystem> | |
<CoinmarketProvider> | |
<MessagesProvider> | |
{mounted && children} | |
</MessagesProvider> | |
</CoinmarketProvider> | |
</ThemeProvider> | |
</RainbowKitProvider> | |
</WagmiConfig> | |
</QueryClientProvider> | |
} | |
export default Providers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment