Init wallet and wallet connect client
This commit is contained in:
parent
9e9b275f22
commit
1f71001175
@ -9,7 +9,7 @@ export default function PageHeader({ children }: Props) {
|
|||||||
<Text
|
<Text
|
||||||
h3
|
h3
|
||||||
css={{
|
css={{
|
||||||
textGradient: '45deg, $cyan300 -30%, $green600 100%'
|
textGradient: '45deg, $primary -30%, $secondary 100%'
|
||||||
}}
|
}}
|
||||||
weight="bold"
|
weight="bold"
|
||||||
>
|
>
|
||||||
|
@ -1,20 +1,28 @@
|
|||||||
import { WalletContext } from '@/contexts/WalletContext'
|
import { WalletContext } from '@/contexts/WalletContext'
|
||||||
import { Card, Container, Divider, Loading } from '@nextui-org/react'
|
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 {
|
interface Props {
|
||||||
children: ReactNode | ReactNode[]
|
children: ReactNode | ReactNode[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function GlobalLayout({ children }: Props) {
|
export default function GlobalLayout({ children }: Props) {
|
||||||
const { state, actions } = useContext(WalletContext)
|
const {
|
||||||
const hasWallet = state.wallet !== undefined
|
state: { initialized },
|
||||||
|
actions
|
||||||
|
} = useContext(WalletContext)
|
||||||
|
|
||||||
|
const onInitialize = useCallback(async () => {
|
||||||
|
actions.createWallet()
|
||||||
|
await actions.createWalletConnectClient()
|
||||||
|
actions.setInitialized()
|
||||||
|
}, [actions])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!hasWallet) {
|
if (!initialized) {
|
||||||
actions.createWallet()
|
onInitialize()
|
||||||
}
|
}
|
||||||
}, [actions, hasWallet])
|
}, [initialized, onInitialize])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container
|
<Container
|
||||||
@ -26,9 +34,15 @@ export default function GlobalLayout({ children }: Props) {
|
|||||||
<Card
|
<Card
|
||||||
bordered
|
bordered
|
||||||
borderWeight="light"
|
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>
|
<Fragment>
|
||||||
<Card.Header>Header</Card.Header>
|
<Card.Header>Header</Card.Header>
|
||||||
<Divider />
|
<Divider />
|
||||||
|
@ -1,15 +1,21 @@
|
|||||||
import { ethers } from 'ethers'
|
import WalletConnectClient from '@walletconnect/client'
|
||||||
import { createContext, ReactNode, useState } from 'react'
|
import { Wallet } from 'ethers'
|
||||||
|
import KeyValueStorage from 'keyvaluestorage'
|
||||||
|
import { createContext, ReactNode, useMemo, useState } from 'react'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Types
|
* Types
|
||||||
*/
|
*/
|
||||||
interface State {
|
interface State {
|
||||||
wallet: ethers.Wallet
|
initialized: boolean
|
||||||
|
wallet: Wallet
|
||||||
|
walletConnectClient: WalletConnectClient
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Actions {
|
interface Actions {
|
||||||
|
setInitialized: () => void
|
||||||
createWallet: () => void
|
createWallet: () => void
|
||||||
|
createWalletConnectClient: () => Promise<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Context {
|
interface Context {
|
||||||
@ -27,14 +33,42 @@ interface Props {
|
|||||||
export const WalletContext = createContext<Context>({} as Context)
|
export const WalletContext = createContext<Context>({} as Context)
|
||||||
|
|
||||||
export function WalletContextProvider({ children }: Props) {
|
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 = useMemo(
|
||||||
|
() => ({
|
||||||
|
setInitialized() {
|
||||||
|
setState(s => ({ ...s, initialized: true }))
|
||||||
|
},
|
||||||
|
|
||||||
const actions = {
|
|
||||||
createWallet() {
|
createWallet() {
|
||||||
const wallet = ethers.Wallet.createRandom()
|
const wallet = Wallet.createRandom()
|
||||||
setState(s => ({ ...s, wallet }))
|
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>
|
return <WalletContext.Provider value={{ state, actions }}>{children}</WalletContext.Provider>
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
import Layout from '@/containers/GlobalLayout'
|
import Layout from '@/containers/GlobalLayout'
|
||||||
import { WalletContextProvider } from '@/contexts/WalletContext'
|
import { WalletContextProvider } from '@/contexts/WalletContext'
|
||||||
import { theme } from '@/utils/ThemeUtil'
|
|
||||||
import { NextUIProvider } from '@nextui-org/react'
|
import { NextUIProvider } from '@nextui-org/react'
|
||||||
import { AppProps } from 'next/app'
|
import { AppProps } from 'next/app'
|
||||||
|
|
||||||
export default function App({ Component, pageProps }: AppProps) {
|
export default function App({ Component, pageProps }: AppProps) {
|
||||||
return (
|
return (
|
||||||
<NextUIProvider theme={theme}>
|
<NextUIProvider>
|
||||||
<WalletContextProvider>
|
<WalletContextProvider>
|
||||||
<Layout>
|
<Layout>
|
||||||
<Component {...pageProps} />
|
<Component {...pageProps} />
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
import { createTheme } from '@nextui-org/react'
|
|
||||||
|
|
||||||
export const theme = createTheme({
|
|
||||||
type: 'dark'
|
|
||||||
})
|
|
Loading…
Reference in New Issue
Block a user