-
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 #99 from lidofinance/develop
Develop to main
- Loading branch information
Showing
28 changed files
with
312 additions
and
228 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 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
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
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
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 |
---|---|---|
@@ -1,71 +1,29 @@ | ||
import { SWRConfiguration } from 'swr'; | ||
import { BigNumber } from '@ethersproject/bignumber'; | ||
import { getAggregatorContract } from '@lido-sdk/contracts'; | ||
import { getAggregatorAddress, CHAINS } from '@lido-sdk/constants'; | ||
import { divide } from '@lido-sdk/helpers'; | ||
import { useSDK } from './useSDK'; | ||
import { SWRResponse } from './useLidoSWR'; | ||
import { useContractSWR } from './useContractSWR'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { SWRResponse, useLidoSWR } from './useLidoSWR'; | ||
|
||
const getEthPrice = (decimals?: number, latestAnswer?: BigNumber) => { | ||
if (decimals == null || latestAnswer == null) { | ||
return undefined; | ||
} | ||
type useEthPriceResult = number; | ||
|
||
return divide(latestAnswer, BigNumber.from(10).pow(decimals)); | ||
}; | ||
|
||
export const useEthPrice = (): Omit<SWRResponse<number>, 'mutate'> => { | ||
export const useEthPrice = ( | ||
config?: SWRConfiguration<useEthPriceResult, unknown>, | ||
): SWRResponse<useEthPriceResult> => { | ||
const { providerMainnetRpc } = useSDK(); | ||
const address = getAggregatorAddress(CHAINS.Mainnet); | ||
const aggregatorContract = getAggregatorContract(address, providerMainnetRpc); | ||
|
||
const decimals = useContractSWR({ | ||
contract: aggregatorContract, | ||
method: 'decimals', | ||
}); | ||
|
||
const latestAnswer = useContractSWR({ | ||
contract: aggregatorContract, | ||
method: 'latestAnswer', | ||
}); | ||
|
||
const decimalsData = decimals.data; | ||
const latestAnswerData = latestAnswer.data; | ||
|
||
const data = useMemo(() => { | ||
return getEthPrice(decimalsData, latestAnswerData); | ||
}, [decimalsData, latestAnswerData]); | ||
|
||
const updateDecimals = decimals.update; | ||
const updateLatestAnswer = latestAnswer.update; | ||
|
||
const update = useCallback(async () => { | ||
const [decimals, latestAnswer] = await Promise.all([ | ||
updateDecimals(), | ||
updateLatestAnswer(), | ||
]); | ||
|
||
return getEthPrice(decimals, latestAnswer); | ||
}, [updateDecimals, updateLatestAnswer]); | ||
|
||
return { | ||
update, | ||
data, | ||
|
||
/* | ||
* support dependency collection | ||
* https://swr.vercel.app/advanced/performance#dependency-collection | ||
*/ | ||
|
||
get loading() { | ||
return decimals.loading || latestAnswer.loading; | ||
}, | ||
get initialLoading() { | ||
return decimals.initialLoading || latestAnswer.initialLoading; | ||
}, | ||
get error() { | ||
return decimals.error || latestAnswer.error; | ||
return useLidoSWR( | ||
['lido-swr:eth-price', aggregatorContract], | ||
async () => { | ||
const [decimals, latestAnswer] = await Promise.all([ | ||
aggregatorContract.decimals(), | ||
aggregatorContract.latestAnswer(), | ||
]); | ||
return divide(latestAnswer, BigNumber.from(10).pow(decimals)); | ||
}, | ||
}; | ||
config, | ||
); | ||
}; |
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
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import { useContext } from 'react'; | ||
import { SDKContext, SDKContextValue } from '../context'; | ||
import invariant from 'tiny-invariant'; | ||
|
||
export const useSDK = (): SDKContextValue => { | ||
return useContext(SDKContext); | ||
const contextValue = useContext(SDKContext); | ||
invariant(contextValue, 'useSDK was used outside of SDKContext'); | ||
return contextValue; | ||
}; |
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
Oops, something went wrong.