Building a React dApp to Interact with Token Factory Tokens

This guide walks through creating a React-based decentralized application (app) that interacts with a token created using the XION Token Factory. This app will:

  • Authenticate users with a Meta Account that would have received a token created via the Token Factory. See the following guide on how to create your token.

  • Display the token balance for the logged-in user

  • Enable the logged in user to send tokens to another address

Setting Up the Project

Initialize a New Next.js Project

Run the following command to create a new Next.js project configured with TypeScript, ESLint, and Tailwind CSS:

npx [email protected] xion-token-dapp \
  --use-npm --ts --eslint --tailwind --app --src-dir --import-alias "@/*"
cd xion-token-dapp

Install Dependencies

Install the Abstraxion SDK:

npm install @burnt-labs/abstraxion

Start the Development Server

Open http://localhost:3000 in your browser to view the default Next.js page.

Deploying a Treasury Contract for Gasless Transactions

Before integrating the Abstraxion SDK into the application, we first need to deploy a Treasury Contract. This contract facilitates gasless transactions by executing fee grants on behalf of users.

Steps to Deploy a Treasury Contract

  1. Login to the XION Developer Portal.

  2. Click on "New Treasury" to create a new treasury contract.

  3. Select the appropriate configuration based on your use case. The following "Fee Grant" and "Grant Config" images gives a recommended configuration that works for this particular use case:

Fee Grant

Example of a general Fee Grant configuration
  1. Enter a "Description" in the description field. This will reflect the intended purpose of the request.

  2. In the "Allowance Type" field, enter "/cosmwasm.feegrant.v1beta1.BasicAllowance".

  3. You can apply a "Spend Limit" if you wish, which will be applied to each user .

  4. Click the "Save" button to apply the configuration.

Grant Config

Example of additional Grant configuration
  1. For this example the "Type URL" would be "/cosmos.bank.v1beta1.MsgSend" as this would allow for the app the send tokens on behalf of the user.

  2. Enter a "Description" in the description field. This will reflect the intended purpose of the request. This description will be displayed to users when they click "Allow" after connecting their account.

  3. In the "Authorization Type" field, select "/cosmos.authz.v1beta1.GenericAuthorization".

  4. Then click the "Save" button which generates the "Treasury Instance Preview"

Treasury Instance Preview

Once the preview is to your liking click the "Create" button to create the Treasury contract.

Learn more about Treasury Contracts here.

Configuring Abstraxion for Authentication

To enable account abstraction and gasless transactions, integrate the Abstraxion provider into your application.

Configure the Abstraxion Provider

Replace the contents of src/app/layout.tsx with the following:

Replace YOUR_TREASURY_CONTRACT_ADDRESS_HERE with the actual treasury contract address.

Bringing it Together

Replace the contents of src/app/page.tsx with the following code:

Replace TOKEN_DENOM with the actual Token Factory token denomination. Also, replace DENOM_DISPLAY_NAME with the name of the token you want to display next to the balance.

The sections below will provide a walkthrough of the code above.

Token Configuration

  • Defines the Token Factory token’s denomination that will be queried and transferred.

  • DENOM_DISPLAY_NAME is used for a more readable display instead of the full denom.

Fetching Token Balance

  • Queries the XION blockchain for the user’s Token Factory token balance.

  • Uses the Abstraxion Client (queryClient.getBalance()).

  • Updates the balance state with the retrieved amount.

Sending Tokens

  • Uses sendTokens() to send tokens from the logged-in user’s address to the recipient.

  • Uses a gas adjustment of 1.5 for reliability.

  • If successful, stores transaction details (transactionHash, height).

  • Calls getTokenBalance() to update the UI after sending tokens.

UI Rendering

Displaying User’s Address

  • Shows the logged-in user's address for easy reference.

Displaying Token Balance

  • Displays the user’s current Token Factory balance.

Token Transfer Form

  • Allows the user to enter a recipient address and token amount.

  • Calls handleSend() when "Send Tokens" is clicked.

Displaying Transaction Details After Sending Tokens

  • Displays transaction details after sending tokens.

  • Provides a block explorer link for verification.

A Quick Walkthrough

The following steps outline how to use the app.

Log into the app

The first step after accessing the app at http://localhost:3000/ is to log in by clicking the CONNECT button.

Get Token Balance

After logging in, you will see your Meta Account address. You need to send some of your custom Token Factory tokens to that address.

Once the tokens have been sent, click the Get Token Balance button to retrieve the updated balance.

Sending Tokens

To send tokens to another address:

  1. Enter a valid address in the Recipient Address text box.

  2. Enter the amount to send in the Amount text box.

  3. Click the Send Tokens button.

After submitting the transaction, you will see the transaction hash, the block height at which the transaction was executed, and a URL to view the transaction details in the block explorer.

You now have a functional React app that:

  • Authenticates users via Meta Accounts

  • Fetches and displays Token Factory token balance

  • Enables users to send tokens to another address

Last updated

Was this helpful?