Active Account
The Active Account
feature in the Beacon SDK is crucial for your dApp to interact effectively with the user's wallet.
Here's how it works:
getActiveAccount
: This function returns the current active account. Use it to check the user's active account when they first start using your dApp.- Active Account Subscription: Beyond just checking the current account, Beacon SDK allows your dApp to subscribe to any changes in the active account (
BeaconEvent.ACTIVE_ACCOUNT_SET
): This includes when the user switches to a different account or disconnects an account from their wallet.
It's important to implement this subscription so your dApp can react to account changes in real-time. This ensures your application always interacts with the correct account.
- Beacon
- Taquito
Example
import { BeaconEvent, DAppClient } from "@airgap/beacon-sdk"; const dAppClient = new DAppClient({ name: "Beacon Docs" }); // Listen for all the active account changes dAppClient.subscribeToEvent(BeaconEvent.ACTIVE_ACCOUNT_SET, async (account) => { // An active account has been set, update the dApp UI console.log(`${BeaconEvent.ACTIVE_ACCOUNT_SET} triggered: `, account); }); try { console.log("Requesting permissions..."); const permissions = await dAppClient.requestPermissions(); console.log("Got permissions:", permissions.address); } catch (error) { console.error("Got error:", error.message); }
Loading...
Example
import { TezosToolkit } from "@taquito/taquito"; import { BeaconWallet } from "@taquito/beacon-wallet"; import { BeaconEvent } from "@airgap/beacon-sdk"; const Tezos = new TezosToolkit("https://mainnet.api.tez.ie"); const wallet = new BeaconWallet({ name: "Beacon Docs Taquito" }); Tezos.setWalletProvider(wallet); wallet.client.subscribeToEvent(BeaconEvent.ACTIVE_ACCOUNT_SET, (account) => { // An active account has been set console.log(`${BeaconEvent.ACTIVE_ACCOUNT_SET} triggered: `, account); }); try { console.log("Requesting permissions..."); const permissions = await wallet.client.requestPermissions(); console.log("Got permissions:", permissions.address); } catch (error) { console.error("Got error:", error.message); }
Loading...