Check Wallet Balance
Arcana wallet is an embedded Web3 wallet available to all the dApps that integrate with the Arcana Auth SDK.
In this guide, you will learn how Web3 applications that integrate with the Auth SDK can use the JSON RPC calls supported by the Arcana wallet and allow users to check their account balance.
Prerequisites
Register and configure your application using the Arcana Developer Dashboard. Next, install the @arcana/auth
package. Integrate the Auth SDK with your dApp. For details, see Arcana Auth Quick Start Guide.
const { AuthProvider } = window.arcana.auth // From CDN
//or
import { AuthProvider, CHAIN } from '@arcana/auth' //From npm
//clientId : Arcana Unique App Identifier via Dashboard
const clientId = 'xar_test_445007f942f9Ba718953094BbeeeeeB9484cAfd2'
const auth = new AuthProvider(`${clientId}`, { //required
network: 'testnet', //defaults to 'testnet'
position: 'left', //defaults to right
theme: 'light', //defaults to dark
alwaysVisible: false, //defaults to true, wallet always visible
chainConfig: {
chainId: CHAIN.POLYGON_MAINNET, //defaults to CHAIN.ETHEREUM_MAINNET
rpcUrl: 'https://polygon-rpc.com', //defaults to 'https://rpc.ankr.com/eth'
},
})
try {
await auth.init()
} catch (e) {
// Handle exception case
}
Make sure you have already initialized the wallet in your dApp code before invoking any JSON RPC call to check the Arcana wallet balance.
// Assuming Auth SDK is integrated and initialized
try {
provider = auth.provider
const connected = await auth.isLoggedIn()
console.log({ connected })
setHooks()
} catch (e) {
// Handle exception case
}
// setHooks: Manage chain or account switch in Arcana wallet
function setHooks() {
provider.on('connect', async (params) => {
console.log({ type: 'connect', params: params })
const isLoggedIn = await auth.isLoggedIn()
console.log({ isLoggedIn })
})
provider.on('accountsChanged', (params) => {
//Handle
console.log({ type: 'accountsChanged', params: params })
})
provider.on('chainChanged', async (params) => {
console.log({ type: 'chainChanged', params: params })
})
}
To check the account balance for the dApp user account set up in the Arcana wallet, a dApp must first access the account address and then get the account balance for the sender's wallet address using JSON RPC calls before displaying it to the user.
Step1: Get Account
// get from eth_accounts
let from = ''
async function getAccounts() {
console.log('Requesting accounts')
try {
const accounts = await provider.request({ method: 'eth_accounts' })
console.log({ accounts })
from = accounts[0] // Use this account address to get wallet balance
} catch (e) {
console.log({ e })
}
}
Step2: Get Account Balance
let balance = ''
async function getBalance() {
console.log('Requesting Balance')
try {
provider.request({ method: 'eth_getBalance' }).then((balance) => {
// convert a currency unit from wei to ether
const balanceInEth = ethers.utils.formatEther(balance)
console.log(`balance: ${balanceInEth} ETH`)
})
} catch (e) {
console.log({ e })
}
}
The following figure shows how Arcana wallet displays the balance for the dApp user account:
That is all!
You are all set to enable dApp users to view their Arcana wallet account balance.
What's Next?
For a complete list of other JSON RPC calls supported by Arcana wallet, see JSON-RPC Specifications.
See also
- Arcana wallet capabilities
- Configure Arcana wallet Visibility
- Sign transactions
- Send transaction
- Add/Switch Network )
- Watch Token Assets
- Auth SDK Reference Guide