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

feat: Add chain RPC health test and warning banner #329

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
"@vercel/functions": "^1.5.0",
"bignumber.js": "^9.1.2",
"buffer": "^6.0.3",
"clsx": "^2.1.1",
"cosmjs-types": "^0.9.0",
"formik": "^2.4.6",
"framer-motion": "^10.16.4",
20 changes: 14 additions & 6 deletions src/components/banner/FormWarningBanner.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import clsx from 'clsx';
import { ComponentProps } from 'react';
import { WarningBanner } from '../../components/banner/WarningBanner';
import { cardStyles } from '../layout/Card';

export function FormWarningBanner({ className, ...props }: ComponentProps<typeof WarningBanner>) {
export function FormWarningBanner({
className,
isVisible,
...props
}: ComponentProps<typeof WarningBanner>) {
return (
<WarningBanner
className={`z-10 ${cardStyles.inverseMargin} mb-0 sm:mb-0 md:mb-0 ${className}`}
{...props}
/>
<div>
<WarningBanner
className={clsx('absolute left-0 right-0 top-0 z-10', className)}
isVisible={isVisible}
{...props}
/>
<div className={clsx('transition-all duration-500', isVisible ? 'pb-10' : 'pb-0')}></div>
</div>
);
}
2 changes: 1 addition & 1 deletion src/components/banner/WarningBanner.tsx
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ export function WarningBanner({
return (
<div
className={`flex items-center justify-between gap-2 bg-amber-400 px-4 text-sm ${
isVisible ? 'mb-2 max-h-28 py-2' : 'mb-0 max-h-0'
isVisible ? 'max-h-28 py-2' : 'max-h-0'
} overflow-hidden transition-all duration-500 ${className}`}
>
<div className="flex items-center gap-2">
9 changes: 1 addition & 8 deletions src/components/layout/Card.tsx
Original file line number Diff line number Diff line change
@@ -7,16 +7,9 @@ interface Props {
export function Card({ className, children }: PropsWithChildren<Props>) {
return (
<div
className={`${cardStyles.padding} relative overflow-auto rounded-2xl bg-white ${className}`}
className={`relative overflow-auto rounded-2xl bg-white p-1.5 xs:p-2 sm:p-3 md:p-4 ${className}`}
>
{children}
</div>
);
}

export const cardStyles = {
padding: 'p-1.5 xs:p-2 sm:p-3 md:p-4',
// Should be inverse of cardPadding, used when something
// should be flush with card edge
inverseMargin: '-m-1.5 xs:-m-2 sm:-m-3 md:-m-4',
};
75 changes: 75 additions & 0 deletions src/features/chains/ChainConnectionWarning.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { ChainMetadata, isRpcHealthy } from '@hyperlane-xyz/sdk';
import { useQuery } from '@tanstack/react-query';
import { useState } from 'react';
import { FormWarningBanner } from '../../components/banner/FormWarningBanner';
import { logger } from '../../utils/logger';
import { ChainSelectListModal } from './ChainSelectModal';
import { useMultiProvider } from './hooks';
import { getChainDisplayName } from './utils';

export function ChainConnectionWarning({
origin,
destination,
}: {
origin: ChainName;
destination: ChainName;
}) {
const multiProvider = useMultiProvider();
const originMetadata = multiProvider.getChainMetadata(origin);
const destinationMetadata = multiProvider.getChainMetadata(destination);

const { data } = useQuery({
queryKey: ['ChainConnectionWarning', originMetadata, destinationMetadata],
queryFn: async () => {
const isOriginHealthy = await checkRpcHealth(originMetadata);
const isDestinationHealthy = await checkRpcHealth(destinationMetadata);
return { isOriginHealthy, isDestinationHealthy };
},
refetchInterval: 5000,
});

const unhealthyChain =
data &&
((!data.isOriginHealthy && originMetadata) ||
(!data.isDestinationHealthy && destinationMetadata) ||
undefined);

const displayName = getChainDisplayName(
multiProvider,
unhealthyChain?.name || originMetadata.name,
true,
);

const [isModalOpen, setIsModalOpen] = useState(false);

const onClickEdit = () => {
if (!unhealthyChain) return;
setIsModalOpen(true);
};

return (
<>
<FormWarningBanner isVisible={!!unhealthyChain} cta="Edit" onClick={onClickEdit}>
{`Connection to ${displayName} is unstable. Consider adding a more reliable RPC URL.`}
</FormWarningBanner>
<ChainSelectListModal
isOpen={isModalOpen}
close={() => setIsModalOpen(false)}
onSelect={() => {}}
showChainDetails={unhealthyChain?.name}
/>
</>
);
}

async function checkRpcHealth(chainMetadata: ChainMetadata) {
try {
// Note: this currently checks the health of only the first RPC,
// which is what wallets and wallet libs (e.g. wagmi) will use
const isHealthy = await isRpcHealthy(chainMetadata, 0);
return isHealthy;
} catch (error) {
logger.warn('Error checking RPC health', error);
return false;
}
}
13 changes: 7 additions & 6 deletions src/features/chains/ChainSelectModal.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { ChainMetadata } from '@hyperlane-xyz/sdk';
import { ChainSearchMenu, ChainSearchMenuProps, Modal } from '@hyperlane-xyz/widgets';
import { useStore } from '../store';
import { useMultiProvider } from './hooks';

export function ChainSelectListModal({
isOpen,
close,
onSelect,
customListItemField,
showChainDetails,
}: {
isOpen: boolean;
close: () => void;
onSelect: (chain: ChainName) => void;
customListItemField: ChainSearchMenuProps['customListItemField'];
customListItemField?: ChainSearchMenuProps['customListItemField'];
showChainDetails?: ChainSearchMenuProps['showChainDetails'];
}) {
const multiProvider = useMultiProvider();

const { chainMetadataOverrides, setChainMetadataOverrides } = useStore((s) => ({
const { chainMetadata, chainMetadataOverrides, setChainMetadataOverrides } = useStore((s) => ({
chainMetadata: s.chainMetadata,
chainMetadataOverrides: s.chainMetadataOverrides,
setChainMetadataOverrides: s.setChainMetadataOverrides,
}));
@@ -29,12 +29,13 @@ export function ChainSelectListModal({
return (
<Modal isOpen={isOpen} close={close} panelClassname="p-4 sm:p-5 max-w-lg min-h-[40vh]">
<ChainSearchMenu
chainMetadata={multiProvider.metadata}
chainMetadata={chainMetadata}
onClickChain={onSelectChain}
overrideChainMetadata={chainMetadataOverrides}
onChangeOverrideMetadata={setChainMetadataOverrides}
customListItemField={customListItemField}
defaultSortField="custom"
showChainDetails={showChainDetails}
/>
</Modal>
);
10 changes: 5 additions & 5 deletions src/features/chains/ChainWalletWarning.tsx
Original file line number Diff line number Diff line change
@@ -7,29 +7,29 @@ import { logger } from '../../utils/logger';
import { useMultiProvider } from './hooks';
import { getChainDisplayName } from './utils';

export function ChainWalletWarning({ originChain }: { originChain: ChainName }) {
export function ChainWalletWarning({ origin }: { origin: ChainName }) {
const multiProvider = useMultiProvider();

const wallets = useWalletDetails();
const connectFns = useConnectFns();
const disconnectFns = useDisconnectFns();

const { isVisible, chainDisplayName, walletWhitelist, connectFn, disconnectFn } = useMemo(() => {
const protocol = multiProvider.tryGetProtocol(originChain);
const walletWhitelist = config.chainWalletWhitelists[originChain]?.map((w) =>
const protocol = multiProvider.tryGetProtocol(origin);
const walletWhitelist = config.chainWalletWhitelists[origin]?.map((w) =>
w.trim().toLowerCase(),
);
if (!protocol || !walletWhitelist?.length)
return { isVisible: false, chainDisplayName: '', walletWhitelist: [] };

const chainDisplayName = getChainDisplayName(multiProvider, originChain, true);
const chainDisplayName = getChainDisplayName(multiProvider, origin, true);
const walletName = wallets[protocol]?.name?.trim()?.toLowerCase();
const connectFn = connectFns[protocol];
const disconnectFn = disconnectFns[protocol];
const isVisible = !!walletName && !walletWhitelist.includes(walletName);

return { isVisible, chainDisplayName, walletWhitelist, connectFn, disconnectFn };
}, [multiProvider, originChain, wallets, connectFns, disconnectFns]);
}, [multiProvider, origin, wallets, connectFns, disconnectFns]);

const onClickChange = () => {
if (!connectFn || !disconnectFn) return;
18 changes: 15 additions & 3 deletions src/features/transfer/TransferTokenForm.tsx
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import { TextField } from '../../components/input/TextField';
import { config } from '../../consts/config';
import { Color } from '../../styles/Color';
import { logger } from '../../utils/logger';
import { ChainConnectionWarning } from '../chains/ChainConnectionWarning';
import { ChainSelectField } from '../chains/ChainSelectField';
import { ChainWalletWarning } from '../chains/ChainWalletWarning';
import { useChainDisplayName, useMultiProvider } from '../chains/hooks';
@@ -64,9 +65,9 @@ export function TransferTokenForm() {
validateOnChange={false}
validateOnBlur={false}
>
{({ isValidating, values }) => (
{({ isValidating }) => (
<Form className="flex w-full flex-col items-stretch">
<ChainWalletWarning originChain={values.origin} />
<WarningBanners />
<ChainSelectSection isReview={isReview} />
<div className="mt-3.5 flex items-end justify-between space-x-4">
<TokenSection setIsNft={setIsNft} isReview={isReview} />
@@ -126,7 +127,7 @@ function ChainSelectSection({ isReview }: { isReview: boolean }) {
}, [values.destination, warpCore]);

return (
<div className="mt-4 flex items-center justify-between gap-4">
<div className="mt-2 flex items-center justify-between gap-4">
<ChainSelectField
name="origin"
label="From"
@@ -435,6 +436,17 @@ function ReviewDetails({ visible }: { visible: boolean }) {
);
}

function WarningBanners() {
const { values } = useFormikContext<TransferFormValues>();
return (
// Max height to prevent double padding if multiple warnings are visible
<div className="max-h-10">
<ChainWalletWarning origin={values.origin} />
<ChainConnectionWarning origin={values.origin} destination={values.destination} />
</div>
);
}

function useFormInitialValues(): TransferFormValues {
const warpCore = useWarpCore();
return useMemo(() => {
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -3997,6 +3997,7 @@ __metadata:
autoprefixer: "npm:^10.4.20"
bignumber.js: "npm:^9.1.2"
buffer: "npm:^6.0.3"
clsx: "npm:^2.1.1"
cosmjs-types: "npm:^0.9.0"
eslint: "npm:^9.14.0"
eslint-config-next: "npm:^15.0.2"