How to send your first gasless transaction with Gelato Relay

Gelato Team

Jan 20, 2023

Creating a gasless user experience for your application allows you to offer users the ability to send transactions

without needing native tokens to pay for gas.


Gelato Relay provides out-of-the-box authentication with flexible payment support.

Just answer two easy questions first and in the time it takes to make a coffee, your first gasless tx will be ready to execute!

Questions to ask yourself before starting

1. Do you require user authentication?

If you need to authenticate a user, you need to know their original address. For gasless transactions,

Gelato Relay supports ERC2771 and EIP-712 signatures to verify and recover addresses on-chain.


The _msgSender() function is available via inheritance to access the original sender. If you use msg.sender directly, you’ll get the Gelato Relay contract address instead.

For example, our demo app authenticates proposal submissions to prevent double voting.

However, if anyone can create proposals, you may not need authentication at all.


2. What is the funding strategy?

Transactions can be funded in two ways:


Note: We recommend 1Balance for a streamlined experience.

For our first demo transaction, we’ll pay from the target contract balance. For the second, we’ll use 1Balance with our own SponsorKeys.


Two Steps to Best-in-Class UX

Once you’ve decided on authentication and payment strategy, just follow two steps:


  1. Prepare the target contract by inheriting a Gelato helper contract.

  2. Build & send the transaction in your frontend.

Reference table:

Gelato Auth

Payment

Inheriting Contract

SDK/API method

No

User

GelatoRelayContext

relayWithSyncFee

Yes

User

GelatoRelayContextERC2771

relayWithSyncFeeERC2771

No

1Balance

n.a.

relayWithSponsoredCall

Yes*

1Balance

ERC2771Context

relayWithSponsoredCallERC2771

* A SponsorKey is required, available via Gelato 1Balance.

Step #1: Prepare the contract

Example (no authentication, fees paid by contract):


// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import { GelatoRelayContext } from "@gelatonetwork/relay-context/contracts/GelatoRelayContext.sol";

contract GaslessProposing is GelatoRelayContext {
  function createProposal(bytes calldata payload) external onlyGelatoRelay {
    _transferRelayFee();
    // proposal logic...
  }
}

Example (1Balance, authenticated users):


// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import { ERC2771Context } from "@gelatonetwork/relay-context/contracts/vendor/ERC2771Context.sol";

contract GaslessVoting is ERC2771Context {
  constructor() ERC2771Context(0xBf175FCC7086b4f9bd59d5EAE8eA67b8f940DE0d) {}

  modifier onlyTrustedForwarder() {
    require(isTrustedForwarder(msg.sender), "Only callable by Trusted Forwarder");
    _;
  }

  function votingProposal(bool positive) external onlyTrustedForwarder {
    address voter = _msgSender();
    _votingProposal(positive, voter);
  }
}

Step #2: Build & Send the Transaction

Example (no authentication, fees paid by contract):

import { GelatoRelaySDK } from '@gelatonetwork/relay-sdk';

const feeToken = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
const { data } = await this.readGaslessProposing.populateTransaction.createProposal(payload);

const request = {
  chainId: 5,
  target: targetContract.address,
  data: data!,
  isRelayContext: true,
  feeToken: feeToken,
};

const relayResponse = await GelatoRelaySDK.relayWithSyncFee(request);

Example (1Balance, authenticated users):

const { data } = await this.gaslessVoting.populateTransaction.votingProposal(value);

const request = {
  chainId: 5,
  target: targetContract.address,
  data: data!,
  user: userAddress!,
};

const sponsorApiKey = 'YOUR KEY';

const relayResponse = await GelatoRelaySDK.relayWithSponsoredCallERC2771(
  request,
  provider,
  sponsorApiKey
);

Conclusion

By making your contracts gasless with Gelato Relay, you remove friction for users.

They no longer need to obtain native tokens to interact with your app—making onboarding seamless and UX smoother.


About Gelato

Gelato is a Web3 Cloud Platform empowering developers to create automated, gasless, and off-chain-aware Layer 2 chains and smart contracts.

Over 400 web3 projects rely on Gelato to facilitate millions of transactions across DeFi, NFTs, and gaming.


  • Gelato RaaS: Deploy ZK or OP L2s with account abstraction and middleware baked in.

  • Web3 Functions: Connect contracts to off-chain data via decentralized cloud functions.

  • Automate: Automate contract execution in a reliable, decentralized manner.

  • Relay: Provide robust, scalable gasless transactions via API.

  • Account Abstraction SDK: Built with Safe, combining gasless txs with secure wallets.

Subscribe to our newsletter and turn on Twitter notifications for the latest Gelato updates!
Interested in building the future of Web3? Explore open roles and apply here.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.