XION
DiscordGithub
  • Welcome to XION
  • XION'S Core
    • Concepts
      • Generalized Chain Abstraction
      • Intro to Account Abstraction
      • XION's Meta Accounts
      • Meta Accounts Design
      • Architecture & Tech Glossary
      • Use Cases
  • developers
    • Xion Quick Start
      • Zero to Dapp in 5 Minutes
        • Launch a User Map Dapp on XION in 5 Minutes
        • React Native Mobile Dapp on XION in 5 Minutes
    • Mobile dapp Development on XION
      • Set up your XION Mobile Development Environment
      • Create Mobile Dapp and Integrate Meta Account Authentication
    • Getting Started (Advanced)
      • Set Up Local Environment
        • Setting up your Local Smart Contract Development Environment for XION
          • Setting up your XION Smart Contract Development Environment on Windows (WSL2 + Ubuntu)
        • Set Up an Integrated Development Environment (IDE)
        • Interacting with Xion Chain using Xion Daemon
      • Your First Contract
        • Deploying Your First Smart Contract on XION
      • Gasless UX & Permission Grants
        • Enabling Gasless Transactions with Treasury Contracts
      • Dapp Development
        • Account Abstraction with Gasless Transactions
        • Interact with XION via your Backend Service
    • Re-using Existing Contracts
      • Deployed Contracts on XION
      • Non-Fungible Tokens (NFTs)
      • Fungible Tokens
      • Marketplace
      • Multisig
      • Proxy Contracts
      • Membership Management
      • Governance
    • Web3 for Web2 Developers
      • Web2 vs Web3 App Architecture: A Comparison
      • Misconceptions and Misapplied Web2 Patterns
      • Recommended Architecture for Dapps on XION
    • Building for Mainnet
      • Xion Testnet: Your Development Playground
      • Building with Audited & Battle-Tested Contracts
      • Community Engagement: Building Support for Your dApp
      • Deploying to Xion Mainnet
        • Smart Contract Audits: Process, Costs & Support
        • Governance Process to Deploying Smart Contracts to Mainnet
    • Learn & Build
      • Token Factory
        • Creating, Minting, and Interacting with a Token Factory Token
        • Building a React dApp to Interact with Token Factory Tokens
        • Integrating a Token Factory Token in a Smart Contract
      • Websockets
        • WebSockets with Xion: Real-Time Communication
      • Mobile Development
        • Building a React Native Mobile App with Abstraxion (Xion.js)
      • Oracles
        • Creating a Smart Contract with Pyth Oracle Integration
      • Indexers: Optimized Data Retrieval
        • SubQuery
      • Use Cases
        • Building a Per-User Data Storage Dapp
    • Reference and Resources
      • Requesting XION Testnet Tokens
      • Public Endpoints & Resources
      • Block Explorers
      • Governance
        • Deploying Smart Contracts to Mainnet
      • Developer Tools: Abstract
      • IBC Denoms on XION Networks
      • Frequently Asked Questions
      • XION Token Contract Addresses on EVM Chains
  • Nodes & Validators
    • Run a Node
      • System Specifications
      • Build the Xion Daemon
      • Download the Xion Daemon
      • Configure the Xion Daemon
        • app.toml
        • client.toml
        • config.toml
      • Join the XION Network
        • xion-testnet-1
      • Confirm node is running
    • Become a Validator
      • Initial Setup
      • Obtain a XION Wallet Address
      • Obtain Funds
        • Testnet
      • Create Validator
    • IBC Relayers and Tokens
  • Others
    • Resources
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. developers
  2. Getting Started (Advanced)
  3. Dapp Development
  4. Create a Gas-less User Experience

Fee Granting

Demonstrating Fee Allowance

This guide outlines how to grant a fee allowance, so that you can enable a gas-less experience for your end users. This guide also demonstrates how to validate its functionality by sending tokens back using AAClient. The process involves:

  1. Initial Setup: Creating client instances for a funding account and a recipient account.

  2. Granting Fee Allowance: Allowing the recipient account to use the funding account's resources for transaction fees.

  3. Validating Fee Allowance: Sending a token from the recipient account to the funding account to demonstrate the successful fee grant.

Initial Setup

Begin by initializing client instances for both accounts using user-provided private keys:

import { AAClient } from "@burnt-labs/signers";

// These are used in the following code examples.
import { MsgGrantAllowance } from "cosmjs-types/cosmos/feegrant/v1beta1/tx";
import { BasicAllowance } from "cosmjs-types/cosmos/feegrant/v1beta1/feegrant";


export async function buildClient(
  key: string
): Promise<[AAClient, AccountData]> {
  const signer: OfflineDirectSigner = await DirectSecp256k1Wallet.fromKey(
    fromHex(key),
    burntChainInfo.bech32Config.bech32PrefixAccAddr
  );

  const [accountData] = await signer.getAccounts();
  const client = await AAClient.connectWithSigner(
    // This can be any RPC endpoint.
    "https://rpc.xion-testnet-2.burnt.com:443",
    signer
  );

  return [client, accountData];
}

const [fundingAccountClient, fundingAccount] = await buildClient("<private_key_of_funding_account>");
const [recipientAccountClient, recipientAccount] = await buildClient("<private_key_of_recipient_account>");

Granting Fee Allowance

Grant a fee allowance from the funding account to the recipient account:

  // Construct the grant allowance message
  const msgGrantAllowance = {
    typeUrl: "/cosmos.feegrant.v1beta1.MsgGrantAllowance",
    value: MsgGrantAllowance.fromPartial({
      granter: fundingAccount.address,
      grantee: recipientAccount.address,
      allowance: {
        typeUrl: "/cosmos.feegrant.v1beta1.BasicAllowance",
        value: BasicAllowance.encode(
          BasicAllowance.fromPartial({
            spendLimit: [
              {
                denom: "uxion",
                amount: "<amount of token to grant>",
              },
            ],
            expiration: {
              seconds: Math.floor((Date.now() + <Number of seconds until grant expires>) / 1000),
            },
          })
        ).finish(),
      },
    }),
  };

  // Send the grant allowance transaction
  const grantResponse = await fundingAccountClient.signAndBroadcast(
    fundingAccount.address,
    [msgGrantAllowance],
    defaultFee,
    "Granting fee allowance to recipient account"
  );

Validating Fee Allowance

To confirm the fee allowance, send a token back from the recipient account to the funding account:

  // Construct the send token message
  const msgSend = {
    typeUrl: "/cosmos.bank.v1beta1.MsgSend",
    value: {
      fromAddress: recipientAccount.address,
      toAddress: fundingAccount.address,
      amount: [
        {
          denom: "uxion",
          amount: "1",
        },
      ],
    },
  };

  // Send the token transaction
  const sendResponse = await recipientAccountClient.signAndBroadcast(
    recipientAccount.address,
    [msgSend],
    {
      ...defaultFee,
      granter: fundingAccount.address,
    },
    "Returning token to funding account"
  );

  console.log("sendResponse", sendResponse);

This complete example shows how to set up a fee allowance and verify its success through a token transaction.

Last updated 2 months ago

Was this helpful?