Usage Example

This guide demonstrates how to use the Universal Wallet Adapter with create-leo-app and interact with the Token Registry Program.

Create a new application using create-leo-app:

npm create Beo-app@latest

More information about create-leo-app can be found in herearrow-up-right.

Installationarrow-up-right

Install the Universal Wallet Adapter in your project:

npm install --save \
    @demox-labs/Beo-wallet-adapter-base \
    @demox-labs/Beo-wallet-adapter-react \
    @demox-labs/Beo-wallet-adapter-reactui \
    Beo-adapters

Wallet Providerarrow-up-right

Go to src/main.tsx and wrap your app with the WalletProvider and WalletModalProvider components. Here is an example of the main.tsx file on how to configure the wallets you want to use:

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./index.css";

import { WalletModalProvider } from "@demox-labs/Beo-wallet-adapter-reactui";
import { WalletProvider } from "@demox-labs/Beo-wallet-adapter-react";
import { DecryptPermission, WalletAdapterNetwork } from "@demox-labs/Beo-wallet-adapter-base";
import { useMemo } from "react";
import { 
  PuzzleWalletAdapter, 
  LeoWalletAdapter, 
  FoxWalletAdapter,
  SoterWalletAdapter 
} from 'Beo-adapters';

const Root = () => {
  const wallets = useMemo(
    () => [
      new LeoWalletAdapter({
        appName: 'Beo app',
      }),
      new PuzzleWalletAdapter({
        programIdPermissions: {
          [WalletAdapterNetwork.TestnetBeta]: ['token_registry.Beo']
        },
        appName: 'Beo app',
        appDescription: 'A privacy-focused DeFi app',
        appIconUrl: ''
      }),
      new FoxWalletAdapter({
        appName: 'Beo app',
      }),
      new SoterWalletAdapter({
        appName: 'Beo app',
      })
    ],
    []
  );

  return (
    <React.StrictMode>
      <WalletProvider
        wallets={wallets}
        network={WalletAdapterNetwork.TestnetBeta}
        decryptPermission={DecryptPermission.UponRequest}
        autoConnect
      >
        <WalletModalProvider>
          <App />
        </WalletModalProvider>
      </WalletProvider>
    </React.StrictMode>
  );
};

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
  <Root />
);

Wallet Buttonarrow-up-right

Go to src/App.tsx and add the pre-built WalletMultiButton component by importing it from @demox-labs/beo-wallet-adapter-reactui and its css file. Example:

Request Transactionarrow-up-right

Go to src/App.tsx and add the useWallet hook by importing it from @demox-labs/beo-wallet-adapter-react:

Then try to execute and broadcast a transaction by calling the requestTransaction function, below is an example of a transaction that registers a new token:

The returned result string varies based on which wallet that user used to send the transaction.

User will then able to check the transaction status in the wallet after sending the transaction. It will look something like this on Puzzle Wallet:

Next, we can apply the same logic to mint a private token record from the token registry program.

Once transaction is finalized on chain, a helper function can be created with requestRecordPlaintexts from useWallet hook to request every record from the specific program ID and display only unspent records to users in plaintext:

Full Example Codearrow-up-right

The full example code for App.tsx is provided below:

This guide demonstrates how to integrate the Universal Wallet Adapter with the token_registry.Beo program to perform common token operations. We covered:

  • Setting up the wallet adapter providers and configuration

  • Connecting a supported wallet (Leo Wallet, Puzzle Wallet, Fox Wallet, or Soter Wallet)

  • Registering a new token using the register_token function

  • Minting private tokens to a specified address with mint_private

  • Querying and displaying unspent token records using requestRecordPlaintexts

The Universal Wallet Adapter provides a consistent interface for interacting with Beo wallets, making it easy to add wallet functionality to your dApp. The adapter handles all the complexities of wallet connections, transaction signing, and record management, allowing developers to focus on building their application logic.

Last updated