First dApp
Installation
Let's get started with your first dApp. You have to install the necessary dependencies.
- Beacon
- Taquito
npm install --save @airgap/beacon-sdk
npm install --save @taquito/taquito @taquito/beacon-wallet
Setup
You can then import the beacon-sdk
package and create a DAppClient
instance. This instance will be used throughout your dApp to interact with the users wallet.
Once created, you can send a permission request to prompt the user to connect to his wallet.
- Beacon
- Taquito
import { 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, (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); }
import { TezosToolkit } from "@taquito/taquito"; import { BeaconWallet } from "@taquito/beacon-wallet"; const Tezos = new TezosToolkit("https://mainnet.api.tez.ie"); const wallet = new BeaconWallet({ name: "Beacon Docs Taquito" }); Tezos.setWalletProvider(wallet); // Listen for all the active account changes wallet.client.subscribeToEvent(BeaconEvent.ACTIVE_ACCOUNT_SET, (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 wallet.client.requestPermissions(); console.log("Got permissions:", permissions.address); } catch (error) { console.error("Got error:", error); }
After the connection is successfully established and the user has shared his account, the result will be returned.
The DAppClient
instance should be a singleton. Avoid creating multiple copies of it, which could lead to unexpected behaviour.
Check
Now let's introduce a check to see if the SDK is already connected to the dApp. This code should be run after the page is loaded to get the users address and show it in your UI. If the following code returns an address, there is no need to send another permission request, unless you want to pair a different account.
- Beacon
- Taquito
import { DAppClient } from "@airgap/beacon-sdk"; const dAppClient = new DAppClient({ name: "Beacon Docs" }); // The following code should always be run during pageload if you want to show if the user is connected. const activeAccount = await dAppClient.getActiveAccount(); if (activeAccount) { // User already has account connected, everything is ready // You can now do an operation request, sign request, or send another permission request to switch wallet console.log("Already connected:", activeAccount.address); return activeAccount; } else { // The user is not connected. A button should be displayed where the user can connect to his wallet. console.error("Not connected!"); }
import { TezosToolkit } from "@taquito/taquito"; import { BeaconWallet } from "@taquito/beacon-wallet"; const Tezos = new TezosToolkit("https://mainnet.api.tez.ie"); const wallet = new BeaconWallet({ name: "Beacon Docs Taquito" }); Tezos.setWalletProvider(wallet); // The following code should always be run during pageload if you want to show if the user is connected. const activeAccount = await wallet.client.getActiveAccount(); if (activeAccount) { // User already has account connected, everything is ready // You can now do an operation request, sign request, or send another permission request to switch wallet console.log("Already connected:", activeAccount.address); return activeAccount; } else { // The user is not connected. A button should be displayed where the user can connect to his wallet. console.error("Not connected!"); }
The beacon-sdk
is now fully set up and ready to receive operation requests.
Operation Request
This code snippet will construct an Operation request. If the user has already granted permissions and is connected, you can directly send them operation requests for signing. Remember, each operation request should be constructed carefully, considering the specific action you want the user to authorize, like transferring tokens or interacting with a smart contract.
- Beacon
- Taquito
import { BeaconEvent, DAppClient, TezosOperationType, } 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); // At this point we are connected to an account. // Let's send a simple transaction to the wallet that sends 1 mutez to ourselves. const response = await dAppClient.requestOperation({ operationDetails: [ { kind: TezosOperationType.TRANSACTION, destination: account.address, // Send to ourselves amount: "1", // Amount in mutez, the smallest unit in Tezos }, ], }); console.log(response); }); // Check if we are connected. If not, do a permission request first. const activeAccount = await dAppClient.getActiveAccount(); if (!activeAccount) { await dAppClient.requestPermissions(); }
import { TezosToolkit } from "@taquito/taquito"; import { BeaconWallet } from "@taquito/beacon-wallet"; import { BeaconEvent, DAppClient, TezosOperationType, } from "@airgap/beacon-sdk"; const Tezos = new TezosToolkit("https://mainnet.api.tez.ie"); const wallet = new BeaconWallet({ name: "Beacon Docs Taquito" }); Tezos.setWalletProvider(wallet); // Listen for all the active account changes wallet.client.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); // At this point we are connected to an account. // Let's send a simple transaction to the wallet that sends 1 mutez to ourselves. const response = await wallet.sendOperations([ { kind: TezosOperationType.TRANSACTION, destination: account.address, // Send to ourselves amount: "1", // Amount in mutez, the smallest unit in Tezos }, ]); console.log(response); }, ); // Check if we are connected. If not, do a permission request first. const activeAccount = await wallet.client.getActiveAccount(); if (!activeAccount) { await wallet.client.requestPermissions(); }
Disconnect
Please refer to our dedicated page on how to disconnect your dApp based on your needs.
Destroy
destroy
should be implemented to ensure proper lifecycle management of dAppClient
.
This function is designed to be called as the final action of dAppClient
when it is no longer needed.
The destroy function is used to clean up resources and remove all Beacon values from storage.
Once this function is executed, the current instance of dAppClient
becomes unusable.
- Beacon
- Taquito
import { DAppClient } from "@airgap/beacon-sdk"; const dAppClient = new DAppClient({ name: "Beacon Docs" }); dAppClient .destroy() .then(() => { logger.log("Instance destroyed."); }) .catch((err) => logger.log("Error: ", err.message));
import { TezosToolkit } from "@taquito/taquito"; import { BeaconWallet } from "@taquito/beacon-wallet"; const Tezos = new TezosToolkit("https://mainnet.api.tez.ie"); const wallet = new BeaconWallet({ name: "Beacon Docs Taquito" }); Tezos.setWalletProvider(wallet); try { await wallet.disconnect(); } catch (err: any) { logger.log("Error: ", err.message); }
User Interaction Best Practice
Take a look a the best practice for recommendations on Beacon user interface components.