Get Balance
- React
- React-Native
- TypeScript
- Unity
useBalance
Hook for getting a wallet's current balance of an ERC20 token, (including native tokens such as Ether).
import { useBalance } from "@thirdweb-dev/react";
Usage
Provide the token contract address as the argument.
import { useBalance } from "@thirdweb-dev/react";
// Your ERC20 token smart contract address
const tokenAddress = "{{token_address}}";
function App() {
const { data, isLoading } = useBalance(tokenAddress);
}
To load the balance of the native token to the chain, such as Ether on Ethereum, import the NATIVE_TOKEN_ADDRESS
constant from @thirdweb-dev/sdk
.
import { useBalance } from "@thirdweb-dev/react";
import { NATIVE_TOKEN_ADDRESS } from "@thirdweb-dev/sdk";
function App() {
const { data, isLoading } = useBalance(NATIVE_TOKEN_ADDRESS);
}
Return Value
The hook's data
property, once loaded, contains the following properties:
{
symbol: string;
value: BigNumber;
name: string;
decimals: number;
displayValue: string;
} | undefined>;
useBalance
Hook for getting a wallet's current balance of an ERC20 token, (including native tokens such as Ether).
import { useBalance } from "@thirdweb-dev/react-native";
Usage
Provide the token contract address as the argument.
import { useBalance } from "@thirdweb-dev/react-native";
// Your ERC20 token smart contract address
const tokenAddress = "{{token_address}}";
function App() {
const { data, isLoading } = useBalance(tokenAddress);
}
To load the balance of the native token to the chain, such as Ether on Ethereum, import the NATIVE_TOKEN_ADDRESS
constant from @thirdweb-dev/sdk
.
import { useBalance } from "@thirdweb-dev/react-native";
import { NATIVE_TOKEN_ADDRESS } from "@thirdweb-dev/sdk";
function App() {
const { data, isLoading } = useBalance(NATIVE_TOKEN_ADDRESS);
}
Return Value
The hook's data
property, once loaded, contains the following properties:
{
symbol: string;
value: BigNumber;
name: string;
decimals: number;
displayValue: string;
} | undefined>;
getBalance
Get the the native token balance or ERC20 token balance of the wallet.
// for native token balance
const balance = await wallet.getBalance();
// for ERC20 token balance
const balance = await wallet.getBalance(tokenAddress);
tokenAddress
Address of the ERC20 token. If no address is provided, the native token balance is returned.
Return Value
Promise<{
symbol: string;
value: BigNumber;
name: string;
decimals: number;
displayValue: string;
}>;
GetBalance
Fetch the native or ERC20 token balance of the connected wallet.
Usage
Use this method to fetch the balance of a wallet.
You can use the native currency address to fetch the balance of the native currency, or a specific ERC20 token contract address to fetch the ERC20 balance.
var data = await sdk.wallet.GetBalance();
Configuration
currencyAddress (optional)
The address of the token smart contract that you want to get the balance for.
If no address is provided, the balance of the native currency will be used.
var data = await sdk.wallet.GetBalance("{{currency_contract_address}}");
Return Value
Returns a CurrencyValue
struct containing the following properties:
{
string name;
string symbol;
string decimals;
string value;
string displayValue;
}