Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for displaying custom token address #154

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/connectkit/src/components/BalanceButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { useAccount, useBalance, useNetwork } from 'wagmi';
import useIsMounted from '../../hooks/useIsMounted';

import Chain from '../Common/Chain';
import supportedChains from '../../constants/supportedChains';
import supportedChains, { ChainIds } from '../../constants/supportedChains';
import ThemedButton from '../Common/ThemedButton';
import { nFormatter } from '../../utils';
import { useContext } from '../ConnectKit';

const Container = styled(motion.div)`
display: flex;
Expand Down Expand Up @@ -47,14 +48,21 @@ type BalanceProps = {
export const Balance: React.FC<BalanceProps> = ({ hideIcon, hideSymbol }) => {
const isMounted = useIsMounted();
const [isInitial, setIsInitial] = useState(true);
const context = useContext();

const { address } = useAccount();
const { chain } = useNetwork();

const customTokenAddress =
context?.options?.customTokenAddress && chain
? context.options.customTokenAddress[chain.id as ChainIds]
: undefined;

const { data: balance } = useBalance({
address,
chainId: chain?.id,
watch: true,
...(customTokenAddress && { token: customTokenAddress }),
});

const currentChain = supportedChains.find((c) => c.id === chain?.id);
Expand Down Expand Up @@ -112,11 +120,13 @@ export const Balance: React.FC<BalanceProps> = ({ hideIcon, hideSymbol }) => {
</Container>
) : (
<Container>
{!hideIcon && <Chain id={chain?.id} />}
{!hideIcon && !customTokenAddress ? (
<Chain id={chain?.id} />
) : null}
<span style={{ minWidth: 32 }}>
{nFormatter(Number(balance?.formatted))}
</span>
{!hideSymbol && ` ${balance?.symbol}`}
{!hideSymbol || customTokenAddress ? ` ${balance?.symbol}` : null}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think these || customTokenAddress checks should be here, nothing should override the hide options.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK !hideSymbol is not configurable on the consumer side yet? Please correct me if I'm wrong :D

</Container>
)}
</motion.div>
Expand Down
256 changes: 130 additions & 126 deletions packages/connectkit/src/components/Common/ChainSelectList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,146 +68,150 @@ const ChainSelectList: React.FC = () => {
<ChainButtons>
{chains.map((x) => {
const c = supportedChains.find((ch) => ch.id === x.id);
const ch = { ...c, ...x };
return (
<ChainButton
key={`${ch?.id}-${ch?.name}`}
disabled={
!switchNetwork ||
ch.id === chain?.id ||
(isLoading && pendingChainId === ch.id)
}
onClick={() => switchNetwork?.(ch.id)}
style={{
opacity:
!switchNetwork && ch.id !== chain?.id ? 0.4 : undefined,
}}
>
<span

if (c) {
jcheese1 marked this conversation as resolved.
Show resolved Hide resolved
const ch = { ...c, ...x };
return (
<ChainButton
key={`${ch?.id}-${ch?.name}`}
disabled={
!switchNetwork ||
ch.id === chain?.id ||
(isLoading && pendingChainId === ch.id)
}
onClick={() => switchNetwork?.(ch.id)}
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-start',
gap: 12,
color:
ch.id === chain?.id
? 'var(--ck-dropdown-active-color, inherit)'
: 'inherit',
opacity:
!switchNetwork && ch.id !== chain?.id ? 0.4 : undefined,
}}
>
<ChainLogoContainer>
<ChainLogoSpinner
initial={{ opacity: 0 }}
animate={{
opacity: isLoading && pendingChainId === ch.id ? 1 : 0,
}}
transition={{
ease: [0.76, 0, 0.24, 1],
duration: 0.15,
}}
>
<motion.div
key={`${ch?.id}-${ch?.name}`}
animate={
// UI fix for Coinbase Wallet on mobile does not remove isLoading on rejection event
mobile &&
connector?.id === 'coinbaseWallet' &&
isLoading &&
pendingChainId === ch.id
? {
opacity: [1, 0],

transition: { delay: 4, duration: 3 },
}
: { opacity: 1 }
}
>
{Spinner}
</motion.div>
</ChainLogoSpinner>
<ChainIcon>
{ch.logo ?? <ChainIcons.UnknownChain />}
</ChainIcon>
</ChainLogoContainer>
{ch.name}
</span>
<ChainButtonStatus>
<AnimatePresence initial={false} exitBeforeEnter>
{ch.id === chain?.id && (
<motion.span
key={'connectedText'}
style={{
color:
'var(--ck-dropdown-active-color, var(--ck-focus-color))',
display: 'block',
position: 'relative',
}}
initial={{ opacity: 0, x: -4 }}
animate={{ opacity: 1, x: 0 }}
exit={{
opacity: 0,
x: 4,
transition: { duration: 0.1, delay: 0 },
}}
transition={{
ease: [0.76, 0, 0.24, 1],
duration: 0.3,
delay: 0.2,
<span
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-start',
gap: 12,
color:
ch.id === chain?.id
? 'var(--ck-dropdown-active-color, inherit)'
: 'inherit',
}}
>
<ChainLogoContainer>
<ChainLogoSpinner
initial={{ opacity: 0 }}
animate={{
opacity:
isLoading && pendingChainId === ch.id ? 1 : 0,
}}
>
{locales.connected}
</motion.span>
)}
{isLoading && pendingChainId === ch.id && (
<motion.span
key={'approveText'}
style={{
color: 'var(--ck-dropdown-pending-color, inherit)',
display: 'block',
position: 'relative',
}}
initial={{
opacity: 0,
x: -4,
}}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: 4 }}
transition={{
ease: [0.76, 0, 0.24, 1],
duration: 0.3,
duration: 0.15,
}}
>
<motion.span
<motion.div
key={`${ch?.id}-${ch?.name}`}
animate={
// UI fix for Coinbase Wallet on mobile does not remove isLoading on rejection event
mobile &&
connector?.id === 'coinbaseWallet' && {
opacity: [1, 0],
transition: { delay: 4, duration: 4 },
}
connector?.id === 'coinbaseWallet' &&
isLoading &&
pendingChainId === ch.id
? {
opacity: [1, 0],

transition: { delay: 4, duration: 3 },
}
: { opacity: 1 }
}
>
{locales.approveInWallet}
{Spinner}
</motion.div>
</ChainLogoSpinner>
<ChainIcon>
{ch.logo ?? <ChainIcons.UnknownChain />}
</ChainIcon>
</ChainLogoContainer>
{ch.name}
</span>
<ChainButtonStatus>
<AnimatePresence initial={false} exitBeforeEnter>
{ch.id === chain?.id && (
<motion.span
key={'connectedText'}
style={{
color:
'var(--ck-dropdown-active-color, var(--ck-focus-color))',
display: 'block',
position: 'relative',
}}
initial={{ opacity: 0, x: -4 }}
animate={{ opacity: 1, x: 0 }}
exit={{
opacity: 0,
x: 4,
transition: { duration: 0.1, delay: 0 },
}}
transition={{
ease: [0.76, 0, 0.24, 1],
duration: 0.3,
delay: 0.2,
}}
>
{locales.connected}
</motion.span>
)}
{isLoading && pendingChainId === ch.id && (
<motion.span
key={'approveText'}
style={{
color: 'var(--ck-dropdown-pending-color, inherit)',
display: 'block',
position: 'relative',
}}
initial={{
opacity: 0,
x: -4,
}}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: 4 }}
transition={{
ease: [0.76, 0, 0.24, 1],
duration: 0.3,
}}
>
<motion.span
animate={
// UI fix for Coinbase Wallet on mobile does not remove isLoading on rejection event
mobile &&
connector?.id === 'coinbaseWallet' && {
opacity: [1, 0],
transition: { delay: 4, duration: 4 },
}
}
>
{locales.approveInWallet}
</motion.span>
</motion.span>
</motion.span>
)}
</AnimatePresence>
</ChainButtonStatus>
{
//hover === ch.name && (
ch.id === chain?.id && (
<ChainButtonBg
layoutId="activeChain"
layout="position"
transition={{
duration: 0.3,
ease: 'easeOut',
}}
/>
)
}
</ChainButton>
);
)}
</AnimatePresence>
</ChainButtonStatus>
{
//hover === ch.name && (
ch.id === chain?.id && (
<ChainButtonBg
layoutId="activeChain"
layout="position"
transition={{
duration: 0.3,
ease: 'easeOut',
}}
/>
)
}
</ChainButton>
);
}
})}
</ChainButtons>
</ChainButtonContainer>
Expand Down
4 changes: 4 additions & 0 deletions packages/connectkit/src/components/ConnectKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { ThemeProvider } from 'styled-components';
import { useThemeFont } from '../hooks/useGoogleFont';
import { useAccount, useNetwork } from 'wagmi';
import { SIWEContext } from './../siwe';
import { getGlobalChains } from '../defaultClient';
jcheese1 marked this conversation as resolved.
Show resolved Hide resolved
import { ChainIds } from '../constants/supportedChains';
import { useChains } from '../hooks/useChains';

export const routes = {
Expand Down Expand Up @@ -81,6 +83,7 @@ export type ConnectKitOptions = {
ethereumOnboardingUrl?: string;
walletOnboardingUrl?: string;
disableSiweRedirect?: boolean; // Disable redirect to SIWE page after a wallet is connected
customTokenAddress?: Partial<Record<ChainIds, `0x${string}`>>;
};

type ConnectKitProviderProps = {
Expand Down Expand Up @@ -128,6 +131,7 @@ export const ConnectKitProvider: React.FC<ConnectKitProviderProps> = ({
ethereumOnboardingUrl: undefined,
walletOnboardingUrl: undefined,
disableSiweRedirect: false,
customTokenAddress: undefined,
};

const opts: ConnectKitOptions = Object.assign({}, defaultOptions, options);
Expand Down
8 changes: 8 additions & 0 deletions packages/connectkit/src/components/Pages/Profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import CopyToClipboard from '../../Common/CopyToClipboard';
import { AnimatePresence } from 'framer-motion';
import { useThemeContext } from '../../ConnectKitThemeProvider/ConnectKitThemeProvider';
import useLocales from '../../../hooks/useLocales';
import { ChainIds } from '../../../constants/supportedChains';

const Profile: React.FC<{ closeModal?: () => void }> = ({ closeModal }) => {
const context = useContext();
Expand All @@ -51,8 +52,15 @@ const Profile: React.FC<{ closeModal?: () => void }> = ({ closeModal }) => {
chainId: 1,
address: address,
});

const customTokenAddress =
context?.options?.customTokenAddress && chain
? context.options.customTokenAddress[chain.id as ChainIds]
: undefined;

const { data: balance } = useBalance({
address,
...(customTokenAddress && { token: customTokenAddress }),
//watch: true,
});

Expand Down
7 changes: 4 additions & 3 deletions packages/connectkit/src/constants/supportedChains.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ReactNode } from 'react';
import Logos from './../assets/chains';

type Chain = { id: number; name: string; logo: ReactNode };
const supportedChains: Chain[] = [
Comment on lines -4 to -5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why this type has been removed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as const already infers it, so I thought its unnecessary

const supportedChains = [
{
id: 1,
name: 'Ethereum',
Expand Down Expand Up @@ -73,6 +72,8 @@ const supportedChains: Chain[] = [
name: 'Arbitrum Goerli',
logo: <Logos.Arbitrum testnet />,
},
];
] as const;

export type ChainIds = typeof supportedChains[number]['id'];

export default supportedChains;