Skip to content

Commit

Permalink
Merge pull request #92 from lidofinance/fix/autoconnect-terms-handle-…
Browse files Browse the repository at this point in the history
…errors

Handle connection errors in AcceptTermsModal
  • Loading branch information
alx-khramov authored Nov 20, 2023
2 parents 45a6a5a + a7f00be commit ac770e8
Show file tree
Hide file tree
Showing 23 changed files with 171 additions and 50 deletions.
4 changes: 2 additions & 2 deletions apps/demo-react/components/ProviderWeb3WithProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const supportedChains = [holesky, mainnet, goerli];
const supportedChainsIds = supportedChains.map((chain) => chain.id);
const defaultChainId = holesky.id;

const jsonRcpBatchProvider = (chain: Chain) => ({
const jsonRpcBatchProvider = (chain: Chain) => ({
provider: () =>
getStaticRpcBatchProvider(chain.id, getRPCPath(chain.id), undefined, 12000),
chain,
});

const { chains, provider, webSocketProvider } = configureChains(
supportedChains,
[jsonRcpBatchProvider],
[jsonRpcBatchProvider],
);

const connectors = getConnectors({
Expand Down
1 change: 1 addition & 0 deletions apps/demo-react/config/chains.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum CHAINS {
Mainnet = 1,
Goerli = 5,
Holesky = 17000,
}
6 changes: 6 additions & 0 deletions packages/connect-wallet-modal/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @reef-knot/connect-wallet-modal

## 1.10.0

### Minor Changes

- Handle connection errors in AcceptTermsModal

## 1.9.0

### Minor Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/connect-wallet-modal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/connect-wallet-modal",
"version": "1.9.0",
"version": "1.10.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -45,12 +45,12 @@
"@types/react-dom": "17"
},
"devDependencies": {
"@reef-knot/core-react": "^1.5.1",
"@reef-knot/core-react": "^1.7.0",
"@reef-knot/types": "^1.3.0",
"@reef-knot/ui-react": "^1.0.7",
"@reef-knot/wallets-helpers": "^1.1.5",
"@reef-knot/wallets-icons": "^1.2.0",
"@reef-knot/web3-react": "^1.7.0",
"@reef-knot/web3-react": "^1.8.0",
"@reef-knot/ledger-connector": "^1.1.0",
"@types/ua-parser-js": "^0.7.36",
"eslint-config-custom": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Checkbox, CheckboxProps, Link } from '@reef-knot/ui-react';
import { TermsStyle, TermsTextStyle } from './styles';
import { Metrics } from '../WalletsModal';

type WalletModalConnectTermsProps = Pick<
export type WalletModalConnectTermsProps = Pick<
CheckboxProps,
'checked' | 'onChange'
> & { metrics?: Metrics; termsLink: string; privacyNoticeLink: string };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useContext, useState, ReactElement } from 'react';
import { Button, Modal } from '@reef-knot/ui-react';
import React, { useCallback, useContext, useState } from 'react';
import { Modal } from '@reef-knot/ui-react';
import {
AcceptTermsModalContext,
LS_KEY_TERMS_ACCEPTANCE,
Expand All @@ -9,12 +9,13 @@ import {
ButtonsCommonProps,
RequirementsData,
} from './types';
import { Terms } from '../Terms';
import { WalletsButtonsContainer, CommonButtonsContainer } from './styles';
import { Terms, WalletModalConnectTermsProps } from '../Terms';
import { WalletsButtonsContainer } from './styles';
import { NOOP, useLocalStorage } from '../../helpers';
import { LedgerModal } from '../Ledger';
import { AcceptTermsModal } from './components';

export function WalletsModal(props: WalletsModalProps): ReactElement {
export function WalletsModal(props: WalletsModalProps) {
const {
onClose,
shouldInvertWalletIcon = false,
Expand All @@ -33,7 +34,7 @@ export function WalletsModal(props: WalletsModalProps): ReactElement {
setTermsChecked((currentValue: boolean) => !currentValue);
}, [setTermsChecked]);

const termsProps = {
const termsProps: WalletModalConnectTermsProps = {
onChange: handleTermsToggle,
checked: termsChecked,
termsLink: termsLink || 'https://lido.fi/terms-of-use',
Expand Down Expand Up @@ -111,25 +112,13 @@ export function WalletsModal(props: WalletsModalProps): ReactElement {

if (acceptTermsModal?.isVisible) {
return (
<Modal
{...props} // the props are overridden here on purpose
<AcceptTermsModal
open
onClose={undefined} // the modal should not be closable
title="Confirm connection"
>
<Terms {...termsProps} />
<CommonButtonsContainer>
<Button
fullwidth
disabled={!termsChecked}
onClick={() => {
acceptTermsModal?.onContinue?.();
}}
>
Connect
</Button>
</CommonButtonsContainer>
</Modal>
termsProps={termsProps}
termsChecked={termsChecked}
onContinue={acceptTermsModal.onContinue}
error={acceptTermsModal.error}
/>
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { Button, Modal } from '@reef-knot/ui-react';
import styled, { css } from '@reef-knot/ui-react/styled-wrapper';
import { helpers } from '@reef-knot/web3-react';
import { Terms, WalletModalConnectTermsProps } from '../../Terms';
import { CommonButtonsContainer } from '../styles';
import { useReefKnotContext } from '@reef-knot/core-react';

export interface AcceptTermsModalProps {
open: boolean;
termsProps: WalletModalConnectTermsProps;
termsChecked: boolean;
onContinue?: () => unknown;
error: Error | undefined;
}

const ErrorBlock = styled.div`
${({ theme: { fontSizesMap, spaceMap, borderRadiusesMap } }) => css`
background: var(--lido-color-error);
color: var(--lido-color-errorContrast);
font-size: ${fontSizesMap.xxs}px;
line-height: 1.6em;
padding: ${spaceMap.lg}px;
margin-top: ${spaceMap.sm}px;
margin-bottom: ${spaceMap.md}px;
border-radius: ${borderRadiusesMap.lg}px;
`}
`;

export const AcceptTermsModal = ({
open,
termsProps,
termsChecked,
onContinue,
error,
}: AcceptTermsModalProps) => {
const { chains: supportedChains } = useReefKnotContext();
let errorMessage = error?.message;
if (error && error.name == 'UnsupportedChainIdError') {
errorMessage = helpers.getUnsupportedChainError(supportedChains).message;
}

return (
<Modal title="Confirm connection" open={open}>
<Terms {...termsProps} />
{error && <ErrorBlock> {errorMessage} </ErrorBlock>}
<CommonButtonsContainer>
<Button
fullwidth
disabled={!termsChecked}
onClick={() => {
onContinue?.();
}}
>
Connect
</Button>
</CommonButtonsContainer>
</Modal>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './LidoModalLogo';
export * from './AcceptTermsModal';
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ function getWalletsButtons(
});
}

export function WalletsModalForEth(
props: WalletsModalForEthProps,
): JSX.Element {
export function WalletsModalForEth(props: WalletsModalForEthProps) {
const { walletDataList } = useReefKnotContext();

return (
Expand Down
6 changes: 6 additions & 0 deletions packages/core-react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @reef-knot/core-react

## 1.7.0

### Minor Changes

- Handle connection errors in AcceptTermsModal

## 1.6.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/core-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/core-react",
"version": "1.6.0",
"version": "1.7.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down
8 changes: 7 additions & 1 deletion packages/core-react/src/context/acceptTermsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export type AcceptTermsModalContextValue = {
setVisible: React.Dispatch<React.SetStateAction<boolean>>;
onContinue: () => void;
setOnContinue: React.Dispatch<React.SetStateAction<() => void>>;
error?: Error;
setError: React.Dispatch<React.SetStateAction<Error | undefined>>;
};
};

Expand All @@ -28,16 +30,20 @@ export const AcceptTermsModalContextProvider: FC = ({ children }) => {
() => onContinueDefaultValue,
);

const [error, setError] = useState(undefined);

const contextValue = useMemo(
() => ({
acceptTermsModal: {
isVisible: isAcceptTermsModalVisible,
setVisible: setIsAcceptTermsModalVisible,
onContinue: onAcceptTermsModalContinue,
setOnContinue: setOnAcceptTermsModalContinue,
error,
setError,
},
}),
[isAcceptTermsModalVisible, onAcceptTermsModalContinue],
[error, isAcceptTermsModalVisible, onAcceptTermsModalContinue],
);

return (
Expand Down
4 changes: 3 additions & 1 deletion packages/core-react/src/context/reefKnot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ReefKnotContextProps {
export type ReefKnotContextValue = {
rpc: Record<number, string>;
walletDataList: WalletAdapterData[];
chains: Chain[];
};

export const ReefKnotContext = createContext({} as ReefKnotContextValue);
Expand All @@ -37,8 +38,9 @@ export const ReefKnot: FC<ReefKnotContextProps> = ({
() => ({
rpc,
walletDataList,
chains,
}),
[rpc, walletDataList],
[rpc, walletDataList, chains],
);

return (
Expand Down
10 changes: 10 additions & 0 deletions packages/reef-knot/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# reef-knot

## 1.10.6

### Patch Changes

- Updated dependencies
- Updated dependencies
- @reef-knot/connect-wallet-modal@1.10.0
- @reef-knot/core-react@1.7.0
- @reef-knot/web3-react@1.8.0

## 1.10.5

### Patch Changes
Expand Down
8 changes: 4 additions & 4 deletions packages/reef-knot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reef-knot",
"version": "1.10.5",
"version": "1.10.6",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -41,9 +41,9 @@
"lint": "eslint --ext ts,tsx,js,mjs ."
},
"dependencies": {
"@reef-knot/connect-wallet-modal": "1.9.0",
"@reef-knot/core-react": "1.6.0",
"@reef-knot/web3-react": "1.7.0",
"@reef-knot/connect-wallet-modal": "1.10.0",
"@reef-knot/core-react": "1.7.0",
"@reef-knot/web3-react": "1.8.0",
"@reef-knot/ui-react": "1.0.7",
"@reef-knot/wallets-icons": "1.2.0",
"@reef-knot/wallets-list": "1.6.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/web3-react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @reef-knot/web3-react

## 1.8.0

### Minor Changes

- Add holesky to wagmiChainsArray, handle connection errors in AcceptTermsModal

## 1.7.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/web3-react",
"version": "1.7.0",
"version": "1.8.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -49,7 +49,7 @@
"@lido-sdk/constants": "^3.2.0",
"@lido-sdk/providers": "^1.4.13",
"@lido-sdk/react": "^2.0.2",
"@reef-knot/core-react": "^1.5.1",
"@reef-knot/core-react": "^1.7.0",
"@reef-knot/ledger-connector": "^1.1.0",
"@testing-library/react": "^12.1.5",
"@testing-library/react-hooks": "^7.0.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-react/src/context/web3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CHAINS } from '@lido-sdk/constants';
import { getStaticRpcBatchProvider } from '@lido-sdk/providers';
import { ProviderSDK as ProviderSDKBase } from '@lido-sdk/react';
import { useWeb3React, Web3ReactProvider } from '@web3-react/core';
import { ReefKnot } from '@reef-knot/core-react';
import { holesky, ReefKnot } from '@reef-knot/core-react';
import { useAccount } from 'wagmi';
import * as wagmiChains from 'wagmi/chains';
import { SWRConfiguration } from 'swr';
Expand Down Expand Up @@ -122,7 +122,7 @@ const ProviderWeb3: FC<ProviderWeb3Props> = (props) => {
} = props;
const { defaultChainId, supportedChainIds } = props;
const connectorsProps = { rpc, appName, appLogoUrl, defaultChainId };
const wagmiChainsArray = Object.values(wagmiChains);
const wagmiChainsArray = Object.values({ ...wagmiChains, holesky });
const supportedWagmiChains = wagmiChainsArray.filter((chain) =>
supportedChainIds.includes(chain.id),
);
Expand Down
1 change: 1 addition & 0 deletions packages/web3-react/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './ua';
export * from './openWindow';
export { default as isUrl } from './isUrl';
export * from './interceptLedgerError';
export * from './unsupportedChainError';
16 changes: 16 additions & 0 deletions packages/web3-react/src/helpers/unsupportedChainError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Chain } from 'wagmi/chains';

export const getUnsupportedChainError = (supportedChains: Chain[]) => {
// Get names of supported chains to suggest them in case of "unsupported network" error
const supportedChainsNames = (() => {
const chains = supportedChains
.map(({ name }) => name)
.filter((chainName) => chainName !== 'unknown');
const lastChain = chains.pop();
return [chains.join(', '), lastChain].filter((chain) => chain).join(' or ');
})();

return new Error(
`Unsupported chain. Please switch to ${supportedChainsNames} in your wallet and restart the page.`,
);
};
Loading

0 comments on commit ac770e8

Please sign in to comment.