The Power of Meta-Transactions: Overcoming Gas Fee Barriers for a Smoother Ethereum Experience
Enabling Gas-Less Transactions: Meta-Transactions and OpenZeppelin’s Relayer Service
In the area of blockchain technology, Ethereum has become a potent foundation for smart contracts and decentralized applications (dApps). However, a significant barrier to the network’s wider adoption has been its reliance on gas fees. Thus, meta-transactions, are a concept that offers a solution to the gas fee barrier and aims to create a smoother Ethereum experience.
In this blog post, we will try to convey our thoughts on the requirements for the meta-transactions, the benefits of meta-transactions, and how to implement a sample dApp to send gas-less transactions using openZeppeline’s defender and relayer services.
Why Meta-Transactions are Needed
When engaging with a dApp, users must sign and submit transactions using tools like MetaMask or other web3 wallets.
If the transaction involves modifying or writing data on the blockchain, users must pay a gas fee to support the miners. To pay the fee, users need to have enough crypto in the wallet. But, as web3 is an emerging technology, it is plausible that many end-users may possess sufficient funds in fiat currency but not enough crypto. This can limit their ability to use dApps, unless they can convert their fiat money into crypto.
Benefits of Meta-Transactions
Meta-transactions let users do transactions on Ethereum without paying gas fees. Here, a third-party relayer or a smart contract pays the gas fees for the user. This makes transactions easier and smoother for Ethereum users. Here are some reasons developers use meta-transactions on their dApps:
- User-Friendly Experience: Meta-transactions remove the burden of users having to manage and hold Ether (ETH) to pay for gas fees. This greatly simplifies the user experience and makes Ethereum more accessible to a wider audience. Users can interact with dApps seamlessly without needing to acquire Ether or worry about managing their gas reserves.
- Cost Efficiency: Gas fees can change a lot and be expensive, especially during network congestion. Meta-transactions help users avoid the changing gas fees. Relayers or smart contracts pay the gas fees with their own money or by combining many transactions. This help save money for users.
- Onboarding New Users: Meta-transactions make it easier for new users to join Ethereum. Users don’t have to pay or worry about gas fees to use dApps. This helps Ethereum grow, attract more users, and create new things.
Implementing Meta-Transactions Using Sample Smart Contract
Before we start, we need to know what a “Relayer Service” is. Relayers are helpers that do meta-transactions for users. They connect users and the blockchain network. Their primary function is to send meta-transactions for users.
In the context of this blog, we will use OpenZeppelin’s Defender Relayer service to show how it works.
Step 1: Get started with Openzeppeline’s Defender
Upon accessing OpenZeppelin’s Defender platform, you will be redirected to an intuitive admin dashboard page, as shown in the screenshot below:
Step 2: Navigate to Relayer Service
Navigate to the “Relay” option on the left sidebar, and you will be redirected to the page shown below:
Step 3: Create a new Relayer Service
To proceed, click on the “CreateRelayer” button on the top right. As prompted, fill in the necessary information, such as the name and network. Once done, you will find your newly created relayer service on your dashboard.
Step 4: Add funds to “MyDemoRelayer” to pay gas fees
You can choose any suitable approach to deposit funds to the provided address associated with the relayer service.
Step 5: Generate a new API Key for Relayer Service
Click on the settings icon and choose the ‘Create new API Key’ option. This will generate an API Key and secret for you, which must be securely stored to ensure their confidentiality.
Smart Contract
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Counter {
uint256 public count;
function getCount() public view returns (uint256) {
return count;
}
function setCount(uint256 newCount) public {
count = newCount;
}
}
This is the sample smart contract that has been deployed and verified on the polygon Mumbai testnet.
The “Counter” contract has a private variable called count
of type uint256
. It includes a public getter function getCount()
to retrieve the current value of count
, and a public setter function setCount(uint256 newCount)
to update the value of count
. The contract allows external entities to access and modify the value of count
through these functions.
// Import necessary dependencies
const {
DefenderRelaySigner,
DefenderRelayProvider,
} = require('defender-relay-client/lib/ethers');
const { ethers } = require('ethers');
// abi for the smart contract
const ABI = require('./sample-dapp/src/contract/abi.json');
const contractAddress = '0x86D2b564159c7a0c897E711133BFdbAB4c50291e';
// Add your API Key and Secret for the Openzeppeline Relayer Service (generated in step 5)
const apiKey = '';
const apiSecret =
'';
// Main function
(async () => {
// Set up credentials
const credentials = { apiKey, apiSecret };
// Create provider and signer using the DefenderRelayProvider and DefenderRelaySigner
const provider = new DefenderRelayProvider(credentials);
const signer = new DefenderRelaySigner(credentials, provider, {
speed: 'fast',
});
// Create contract instance
const contract = new ethers.Contract(contractAddress, ABI, signer);
// Invoke the setCount function with the desired parameter (e.g., 7)
const tx = await contract.setCount(7);
// Output the transaction details
console.log(tx);
})();
This JavaScript code snippet enables the transaction to set the count variable of the Counter smart contract. By utilizing the Relayer service we have created, users are relieved from the responsibility of paying the gas fee for the transaction.
Instead, the relayer service covers the gas fee, while users can compensate the relayer service in fiat currency through a mutually agreed contract.
Upon executing this script, you can verify the transaction on the Mumbai network of PolygonScan. By visiting the platform and inspecting the transaction history, you will observe the transaction details like in the image below:
Wrap Up
Meta-transactions help users avoid gas fee problems and use Ethereum better.
With OpenZeppelin’s Defender and Relayer services, developers can make transactions easier, cheaper, and more accessible for new users. Overall, meta-transactions have the potential to drive further growth, adoption, and innovation within the Ethereum community.
Follow Simform Engineering to keep up with the latest trends in the development ecosystem.