Skip to content

Commit

Permalink
add base & supported chain check
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarnadas committed Jun 5, 2024
1 parent 8a73362 commit d4b65f2
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 164 deletions.
File renamed without changes
File renamed without changes
1 change: 1 addition & 0 deletions public/assets/base.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/assets/base_sepolia.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
155 changes: 17 additions & 138 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
import {
Button,
Callout,
Container,
DropdownMenu,
Flex,
Heading,
Tabs,
Text,
TextField
} from '@radix-ui/themes';
import { Button, Callout, Container, Flex, Heading, Tabs, Text, TextField } from '@radix-ui/themes';
import { useConnectWallet, useSetChain } from '@web3-onboard/react';
import { useEffect, useState } from 'react';

import { Account } from './Account';
import { Assets } from './Assets';
import { DelegateSigner } from './DelegateSigner';
import Arbitrum from './assets/arbitrum.svg?raw';
import ArbitrumSepolia from './assets/arbitrum_sepolia.svg?raw';
import Optimism from './assets/optimism.svg?raw';
import OptimismSepolia from './assets/optimism_sepolia.svg?raw';
import Questionmark from './assets/questionmark.svg?raw';
import { WalletConnection } from './WalletConnection';
import {
DelegateSignerResponse,
exampleDelegateContract,
Expand All @@ -29,7 +15,8 @@ import {
loadBrokerId,
loadContractAddress,
saveBrokerId,
saveContractAddress
saveContractAddress,
supportedChainIds
} from './helpers';

function App() {
Expand All @@ -41,8 +28,8 @@ function App() {
const [showEOA, setShowEOA] = useState<boolean>(false);
const [activeTab, setActiveTab] = useState<string>('account');

const [{ wallet }, connectWallet, disconnectWallet] = useConnectWallet();
const [{ connectedChain }, setChain] = useSetChain();
const [{ wallet }, connectWallet] = useConnectWallet();
const [{ connectedChain }] = useSetChain();

useEffect(() => {
connectWallet();
Expand Down Expand Up @@ -72,29 +59,7 @@ function App() {
}
}, [accountId]);

let chainIcon;
switch (connectedChain?.id) {
case '0xa4b1':
chainIcon = Arbitrum;
break;
case '0xa':
chainIcon = Optimism;
break;
case '0x66eee':
chainIcon = ArbitrumSepolia;
break;
case '0xaa37dc':
chainIcon = OptimismSepolia;
break;
default:
chainIcon = Questionmark;
}

const selectChain = (chainId: string) => () => {
setChain({
chainId
});
};
const isChainSupported = supportedChainIds.includes(Number(connectedChain?.id));

return (
<Flex
Expand All @@ -120,100 +85,7 @@ function App() {

<Flex gap="4" align="end" wrap="wrap">
<Container style={{ width: '100%' }}>
{wallet ? (
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Button>
<img
src={`data:image/svg+xml;base64,${btoa(chainIcon)}`}
style={{ marginRight: '0.3rem', height: '1.8rem' }}
/>
{`${wallet.accounts[0].address.substring(
0,
6
)}...${wallet.accounts[0].address.substr(-4)}`}
</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<DropdownMenu.Label>Mainnet</DropdownMenu.Label>
<DropdownMenu.Item
onSelect={selectChain('0xa4b1')}
style={{
backgroundColor: connectedChain?.id === '0xa4b1' ? 'lightgrey' : undefined,
color: connectedChain?.id === '0xa4b1' ? 'black' : undefined,
fontWeight: connectedChain?.id === '0xa4b1' ? '600' : undefined
}}
>
<img
src={`data:image/svg+xml;base64,${btoa(Arbitrum)}`}
style={{ marginRight: '0.3rem', height: '1.8rem' }}
/>
Arbitrum One
</DropdownMenu.Item>
<DropdownMenu.Item
onSelect={selectChain('0xa')}
style={{
backgroundColor: connectedChain?.id === '0xa' ? 'lightgrey' : undefined,
color: connectedChain?.id === '0xa' ? 'black' : undefined,
fontWeight: connectedChain?.id === '0xa' ? '600' : undefined
}}
>
<img
src={`data:image/svg+xml;base64,${btoa(Optimism)}`}
style={{ marginRight: '0.3rem', height: '1.8rem' }}
/>
OP Mainnet
</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Label>Testnet</DropdownMenu.Label>
<DropdownMenu.Item
onSelect={selectChain('0x66eee')}
style={{
backgroundColor: connectedChain?.id === '0x66eee' ? 'lightgrey' : undefined,
color: connectedChain?.id === '0x66eee' ? 'black' : undefined,
fontWeight: connectedChain?.id === '0x66eee' ? '600' : undefined
}}
>
<img
src={`data:image/svg+xml;base64,${btoa(ArbitrumSepolia)}`}
style={{ marginRight: '0.3rem', height: '1.8rem' }}
/>
Arbitrum Sepolia
</DropdownMenu.Item>
<DropdownMenu.Item
onSelect={selectChain('0xaa37dc')}
style={{
backgroundColor: connectedChain?.id === '0xaa37dc' ? 'lightgrey' : undefined,
color: connectedChain?.id === '0xaa37dc' ? 'black' : undefined,
fontWeight: connectedChain?.id === '0xaa37dc' ? '600' : undefined
}}
>
<img
src={`data:image/svg+xml;base64,${btoa(OptimismSepolia)}`}
style={{ marginRight: '0.3rem', height: '1.8rem' }}
/>
OP Sepolia
</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Item
onSelect={() => {
disconnectWallet({ label: wallet.label });
}}
>
Disconnect
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
) : (
<Button
onClick={async () => {
if (wallet) return;
await connectWallet();
}}
>
Connect wallet
</Button>
)}
<WalletConnection />
</Container>

<Flex gap="4" align="end">
Expand All @@ -229,7 +101,7 @@ function App() {
</label>

<Button
disabled={!brokerId || !connectedChain}
disabled={!brokerId || !connectedChain || !isChainSupported}
onClick={async () => {
if (!brokerId || !wallet || !connectedChain) return;
const userAddress = wallet.accounts[0].address;
Expand Down Expand Up @@ -261,7 +133,8 @@ function App() {
!brokerId ||
!address ||
!connectedChain ||
address.toLowerCase() === wallet?.accounts[0].address.toLowerCase()
address.toLowerCase() === wallet?.accounts[0].address.toLowerCase() ||
!isChainSupported
}
onClick={async () => {
if (!brokerId || !address || !connectedChain) return;
Expand Down Expand Up @@ -328,6 +201,12 @@ function App() {
<Callout.Text>Please insert your Broker ID and Wallet Address.</Callout.Text>
</Callout.Root>
)}

{!isChainSupported && (
<Callout.Root variant="outline" color="red" style={{ alignSelf: 'center' }}>
<Callout.Text>Please connect to a supported chain.</Callout.Text>
</Callout.Root>
)}
</Flex>
);
}
Expand Down
18 changes: 18 additions & 0 deletions src/ConnectWalletButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Button } from '@radix-ui/themes';
import { useConnectWallet } from '@web3-onboard/react';
import { FC } from 'react';

export const ConnectWalletButton: FC = () => {
const [{ wallet }, connectWallet] = useConnectWallet();

return (
<Button
onClick={async () => {
if (wallet) return;
await connectWallet();
}}
>
Connect wallet
</Button>
);
};
85 changes: 85 additions & 0 deletions src/WalletConnection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Button, DropdownMenu } from '@radix-ui/themes';
import { useConnectWallet, useSetChain } from '@web3-onboard/react';
import { FC } from 'react';

import { ConnectWalletButton } from './ConnectWalletButton';
import { supportedChains } from './helpers';

export const WalletConnection: FC = () => {
const [{ wallet }, _, disconnectWallet] = useConnectWallet();
const [{ connectedChain }, setChain] = useSetChain();

const chainIcon =
supportedChains.find(({ id }) => id === connectedChain?.id)?.icon ?? '/assets/questionmark.svg';

const selectChain = (chainId: string) => () => {
setChain({
chainId
});
};

return wallet ? (
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Button>
<img
src={chainIcon}
alt="connected chain"
style={{ marginRight: '0.3rem', height: '1.8rem' }}
/>
{`${wallet.accounts[0].address.substring(
0,
6
)}...${wallet.accounts[0].address.substr(-4)}`}
</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<DropdownMenu.Label>Mainnet</DropdownMenu.Label>
{supportedChains
.filter(({ network }) => network === 'mainnet')
.map(({ id, icon, label }) => (
<DropdownMenu.Item
key={id}
onSelect={selectChain(id)}
style={{
backgroundColor: connectedChain?.id === id ? 'lightgrey' : undefined,
color: connectedChain?.id === id ? 'black' : undefined,
fontWeight: connectedChain?.id === id ? '600' : undefined
}}
>
<img src={icon} alt={label} style={{ marginRight: '0.3rem', height: '1.8rem' }} />
{label}
</DropdownMenu.Item>
))}
<DropdownMenu.Separator />
<DropdownMenu.Label>Testnet</DropdownMenu.Label>
{supportedChains
.filter(({ network }) => network === 'testnet')
.map(({ id, icon, label }) => (
<DropdownMenu.Item
key={id}
onSelect={selectChain(id)}
style={{
backgroundColor: connectedChain?.id === id ? 'lightgrey' : undefined,
color: connectedChain?.id === id ? 'black' : undefined,
fontWeight: connectedChain?.id === id ? '600' : undefined
}}
>
<img src={icon} alt={label} style={{ marginRight: '0.3rem', height: '1.8rem' }} />
{label}
</DropdownMenu.Item>
))}
<DropdownMenu.Separator />
<DropdownMenu.Item
onSelect={() => {
disconnectWallet({ label: wallet.label });
}}
>
Disconnect
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
) : (
<ConnectWalletButton />
);
};
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from './constants';

export * from './constants';
export * from './network';

export const usdFormatter = new Intl.NumberFormat('en-US', { maximumFractionDigits: 2 });

Expand Down
56 changes: 56 additions & 0 deletions src/helpers/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { Chain } from '@web3-onboard/common';

type SupportedChain = Chain & { network: 'mainnet' | 'testnet'; icon: string };

export const supportedChains: SupportedChain[] = [
{
network: 'mainnet',
icon: '/assets/arbitrum.svg',
id: '0xa4b1',
token: 'ETH',
label: 'Arbitrum One',
rpcUrl: 'https://arbitrum-one.publicnode.com'
},
{
network: 'mainnet',
icon: '/assets/optimism.svg',
id: '0xa',
token: 'ETH',
label: 'OP Mainnet',
rpcUrl: 'https://mainnet.optimism.io'
},
{
network: 'mainnet',
icon: '/assets/base.svg',
id: '0x2105',
token: 'ETH',
label: 'Base',
rpcUrl: 'https://base-rpc.publicnode.com'
},
{
network: 'testnet',
icon: '/assets/arbitrum_sepolia.svg',
id: '0x66eee',
token: 'ETH',
label: 'Arbitrum Sepolia',
rpcUrl: 'https://arbitrum-sepolia.publicnode.com'
},
{
network: 'testnet',
icon: '/assets/optimism_sepolia.svg',
id: '0xaa37dc',
token: 'ETH',
label: 'OP Sepolia',
rpcUrl: 'https://optimism-sepolia.publicnode.com'
},
{
network: 'testnet',
icon: '/assets/base_sepolia.svg',
id: '0x14a34',
token: 'ETH',
label: 'Base Sepolia',
rpcUrl: 'https://base-sepolia-rpc.publicnode.com'
}
];

export const supportedChainIds = supportedChains.map(({ id }) => Number(id));
Loading

0 comments on commit d4b65f2

Please sign in to comment.