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

Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
418119d
feat: wire legacy sdk provider trough viem+new sdk
Jeday Oct 14, 2024
a924ee7
fix: use only supported chainId
Jeday Oct 14, 2024
3b37cce
Merge branch 'develop' of github.com:lidofinance/ethereum-staking-wid…
Jeday Oct 15, 2024
a9b4d95
chore: supported chains
Jeday Oct 15, 2024
dd9f550
chore(refactor): web3 module
Jeday Oct 16, 2024
070b24d
chore: remove unused deps
Jeday Oct 16, 2024
c42688e
chore: refactor dapp chain
Jeday Oct 16, 2024
c7605a2
chore: rename capitalize
Jeday Oct 16, 2024
c7b65ef
chore: remove unused code
Jeday Oct 16, 2024
5d3db69
chore: simplify supported chains
Jeday Oct 16, 2024
dedc083
chore: comments
Jeday Oct 16, 2024
69bc124
Merge branch 'develop' of github.com:lidofinance/ethereum-staking-wid…
Jeday Oct 16, 2024
0cf7cad
fix: use dapp status
Jeday Oct 16, 2024
00da103
fix: build
Jeday Oct 16, 2024
0555c2d
Merge branch 'develop' of github.com:lidofinance/ethereum-staking-wid…
Jeday Oct 16, 2024
77964d9
chore(refactor): merge supported chains and dapp chain context
Jeday Oct 18, 2024
35b68a7
chore: web3 transport comments and logging
Jeday Oct 21, 2024
e3f5f44
chore: rework fallback banners and dapp chain
Jeday Oct 21, 2024
297c223
Merge branch 'develop' of github.com:lidofinance/ethereum-staking-wid…
Jeday Oct 21, 2024
02c9531
chore: rename use-version-status
Jeday Oct 22, 2024
1b3783c
fix: use dapp-status
Jeday Oct 23, 2024
3641448
fix: optimize dapp status
Jeday Oct 23, 2024
a6e59fc
fix: build
Jeday Oct 23, 2024
ff8ba33
chore: remove dirived state
Jeday Oct 24, 2024
97d6bcf
chore: remove complex conditions from non l2
Jeday Oct 24, 2024
ae8b886
fix: optimize ether balance query
Jeday Oct 24, 2024
2d112fc
chore: simplify dapp status
Jeday Oct 24, 2024
9df5512
fix: secure DappChainContext
Jeday Oct 24, 2024
68f2ed0
fix: dedup hook
Jeday Oct 25, 2024
a46a64d
Merge branch 'develop' of github.com:lidofinance/ethereum-staking-wid…
Jeday Oct 25, 2024
48bc43e
refactor: a showing the allowance condition
solidovic Oct 25, 2024
58a81b1
refactor: show 'Max transaction cost' if the ChainType not matched
solidovic Oct 28, 2024
e95f2a4
refactor(unwrap): show 'stats' if the ChainType not matched
solidovic Oct 28, 2024
27f5562
chore: comments
solidovic Oct 28, 2024
eddc38b
chore: comments
solidovic Oct 28, 2024
83f8197
refactor: remake the ChainSwitcher to own code
solidovic Oct 28, 2024
611d614
fix: typing
solidovic Oct 28, 2024
e535ccd
refactor: split the ChainSwitcher to files
solidovic Oct 28, 2024
e323f3b
fix: popup position
solidovic Oct 29, 2024
1170532
chore: remove todos
solidovic Oct 29, 2024
ad12dfd
feat: add isChainTypeOnL2 flag
solidovic Oct 29, 2024
5d8231a
chore: add comments
solidovic Oct 29, 2024
ca800ab
refactor: add important check for the chainTypeChainId
solidovic Oct 29, 2024
244c4b8
refactor: simplified the code
solidovic Oct 29, 2024
cc2176f
chore: comments
solidovic Oct 29, 2024
0791588
refactor: renaming vars in useMaxGasPrice
solidovic Oct 29, 2024
1173fc7
refactor: reset the chainType after disconnect
solidovic Oct 29, 2024
2077ca3
refactor: simplify
solidovic Oct 29, 2024
21a0270
refactor: simplify
solidovic Oct 29, 2024
51a4870
refactor: simplify
solidovic Oct 30, 2024
5907924
Merge pull request #524 from lidofinance/feature/si-1637-widget-l2-te…
Jeday Oct 30, 2024
c8eae6f
Merge branch 'develop' into feature/si-1641-refactoring-single-source…
Jeday Oct 30, 2024
8f9c4c2
fix: add mainnet to transport
Jeday Oct 30, 2024
ea5975f
fix: add mainnet to allowed chains
Jeday Oct 30, 2024
3990b0c
fix: show the allowance
solidovic Oct 30, 2024
a210af0
fix: enable all forms for not connected wallet
solidovic Oct 30, 2024
a792167
refactor: wrong network banner text
solidovic Oct 30, 2024
cf659ba
fix: chain switcher on mobiles
solidovic Oct 31, 2024
a421378
fix: pointer for chain switcher
solidovic Oct 31, 2024
3154b21
fix(useWeb3Transport): remove direct change of supportedChains
solidovic Nov 1, 2024
95d8c5b
Merge branch 'develop' into feature/si-1641-refactoring-single-source…
solidovic Nov 1, 2024
c1b358e
chore: lock file
solidovic Nov 1, 2024
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 = () => {
Jeday marked this conversation as resolved.
Show resolved Hide resolved
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}
Jeday marked this conversation as resolved.
Show resolved Hide resolved
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();
DiRaiks marked this conversation as resolved.
Show resolved Hide resolved
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