Simple Example
The following example will show:
- How to connect a dApp to a wallet
- How to re-use an existing connection after a page refreshed
- How to send an operation request
- Beacon
- Taquito
Example
import { DAppClient, TezosOperationType } from "@airgap/beacon-sdk"; const dAppClient = new DAppClient({ name: "Beacon Docs" }); let myAddress: string | undefined; // This code should be called every time the page is loaded or refreshed to see if the user has already connected to a wallet. const activeAccount = await dAppClient.getActiveAccount(); if (activeAccount) { // If defined, the user is connected to a wallet. // You can now do an operation request, sign request, or send another permission request to switch wallet console.log("Already connected:", activeAccount.address); myAddress = activeAccount.address; } else { const permissions = await dAppClient.requestPermissions(); console.log("New connection:", permissions.address); myAddress = permissions.address; } // 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: myAddress, // Send to ourselves amount: "1", // Amount in mutez, the smallest unit in Tezos }, ], }); console.log("Operation Hash: ", response.transactionHash);
Loading...
Example
import { TezosToolkit } from "@taquito/taquito"; import { BeaconWallet } from "@taquito/beacon-wallet"; import { TezosOperationType } from "@airgap/beacon-sdk"; const Tezos = new TezosToolkit("https://mainnet.api.tez.ie"); const wallet = new BeaconWallet({ name: "Beacon Docs Taquito" }); // Takes the same arguments as the DAppClient constructor Tezos.setWalletProvider(wallet); let myAddress: string | undefined; // This code should be called every time the page is loaded or refreshed to see if the user has already connected to a wallet. const activeAccount = await wallet.client.getActiveAccount(); if (activeAccount) { // If defined, the user is connected to a wallet. // You can now do an operation request, sign request, or send another permission request to switch wallet console.log("Already connected:", activeAccount.address); myAddress = activeAccount.address; } else { await wallet.requestPermissions(); myAddress = await wallet.getPKH(); console.log("New connection:", myAddress); } // 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 hash = await wallet.sendOperations([ { kind: TezosOperationType.TRANSACTION, destination: myAddress, // Send to ourselves amount: "1", // Amount in mutez, the smallest unit in Tezos }, ]); console.log("Operation Hash: ", hash);
Loading...