Biconomy - Gas less Transactions

Jay NalamJay Nalam
4 min read

Hello Everyone..

Welcome to another day of exploring Web3 Engineering. Today, we will look into Biconomy and how it achieves gas less transactions using Account abstraction. So, without any further ado, let's get started.

Biconomy

Biconomy actually started as a plug and play protocol on EVM blockchains to provide gas-less with it's relayer infrastructure. Initially they provided gas less or Meta transactions using ERC-2771 standard. The user has to create a biconomy on it's dashboard and fill the gas tank with the respective native token of the chain they are providing the gas less transactions.

ERC 2771 Standard

ERC-2771 simplifies Ethereum smart contract interactions by enabling gasless transactions and meta transactions. It provides a secure protocol for native meta transactions, ensuring that transactions are correctly relayed and executed. By leveraging ERC-2771, developers can create more user-friendly and efficient dApps.

Key Components

  • Transaction Signer: The user who initiates the transaction and signs the request off-chain.

  • Gas Relay: A third-party account that pays for the gas fees and turns the signed request into a valid on-chain transaction.

  • Trusted Forwarder: A contract that verifies the transaction data and ensures it is correctly relayed to the intended recipient contract.

  • Recipient Contract: The smart contract that accepts and executes the meta transaction.

Workflow

  • Off-Chain Signing: The user signs a request off-chain using their private key.

  • Gas Relay: The gas relay receives the signed request and pays the gas fee.

  • Trusted Forwarder: The trusted forwarder verifies the transaction data and ensures it is correctly relayed to the recipient contract.

  • On-Chain Execution: The recipient contract executes the transaction.

But later, Biconomy switched from ERC2771 to ERC 4337 Account Abstraction Model to provide which uses new way of making transactions and efficient transaction batching which reduces the gas fees for the sponsors.

ERC 4337 Account Abstraction

ERC-4337 is another standard that enables account abstraction and provides smart contracts with EAO features. While both standards simplify transactions, ERC-2771 focuses on gasless transactions and meta transactions, whereas ERC-4337 introduces a new object type called UserOperations and a paymaster mechanism for managing gas fees.

Key Components

  • UserOperations: These are "to-do lists" that users provide to their Ethereum accounts, which can include actions like transferring funds, interacting with smart contracts, or a combination of multiple actions. UserOperations are bundled together into a single transaction, reducing the need for multiple transactions.

  • EntryPoint Contract: This contract acts as a proxy for the user, executing the UserOperations and ensuring that the user's intent is correctly relayed to the intended recipient contract.

  • Paymaster Mechanism: This mechanism allows users to pay gas fees using ERC-20 tokens instead of ETH or enables a third party to sponsor gas fees in a decentralized manner.

Biconomy Implementation

In the latest version of Biconomy (v4), we have the Biconomy Account Abstraction toolkit is available which we can use to create smart account for the users in order to make the ERC4337 transactions.

Before starting with that, we first have to setup our app on the biconomy dashboard where we set up the Paymaster and we obtain the Bundler url from it.

We can fill the paymaster with the required tokens and pass the required credentials to the SDK for creating the smart accounts. Here is the script to create the smart accounts for a given user.

Let us check a typescript example on how to create a smart account for the user using viem and typescript/javascript.

import {
  Hex,
  createWalletClient,
  encodeFunctionData,
  http,
  parseAbi,
  zeroAddress,
} from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { polygonAmoy } from "viem/chains";
import { createSmartAccountClient } from "@biconomy/account";

const bundlerUrl = "BUNDLER_URL"; // Found at https://dashboard.biconomy.io

export const createAccountAndMintNft = async () => {
  // ----- 1. Generate EOA from private key
  const account = privateKeyToAccount("0x" + "PRIVATE_KEY");
  const client = createWalletClient({
    account,
    chain: polygonAmoy,
    transport: http(),
  });
  const eoa = client.account.address;
  console.log(`EOA address: ${eoa}`);

  // ------ 2. Create biconomy smart account instance
  const smartAccount = await createSmartAccountClient({
    signer: client,
    bundlerUrl,
  });
  const saAddress = await smartAccount.getAccountAddress();
  console.log("SA Address", saAddress);
};
createAccountAndMintNft();

Now, let us look into how to make a gasless transaction using the Smart account.

try {
  const nftAddress = "0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e";
  const parsedAbi = parseAbi(["function safeMint(address _to)"]);
  const nftData = encodeFunctionData({
    abi: parsedAbi,
    functionName: "safeMint",
    args: [saAddress as Hex],
  });

  // ------ 4. Send transaction
  const userOpResponse = await smartAccount.sendTransaction({
    to: nftAddress,
    data: nftData,
  });
  const { transactionHash } = await userOpResponse.waitForTxHash();
  console.log("transactionHash", transactionHash);
  const userOpReceipt = await userOpResponse.wait();
  if (userOpReceipt.success == "true") {
    console.log("UserOp receipt", userOpReceipt);
    console.log("Transaction receipt", userOpReceipt.receipt);
  }
} catch (error: unknown) {
  if (error instanceof Error) {
    console.error("Transaction Error:", error.message);
  }
}

NOTE: If we ever want to make any payable transactions using our smart account (send ether in a contract call), we need to contain funds on the smart account.

0
Subscribe to my newsletter

Read articles from Jay Nalam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jay Nalam
Jay Nalam

Hi, I'm Jay Nalam, a seasoned Web3 Engineer committed to advancing decentralized technologies. Specializing in EVM-based blockchains, smart contracts, and web3 protocols, I've developed NFTs, DeFi protocols, and more, pushing boundaries in the crypto realm.