Pay gas in ERC20 tokens (2024)

Overview

This tutorial demonstrates how to use the Biconomy Smart Account to perform a transaction with ERC-20 token payment. The provided code includes creating a Biconomy Smart Account, encoding a function call, building a transaction, and sending it with ERC-20 token payment.

Prerequisites

  • Node.js installed on your machine
  • A Biconomy API key and Bundler url, get from dashboard

For this tutorial we are going to mint and NFT on Amoy, this means we can use the following:

  • bundlerUrl -->
    https://bundler.biconomy.io/api/v2/80002/nJPK7B3ru.dd7f7861-190d-41bd-af80-6877f74b8f44
  • preferred ERC20 token for gas payment --> 0xf555F8d9Cf90f9d95D34488e6C852796D9acBd31 (USDC)
  • nft address --> 0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e

Step 1: Create Biconomy Smart Account and Encode Function Call

const smartWallet = await createSmartAccountClient({ signer, // can be viem client or ethers signer bundlerUrl, biconomyPaymasterApiKey,}); const encodedCall = encodeFunctionData({ abi: parseAbi(["function safeMint(address _to)"]), functionName: "safeMint", args: [recipient],});

Create a Biconomy Smart Account instance and encode the function call for the transaction.

Step 2: Build the Transaction

const nftAddress = "0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e";const transaction = { to: nftAddress, data: encodedCall,};

Build the transaction with the encoded function call.

Step 3: Send Transaction with ERC-20 Preferred Token Payment

const { wait } = await smartWallet.sendTransaction(transaction, { paymasterServiceData: { mode: PaymasterMode.ERC20, preferredToken: "0xf555F8d9Cf90f9d95D34488e6C852796D9acBd31", },}); const { receipt: { transactionHash }, userOpHash, success,} = await wait(); if (success == "true") { console.log("UserOp receipt", receipt); console.log("Transaction receipt", receipt.receipt);}

You can choose from a list of ERC20 tokens to pay for the gas, not giving a preferred token will allow you to select from current supported tokens.

const feeQuotesResponse = await smartWallet.getTokenFees(transaction, { paymasterServiceData: { mode: PaymasterMode.ERC20 },}); const userSelectedFeeQuote = feeQuotesResponse.feeQuotes?.[0]; // Allow user to pick desired token const { wait } = await smartWallet.sendTransaction(transaction, { paymasterServiceData: { mode: PaymasterMode.ERC20, feeQuote: userSelectedFeeQuote, spender: feeQuotesResponse.tokenPaymasterAddress, maxApproval: true, },}); const { receipt: { transactionHash }, userOpHash, success,} = await wait(); if (success == "true") { console.log("UserOp receipt", receipt); console.log("Transaction receipt", receipt.receipt);}

Send the transaction using the Biconomy Smart Account, specifying ERC-20 token payment details. Wait for the transaction to be mined and get the transaction hash.

That's it! You've successfully performed a transaction with ERC-20 token payment using the Biconomy Smart Account. Feel free to customize this example based on your specific use case.

Pay gas in ERC20 tokens (2024)
Top Articles
Enterprise value – calculation and mis-calculation | The Footnotes Analyst
Top 10 Ways Real Estate Can Build Wealth
Hometown Pizza Sheridan Menu
Craftsman M230 Lawn Mower Oil Change
Faint Citrine Lost Ark
Summit County Juvenile Court
From Algeria to Uzbekistan-These Are the Top Baby Names Around the World
Hawkeye 2021 123Movies
Weather In Moon Township 10 Days
Ncaaf Reference
How Many Slices Are In A Large Pizza? | Number Of Pizzas To Order For Your Next Party
Craigslist Alabama Montgomery
Aldi Sign In Careers
Nail Salon Goodman Plaza
Ups Print Store Near Me
U Of Arizona Phonebook
How to Download and Play Ultra Panda on PC ?
Who is Jenny Popach? Everything to Know About The Girl Who Allegedly Broke Into the Hype House With Her Mom
[PDF] PDF - Education Update - Free Download PDF
Walgreens Bunce Rd
Which Sentence is Punctuated Correctly?
Surplus property Definition: 397 Samples | Law Insider
Wiseloan Login
Copper Pint Chaska
Cona Physical Therapy
Bfsfcu Truecar
Ordensfrau: Der Tod ist die Geburt in ein Leben bei Gott
Ryujinx Firmware 15
Otis Offender Michigan
Orange Pill 44 291
Rocketpult Infinite Fuel
Nacho Libre Baptized Gif
19 Best Seafood Restaurants in San Antonio - The Texas Tasty
RALEY MEDICAL | Oklahoma Department of Rehabilitation Services
Stewartville Star Obituaries
Ferguson Showroom West Chester Pa
Seminary.churchofjesuschrist.org
Sun Tracker Pontoon Wiring Diagram
Todd Gutner Salary
How I Passed the AZ-900 Microsoft Azure Fundamentals Exam
Patricia And Aaron Toro
Killer Intelligence Center Download
Aurora Southeast Recreation Center And Fieldhouse Reviews
Dayton Overdrive
Argus Leader Obits Today
Zadruga Elita 7 Live - Zadruga Elita 8 Uživo HD Emitirani Sat Putem Interneta
Roller Znen ZN50QT-E
Craigslist Pets Lewiston Idaho
sin city jili
Peugeot-dealer Hedin Automotive: alles onder één dak | Hedin
Land of Samurai: One Piece’s Wano Kuni Arc Explained
Famous Dave's BBQ Catering, BBQ Catering Packages, Handcrafted Catering, Famous Dave's | Famous Dave's BBQ Restaurant
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 5425

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.