AvaCloud Gas Relayer Developer Guide
Prerequisite
Gasless Relayer has been set up by following this.
Introduction
Gas relayer service is a meta-transaction relayer following the EIP2771 standard. The specific implementation is based on Gas Station Network. This guide will show you how to use the service and how to implement smart contracts with support for meta-transactions.
Getting Started
After your gas relayer setup in AvaCloud is completed, you’ll have access to the following info in the dashboard:
- Gas relayer RPC URL
- Gas relayer wallet address
- Forwarder contract address
- Domain name
- Domain version
- Request type
- Request suffix
Keep the dashboard open as you’ll need this info to interact with the gas relayer. Let’s start with deploying gasless counter contract example.
1. Install Rust and make sure the following works:
2. Install Foundry, and make sure the following works:
3. Clone and setup the repo
4. Deploy gasless counter contract
5. Send the gasless transaction. Make sure you use the values from the dashboard.
6. Verify that transaction was successfully processed
Congratulations! You’ve successfully deployed and interacted with a gasless contract through your relayer. Next, let’s see how to interact with gas relayer in code with ethers in javascript and web3py in python.
Usage
The steps to interact with the gas relayer are as follows, and both ethers and python scripts will follow these steps:
- Initializes the provider for your L1
- Gets the account nonce from the forwarder contract
- Estimates the gas for the destination contract function
- Creates the EIP712 message
- Signs the typed message
- Recover the signer address to verify the signature
- Sends the signed message to the gas relayer
- Fetch the receipt and verify transaction was successful
As we mentioned in the introduction, the gas relayer service is based on the GSN standard and submits all transactions to the forwarder contract for verification. The forwarder contract checks the signature and forwards the transaction to the target contract.
Step 1. To prepare the data for the request to gas relayer, you’ll need to fetch the nonce from the forwarder contract and estimate the gas for destination contract function. To do this, you’ll need to initialize the provider for your L1 with the public RPC URL.
Step 2. Full forwarder interface can be found here and implementation here.
Step 3. Since the forwarder message requires gas to be passed, we need to estimate the gas for the destination contract function. When implementing your own contract logic, make sure that you estimate the gas upfront and pass sufficient gas as a value, otherwise the transaction will fail.
Steps 4. and 5. create the EIP712 typed message and sign it. It is important that you use the correct values available in the dashboard.
Step 6. is optional, but can be used to verify that the signature is correct, before sending a request to the gas relayer.
Step 7. Gas relayer exposes POST
endpoint for eth_sendRawTransaction
method, which accepts the signed message in the JSON RPC format. The request should be sent to the gas relayer server URL.
Step 8. Gas relayer will return the transaction hash in response, which can be used to fetch the transaction receipt. One should always verify the transaction receipt to ensure that the transaction was successfully executed on chain.
Usage with Typescript ethers
library
Prerequisites:
- Install node v18.16.1
- Install yarn
1. Create new directory and initialize the project
2. Create .env
from the template below and fill in the values using the AvaCloud dashboard info.
3. Create a src
directory and new file increment.ts
4. Add the following code to increment.ts
5. Run the script
Beside the steps outlined in the general usage section, there are some things to point out.
For interacting with the forwarder contract, script uses minimal forwarder ABI and just utilizes the getNonce
function call, which returns the forwarder contract nonce for the given address.
Request suffix that is available in the dashboard needs to be split into type and name. The type is the first part of the suffix, and the name is the rest of the suffix without the last character()
). These values are then used in getEip712Message
function to create the EIP712 message.
Usage with Python
Prerequisites:
- Python 3.11 installed
1. Create new directory, setup and activate, virtual environment and install the dependencies
2. Create .env
from the template below and fill in the values using the AvaCloud dashboard info
3. Create a new file increment.py
and paste the following code
4. Run the script
Implementing smart contracts
To add support for the transaction to be relayed to your smart contracts, you need to implement the following:
1. Your contract must be an instance of the ERC2771Recipient
contract or implement the IERC2771Recipient
interface.
2. For any function that you want to call via the gas relayer, make sure that msg.sender
is not used directly. Instead, use _msgSender()
function from IERC2771Recipient
interface.
3. In the constructor of your contract, set the trusted forwarder address to the address of the forwarder contract.
See GaslessCounter.sol
for full code example.
Restricting access
Gas relayer supports restricting access to the service by using the allow/deny list. It is used to only allow/deny specific addresses to use the service. The allow/deny list is set in the dashboard and can be updated at any time.
Other examples
See other examples from the community:
For any additional questions, please view our other knowledge base articles or contact a support team member via the chat button. Examples are for illustrative purposes only.
Learn More About AvaCloud | Download Case Studies | Schedule an AvaCloud Demo