From b6b770277d85634563bcfd082d54d467cd63cd89 Mon Sep 17 00:00:00 2001 From: Anton Shalimov Date: Thu, 26 Sep 2024 13:48:52 +0300 Subject: [PATCH] refactor: init new sdk --- .../wrap/hooks/use-wrap-form-processing.ts | 8 +- providers/lido-sdk.tsx | 85 +++++-------------- shared/hooks/use-balance.ts | 20 ++--- 3 files changed, 36 insertions(+), 77 deletions(-) diff --git a/features/wsteth/wrap/hooks/use-wrap-form-processing.ts b/features/wsteth/wrap/hooks/use-wrap-form-processing.ts index 511376ec..056390e4 100644 --- a/features/wsteth/wrap/hooks/use-wrap-form-processing.ts +++ b/features/wsteth/wrap/hooks/use-wrap-form-processing.ts @@ -6,6 +6,7 @@ import { useSDK, useWSTETHContractRPC } from '@lido-sdk/react'; import { CHAINS, isSDKSupportedL2Chain } from 'consts/chains'; import { useCurrentStaticRpcProvider } from 'shared/hooks/use-current-static-rpc-provider'; +import { useLidoSDK } from 'providers/lido-sdk'; import { runWithTransactionLogger } from 'utils'; import { isContract } from 'utils/isContract'; import { useTxConfirmation } from 'shared/hooks/use-tx-conformation'; @@ -16,7 +17,6 @@ import type { } from '../wrap-form-context'; import { useWrapTxProcessing } from './use-wrap-tx-processing'; import { useTxModalWrap } from './use-tx-modal-stages-wrap'; -import { useLidoSDK } from '../../../../providers/lido-sdk'; type UseWrapFormProcessorArgs = { approvalData: WrapFormApprovalData; @@ -37,7 +37,7 @@ export const useWrapFormProcessor = ({ const { txModalStages } = useTxModalWrap(); const processWrapTx = useWrapTxProcessing(); - const { l2 } = useLidoSDK(); + const { sdk } = useLidoSDK(); const waitForTx = useTxConfirmation(); const { isApprovalNeededBeforeWrap, processApproveTx } = approvalData; @@ -75,7 +75,7 @@ export const useWrapFormProcessor = ({ let txHash; if (isSDKSupportedL2Chain(chainId as CHAINS)) { // The operation 'stETH to wstETH' on L2 is unwrap - const tx = await l2.unwrap({ + const tx = await sdk.l2.unwrap({ // value: amount.toString(), <- Not working value: amount.toBigInt(), }); @@ -118,7 +118,7 @@ export const useWrapFormProcessor = ({ wstETHContractRPC, isApprovalNeededBeforeWrap, txModalStages, - l2, + sdk, onConfirm, processApproveTx, processWrapTx, diff --git a/providers/lido-sdk.tsx b/providers/lido-sdk.tsx index a482e1da..eeb47fb3 100644 --- a/providers/lido-sdk.tsx +++ b/providers/lido-sdk.tsx @@ -1,29 +1,14 @@ import { createContext, useContext, useMemo } from 'react'; -import { LidoSDKCore } from '@lidofinance/lido-ethereum-sdk/core'; -import { - LidoSDKstETH, - LidoSDKwstETH, -} from '@lidofinance/lido-ethereum-sdk/erc20'; -import { - LidoSDKL2Steth, - LidoSDKL2Wsteth, - LidoSDKL2, -} from '@lidofinance/lido-ethereum-sdk/l2'; +import { LidoSDK } from '@lidofinance/lido-ethereum-sdk'; import invariant from 'tiny-invariant'; -import { useChainId, useClient, useConnectorClient } from 'wagmi'; +// import { useChainId, useClient, useWalletClient } from 'wagmi'; +import { useChainId, usePublicClient, useWalletClient } from 'wagmi'; -import { usePublicClient, useWalletClient } from 'wagmi'; - -import { useTokenTransferSubscription } from 'shared/hooks/use-balance'; import { useGetRpcUrlByChainId } from 'config/rpc'; +import { useTokenTransferSubscription } from 'shared/hooks/use-balance'; type LidoSDKContextValue = { - core: LidoSDKCore; - steth: LidoSDKstETH; - wsteth: LidoSDKwstETH; - l2: LidoSDKL2; - l2Steth: LidoSDKL2Steth; - l2Wsteth: LidoSDKL2Wsteth; + sdk: LidoSDK; subscribeToTokenUpdates: ReturnType; }; @@ -38,59 +23,33 @@ export const useLidoSDK = () => { export const LidoSDKProvider = ({ children }: React.PropsWithChildren) => { const subscribe = useTokenTransferSubscription(); - const publicClient = useClient(); + // TODO: useClient() or usePublicClient() ? + // const publicClient = useClient(); + const publicClient = usePublicClient(); const chainId = useChainId(); const getRpcUrl = useGetRpcUrlByChainId(); const fallbackRpcUrl = !publicClient ? getRpcUrl(chainId) : undefined; - const { data: walletClient } = useConnectorClient(); - - const publicClient2 = usePublicClient(); - const { data: walletClient2 } = useWalletClient(); - - const sdk = useMemo(() => { - const core = new LidoSDKCore({ - chainId, - logMode: 'none', - rpcProvider: publicClient as any, - web3Provider: walletClient as any, - // viem client can be unavailable on ipfs+dev first renders - rpcUrls: !publicClient && fallbackRpcUrl ? [fallbackRpcUrl] : undefined, - }); - - const l2 = new LidoSDKL2({ - chainId, + const { data: walletClient } = useWalletClient(); + + const contextValue = useMemo(() => { + // @ts-expect-error: typing + const sdk = new LidoSDK({ + chainId: chainId, + rpcProvider: publicClient, + web3Provider: walletClient, logMode: 'info', - rpcProvider: publicClient2, - web3Provider: walletClient2, // viem client can be unavailable on ipfs+dev first renders - // rpcUrls: !publicClient && fallbackRpcUrl ? [fallbackRpcUrl] : undefined, + rpcUrls: !publicClient && fallbackRpcUrl ? [fallbackRpcUrl] : undefined, }); - const steth = new LidoSDKstETH({ core }); - const wsteth = new LidoSDKwstETH({ core }); - - const l2Steth = l2.steth; - const l2Wsteth = l2.wsteth; - return { - core, - steth, - wsteth, - l2, - l2Steth, - l2Wsteth, + sdk, subscribeToTokenUpdates: subscribe, }; - }, [ - chainId, - fallbackRpcUrl, - publicClient, - publicClient2, - subscribe, - walletClient, - walletClient2, - ]); + }, [chainId, fallbackRpcUrl, publicClient, subscribe, walletClient]); return ( - {children} + + {children} + ); }; diff --git a/shared/hooks/use-balance.ts b/shared/hooks/use-balance.ts index 3df1fb53..f59d6643 100644 --- a/shared/hooks/use-balance.ts +++ b/shared/hooks/use-balance.ts @@ -255,17 +255,17 @@ export const useStethBalance = ({ const { address } = useAccount(); const mergedAccount = account ?? address; - const { steth, l2Steth, core } = useLidoSDK(); + const { sdk } = useLidoSDK(); const { data: contract, isLoading } = useQuery({ - queryKey: ['steth-contract', core.chainId], + queryKey: ['steth-contract', sdk.core.chainId], enabled: !!mergedAccount, staleTime: Infinity, queryFn: async () => - isSDKSupportedL2Chain(core.chainId as CHAINS) - ? l2Steth.getContract() - : steth.getContract(), + isSDKSupportedL2Chain(sdk.core.chainId as CHAINS) + ? sdk.l2.steth.getContract() + : sdk.steth.getContract(), }); const balanceData = useTokenBalance( @@ -284,16 +284,16 @@ export const useWstethBalance = ({ const { address } = useAccount(); const mergedAccount = account ?? address; - const { wsteth, l2Wsteth, core } = useLidoSDK(); + const { sdk } = useLidoSDK(); const { data: contract, isLoading } = useQuery({ - queryKey: ['wsteth-contract', core.chainId], + queryKey: ['wsteth-contract', sdk.core.chainId], enabled: !!mergedAccount, staleTime: Infinity, queryFn: async () => - isSDKSupportedL2Chain(core.chainId as CHAINS) - ? l2Wsteth.getContract() - : wsteth.getContract(), + isSDKSupportedL2Chain(sdk.core.chainId as CHAINS) + ? sdk.l2.wsteth.getContract() + : sdk.wsteth.getContract(), }); const balanceData = useTokenBalance(