Skip to content

Commit

Permalink
fix: useBalance hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
solidovic committed Sep 24, 2024
1 parent e39e0b4 commit 3a00f17
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
5 changes: 5 additions & 0 deletions consts/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ export const SDK_LEGACY_SUPPORTED_CHAINS = [
export const SDK_SUPPORTED_MULTICHAIN_CHAINS = [
LIDO_MULTICHAIN_CHAINS.OptimismSepolia,
];

// TODO: move to @lidofinance/lido-ethereum-sdk package
export const isSDKSupportedL2Chain = (chainId) => {
return SDK_SUPPORTED_MULTICHAIN_CHAINS.indexOf(chainId) > -1;
};
13 changes: 7 additions & 6 deletions shared/hooks/use-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import {
import type { GetBalanceData } from 'wagmi/query';

import { config } from 'config';

import { useDappStatus } from './use-dapp-status';
import { isSDKSupportedL2Chain } from 'consts/chains';

const nativeToBN = (data: bigint) => BigNumber.from(data.toString());

Expand Down Expand Up @@ -254,7 +253,6 @@ export const useStethBalance = ({
shouldSubscribeToUpdates = true,
}: UseBalanceProps = {}) => {
const { address } = useAccount();
const { isDappActiveOnL2 } = useDappStatus();
const mergedAccount = account ?? address;

const { steth, l2Steth, core } = useLidoSDK();
Expand All @@ -265,7 +263,9 @@ export const useStethBalance = ({

staleTime: Infinity,
queryFn: async () =>
isDappActiveOnL2 ? l2Steth.getContract() : steth.getContract(),
isSDKSupportedL2Chain(core.chainId)
? l2Steth.getContract()
: steth.getContract(),
});

const balanceData = useTokenBalance(
Expand All @@ -282,7 +282,6 @@ export const useWstethBalance = ({
shouldSubscribeToUpdates = true,
}: UseBalanceProps = {}) => {
const { address } = useAccount();
const { isDappActiveOnL2 } = useDappStatus();
const mergedAccount = account ?? address;

const { wsteth, l2Wsteth, core } = useLidoSDK();
Expand All @@ -292,7 +291,9 @@ export const useWstethBalance = ({
enabled: !!mergedAccount,
staleTime: Infinity,
queryFn: async () =>
isDappActiveOnL2 ? l2Wsteth.getContract() : wsteth.getContract(),
isSDKSupportedL2Chain(core.chainId)
? l2Wsteth.getContract()
: wsteth.getContract(),
});

const balanceData = useTokenBalance(
Expand Down
7 changes: 2 additions & 5 deletions shared/hooks/use-dapp-status.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { useMemo } from 'react';
import { useAccount } from 'wagmi';

import {
LIDO_MULTICHAIN_CHAINS,
SDK_SUPPORTED_MULTICHAIN_CHAINS,
} from 'consts/chains';
import { isSDKSupportedL2Chain, LIDO_MULTICHAIN_CHAINS } from 'consts/chains';

import { useIsSupportedChain } from './use-is-supported-chain';
import { useConfig } from 'config';
Expand Down Expand Up @@ -32,7 +29,7 @@ export const useDappStatus = () => {
const isDappActiveOnL2 = useMemo(() => {
if (!chainId) return false;

return SDK_SUPPORTED_MULTICHAIN_CHAINS.indexOf(chainId) > -1;
return isSDKSupportedL2Chain(chainId);
}, [chainId]);

return {
Expand Down

0 comments on commit 3a00f17

Please sign in to comment.