Skip to content

Commit

Permalink
Merge pull request #18 from NikhilSharma03/auto_wallet_connect
Browse files Browse the repository at this point in the history
feat: add `auto connect` wallet
  • Loading branch information
NikhilSharma03 committed Dec 30, 2023
2 parents 825f900 + 509b89b commit ccf38eb
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 1,890 deletions.
1,866 changes: 63 additions & 1,803 deletions ui/package-lock.json

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"dependencies": {
"@reduxjs/toolkit": "^1.8.1",
"@tippyjs/react": "^4.2.6",
"@walletconnect/web3-provider": "^1.8.0",
"axios": "^0.26.1",
"notistack": "^3.0.1",
"react": "^18.0.0",
Expand All @@ -26,8 +25,7 @@
"redux-thunk": "^2.4.1",
"styled-components": "^5.3.5",
"web-vitals": "^2.1.4",
"web3": "^1.7.3",
"web3modal": "^1.9.9"
"web3": "^1.7.3"
},
"devDependencies": {
"@types/node": "^16.11.26",
Expand All @@ -37,6 +35,7 @@
"assert": "^2.0.0",
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.0",
"gh-pages": "^5.0.0",
"https-browserify": "^1.0.0",
"os-browserify": "^0.3.0",
"prettier": "2.6.2",
Expand All @@ -45,8 +44,7 @@
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"typescript": "^4.6.3",
"url": "^0.11.0",
"gh-pages": "^5.0.0"
"url": "^0.11.0"
},
"browserslist": {
"production": [
Expand Down
16 changes: 14 additions & 2 deletions ui/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import { ThemeProvider } from 'styled-components';
import { SnackbarProvider } from 'notistack';
Expand All @@ -9,7 +9,9 @@ import Layout from './components/Layout/layout';

import themeScheme from './theme/schema';

import { useTheme } from './hooks/hooks';
import { useAppDispatch, useTheme } from './hooks/hooks';

import { onConnectWallet } from './redux/actions/user';

import {
Campaign,
Expand All @@ -26,6 +28,16 @@ const App: React.FC = () => {
return selected === 'light' ? themeScheme.light : themeScheme.dark;
};

const dispatch = useAppDispatch();
const connectWallet = () => dispatch(onConnectWallet());

useEffect(() => {
const userConnect = localStorage.getItem('user-address');
if (userConnect) {
connectWallet();
}
}, []);

return (
<ThemeProvider theme={getTheme(currentTheme)}>
<GlobalStyle />
Expand Down
103 changes: 61 additions & 42 deletions ui/src/components/Navbar/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
UserAuthBtn,
} from './navbar.style';

import Metamask from '../Modals/Metamask/metamask';

import { IoMdCreate } from 'react-icons/io';
import { MdCampaign } from 'react-icons/md';
import { FaHome, FaWallet } from 'react-icons/fa';
Expand All @@ -20,6 +22,8 @@ import { useAppDispatch, useAppSelector } from './../../hooks/hooks';
import { formatAddress } from '../../utils/formatAddress';

import { onConnectWallet } from './../../redux/actions/user';
import { onClearUserError } from './../../redux/reducers/user';
import ErrorModal from '../Modals/Error/error';

interface Props {
toggleSideDrawer: React.MouseEventHandler<HTMLDivElement> | undefined;
Expand All @@ -34,9 +38,11 @@ const Navbar: React.FC<Props> = ({ toggleSideDrawer }) => {
const userWalletAccount: string = useAppSelector(
(state) => state.users.userWalletAccount
);
const error: string = useAppSelector((state) => state.users.error);

const dispatch = useAppDispatch();
const connectWallet = () => dispatch(onConnectWallet());
const clearUserError = () => dispatch(onClearUserError());

const navbarScrollHandler = () => {
if (window.scrollY >= 100) {
Expand All @@ -53,50 +59,63 @@ const Navbar: React.FC<Props> = ({ toggleSideDrawer }) => {
}, []);

return (
<Container changeNavbarColor={changeNavbarColor}>
<Title>
<Link to="/">
AEI<span>OU</span>
</Link>
</Title>
{isWalletConnected && (
<NavMain>
<Nav>
<NavItems>
<NavItem to="/">
<FaHome size={25} color="#ccc" /> Home
</NavItem>
</NavItems>
<NavItems>
<NavItem to="/campaigns">
<MdCampaign size={25} color="#ccc" /> Campaigns
</NavItem>
</NavItems>
<NavItems>
<NavItem to="/campaigns/new">
<IoMdCreate size={25} color="#ccc" /> New
Campaign
</NavItem>
</NavItems>
</Nav>
</NavMain>
<>
{error === 'Please Install Metamask' && (
<Metamask showModal={!!error} closeModal={clearUserError} />
)}
{error !== 'Please Install Metamask' && (
<ErrorModal
content={error}
showModal={!!error}
closeModal={clearUserError}
/>
)}
<SideBarMenu onClick={toggleSideDrawer}>
<div></div>
<div></div>
<div></div>
</SideBarMenu>
<UserAuthBtn onClick={connectWallet}>
{isWalletConnected ? (
<div>
<FaWallet size={15} color="#ccc" />{' '}
<span>{formatAddress(userWalletAccount)}</span>
</div>
) : (
'Connect Wallet'
<Container changeNavbarColor={changeNavbarColor}>
<Title>
<Link to="/">
AEI<span>OU</span>
</Link>
</Title>
{isWalletConnected && (
<NavMain>
<Nav>
<NavItems>
<NavItem to="/">
<FaHome size={25} color="#ccc" /> Home
</NavItem>
</NavItems>
<NavItems>
<NavItem to="/campaigns">
<MdCampaign size={25} color="#ccc" />{' '}
Campaigns
</NavItem>
</NavItems>
<NavItems>
<NavItem to="/campaigns/new">
<IoMdCreate size={25} color="#ccc" /> New
Campaign
</NavItem>
</NavItems>
</Nav>
</NavMain>
)}
</UserAuthBtn>
</Container>
<SideBarMenu onClick={toggleSideDrawer}>
<div></div>
<div></div>
<div></div>
</SideBarMenu>
<UserAuthBtn onClick={connectWallet}>
{isWalletConnected ? (
<div>
<FaWallet size={15} color="#ccc" />{' '}
<span>{formatAddress(userWalletAccount)}</span>
</div>
) : (
'Connect Wallet'
)}
</UserAuthBtn>
</Container>
</>
);
};

Expand Down
6 changes: 1 addition & 5 deletions ui/src/pages/Campaign/campaign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,12 @@ const Campaign: React.FC = () => {
};

useEffect(() => {
if (!isWalletConnected) {
navigate('/');
return;
}
if (web3) {
dispatch(
onGetContractByAddress({ address: String(address), web3 })
);
}
}, []);
}, [isWalletConnected]);

return (
<React.Fragment>
Expand Down
6 changes: 1 addition & 5 deletions ui/src/pages/Campaigns/campaigns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@ const Campaigns: React.FC = () => {
const clearContractError = () => dispatch(onClearContractError());

useEffect(() => {
if (!isWalletConnected) {
navigate('/');
return;
}
if (web3) {
dispatch(onGetAllContracts(web3));
}
}, []);
}, [isWalletConnected]);

return (
<React.Fragment>
Expand Down
6 changes: 0 additions & 6 deletions ui/src/pages/NewCampaign/newcampaign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,6 @@ const NewCampaign: React.FC = () => {
);
};

useEffect(() => {
if (!isWalletConnected) {
navigate('/');
}
}, []);

return (
<Container>
<ErrorModal
Expand Down
18 changes: 13 additions & 5 deletions ui/src/redux/actions/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createAsyncThunk } from '@reduxjs/toolkit';
import web3Modal from '../../utils/web3modal';
import Web3 from 'web3';

export const toHex = (num) => {
Expand All @@ -13,15 +12,24 @@ export const onConnectWallet = createAsyncThunk<
{ rejectValue: string }
>('user/connectWallet', async (_, { rejectWithValue }) => {
try {
const provider = await web3Modal.connect();
const web3 = new Web3(provider);
if (typeof window.ethereum === 'undefined') {
return rejectWithValue('Please Install Metamask');
}

const provider = window.ethereum;

const accounts = await provider.request({
method: 'eth_requestAccounts',
});

provider.request({
await provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: toHex(5) }],
});

const accounts = await web3.eth.getAccounts();
const web3 = new Web3(provider);

localStorage.setItem('user-address', accounts[0].toLowerCase());

return { web3, account: accounts[0].toLowerCase() };
} catch (err) {
Expand Down
17 changes: 0 additions & 17 deletions ui/src/utils/web3modal.ts

This file was deleted.

0 comments on commit ccf38eb

Please sign in to comment.