What are Meta Transactions (ERC-2771)?
Written by Harpalsinh Jadeja
Reviewed by Logan Ross
All Ethereum transactions use gas, and the sender of each transaction must have enough Ether to pay for the gas spent. This forces new users to purchase Ether (which can be a daunting task) before they can start using a dapp. This is a major hurdle in user onboarding.
So-called “Meta transactions” made it possible to pay gas fees on behalf of users.
However, many such solutions are transitioning towards Account Abstraction (ERC-4337) infrastructure as it is a more promising and sophisticated way of gas abstraction with many more features!
What are Meta Transactions?
The idea of Meta Transactions is simple: a third party, called a Relayer, sends the transaction on behalf of the user and pays for the gas fees.
Users sign messages (preferably EIP-712 compliant) which have information about the transaction to be executed.
The signed message is passed to the Relayer who is then responsible to validate whether the Relayer will get paid or not (this could be optional more on this later), have enough funds to pay for gas fees, sign a native transaction and submit it for execution.
Let us understand Meta Transactions by taking OpenZepplin’s Relayer as an example:
What Do Meta Transaction Request Look Like?
This is how a sample Meta Transaction Message looks...
The above message mints an NFT and is to be signed by the from address 0x7099797...
Once the message is signed, the client (or user) can create a Meta Transaction request (seen below). This is sent as a POST request to the Autotask, a webhook that when invoked internally uses the Relayer private key to sign native transactions.
What is the Meta Transaction Relayer?
A Relayer is an Ethereum Account containing the funds for sponsoring the user’s gas fees. The private key of the Account is stored in a secure vault on the provider’s server.
The developer can then send funds to the Relayer, which will cover their user’s transaction fees.
Developers can spin up as many Relayers as they want, each of which need to be separately funded with ETH.
OZ Relay allows pausing the Relayer, accepting requests from Whitelisted Addresses, and Gas Price Capping. More conditional logic can be programmed into the Autotask if desired.
Keep in mind, the user-defined transaction now lives inside the data field of the Relayer’s native transaction. A Meta Transaction is this transaction inside of another transaction!
What is the MinimalForwarder in Meta Transactions?
Until now, no on-chain validation has been performed. This is where the MinimalForwarder comes in.
MinimalForwarder is an on-chain smart contract that validates the user-signed message to ensure validity and replay-protection.
struct ForwardRequest {
address from;
address to;
uint256 value;
uint256 gas;
uint256 nonce;
bytes data;
}
function verify(ForwardRequest calldata req, bytes calldata signature) public view returns (bool) {
address signer = _hashTypedDataV4(
keccak256(
abi.encode(
_TYPEHASH,
req.from,
req.to,
req.value,
req.gas,
req.nonce,
keccak256(req.data)
)
)
).recover(signature);
return _nonces[req.from] == req.nonce && signer == req.from;
}
On successful validation, MinimalForwarder executes the transaction by making the call to the request contract with appropriate calldata
function execute(ForwardRequest calldata req, bytes calldata signature)
public
payable
returns (bool, bytes memory)
{
require(verify(req, signature), "MinimalForwarder: signature does not match request");
_nonces[req.from] = req.nonce + 1;
(bool success, bytes memory returndata) = req.to.call{gas: req.gas, value: req.value}(abi.encodePacked(req.data, req.from));
// Validate that the relayer has sent enough gas for the call.
// See
if (gasleft() <= req.gas / 63) {
// We explicitly trigger invalid opcode to consume all gas and bubble-up the effects, since
// neither revert or assert consume all gas since Solidity 0.8.0
//
/// @solidity memory-safe-assembly
assembly {
invalid()
}
}
return (success, returndata);
}
This is also where the disadvantages of Meta Transactions come into play because the contract being called by the MinimalForwarder needs to allow the MinimalForwarder to call the contract on behalf of the users.
What is ERC-2771?
Finally, when the call reaches the intended target contract, it needs to be able to figure the original user (msg.sender) and calldata (msg.data) which are nested inside of the Relayer’s transaction. To standardize this process ERC2771 was proposed.
OpenZepplin provides a utility contract called ERC2771Context to make it easy for the smart contract developer to extract the intended msg.sender and msg.data out of the data sent by the MinimalForwarder.
contract YourContract is ERC2771Context {
constructor(MinimalForwarder forwarder, string memory uri_)
ERC2771Context(address(forwarder))
{}
// Depending on whether the call is made by the `MinimalForwarder` or not the `msg.sender` and `msg.data` will be inferred accordingly
}
Because each smart contract must know how to read Meta Transactions, implementing them for already-deployed contracts is difficult and can lead to the introduction of bugs!
Even though there are many other Meta Transaction Infra services, all of them require the target contract to inherit EIP-712 or EIP-2771 based contract for deducing msg.sender and msg.data.
While this standard is great, it doesn’t work retroactively. This is why ERC-4337 was created, and is replacing most of the ERC-2771 solutions out there.
Related overviews
Learn the Function of Paymaster Smart Contracts in ERC-4337
Learn How ERC-4337 Compliant Smart Contract Wallets Work
Learn How this Piece of AA Infrastructure Bundles User Operations to Unlock the Full Power of ERC-4337