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

Refactoring single source of truth for dapp state #510

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 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
7 changes: 0 additions & 7 deletions consts/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ export enum LIDO_MULTICHAIN_CHAINS {
'Mode Chain' = 34443,
}

// TODO: move to legacy lido-js-sdk package
export const SDK_LEGACY_SUPPORTED_CHAINS = [
CHAINS.Mainnet,
CHAINS.Holesky,
CHAINS.Sepolia,
];

// TODO: move to @lidofinance/lido-ethereum-sdk package
export const isSDKSupportedL2Chain = (chainId?: CHAINS) => {
return chainId && !!LIDO_L2_CONTRACT_ADDRESSES[chainId];
Expand Down
11 changes: 7 additions & 4 deletions features/ipfs/security-status-banner/use-version-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import buildInfo from 'build-info.json';
import { config } from 'config';
import { useUserConfig } from 'config/user-config';
import { STRATEGY_IMMUTABLE } from 'consts/swr-strategies';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useDappStatus } from 'modules/web3';
import { overrideWithQAMockBoolean } from 'utils/qa';

import { isVersionLess } from './utils';
Expand All @@ -19,7 +19,7 @@ const URL_CID_REGEX =
/[/.](?<cid>Qm[1-9A-HJ-NP-Za-km-z]{44,}|b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,}|z[1-9A-HJ-NP-Za-km-z]{48,}|F[0-9A-F]{50,})([./#?]|$)/;

export const useVersionCheck = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

The name does not reflect the return type. I mean that this hook does not expect to receive remoteCid, currentCid, remoteCidLink (I expect to receive the status).

I can suggest renaming the hook to "useVersionControl" or "useVersionStatus"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am all in on that, but maybe in other PR. Want to focus on daap/chain state.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, lets set TODO

const { isDappActive } = useDappStatus();
const { isWalletConnected } = useDappStatus();
const { setIsWalletConnectionAllowed } = useUserConfig();
const { forceDisconnect } = useForceDisconnect();
const [areConditionsAccepted, setConditionsAccepted] = useState(false);
Expand Down Expand Up @@ -80,14 +80,17 @@ export const useVersionCheck = () => {
if (isVersionUnsafe) {
setIsWalletConnectionAllowed(false);
}
if (isVersionUnsafe || (config.ipfsMode && isNotVerifiable)) {
if (
isVersionUnsafe ||
(config.ipfsMode && isNotVerifiable && isWalletConnected)
) {
forceDisconnect();
}
}, [
isDappActive,
forceDisconnect,
isNotVerifiable,
isVersionUnsafe,
isWalletConnected,
setIsWalletConnectionAllowed,
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { Zero } from '@ethersproject/constants';
import { useRewardsHistory } from 'features/rewards/hooks';
import { ErrorBlockNoSteth } from 'features/rewards/components/errorBlocks/ErrorBlockNoSteth';
import { RewardsTable } from 'features/rewards/components/rewardsTable';
import { useStethBalance } from 'shared/hooks/use-balance';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useStethBalance, useDappStatus } from 'modules/web3';

import { RewardsListsEmpty } from './RewardsListsEmpty';
import { RewardsListErrorMessage } from './RewardsListErrorMessage';
Expand All @@ -20,8 +19,7 @@ import {
import type { Address } from 'viem';

export const RewardsListContent: FC = () => {
const { isWalletConnected, isSupportedChain, isAccountActiveOnL2 } =
useDappStatus();
const { isSupportedChain } = useDappStatus();
const {
address,
error,
Expand All @@ -39,8 +37,7 @@ export const RewardsListContent: FC = () => {
});
const hasSteth = stethBalance?.gt(Zero);

if ((isWalletConnected && !isSupportedChain) || isAccountActiveOnL2)
return <RewardsListsUnsupportedChain />;
if (!isSupportedChain) return <RewardsListsUnsupportedChain />;

if (!data && !initialLoading && !error) return <RewardsListsEmpty />;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Button, Divider } from '@lidofinance/lido-ui';

import { useUserConfig } from 'config/user-config';
import { MATOMO_CLICK_EVENTS } from 'consts/matomo-click-events';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useDappStatus } from 'modules/web3';
import {
RewardsListEmptyButtonWrapper,
RewardsListEmptyWrapper,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { FC } from 'react';
import { useRewardsHistory } from 'features/rewards/hooks';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useDappStatus } from 'modules/web3';

import { LeftOptions } from './LeftOptions';
import { RightOptions } from './RightOptions';
import { RewardsListHeaderStyle } from './styles';
import { TitleStyle } from './styles';

export const RewardsListHeader: FC = () => {
const { isWalletConnected, isSupportedChain, isAccountActiveOnL2 } =
useDappStatus();
const { isSupportedChain, isDappActiveOnL2 } = useDappStatus();
const { error, data } = useRewardsHistory();

return (
Expand All @@ -18,9 +17,7 @@ export const RewardsListHeader: FC = () => {
{!error &&
data &&
data?.events.length > 0 &&
(!isWalletConnected ||
(isWalletConnected && isSupportedChain) ||
!isAccountActiveOnL2) && (
(isSupportedChain || !isDappActiveOnL2) && (
<>
<LeftOptions />
<RightOptions />
Expand Down
7 changes: 3 additions & 4 deletions features/rewards/features/top-card/top-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import { CHAINS } from '@lido-sdk/constants';
import { getConfig } from 'config';
import { StatsWrapper } from 'features/rewards/components/statsWrapper';
import { Stats } from 'features/rewards/components/stats';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useDappStatus } from 'modules/web3';
import { Fallback } from 'shared/wallet';

import { Wallet } from './wallet';

export const TopCard: FC = () => {
const { defaultChain } = getConfig();
const [visible, setVisible] = useState(false);
const { isWalletConnected, isSupportedChain, isAccountActiveOnL2 } =
useDappStatus();
const { isSupportedChain } = useDappStatus();

// fix flash after reload page
useEffect(() => {
Expand All @@ -24,7 +23,7 @@ export const TopCard: FC = () => {

return (
<>
{(isWalletConnected && !isSupportedChain) || isAccountActiveOnL2 ? (
{!isSupportedChain ? (
<Fallback
error={`Unsupported chain. Please switch to ${CHAINS[defaultChain]} in your wallet.`}
/>
Expand Down
2 changes: 1 addition & 1 deletion features/rewards/hooks/useGetCurrentAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useSDK } from '@lido-sdk/react';

import { resolveEns, isValidEns, isValidAddress } from 'features/rewards/utils';
import { useCurrentStaticRpcProvider } from 'shared/hooks/use-current-static-rpc-provider';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useDappStatus } from 'modules/web3';

type UseGetCurrentAddress = () => {
address: string;
Expand Down
4 changes: 2 additions & 2 deletions features/settings/settings-form/settings-form.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useCallback } from 'react';
import { useForm } from 'react-hook-form';

import { useSDK } from '@lido-sdk/react';
import { Button, ToastSuccess, Block, Input } from '@lidofinance/lido-ui';

import { useUserConfig } from 'config/user-config';
Expand All @@ -15,14 +14,15 @@ import {
DescriptionTitle,
SettingsFormWrap,
} from './styles';
import { useDappStatus } from 'modules/web3';

type FormValues = {
rpcUrl: string;
};

export const SettingsForm = () => {
const { savedUserConfig, setSavedUserConfig } = useUserConfig();
const { chainId } = useSDK();
const { chainId } = useDappStatus();

const formMethods = useForm<FormValues>({
mode: 'onChange',
Expand Down
8 changes: 3 additions & 5 deletions features/stake/stake-form/controls/stake-amount-input.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { Eth } from '@lidofinance/lido-ui';
import { TokenAmountInputHookForm } from 'shared/hook-form/controls/token-amount-input-hook-form';
import { useStakeFormData } from '../stake-form-context';
import { useStakingLimitWarning } from 'shared/hooks/use-staking-limit-warning';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useStakingLimitWarning, useDappStatus } from 'modules/web3';

export const StakeAmountInput = () => {
const { isWalletConnected, isDappActive, isAccountActiveOnL2 } =
useDappStatus();
const { isWalletConnected, isDappActive, isDappActiveOnL2 } = useDappStatus();
const { maxAmount, stakingLimitInfo } = useStakeFormData();
const { limitWarning, limitError } = useStakingLimitWarning(
stakingLimitInfo?.stakeLimitLevel,
);

return (
<TokenAmountInputHookForm
disabled={(isWalletConnected && !isDappActive) || isAccountActiveOnL2}
disabled={(isWalletConnected && !isDappActive) || isDappActiveOnL2}
fieldName="amount"
token={'ETH'}
data-testid="stakeInput"
Expand Down
6 changes: 3 additions & 3 deletions features/stake/stake-form/controls/stake-submit-button.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { LIMIT_LEVEL } from 'types';
import { SubmitButtonHookForm } from 'shared/hook-form/controls/submit-button-hook-form';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useDappStatus } from 'modules/web3';

import { useStakeFormData } from '../stake-form-context';

export const StakeSubmitButton = () => {
const { isDappActive, isAccountActiveOnL2 } = useDappStatus();
const { isDappActive, isDappActiveOnL2 } = useDappStatus();
const { stakingLimitInfo } = useStakeFormData();

return (
<SubmitButtonHookForm
disabled={
!isDappActive ||
isAccountActiveOnL2 ||
isDappActiveOnL2 ||
stakingLimitInfo?.stakeLimitLevel === LIMIT_LEVEL.REACHED
}
data-testid="stakeSubmitBtn"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import {
} from 'shared/hook-form/form-controller';
import { useTokenMaxAmount } from 'shared/hooks/use-token-max-amount';
import { useStakingLimitInfo } from 'shared/hooks/useStakingLimitInfo';
import { useMaxGasPrice } from 'shared/hooks';
import { useIsMultisig } from 'shared/hooks/useIsMultisig';
import { useIsMultisig, useMaxGasPrice } from 'modules/web3';
import { useFormControllerRetry } from 'shared/hook-form/form-controller/use-form-controller-retry-delegate';

import { config } from 'config';
Expand All @@ -37,7 +36,7 @@ import {

import { useStake } from '../use-stake';
import { useStethSubmitGasLimit } from '../hooks';
import { useEthereumBalance, useStethBalance } from 'shared/hooks/use-balance';
import { useEthereumBalance, useStethBalance } from 'modules/web3';

//
// Data context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import invariant from 'tiny-invariant';
import { Zero } from '@ethersproject/constants';

import { validateEtherAmount } from 'shared/hook-form/validation/validate-ether-amount';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useDappStatus } from 'modules/web3';
import { VALIDATION_CONTEXT_TIMEOUT } from 'features/withdrawals/withdrawals-constants';
import { handleResolverValidationError } from 'shared/hook-form/validation/validation-error';
import { awaitWithTimeout } from 'utils/await-with-timeout';
Expand Down
6 changes: 2 additions & 4 deletions features/stake/stake-form/use-stake.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BigNumber } from 'ethers';
import { useCallback } from 'react';
import invariant from 'tiny-invariant';
import { useAccount } from 'wagmi';

import {
useSDK,
Expand All @@ -17,8 +16,7 @@ import { runWithTransactionLogger } from 'utils';
import { MockLimitReachedError, getAddress } from './utils';
import { useTxModalStagesStake } from './hooks/use-tx-modal-stages-stake';

import { sendTx } from 'utils/send-tx';
import { useTxConfirmation } from 'shared/hooks/use-tx-conformation';
import { sendTx, useTxConfirmation, useDappStatus } from 'modules/web3';

type StakeArguments = {
amount: BigNumber | null;
Expand All @@ -32,7 +30,7 @@ type StakeOptions = {

export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {
const stethContractWeb3 = useSTETHContractWeb3();
const { address } = useAccount();
const { address } = useDappStatus();
const stethContract = useSTETHContractRPC();
const { staticRpcProvider } = useCurrentStaticRpcProvider();
const { providerWeb3 } = useSDK();
Expand Down
18 changes: 4 additions & 14 deletions features/stake/stake-form/wallet/wallet.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { memo } from 'react';
import { useAccount } from 'wagmi';

import { CHAINS, TOKENS } from '@lido-sdk/constants';
import { Divider, Question, Tooltip } from '@lidofinance/lido-ui';

Expand All @@ -10,9 +8,8 @@ import { LIDO_APR_TOOLTIP_TEXT, DATA_UNAVAILABLE } from 'consts/text';
import { TokenToWallet } from 'shared/components';
import { FormatToken } from 'shared/formatters';
import { useLidoApr } from 'shared/hooks';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useDappStatus } from 'modules/web3';
import { useTokenAddress } from 'shared/hooks/use-token-address';
import { useLidoMultichainFallbackCondition } from 'shared/hooks/use-lido-multichain-fallback-condition';
import {
CardAccount,
CardBalance,
Expand All @@ -28,7 +25,7 @@ import { LimitMeter } from './limit-meter';
import { FlexCenter, LidoAprStyled, StyledCard } from './styles';

const WalletComponent: WalletComponentType = (props) => {
const { address } = useAccount();
const { address } = useDappStatus();
const { stakeableEther, stethBalance, loading } = useStakeFormData();

const stethAddress = useTokenAddress(TOKENS.STETH);
Expand Down Expand Up @@ -101,20 +98,13 @@ const WalletComponent: WalletComponentType = (props) => {

export const Wallet: WalletComponentType = memo((props) => {
const { defaultChain } = getConfig();
const { isWalletConnected, isDappActive, isAccountActiveOnL2 } =
const { isWalletConnected, isDappActive, isLidoMultichainChain } =
useDappStatus();
const { showLidoMultichainFallback } = useLidoMultichainFallbackCondition();

if (showLidoMultichainFallback) {
if (isLidoMultichainChain) {
return <LidoMultichainFallback textEnding={'to stake'} {...props} />;
}

if (isAccountActiveOnL2) {
return (
<LidoMultichainFallback chainId={10} textEnding={'to stake'} {...props} />
);
}

if (isWalletConnected && !isDappActive) {
return (
<Fallback
Expand Down
6 changes: 3 additions & 3 deletions features/stake/stake.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FaqPlaceholder } from 'features/ipfs';
import { useWagmiKey } from 'shared/hooks/use-wagmi-key';
import { SupportOnlyL1Chains, useWagmiKey } from 'modules/web3';
import NoSSRWrapper from 'shared/components/no-ssr-wrapper';
import { OnlyInfraRender } from 'shared/components/only-infra-render';

Expand All @@ -11,14 +11,14 @@ export const Stake = () => {
const key = useWagmiKey();

return (
<>
<SupportOnlyL1Chains>
<NoSSRWrapper>
<StakeForm key={key} />
</NoSSRWrapper>
<LidoStats />
<OnlyInfraRender renderIPFS={<FaqPlaceholder />}>
<StakeFaq />
</OnlyInfraRender>
</>
</SupportOnlyL1Chains>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
FormControllerContext,
FormControllerContextValueType,
} from 'shared/hook-form/form-controller';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useDappStatus } from 'modules/web3';

import { ClaimFormInputType, ClaimFormValidationContext } from './types';
import { claimFormValidationResolver } from './validation';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { forwardRef } from 'react';
import { useFormState, useWatch } from 'react-hook-form';
import { useAccount } from 'wagmi';

import {
Checkbox,
Expand All @@ -15,6 +14,7 @@ import { useClaimFormData, ClaimFormInputType } from '../../claim-form-context';

import { getNFTUrl } from 'utils';
import { RequestStyled, LinkStyled } from './styles';
import { useSDK } from '@lido-sdk/react';

type RequestItemProps = {
token_id: string;
Expand All @@ -24,7 +24,7 @@ type RequestItemProps = {

export const RequestItem = forwardRef<HTMLInputElement, RequestItemProps>(
({ token_id, name, disabled, index, ...props }, ref) => {
const { chainId } = useAccount();
const { chainId } = useSDK();
const { isSubmitting } = useFormState();
const { canSelectMore, maxSelectedCountReason } = useClaimFormData();
const { checked, status } = useWatch<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useFieldArray, useFormContext, useFormState } from 'react-hook-form';
import { useDappStatus } from 'shared/hooks/use-dapp-status';
import { useDappStatus } from 'modules/web3';

import { ClaimFormInputType } from '../../claim-form-context';
import { RequestItem } from './request-item';
Expand Down
Loading
Loading