1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-22 18:00:06 +01:00

small web3 refactor

This commit is contained in:
Matthias Kretschmann 2020-05-22 15:51:08 +02:00
parent 2405b6c617
commit f054427de4
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 19 additions and 51 deletions

View File

@ -1,14 +1,11 @@
import React from 'react' import React from 'react'
import { render, waitForElement } from '@testing-library/react' import { render } from '@testing-library/react'
import Account from './Account' import Account from './Account'
describe('Account', () => { describe('Account', () => {
it('renders without crashing', async () => { it('renders without crashing', () => {
const { container } = render(<Account />) const { container } = render(<Account />)
const lazyElement = await waitForElement(() => expect(container.firstChild).toBeInTheDocument()
container.querySelector('.balance')
)
expect(lazyElement).toBeInTheDocument()
}) })
}) })

View File

@ -1,14 +1,24 @@
import React, { ReactElement } from 'react' import React, { ReactElement, useState, useEffect } from 'react'
import { toDataUrl } from 'ethereum-blockies' import { toDataUrl } from 'ethereum-blockies'
import { formatEther } from '@ethersproject/units' import { formatEther } from '@ethersproject/units'
import useWeb3 from '../../../hooks/use-web3'
import styles from './Account.module.scss' import styles from './Account.module.scss'
import useWeb3, { getBalance } from '../../../hooks/use-web3'
export default async function Account(): Promise<ReactElement> { export default function Account(): ReactElement {
const { library, account } = useWeb3() const { library, account } = useWeb3()
const ethBalance = account && (await getBalance(account, library)) const [ethBalance, setEthBalance] = useState(0)
const blockies = account && toDataUrl(account) const blockies = account && toDataUrl(account)
useEffect(() => {
if (!library || !account) return
async function init() {
const balance = await library.getBalance(account)
setEthBalance(balance)
}
init()
}, [library, account])
const accountDisplay = const accountDisplay =
account && account &&
`${account.substring(0, 8)}...${account.substring(account.length - 4)}` `${account.substring(0, 8)}...${account.substring(account.length - 4)}`

View File

@ -2,14 +2,9 @@ import { useState, useEffect } from 'react'
import { useWeb3React } from '@web3-react/core' import { useWeb3React } from '@web3-react/core'
import { Web3ReactContextInterface } from '@web3-react/core/dist/types' import { Web3ReactContextInterface } from '@web3-react/core/dist/types'
import * as connectors from './connectors' import * as connectors from './connectors'
import { import { getLibrary, getNetworkName, getErrorMessage } from './utils'
getLibrary,
getNetworkName,
getErrorMessage,
getBalance
} from './utils'
export { connectors, getLibrary, getNetworkName, getErrorMessage, getBalance } export { connectors, getLibrary, getNetworkName, getErrorMessage }
export function useEagerConnect(): boolean { export function useEagerConnect(): boolean {
const { MetaMask } = connectors const { MetaMask } = connectors

View File

@ -1,4 +1,3 @@
import { useState, useEffect } from 'react'
import { UnsupportedChainIdError } from '@web3-react/core' import { UnsupportedChainIdError } from '@web3-react/core'
import { import {
NoEthereumProviderError, NoEthereumProviderError,
@ -51,36 +50,3 @@ export function getErrorMessage(error: Error, chainId: number): string {
return 'An unknown error occurred. Check the console for more details.' return 'An unknown error occurred. Check the console for more details.'
} }
} }
export function getBalance(
account: string,
library: Web3Provider
): Promise<number> {
const [ethBalance, setEthBalance] = useState()
useEffect((): any => {
if (library && account) {
let stale = false
library
.getBalance(account)
.then((balance: any) => {
if (!stale) {
setEthBalance(balance)
}
})
.catch(() => {
if (!stale) {
setEthBalance(null)
}
})
return () => {
stale = true
setEthBalance(undefined)
}
}
}, [library, account])
return ethBalance
}