Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch native balance from wallet provider #356

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions ui/lib/thirdweb/hooks/useNativeBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import { useWallets } from '@privy-io/react-auth'
import { useContext, useEffect } from 'react'
import {
useActiveAccount,
useActiveWalletChain,
useWalletBalance,
} from 'thirdweb/react'
import PrivyWalletContext from '../../privy/privy-wallet-context'
import client from '../client'
import { useContext, useEffect, useState } from 'react'
import PrivyWalletContext from '@/lib/privy/privy-wallet-context'
import ChainContextV5 from '../chain-context-v5'

export function useNativeBalance() {
const account = useActiveAccount()
const address = account?.address
const activeWalletChain = useActiveWalletChain()
const { selectedWallet } = useContext(PrivyWalletContext)
const { selectedChain } = useContext(ChainContextV5)
const { wallets } = useWallets()

const { data: nativeBalance, refetch } = useWalletBalance({
client,
address: address,
chain: activeWalletChain,
})

const [nativeBalance, setNativeBalance] = useState<any>()
useEffect(() => {
async function getNativeBalance() {
const wallet = wallets[selectedWallet]
if (!wallet) return
const provider = await wallet.getEthersProvider()
const balance = await provider.getBalance(wallet.address)
setNativeBalance(Number(+balance?.toString() / 10 ** 18).toFixed(7))
}

const interval = setInterval(() => {
refetch()
getNativeBalance()
}, 5000)
getNativeBalance()
return () => clearInterval(interval)
}, [address, wallets, selectedWallet])
}, [wallets, selectedWallet, selectedChain])

return Number(nativeBalance?.displayValue).toFixed(7)
return nativeBalance
}
Loading