-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: fetch latest src token balance
- Loading branch information
Showing
4 changed files
with
78 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useSelector } from 'react-redux'; | ||
import { zeroAddress } from 'ethereumjs-util'; | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import { Hex } from '@metamask/utils'; | ||
import { Numeric } from '../../../shared/modules/Numeric'; | ||
import { DEFAULT_PRECISION } from '../useCurrencyDisplay'; | ||
import { fetchTokenBalance } from '../../../shared/lib/token-util'; | ||
import { | ||
getCurrentChainId, | ||
getSelectedInternalAccount, | ||
SwapsEthToken, | ||
} from '../../selectors'; | ||
import { SwapsTokenObject } from '../../../shared/constants/swaps'; | ||
import { useAsyncResult } from '../useAsyncResult'; | ||
|
||
/** | ||
* Custom hook to fetch and format the latest balance of a given token or native asset. | ||
* | ||
* @param token - The token object for which the balance is to be fetched. Can be null. | ||
* @param chainId - The chain ID to be used for fetching the balance. Optional. | ||
* @returns An object containing the formatted balance as a string. | ||
*/ | ||
const useLatestBalance = ( | ||
token: SwapsTokenObject | SwapsEthToken | null, | ||
chainId?: Hex, | ||
) => { | ||
const { address: selectedAddress } = useSelector(getSelectedInternalAccount); | ||
const currentChainId = useSelector(getCurrentChainId); | ||
|
||
const { value: latestBalance } = useAsyncResult<string>(async () => { | ||
if (token && chainId && currentChainId === chainId) { | ||
if (!token.address || token.address === zeroAddress()) { | ||
const ethersProvider = new Web3Provider(global.ethereumProvider); | ||
return (await ethersProvider.getBalance(selectedAddress)).toString(); | ||
} | ||
return ( | ||
await fetchTokenBalance( | ||
token.address, | ||
selectedAddress, | ||
global.ethereumProvider, | ||
) | ||
).toString(); | ||
} | ||
// TODO implement fetching balance on non-active chain | ||
return {}; | ||
}, [token, selectedAddress, global.ethereumProvider]); | ||
|
||
return { | ||
formattedBalance: | ||
token && latestBalance | ||
? new Numeric(latestBalance, 10) | ||
.shiftedBy(Number(token?.decimals) ?? 18) | ||
.round(DEFAULT_PRECISION) | ||
.toString() | ||
: undefined, | ||
}; | ||
}; | ||
|
||
export default useLatestBalance; |
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