Skip to content

Commit

Permalink
Merge pull request #427 from lidofinance/feature/si-1198-use-ethereum…
Browse files Browse the repository at this point in the history
…-sdk-on-stake-widget

Wagmi + Lido SDK
  • Loading branch information
Vorobeyko authored Sep 6, 2024
2 parents ecb696b + 5b3e5df commit 6f26854
Show file tree
Hide file tree
Showing 28 changed files with 969 additions and 340 deletions.
4 changes: 3 additions & 1 deletion config/groups/web3.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { parseEther } from '@ethersproject/units';

// interval in ms for RPC event polling for token balance and tx updates
export const PROVIDER_POLLING_INTERVAL = 12_000;
export const PROVIDER_POLLING_INTERVAL = 7_000;
// how long in ms to wait for RPC batching(multicall and provider)
export const PROVIDER_BATCH_TIME = 150;

// account for gas estimation
// will always have >=0.001 ether, >=0.001 stETH, >=0.001 wstETH
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { FC } from 'react';
import { Loader, Divider } from '@lidofinance/lido-ui';
import { useSDK, useTokenBalance } from '@lido-sdk/react';
import { TOKENS, getTokenAddress } from '@lido-sdk/constants';
import { Zero } from '@ethersproject/constants';

import { STRATEGY_LAZY } from 'consts/swr-strategies';
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 { RewardsListsEmpty } from './RewardsListsEmpty';
Expand All @@ -19,6 +17,8 @@ import {
ErrorWrapper,
} from './RewardsListContentStyles';

import type { Address } from 'viem';

export const RewardsListContent: FC = () => {
const { isWalletConnected, isSupportedChain } = useDappStatus();
const {
Expand All @@ -31,14 +31,11 @@ export const RewardsListContent: FC = () => {
setPage,
isLagging,
} = useRewardsHistory();
// temporarily until we switched to a new SDK
const { chainId } = useSDK();
const { data: stethBalance, initialLoading: isStethBalanceLoading } =
useTokenBalance(
getTokenAddress(chainId || 1, TOKENS.STETH),
address,
STRATEGY_LAZY,
);
const { data: stethBalance, isLoading: isStethBalanceLoading } =
useStethBalance({
account: address as Address,
shouldSubscribeToUpdates: false,
});
const hasSteth = stethBalance?.gt(Zero);

if (isWalletConnected && !isSupportedChain)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import { useForm, FormProvider } from 'react-hook-form';
import { useRouter } from 'next/router';

import { useEthereumBalance, useSTETHBalance } from '@lido-sdk/react';
import { parseEther } from '@ethersproject/units';

import {
Expand All @@ -24,7 +23,6 @@ import { useMaxGasPrice } from 'shared/hooks';
import { useIsMultisig } from 'shared/hooks/useIsMultisig';
import { useFormControllerRetry } from 'shared/hook-form/form-controller/use-form-controller-retry-delegate';

import { STRATEGY_LAZY } from 'consts/swr-strategies';
import { config } from 'config';

import {
Expand All @@ -39,6 +37,7 @@ import {

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

//
// Data context
Expand All @@ -60,9 +59,9 @@ export const useStakeFormData = () => {
const useStakeFormNetworkData = (): StakeFormNetworkData => {
const {
data: stethBalance,
update: updateStethBalance,
initialLoading: isStethBalanceLoading,
} = useSTETHBalance(STRATEGY_LAZY);
refetch: updateStethBalance,
isLoading: isStethBalanceLoading,
} = useStethBalance();
const { isMultisig, isLoading: isMultisigLoading } = useIsMultisig();
const gasLimit = useStethSubmitGasLimit();
const { maxGasPrice, initialLoading: isMaxGasPriceLoading } =
Expand All @@ -75,9 +74,10 @@ const useStakeFormNetworkData = (): StakeFormNetworkData => {

const {
data: etherBalance,
update: updateEtherBalance,
initialLoading: isEtherBalanceLoading,
} = useEthereumBalance(undefined, STRATEGY_LAZY);
refetch: updateEtherBalance,
isLoading: isEtherBalanceLoading,
} = useEthereumBalance();

const {
data: stakingLimitInfo,
mutate: mutateStakeLimit,
Expand Down
15 changes: 7 additions & 8 deletions features/stake/stake-form/use-stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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';

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

export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {
const stethContractWeb3 = useSTETHContractWeb3();
const { address, chainId } = useAccount();
const { address } = useAccount();
const stethContract = useSTETHContractRPC();
const { staticRpcProvider } = useCurrentStaticRpcProvider();
const { providerWeb3 } = useSDK();
const { txModalStages } = useTxModalStagesStake();
const waitForTx = useTxConfirmation();

return useCallback(
async ({ amount, referral }: StakeArguments): Promise<boolean> => {
try {
invariant(amount, 'amount is null');
invariant(chainId, 'chainId is not defined');
invariant(address, 'account is not defined');
invariant(providerWeb3, 'providerWeb3 not defined');
invariant(stethContractWeb3, 'steth is not defined');
Expand Down Expand Up @@ -92,11 +93,9 @@ export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {

txModalStages.pending(amount, txHash);

if (!isMultisig) {
await runWithTransactionLogger('Stake block confirmation', () =>
staticRpcProvider.waitForTransaction(txHash),
);
}
await runWithTransactionLogger('Stake block confirmation', () =>
waitForTx(txHash),
);

const stethBalance = await stethContract.balanceOf(address);

Expand All @@ -112,14 +111,14 @@ export const useStake = ({ onConfirm, onRetry }: StakeOptions) => {
}
},
[
chainId,
address,
providerWeb3,
stethContractWeb3,
txModalStages,
staticRpcProvider,
stethContract,
onConfirm,
waitForTx,
onRetry,
],
);
Expand Down
9 changes: 6 additions & 3 deletions features/withdrawals/hooks/contract/useClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useCurrentStaticRpcProvider } from 'shared/hooks/use-current-static-rpc
import { runWithTransactionLogger } from 'utils';
import { isContract } from 'utils/isContract';
import { sendTx } from 'utils/send-tx';
import { useTxConfirmation } from 'shared/hooks/use-tx-conformation';

import { useWithdrawalsContract } from './useWithdrawalsContract';

Expand All @@ -26,6 +27,7 @@ export const useClaim = ({ onRetry }: Args) => {
const { staticRpcProvider } = useCurrentStaticRpcProvider();
const { optimisticClaimRequests } = useClaimData();
const { txModalStages } = useTxModalStagesClaim();
const waitForTx = useTxConfirmation();

return useCallback(
async (sortedRequests: RequestStatusClaimable[]) => {
Expand Down Expand Up @@ -71,8 +73,8 @@ export const useClaim = ({ onRetry }: Args) => {

txModalStages.pending(amount, txHash);

await runWithTransactionLogger('Claim block confirmation', async () =>
staticRpcProvider.waitForTransaction(txHash),
await runWithTransactionLogger('Claim block confirmation', () =>
waitForTx(txHash),
);

await optimisticClaimRequests(sortedRequests);
Expand All @@ -90,8 +92,9 @@ export const useClaim = ({ onRetry }: Args) => {
address,
providerWeb3,
txModalStages,
staticRpcProvider,
optimisticClaimRequests,
staticRpcProvider,
waitForTx,
onRetry,
],
);
Expand Down
32 changes: 14 additions & 18 deletions features/withdrawals/hooks/contract/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import { useTxModalStagesRequest } from 'features/withdrawals/request/transactio
import { useTransactionModal } from 'shared/transaction-modal/transaction-modal';
import { sendTx } from 'utils/send-tx';
import { overrideWithQAMockBoolean } from 'utils/qa';
import { useTxConfirmation } from 'shared/hooks/use-tx-conformation';

// this encapsulates permit/approval & steth/wsteth flows
const useWithdrawalRequestMethods = () => {
const { providerWeb3 } = useSDK();
const { staticRpcProvider } = useCurrentStaticRpcProvider();
const { address, chainId, contractWeb3 } = useWithdrawalsContract();
const { address, contractWeb3 } = useWithdrawalsContract();

const permitSteth = useCallback(
async ({
Expand All @@ -43,8 +44,6 @@ const useWithdrawalRequestMethods = () => {
signature?: GatherPermitSignatureResult;
requests: BigNumber[];
}) => {
invariant(chainId, 'must have chainId');
invariant(address, 'must have address');
invariant(providerWeb3, 'must have providerWeb3');
invariant(signature, 'must have signature');
invariant(contractWeb3, 'must have contractWeb3');
Expand Down Expand Up @@ -72,7 +71,7 @@ const useWithdrawalRequestMethods = () => {

return callback;
},
[address, chainId, contractWeb3, providerWeb3, staticRpcProvider],
[contractWeb3, providerWeb3, staticRpcProvider],
);

const permitWsteth = useCallback(
Expand All @@ -83,8 +82,6 @@ const useWithdrawalRequestMethods = () => {
signature?: GatherPermitSignatureResult;
requests: BigNumber[];
}) => {
invariant(chainId, 'must have chainId');
invariant(address, 'must have address');
invariant(signature, 'must have signature');
invariant(providerWeb3, 'must have providerWeb3');
invariant(contractWeb3, 'must have contractWeb3');
Expand Down Expand Up @@ -112,17 +109,16 @@ const useWithdrawalRequestMethods = () => {

return callback;
},
[address, chainId, contractWeb3, providerWeb3, staticRpcProvider],
[contractWeb3, providerWeb3, staticRpcProvider],
);

const steth = useCallback(
async ({ requests }: { requests: BigNumber[] }) => {
invariant(chainId, 'must have chainId');
invariant(address, 'must have account');
invariant(contractWeb3, 'must have contractWeb3');
invariant(providerWeb3, 'must have providerWeb3');

const isMultisig = await isContract(address, contractWeb3.provider);
const isMultisig = await isContract(address, staticRpcProvider);

const tx = await contractWeb3.populateTransaction.requestWithdrawals(
requests,
Expand All @@ -139,16 +135,15 @@ const useWithdrawalRequestMethods = () => {

return callback;
},
[address, chainId, contractWeb3, staticRpcProvider, providerWeb3],
[address, contractWeb3, staticRpcProvider, providerWeb3],
);

const wstETH = useCallback(
async ({ requests }: { requests: BigNumber[] }) => {
invariant(chainId, 'must have chainId');
invariant(address, 'must have address');
invariant(contractWeb3, 'must have contractWeb3');
invariant(providerWeb3, 'must have providerWeb3');
const isMultisig = await isContract(address, contractWeb3.provider);
const isMultisig = await isContract(address, staticRpcProvider);

const tx =
await contractWeb3.populateTransaction.requestWithdrawalsWstETH(
Expand All @@ -166,7 +161,7 @@ const useWithdrawalRequestMethods = () => {

return callback;
},
[address, chainId, contractWeb3, staticRpcProvider, providerWeb3],
[address, contractWeb3, staticRpcProvider, providerWeb3],
);

return useCallback(
Expand Down Expand Up @@ -201,13 +196,13 @@ export const useWithdrawalRequest = ({
}: useWithdrawalRequestParams) => {
const { chainId } = useSDK();
const withdrawalQueueAddress = getWithdrawalQueueAddress(chainId);
const { staticRpcProvider } = useCurrentStaticRpcProvider();

const { connector, address } = useAccount();
const { isBunker } = useWithdrawals();
const { txModalStages } = useTxModalStagesRequest();
const getRequestMethod = useWithdrawalRequestMethods();
const { isMultisig, isLoading: isMultisigLoading } = useIsMultisig();
const waitForTx = useTxConfirmation();

const wstethContract = useWSTETHContractRPC();
const stethContract = useSTETHContractRPC();
Expand All @@ -222,7 +217,7 @@ export const useWithdrawalRequest = ({
approve,
needsApprove,
allowance,
initialLoading: loadingUseApprove,
isLoading: loadingUseApprove,
} = useApprove(
valueBN,
tokenContract.address,
Expand Down Expand Up @@ -320,8 +315,9 @@ export const useWithdrawalRequest = ({
txModalStages.pending(amount, token, txHash);

if (!isMultisig) {
await runWithTransactionLogger('Stake block confirmation', () =>
staticRpcProvider.waitForTransaction(txHash),
await runWithTransactionLogger(
'Withdrawal Request block confirmation',
() => waitForTx(txHash),
);
}

Expand All @@ -345,8 +341,8 @@ export const useWithdrawalRequest = ({
needsApprove,
onConfirm,
onRetry,
staticRpcProvider,
txModalStages,
waitForTx,
],
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import {
useSTETHContractRPC,
useSTETHBalance,
useWSTETHBalance,
useContractSWR,
} from '@lido-sdk/react';
import { useSTETHContractRPC, useContractSWR } from '@lido-sdk/react';
import { useClaimData } from 'features/withdrawals/contexts/claim-data-context';
import { useWithdrawals } from 'features/withdrawals/contexts/withdrawals-context';
import { useUnfinalizedStETH } from 'features/withdrawals/hooks';
import { useCallback, useMemo } from 'react';
import { useWstethBySteth } from 'shared/hooks';
import { useStethBalance, useWstethBalance } from 'shared/hooks/use-balance';
import { STRATEGY_LAZY } from 'consts/swr-strategies';

// Provides all data fetching for form to function
Expand All @@ -28,14 +24,14 @@ export const useRequestFormDataContextValue = () => {
} = useWithdrawals();
const {
data: balanceSteth,
update: stethUpdate,
initialLoading: isStethBalanceLoading,
} = useSTETHBalance(STRATEGY_LAZY);
refetch: stethUpdate,
isLoading: isStethBalanceLoading,
} = useStethBalance();
const {
data: balanceWSteth,
update: wstethUpdate,
initialLoading: isWstethBalanceLoading,
} = useWSTETHBalance(STRATEGY_LAZY);
refetch: wstethUpdate,
isLoading: isWstethBalanceLoading,
} = useWstethBalance();
const {
data: unfinalizedStETH,
update: unfinalizedStETHUpdate,
Expand Down
Loading

0 comments on commit 6f26854

Please sign in to comment.