I have an async
function outside of my react component which retrieves 2 token balances from 2 addresses. I want to render out the balances of the 2 addresses, in the commented out HTML lines. Specifically linkBalance
and balance
. The useContext
hook I think maybe I could solve my problem using context
but I'm not sure.
import { ethers } from 'ethers'
const { ethereum } = window
const provider = new ethers.providers.Web3Provider(window.ethereum);
const linkAddress = '0xa36085F69e2889c224210F603D836748e7dC0088'
const linkABI = require('../constants/erc20.json')
const linkContract = new ethers.Contract(linkAddress, linkABI, provider);
(async() => {
const accounts = await ethereum.request({ method: 'eth_accounts' })
const account = accounts[0]
const balance = await provider.getBalance(account)
console.log(ethers.utils.formatUnits(balance, 18))
const linkBalance = await linkContract.balanceOf(contractAddress)
console.log(ethers.utils.formatUnits(linkBalance, 18))
})()
const Main = () => {
const { connectWallet, currentAccount } = useContext(PackPlayersContext)
return(
<>
<div>
{currentAccount && (<p>Current Address: {currentAccount}</p>)}
{!currentAccount && (
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onClick={connectWallet}>Connect Wallet</button>
)}
<input className="shadow appearance-none border rounded w-half py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="username" type="text" placeholder="Price"/>
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mb-4 mt-4">Buy Pack</button>
{/*
<div>
<p>Current link balance of contract: {linkBalance}</p>
<p>Your KETH balance: {balance}</p>
</div>
*/}
</div>
</>
)
}
export default Main