wallet-connect-web-examples/dapps/vue-dapp-auth/composables/useAccount.ts
Pavel Yankovski 7e850d66c4
feat(vue-dapp-auth): Add example dapp on Vue 3 (#76)
Co-authored-by: Ben Kremer <contact@bkrem.dev>
2022-11-14 18:13:55 +01:00

35 lines
917 B
TypeScript

import type { MaybeRef } from '@vueuse/shared'
import { providers } from 'ethers'
export const useAccount = (address: MaybeRef<string>) => {
const balance = ref<number | null>(null)
const avatar = ref<string | null>(null)
const isLoading = ref(false)
const updateAccountInfo = async () => {
const addressValue = unref(address)
if (addressValue) {
isLoading.value = true
const provider = providers.getDefaultProvider()
balance.value = (await provider.getBalance(addressValue)).toNumber()
avatar.value = await provider.getAvatar(addressValue)
isLoading.value = false
} else {
avatar.value = null
balance.value = null
}
}
// Register watcher for a ref
// or just call the effect once for regular value
if (isRef(address)) {
watchEffect(updateAccountInfo)
} else {
updateAccountInfo()
}
return { balance, avatar, isLoading }
}