Demo code for relaying meta-transactions using Gelato using the client API.
This project consists of a sample names registry contract that accepts registrations for names either directly or via a meta-transaction, along with a client Dapp, plus the meta-transaction relayer implementation.
Expected .env
file in the project root:
RELAY_API_KEY
: Gelato Relay API key.
To get the Realy keys you will need to sign up to 1Balance. See more information here
Once you set up the .env file you need to install the dependencies
yarn install
Then start the next app
yarn dev
It will start a webpage at localhost:3000 and a backend that is used for APIs.
Gelato comes with an SDK but it's meant to be used directly from the front end as relayWithSponsoredCallERC2771 needs a provider that can sign the transaction, as the wallet in most cases would be the user signing off-chain with Metamask this would have to go on the front end. But if we do that, we would expose our API KEY and an attacker may use your API KEY to relay their transactions.
import { GelatoRelaySDK } from "@gelatonetwork/relay-sdk";
const counter = "0x...";
const abi = ["function increment()"];
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const user = signer.getAddress();
const contract = new ethers.Contract(counter, abi, signer);
const { payload } = await contract.populateTransaction.increment();
const relayRequest = {
chainId: provider.network.chainId;
target: counter;
data: payload;
user: user;
};
const apiKey = "stracciatella";
// Get user signature and send relay request to Gelato Relay
const relayResponse =
await GelatoRelaySDK.relayWithSponsoredCallERC2771(relayRequest, provider, apiKey);
console.log(relayResponse);`
As we need to use this in the backend we ended up using the Gelato API instead.
Take into account that Gelato does not use the value field when relaying transactions so we can't relay ETH transfers.
Example addresses are sometimes wrong in gelato docs, trusted forwarder used by the SDK is 0xBf175FCC7086b4f9bd59d5EAE8eA67b8f940DE0d, but the deployed example contracts are pointing to the wrong address so we had to deploy new ones https://github.com/Think-and-Dev/gelato-relay-contract. In case you are getting errors that don't make much sense try reaching out to their discord
The most important parts are:
- The smart contract needs to implement EIP-2771 and we need to use GelatoRelayERC2771 at 0xBf175FCC7086b4f9bd59d5EAE8eA67b8f940DE0don
- User signing of chain using signType_v4 at signer.js
- Send the signed message to the backend at increment.js, where is sent to the relayer.
This code is based on [Gelato examples which make use of gelato sdk and relayers
on the Goerli network. Functionality is supported across any of Gelato's supported chains -- simply modify the code.