-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split getting useTokenDetails into separate hook
refactor: update useWalletProvider to return Web3Provider
- Loading branch information
1 parent
d8b6cbe
commit 5558b48
Showing
6 changed files
with
79 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { Provider } from '@ethersproject/providers'; | ||
import { Contract } from '@ethersproject/contracts'; | ||
|
||
import { Token, Address } from '../types/types'; | ||
import ERC20 from '../abis/ERC20Detailed'; | ||
|
||
const useTokenDetails = ( | ||
provider?: Provider, | ||
tokenAddress?: Address, | ||
): Token => { | ||
const [tokenDetails, setTokenDetails] = useState<Token>({ | ||
name: '', | ||
symbol: '', | ||
decimals: 18, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!provider || !tokenAddress) return; | ||
|
||
const tokenContract = new Contract(tokenAddress, ERC20.abi, provider); | ||
|
||
const updateToken = async (): Promise<void> => { | ||
const tokenName = tokenContract.name(); | ||
const tokenSymbol = tokenContract.symbol(); | ||
const tokenDecimals = tokenContract.decimals(); | ||
setTokenDetails({ | ||
name: await tokenName, | ||
symbol: await tokenSymbol, | ||
decimals: await tokenDecimals, | ||
}); | ||
}; | ||
|
||
updateToken(); | ||
}, [provider, tokenAddress]); | ||
|
||
return tokenDetails; | ||
}; | ||
|
||
export default useTokenDetails; |
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