From 9e9b275f223485b071993e5dd7622fb9eec62851 Mon Sep 17 00:00:00 2001 From: Ilja Date: Fri, 4 Feb 2022 10:54:54 +0200 Subject: [PATCH] Set up ethers / context and wallet creation --- wallets/react-wallet-v2/package.json | 2 +- .../react-wallet-v2/src/components/Layout.tsx | 33 -------------- .../src/containers/GlobalLayout.tsx | 45 +++++++++++++++++++ .../src/contexts/WalletContext.tsx | 28 ++++++++---- wallets/react-wallet-v2/src/pages/_app.tsx | 2 +- .../react-wallet-v2/src/utils/WalletUtil.ts | 0 wallets/react-wallet-v2/yarn.lock | 8 ++-- 7 files changed, 71 insertions(+), 47 deletions(-) delete mode 100644 wallets/react-wallet-v2/src/components/Layout.tsx create mode 100644 wallets/react-wallet-v2/src/containers/GlobalLayout.tsx delete mode 100644 wallets/react-wallet-v2/src/utils/WalletUtil.ts diff --git a/wallets/react-wallet-v2/package.json b/wallets/react-wallet-v2/package.json index 8cb0cc0..55c0e8a 100644 --- a/wallets/react-wallet-v2/package.json +++ b/wallets/react-wallet-v2/package.json @@ -21,7 +21,7 @@ "devDependencies": { "@walletconnect/types": "2.0.0-beta.22", "@types/node": "17.0.14", - "@types/react": "17.0.38", + "@types/react": "17.0.39", "eslint": "8.8.0", "eslint-config-next": "12.0.10", "eslint-config-prettier": "8.3.0", diff --git a/wallets/react-wallet-v2/src/components/Layout.tsx b/wallets/react-wallet-v2/src/components/Layout.tsx deleted file mode 100644 index 897f71b..0000000 --- a/wallets/react-wallet-v2/src/components/Layout.tsx +++ /dev/null @@ -1,33 +0,0 @@ -import { Card, Container, Divider } from '@nextui-org/react' -import { ReactNode } from 'react' - -interface Props { - children: ReactNode | ReactNode[] -} - -export default function Layout({ children }: Props) { - return ( - - - Header - - - - {children} - - - - Footer - - - ) -} diff --git a/wallets/react-wallet-v2/src/containers/GlobalLayout.tsx b/wallets/react-wallet-v2/src/containers/GlobalLayout.tsx new file mode 100644 index 0000000..381adba --- /dev/null +++ b/wallets/react-wallet-v2/src/containers/GlobalLayout.tsx @@ -0,0 +1,45 @@ +import { WalletContext } from '@/contexts/WalletContext' +import { Card, Container, Divider, Loading } from '@nextui-org/react' +import { Fragment, ReactNode, 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 + + useEffect(() => { + if (!hasWallet) { + actions.createWallet() + } + }, [actions, hasWallet]) + + return ( + + + {hasWallet ? ( + + Header + + {children} + + Footer + + ) : ( + + )} + + + ) +} diff --git a/wallets/react-wallet-v2/src/contexts/WalletContext.tsx b/wallets/react-wallet-v2/src/contexts/WalletContext.tsx index 35aa393..61648e2 100644 --- a/wallets/react-wallet-v2/src/contexts/WalletContext.tsx +++ b/wallets/react-wallet-v2/src/contexts/WalletContext.tsx @@ -1,27 +1,39 @@ +import { ethers } from 'ethers' import { createContext, ReactNode, useState } from 'react' /** * Types */ -interface State {} +interface State { + wallet: ethers.Wallet +} + +interface Actions { + createWallet: () => void +} + +interface Context { + state: State + actions: Actions +} interface Props { children: ReactNode | ReactNode[] } /** - * Context + * Context / Provider */ -export const WalletContext = createContext({}) +export const WalletContext = createContext({} as Context) -/** - * Provider - */ export function WalletContextProvider({ children }: Props) { - const [state, setState] = useState({}) + const [state, setState] = useState({ wallet: undefined }) const actions = { - async initialise() {} + createWallet() { + const wallet = ethers.Wallet.createRandom() + setState(s => ({ ...s, wallet })) + } } return {children} diff --git a/wallets/react-wallet-v2/src/pages/_app.tsx b/wallets/react-wallet-v2/src/pages/_app.tsx index a84ce84..ca36d7d 100644 --- a/wallets/react-wallet-v2/src/pages/_app.tsx +++ b/wallets/react-wallet-v2/src/pages/_app.tsx @@ -1,4 +1,4 @@ -import Layout from '@/components/Layout' +import Layout from '@/containers/GlobalLayout' import { WalletContextProvider } from '@/contexts/WalletContext' import { theme } from '@/utils/ThemeUtil' import { NextUIProvider } from '@nextui-org/react' diff --git a/wallets/react-wallet-v2/src/utils/WalletUtil.ts b/wallets/react-wallet-v2/src/utils/WalletUtil.ts deleted file mode 100644 index e69de29..0000000 diff --git a/wallets/react-wallet-v2/yarn.lock b/wallets/react-wallet-v2/yarn.lock index f1f7c15..895b7e7 100644 --- a/wallets/react-wallet-v2/yarn.lock +++ b/wallets/react-wallet-v2/yarn.lock @@ -565,10 +565,10 @@ resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.4.tgz#fcf7205c25dff795ee79af1e30da2c9790808f11" integrity sha512-rZ5drC/jWjrArrS8BR6SIr4cWpW09RNTYt9AMZo3Jwwif+iacXAqgVjm0B0Bv/S1jhDXKHqRVNCbACkJ89RAnQ== -"@types/react@17.0.38": - version "17.0.38" - resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.38.tgz#f24249fefd89357d5fa71f739a686b8d7c7202bd" - integrity sha512-SI92X1IA+FMnP3qM5m4QReluXzhcmovhZnLNm3pyeQlooi02qI7sLiepEYqT678uNiyc25XfCqxREFpy3W7YhQ== +"@types/react@17.0.39": + version "17.0.39" + resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.39.tgz#d0f4cde092502a6db00a1cded6e6bf2abb7633ce" + integrity sha512-UVavlfAxDd/AgAacMa60Azl7ygyQNRwC/DsHZmKgNvPmRR5p70AJ5Q9EAmL2NWOJmeV+vVUI4IAP7GZrN8h8Ug== dependencies: "@types/prop-types" "*" "@types/scheduler" "*"