Building Gas-less Meta Transaction (ERC-2771) relayer With Google Cloud Pubsub, KMS & BNE

Deep Chenna
Google Cloud - Community
9 min readFeb 13, 2024
Architecture Diagram

This document provides a guide to building a gas-less meta transaction relayer using Google Cloud services. It discusses the challenges of gas fees and meta transactions, introduces the ERC-2771 standard, and explains how to use Google Cloud services to build a relayer that can reduce gas fees for users.

Background

User adoption is one of the key challenges for blockchain based applcations and one of the biggest barriers to user adoption is the need to pay gas fees. Gas fees are a necessary part of the blockchain transaction process, but they can be a significant expense, especially for multiple repeated small transactions. This can make it difficult for users to justify using blockchain for everyday tasks, such as buying coffee or sending a gift card.

Another friction point for user adoption is the need to acquire cryptocurrency. To pay gas fees, users must first purchase cryptocurrency, which can be a daunting task for those who are new to the space. Additionally, cryptocurrency prices can be volatile, which can make it difficult to predict how much a transaction will cost.

These friction points can discourage users from adopting blockchain technology. To address these challenges, blockchain developers are working on ways to reduce gas fees and make it easier for users to purchase cryptocurrency. One solution that is well practiced is Meta Transactions / Gasless Transactions.

What is a Meta-transaction?

Meta Transactions is a technique to reduce the gas fee for the end user. In this approach, a relayer will submit the transaction on behalf of the end user and charge a small fee for the service. The end user can then pay the relayer using their preferred payment method. This approach can significantly reduce the gas fee for the end user and make blockchain more accessible to the masses.

ERC-2771: Secure Protocol for Native Meta Transactions

ERC-2771 is a standard for secure meta transactions on the Ethereum blockchain. It defines a way for users to delegate the signing of their transactions to a third-party service, called a meta transaction relayer. The relayer then submits the transaction on behalf of the user, paying the gas fee.

This standard is designed to reduce the cost of gas fees for users, as they no longer need to pay the gas fee to sign their own transactions. Additionally, it can improve the user experience by making it easier for users to submit transactions.

https://eips.ethereum.org/EIPS/eip-2771

Account Abstraction (ERC-4337) vs. Meta Transactions (ERC-2771)

ERC-2771 (Meta Transactions) and Account Abstraction (primarily focused on ERC-4337) are both techniques designed to improve the user experience on Ethereum. However, it’s not that one is definitively better than the other. Here’s a breakdown to help you understand why:

Account Abstraction (ERC-4337): Decouples the concept of “accounts” from private keys. Ethereum accounts become smart contracts (sometimes called ‘Entry Point contracts’) that define their own logic for validating transactions. This offers wider possibilities for customization.

Here is the blog on why ERC-4337 (Account Abstraction) is gaining preference and below are the few reasons where ERC-2771 could make sense in specific cases

Quick to Implement: If needing a fast solution for gas payment sponsoring in an existing project, ERC-2771 might be simpler to implement right away.

Specific Needs: ERC-2771 might still be viable if your use case is solely focused on gas sponsorship and you don’t see requiring more advanced features offered by account abstraction.

Account Abstraction (ERC-4337) is becoming the preferred standard as it offers a superior degree of flexibility and broader potential than Meta Transactions (ERC-2771). However, there are niche cases where ERC-2771 might be a faster, suitable solution.

Solution

With Google Cloud, you can easily deploy and manage your own ERC2771 relayer. This gives you complete control over your relayer and allows you to customize it to meet your specific needs.

In this blog post, we will discuss the benefits of using an ERC2771 relayer with Google Cloud. We will also provide a tutorial on how to build your own relayer using different GCP services to support meta transactions in your dApp.

Benefits of using an ERC2771 relayer with Google Cloud

  • Security: Google Cloud provides a secure and reliable platform for deploying your relayer. Your relayer will be dedicated and will be able to handle high volumes of transactions.
  • Scalability: Google Cloud is a scalable platform that can grow with your business. As your dAPP becomes more popular, you can easily scale it to meet demand.
  • Cost-Optimized: Google Cloud provides a flexible platform that allows you to customize your relayer to meet your specific needs. You can choose from a variety of instance types, and you can pay for your usage basis.

In this tutorial we will deploy a ERC 2771 compatible smart contract and relay the user transactions via Pubsub and use KMS to store the keys and sign transactions securely. For RPC nodes we will use Google Cloud BNE

Prerequisites

  1. GCP Account
  2. GCP Project
  3. Metamask wallet with Goerli Testnet RPC configure

Step1. Create Key in Key Management

Google Cloud Key Management Service (Cloud KMS) is a managed service for creating, managing, and using cryptographic keys. It provides a secure and scalable way to manage your encryption keys and secrets across a variety of Google Cloud services and your own applications.

  1. In the Cloud Console, go to the Navigation menu > Security > Key Management.
  2. Click + Create Key Ring

3. In the Key Ring Dialog. Enter the Keyring ID and leave others as default

4. In the Create Key dialog, enter the following values and leave other fields default

Step2. Create Pubsub Topic

  1. In the Cloud Console, go to the Navigation menu > Pub/Sub.
  2. Click + Create Topic

3. In Create Topic dialog, give topic id and leave other fields default

Step3. Deploy Sample ERC 2771 Recipient smart contract & Trusted Forwarder

  1. GoTo https://remix.ethereum.org/
  2. Copy the below sample contract to Remix IDE

In the sample code Registry is the Recipient Contract and ERC2771Forwarder is the Forwarder Contract.

3. Got to Compile section and click Compile

4.

4. Deploy the Contracts

Change the Provider to Injected Provider — MetaMask (Connect you metamask to Mumbai Test net before this step

Deploy ERC2771Forwarder first and copy the contract address

Use ERC2771Forwarder contract address from above step and use it while deploying Registry contract.

Step4. Deploy Blockchain Node Engine for Goerli testnet

​​Blockchain Node Engine (BNE) is a fully-managed node-hosting service for Web3 development. Web3 companies who require dedicated nodes can relay transactions, deploy smart contracts, and read or write blockchain data with the reliability, performance, and security they expect from Google Cloud compute and network infrastructure. Learn more.

  1. In the Cloud Console, go to https://console.cloud.google.com/blockchain-node-engine
  2. Click +Create

3. In the Create a Blockchain Node dialog, enter the following values and leave other fields default

4. Copy the JSON-RPC endpoint of the node and keep it into temporary file

5. Create API Key

Your new node can only be queried with an API key. To generate a key in the Google Cloud Console

Go to the APIs & Services page and select Credentials from the left-hand pane.

Select CREATE CREDENTIALS and click API key.

In the API key created dialog, click the copy icon to copy the key and paste it into a temporary file. You will need the API key

Your RPC_URL = <JSON-RPC endpoint>?Key=<API Key>

Note: It will take few minutes for node to sync and ready

Step5. Create Cloud Function for relaying the transactions using Keys in KMS

A cloud function is a piece of code that runs in response to an event, such as an HTTP request, a message from a messaging service, or a file upload. Cloud events are things that happen in your cloud environment. These might be things like changes to data in a database, files added to a storage system, or a new virtual machine instance being created.

Since cloud functions are event-driven, they only run when something happens. This makes them a good choice for tasks that need to be done quickly or that don’t need to be running all the time.

In this step, you’re going to create a cloud function using the console.This function will Read the messages from Pubsub topic, submit the transaction using KMS key via BNE.

To create the function, follow the below steps on the console

  1. In the console, click the Navigation menu > Cloud Functions. (Click on ‘more products’ if you do not find the Cloud Functions in the Navigation menu)
  2. Click Create function.

3. In the Create function dialog, enter the following values:

4. Click ADD Trigger > Pub/Sub Trigger, select Pubsub topic created in step2.

5. Save Trigger and Click Next

Step6. Deploy the function

  1. Still in the Create function dialog, in Source code for Inline editor replace the default code with the following implementation for index.js & package.json

2. In Index.js please replace <YOUR_PROJECT_ID>, <KEY_RING_ID>, <KEY_ID>, <RPC_URL>, <MinimalForwarder_Address> with actual values

Step7. Fund the Relayer

  1. Get the Relayer Address

In the Cloud Console, go to the Navigation menu > Security > Key Management.

Go to the Key details that you created in Step 1

Get the Public Key from Actions and click copy

Take the public key to ASN.1 Decoder to get the hexadecimal Public Key

https://holtstrom.com/michael/tools/asn1decoder.php

Then use ethtools to convert Public Key to Address to get the Relayer Address

2. Use below link to Fund Relayer Address with some GoerliEth

Go to https://goerlifaucet.com/ to fund your relayer address

Step8. Test by submitting signed transactions

  1. Use below script to generate test signed transaction, please replace all the placeholders (enclosed in <> ) with actual values

2. GoTo Messages tab under Pub/Sub Topic and Click Publish

3. Paste the generated meta transaction json and click publish

4. Get the transaction hash from Function logs and check status in explorer

Conclusion

In conclusion, gas fees are a significant barrier to user adoption of blockchain technology. Meta transactions can help reduce gas fees for users, making blockchain more accessible and user-friendly. Google Cloud provides a secure, scalable, and cost-optimized platform for building and deploying ERC2771 relayers. By following the steps in this tutorial, you can build your own relayer using Google Cloud services to support meta transactions in your dApp.

--

--