Ethereum Meta-Transactions 101
Tags: ethereum, smartcontractblockch, metatransactions, tutorial
Published: 2021-01-12 19:14:06

---
title: Ethereum Meta-Transactions 101
published: true
date: 2021-01-07 08:04:29 UTC
tags: ethereum,smartcontractblockch,metatransactions,tutorial
canonical_url: https://medium.com/coinmonks/ethereum-meta-transactions-101-de7f91884a06
---

![](https://cdn-images-1.medium.com/max/1024/1*A6pvhAE_8DRy1vLFQSYW5Q.png)

In the Ethereum world things happen thanks to transactions. These transactions, just like everything in life, don’t come for free. In order to create and execute a transaction you need to pay a fee in the form of “Gas” which costs [ETH](https://coinmarketcap.com/currencies/ethereum/).

But what if you want to create a transaction and you have no money to pay for gas… this is where Meta-Transactions come to the rescue!

> Meta Transactions are transactions who’s data is created and signed off-chain by one person and executed by another person who pays the gas fees.

Since meta transactions are not native to the system, you would need to either use a 3rd party setup (e.g. [GSN](https://opengsn.org/)), or create your own.

### Setup

A very basic setup would consists of 3 entities:

- **Signer —**  the address who has with 0 ether and is the creator of the transaction. This address is the one which signs the transaction data.
- **Sender—** the address which has ether and is willing to send the transaction on your behalf, thus paying the gas.
- **Relay/Proxy — ** a smart contract which would read the signed data, validate it against the provided signature and forward the call to the actual contract.

![](https://cdn-images-1.medium.com/max/362/1*NMR9bzm5A9qKfLzZ8i_44g.png)

_Note that in this case the transaction sender would be the Proxy contract (e.g. the msg.sender). In the case of an ERC-20 transfer, the Signer needs to approve the Proxy contract to transfer tokens on it’s behalf (unless the ERC-20 contract supports meta transactions already. See _[_EIP-1776_](https://github.com/ethereum/EIPs/issues/1776)

Let’s look into a simple use case…

### Use Case: Sending Tokens

_(For the sake of simplifying the example consider that any approval of ERC-20 token transfers has been done in advance)_

Imagine you have an ethereum address with 0 ether in it and a friendly blockchain buddy sends you 1000 [DAI](https://coinmarketcap.com/currencies/multi-collateral-dai/). You are unable to make use of them since you don’t have enough money to execute any transactions.

What you could do instead is make use of meta-transactions.

#### Step #1: Generate Meta Transaction

A meta transaction is basically a message with some data that has been signed by whoever wants to execute a transaction. That signed data is then verified and sent in a normal ethereum transaction by another party (the one paying the Gas fees).

Let’s consider that we want to transfer those tokens to another account which means that we need to create a transaction with the following data:

- **Receiver** : 0x00007e87416D7328fC74663f37e0DF53777188fb
- **Amount** : 1000000000000000000000 (1000 with 18 decimals)

Using web3js we can easily build the calldata for such a function call:

transferFrom(senderAddress, receiverAddress, 1000);

{% gist https://gist.github.com/valdikamenarov/88c01859a11dd3bb0e49972e3946e668 %}

which is translated into the following bytecode:

```
0xa9059cbb
0000000000000000000000000000ef76331b59b1cc5e82ba1d2f840dbac1c73e
00000000000000000000000000007e87416d7328fc74663f37e0df53777188fb
00000000000000000000000000000000000000000000003635c9adc5dea00000
```

#### Step #2: Sign the data

Now that you have the created the calldata of the action you want to perform, it’s time to sign it so that the Proxy contract can verify that this calldata has not been tampered along the way.

In addition to this data, we also want to ensure that the correct contract is being called so let’s add the target contract address to the data we want to sign. The easiest way to do that is to create a hash of all the data you want to sign, and sign the hash instead of the raw data.

```
// pack the DAI Token address and our "transferFrom()" calldata together and sign them.

let rawData = web3.eth.abi.encodeParameters(
  ['address','bytes'],
  [DaiTokenAddress,data]
);

// hash the data.
let hash = web3.utils.soliditySha3(rawData);

// sign the hash.
let signature = web3.eth.sign(hash, signer);
```

#### Step #3: Hand over the data to the Gas payer

Once you have created and signed the data you hand it over to the service/address that will be executing the transaction on the chain.

Depending on how the Proxy contract is structured the Gas Payer might need differently structured data (and your signed data might need to be different as well). But for the sake of simplicity let’s consider that we have the following basic proxy contract functionality (NOTE! Not production safe):

{% gist https://gist.github.com/valdikamenarov/3886bb2e94df9960345e79208be7931f %}

In order to successfully execute the transaction, the gas payer needs to call our Proxy contract’s forward() function with the values supplied by the  **Signer**.

- **\_to:** DAI Token contract address (this is our target contract).
- **\_data:** the generated calldata from the signer. (the one with the transferFrom(...) which we did).
- **\_signature:** the resulting signature from signing the data.

This would then make our Proxy contract call the DAI Token address and execute the transferFrom() function transferring 1000 DAI.

#### Things to consider

There are a lot of security improvements that could/should be added (such as adding a nonce in the signed message which would protect against replay attacks).

But the goal of this example was to show you how **Person A** can sign a message off-chain without paying gas and then be executed by **Person B**. From here on there are limitless improvements and changed that could be done to fit your needs.

> Join Coinmonks [Telegram group](https://t.me/joinchat/EPmjKpNYwRMsBI4p) and learn about crypto trading and investing

#### Also, Read

- [Learn Ethereum and Web3 development](http://blog.coincodecap.com/go/learn)
- The Best [Crypto Trading Bot](https://medium.com/coinmonks/crypto-trading-bot-c2ffce8acb2a)
- [3Commas Review](https://medium.com/coinmonks/3commas-review-an-excellent-crypto-trading-bot-2020-1313a58bec92)
- [Pionex Review](https://medium.com/coinmonks/pionex-review-exchange-with-crypto-trading-bot-1e459d0191ea)
- [AAX Exchange Review](https://medium.com/coinmonks/aax-exchange-review-2021-67c5ea09330c) | Referral Code, Trading Fee, Pros and Cons
- [Deribit Review](https://medium.com/coinmonks/deribit-review-options-fees-apis-and-testnet-2ca16c4bbdb2) | Options, Fees, APIs and Testnet
- [FTX Crypto Exchange Review](https://medium.com/coinmonks/ftx-crypto-exchange-review-53664ac1198f)
- [NGRAVE ZERO review](https://medium.com/coinmonks/ngrave-zero-review-c465cf8307fc)
- [Bybit Exchange Review](https://medium.com/coinmonks/bybit-exchange-review-dbd570019b71)
- [3Commas vs Cryptohopper](https://medium.com/coinmonks/3commas-vs-pionex-vs-cryptohopper-best-crypto-bot-6a98d2baa203)
- The Best Bitcoin [Hardware wallet](https://medium.com/coinmonks/the-best-cryptocurrency-hardware-wallets-of-2020-e28b1c124069?source=friends_link&sk=324dd9ff8556ab578d71e7ad7658ad7c)
- [Crypto Copy Trading Platforms](https://medium.com/coinmonks/top-10-crypto-copy-trading-platforms-for-beginners-d0c37c7d698c)
- Best [monero wallet](https://blog.coincodecap.com/best-monero-wallets)
- [ledger nano s vs x](https://blog.coincodecap.com/ledger-nano-s-vs-x)
- [Bitsgap vs 3Commas vs Quadency](https://blog.coincodecap.com/bitsgap-3commas-quadency)
- The Best [Crypto Tax Software](https://medium.com/coinmonks/best-crypto-tax-tool-for-my-money-72d4b430816b)
- [Best Crypto Trading Platforms](https://medium.com/coinmonks/the-best-crypto-trading-platforms-in-2020-the-definitive-guide-updated-c72f8b874555)
- Best [Crypto Lending Platforms](https://medium.com/coinmonks/top-5-crypto-lending-platforms-in-2020-that-you-need-to-know-a1b675cec3fa)
- [Ledger Nano S vs Trezor one vs Trezor T vs Ledger Nano X](https://blog.coincodecap.com/ledger-nano-s-vs-trezor-one-ledger-nano-x-trezor-t)
- [BlockFi vs Celsius](https://medium.com/coinmonks/blockfi-vs-celsius-vs-hodlnaut-8a1cc8c26630) vs Hodlnaut
- [Bitsgap review](https://medium.com/coinmonks/bitsgap-review-a-crypto-trading-bot-that-makes-easy-money-a5d88a336df2) — A Crypto Trading Bot That Makes Easy Money
- [Quadency Review](https://medium.com/coinmonks/quadency-review-a-crypto-trading-automation-platform-3068eaa374e1)- A Crypto Trading Bot Made For Professionals
- [CoinTracking Review](https://medium.com/coinmonks/cointracking-review-a-reliable-cryptocurrency-tax-software-5114e3eb5737)
- [YouHodler Review](https://medium.com/coinmonks/youhodler-4-easy-ways-to-make-money-98969b9689f2)
- [Ellipal Titan Review](https://medium.com/coinmonks/ellipal-titan-review-85e9071dd029)
- [SecuX Stone Review](https://blog.coincodecap.com/secux-stone-hardware-wallet-review)
- [BlockFi Review](https://medium.com/coinmonks/blockfi-review-53096053c097) | Earn up to 8.6% interests on your Crypto
- [Coinrule review](https://blog.coincodecap.com/coinrule-review-a-perfect-trading-bot)
- [Best Blockchain Analysis Tools](https://bitquery.io/blog/best-blockchain-analysis-tools-and-software)
- [Crypto arbitrage](https://medium.com/coinmonks/crypto-arbitrage-guide-how-to-make-money-as-a-beginner-62bfe5c868f6) guide: How to make money as a beginner
- Best [Crypto Charting Tool](https://medium.com/coinmonks/what-are-the-best-charting-platforms-for-cryptocurrency-trading-85aade584d80)
- What are the [best books to learn about Bitcoin](https://medium.com/coinmonks/what-are-the-best-books-to-learn-bitcoin-409aeb9aff4b)?

> [Get Best Software Deals Directly In Your Inbox](https://medium.com/coinmonks/newsletters/coinmonks)

[![](https://cdn-images-1.medium.com/max/584/1*BoDnKUyK8p4hitHAJZ5Pdw.png)](https://medium.com/coinmonks/newsletters/coinmonks)

* * *