-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from lidofinance/feature/si-1270-rewards-empt…
…y-steth-balance-when-clear-address-input Empty "stETH balance" when clear address input
- Loading branch information
Showing
8 changed files
with
111 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
export const useLaggyDataWrapper = <Data>(data: Data) => { | ||
const laggyDataRef = useRef<Data | undefined>(); | ||
const isDataPresented = data !== undefined && data !== null; | ||
|
||
useEffect(() => { | ||
if (isDataPresented) { | ||
laggyDataRef.current = data; | ||
} | ||
}, [data, isDataPresented]); | ||
|
||
// Return to previous data if current data is not defined. | ||
const dataOrLaggyData = !isDataPresented ? laggyDataRef.current : data; | ||
|
||
// Shows previous data. | ||
const isLagging = !isDataPresented && laggyDataRef.current !== undefined; | ||
|
||
return { | ||
isLagging, | ||
dataOrLaggyData, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useMemo } from 'react'; | ||
import { useSDK, useTokenBalance } from '@lido-sdk/react'; | ||
import { useRewardsHistory } from './useRewardsHistory'; | ||
import { useLaggyDataWrapper } from './use-laggy-data-wrapper'; | ||
|
||
import { ETHER } from 'features/rewards/constants'; | ||
import { TOKENS, getTokenAddress } from '@lido-sdk/constants'; | ||
import { STRATEGY_LAZY } from 'utils/swrStrategies'; | ||
import { Big, BigDecimal } from '../helpers'; | ||
|
||
export const useRewardsBalanceData = () => { | ||
const { chainId } = useSDK(); | ||
const { address, data, currencyObject } = useRewardsHistory(); | ||
|
||
const { data: steth } = useTokenBalance( | ||
getTokenAddress(chainId, TOKENS.STETH), | ||
address, | ||
STRATEGY_LAZY, | ||
); | ||
|
||
const balanceData = useMemo( | ||
() => | ||
steth | ||
? { | ||
stEthBalanceParsed: new Big(steth.toString()), | ||
stEthCurrencyBalance: | ||
data && | ||
new BigDecimal(steth.toString()) // Convert to right BN | ||
.div(ETHER) | ||
.times(data.stETHCurrencyPrice[currencyObject.id]), | ||
} | ||
: null, | ||
[currencyObject.id, data, steth], | ||
); | ||
|
||
const { isLagging, dataOrLaggyData } = useLaggyDataWrapper(balanceData); | ||
|
||
return { isLagging: !!address && isLagging, data: dataOrLaggyData }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { STRATEGY_LAZY } from 'utils/swrStrategies'; | ||
import { useMainnetStaticRpcProvider } from 'shared/hooks/use-mainnet-static-rpc-provider'; | ||
import { useLidoSWR, useSDK } from '@lido-sdk/react'; | ||
|
||
import { stEthEthRequest } from 'features/rewards/fetchers/requesters'; | ||
import { constants } from 'ethers'; | ||
|
||
export const useStethEthRate = () => { | ||
const { chainId } = useSDK(); | ||
const mainnetStaticRpcProvider = useMainnetStaticRpcProvider(); | ||
|
||
const swrResult = useLidoSWR( | ||
`steth-eth-${chainId}`, | ||
async () => { | ||
if (chainId !== 1) { | ||
return constants.WeiPerEther; | ||
} else { | ||
const stEthEth = await stEthEthRequest(mainnetStaticRpcProvider); | ||
return stEthEth; | ||
} | ||
}, | ||
STRATEGY_LAZY, | ||
); | ||
|
||
return swrResult; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters