Skip to main content

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 dApps can use the JSON RPC calls supported by the Arcana wallet to allow users to check their account balance.

Prerequisites

  • Install the @arcana/auth package. Integrate it with your dApp. For details, see Arcana Auth Quick Start Guide.

    const appAddress = '445007f942f9Ba718953094BbeeeeeB9484cAfd2' // App Address Example

    const auth = new AuthProvider(`${appAddress}`, {
    position: 'left', // defaults to right
    theme: 'light', // defaults to dark
    alwaysVisible: false, // defaults to true which is Full UI mode
    chainConfig: {
    chainId: CHAIN.POLYGON_MAINNET,
    rpcUrl: '',
    },
    })
    try {
    await auth.init()
    } catch (e) {
    // Handle exception case
    }

    provider = auth.getProvider()
    // or
    provider = window.ethereum
  • 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 })
    })
    }

Steps

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