Simple Contract Call
The following example shows how to do a simple contract call with Beacon.
tip
The beacon-sdk
itself does not do any type checking on the parameters you pass. Use taquito if you don't want to construct the parameters yourself.
The wallet will process and validate the operation. If there are any errors, they have to be displayed in the wallet. run_operation
errors can optionally be sent back to the dApp as well.
- Beacon
- Taquito
Example
import { DAppClient, TezosOperationType } from "@airgap/beacon-sdk"; // Create a new DAppClient instance const dAppClient = new DAppClient({ name: "Beacon Docs" }); 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); } else { const permissions = await dAppClient.requestPermissions(); console.log("New connection:", permissions.address); } const TZ_BUTTON_COLORS_CONTRACT = "KT1RPW5kTX6WFxg8JK34rGEU24gqEEudyfvz"; const tokenId = "925"; // Setting the color of TzButton is only possible if you are currently the leader and own a color // So this call will likely fail try { const result = await dAppClient.requestOperation({ operationDetails: [ { kind: TezosOperationType.TRANSACTION, amount: "0", destination: TZ_BUTTON_COLORS_CONTRACT, parameters: { entrypoint: "set_color", value: { int: tokenId, }, }, }, ], }); console.log(result); } catch (error) { console.error( `The contract call failed and the following error was returned:`, error?.data[1]?.with?.string, ); }
Loading...
Example
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" }); Tezos.setWalletProvider(wallet); const address = await wallet.getPKH(); if (!address) { await wallet.requestPermissions(); } // We now know we are connected, or the above code threw an error const TZ_BUTTON_COLORS_CONTRACT = "KT1RPW5kTX6WFxg8JK34rGEU24gqEEudyfvz"; const contract = await Tezos.wallet.at(TZ_BUTTON_COLORS_CONTRACT); const tokenId = "925"; try { const result = await contract.methods.set_color(tokenId).send(); } catch (error) { console.error( `The contract call failed and the following error was returned:`, error?.data[1]?.with?.string, ); }
Loading...