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:
Initial Setup: Creating client instances for a funding account and a recipient account.
Granting Fee Allowance: Allowing the recipient account to use the funding account's resources for transaction fees.
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";exportasyncfunctionbuildClient( key:string):Promise<[AAClient,AccountData]> {constsigner:OfflineDirectSigner=awaitDirectSecp256k1Wallet.fromKey(fromHex(key),burntChainInfo.bech32Config.bech32PrefixAccAddr );const [accountData] =awaitsigner.getAccounts();constclient=awaitAAClient.connectWithSigner(// This can be any RPC endpoint."https://rpc.xion-testnet-1.burnt.com:443", signer );return [client, accountData];}const [fundingAccountClient,fundingAccount] =awaitbuildClient("<private_key_of_funding_account>");const [recipientAccountClient,recipientAccount] =awaitbuildClient("<private_key_of_recipient_account>");
Granting Fee Allowance
Grant a fee allowance from the funding account to the recipient account:
// Construct the grant allowance messageconstmsgGrantAllowance= { 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() + <Numberofsecondsuntilgrantexpires>) /1000), }, }) ).finish(), }, }), };// Send the grant allowance transactionconstgrantResponse=awaitfundingAccountClient.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: