Skip to content

Commit

Permalink
refactor: init new sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
solidovic committed Sep 26, 2024
1 parent 34b463c commit b6b7702
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 77 deletions.
8 changes: 4 additions & 4 deletions features/wsteth/wrap/hooks/use-wrap-form-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(),
});
Expand Down Expand Up @@ -118,7 +118,7 @@ export const useWrapFormProcessor = ({
wstETHContractRPC,
isApprovalNeededBeforeWrap,
txModalStages,
l2,
sdk,
onConfirm,
processApproveTx,
processWrapTx,
Expand Down
85 changes: 22 additions & 63 deletions providers/lido-sdk.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof useTokenTransferSubscription>;
};

Expand All @@ -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 (
<LidoSDKContext.Provider value={sdk}>{children}</LidoSDKContext.Provider>
<LidoSDKContext.Provider value={contextValue}>
{children}
</LidoSDKContext.Provider>
);
};
20 changes: 10 additions & 10 deletions shared/hooks/use-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit b6b7702

Please sign in to comment.