Init wallet and wallet connect client

This commit is contained in:
Ilja 2022-02-04 11:37:35 +02:00
parent 9e9b275f22
commit 1f71001175
5 changed files with 68 additions and 26 deletions

View File

@ -9,7 +9,7 @@ export default function PageHeader({ children }: Props) {
<Text
h3
css={{
textGradient: '45deg, $cyan300 -30%, $green600 100%'
textGradient: '45deg, $primary -30%, $secondary 100%'
}}
weight="bold"
>

View File

@ -1,20 +1,28 @@
import { WalletContext } from '@/contexts/WalletContext'
import { Card, Container, Divider, Loading } from '@nextui-org/react'
import { Fragment, ReactNode, useContext, useEffect } from 'react'
import { Fragment, ReactNode, useCallback, useContext, useEffect } from 'react'
interface Props {
children: ReactNode | ReactNode[]
}
export default function GlobalLayout({ children }: Props) {
const { state, actions } = useContext(WalletContext)
const hasWallet = state.wallet !== undefined
const {
state: { initialized },
actions
} = useContext(WalletContext)
const onInitialize = useCallback(async () => {
actions.createWallet()
await actions.createWalletConnectClient()
actions.setInitialized()
}, [actions])
useEffect(() => {
if (!hasWallet) {
actions.createWallet()
if (!initialized) {
onInitialize()
}
}, [actions, hasWallet])
}, [initialized, onInitialize])
return (
<Container
@ -26,9 +34,15 @@ export default function GlobalLayout({ children }: Props) {
<Card
bordered
borderWeight="light"
css={{ height: '92vh', maxWidth: '600px', width: '100%' }}
css={{
height: '92vh',
maxWidth: '600px',
width: '100%',
justifyContent: initialized ? 'normal' : 'center',
alignItems: initialized ? 'normal' : 'center'
}}
>
{hasWallet ? (
{initialized ? (
<Fragment>
<Card.Header>Header</Card.Header>
<Divider />

View File

@ -1,15 +1,21 @@
import { ethers } from 'ethers'
import { createContext, ReactNode, useState } from 'react'
import WalletConnectClient from '@walletconnect/client'
import { Wallet } from 'ethers'
import KeyValueStorage from 'keyvaluestorage'
import { createContext, ReactNode, useMemo, useState } from 'react'
/**
* Types
*/
interface State {
wallet: ethers.Wallet
initialized: boolean
wallet: Wallet
walletConnectClient: WalletConnectClient
}
interface Actions {
setInitialized: () => void
createWallet: () => void
createWalletConnectClient: () => Promise<void>
}
interface Context {
@ -27,14 +33,42 @@ interface Props {
export const WalletContext = createContext<Context>({} as Context)
export function WalletContextProvider({ children }: Props) {
const [state, setState] = useState<State>({ wallet: undefined })
const [state, setState] = useState<State>({
initialized: false,
wallet: undefined,
walletConnectClient: undefined
})
const actions = {
createWallet() {
const wallet = ethers.Wallet.createRandom()
setState(s => ({ ...s, wallet }))
}
}
const actions = useMemo(
() => ({
setInitialized() {
setState(s => ({ ...s, initialized: true }))
},
createWallet() {
const wallet = Wallet.createRandom()
setState(s => ({ ...s, wallet }))
},
async createWalletConnectClient() {
const walletConnectClient = await WalletConnectClient.init({
controller: true,
logger: 'debug',
projectId: '8f331b9812e0e5b8f2da2c7203624869',
relayUrl: 'wss://relay.walletconnect.com',
metadata: {
name: 'React Wallet',
description: 'React Wallet for WalletConnect',
url: 'https://walletconnect.com/',
icons: ['https://avatars.githubusercontent.com/u/37784886']
},
storage: new KeyValueStorage()
})
setState(s => ({ ...s, walletConnectClient }))
}
}),
[]
)
return <WalletContext.Provider value={{ state, actions }}>{children}</WalletContext.Provider>
}

View File

@ -1,12 +1,11 @@
import Layout from '@/containers/GlobalLayout'
import { WalletContextProvider } from '@/contexts/WalletContext'
import { theme } from '@/utils/ThemeUtil'
import { NextUIProvider } from '@nextui-org/react'
import { AppProps } from 'next/app'
export default function App({ Component, pageProps }: AppProps) {
return (
<NextUIProvider theme={theme}>
<NextUIProvider>
<WalletContextProvider>
<Layout>
<Component {...pageProps} />

View File

@ -1,5 +0,0 @@
import { createTheme } from '@nextui-org/react'
export const theme = createTheme({
type: 'dark'
})