Teory of Meta transactions

I am studying the matter of meta transactions

If I have understood the concept correctly, it is about paying the gas commissions of the operations of another user. In this way, it is intended to eliminate the excessive cost of the ethereum network for some business logic.

So we have this example contract. To set an appointment we must sign the change of scope of the contract. After the inherited contract will be responsible for paying the cost of the execution, right?

So we have this example contract. To set an quote we must sign with metamask that change of scope of the contract. The inherited contract will be responsible for paying the cost of the execution, right?


//SPDX-License-Identifier: MIT
pragma solidity 0.6.6;

import "./EIP712MetaTransaction.sol";

contract TestContract is EIP712MetaTransaction("TestContract","1") {

    string public quote;
    address public owner;

    function setQuote(string memory newQuote) public {
        quote = newQuote;
        owner = msgSender();

    function getQuote() view public returns(string memory currentQuote, address currentOwner) {
        currentQuote = quote;
        currentOwner = owner;

This is the contract that would be in charge of paying the transaction

//SPDX-License-Identifier: MIT
pragma solidity 0.6.6;

import "./lib/EIP712Base.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";

contract EIP712MetaTransaction is EIP712Base {
    using SafeMath for uint256;
    bytes32 private constant META_TRANSACTION_TYPEHASH = keccak256(bytes("MetaTransaction(uint256 nonce,address from,bytes functionSignature)"));

    event MetaTransactionExecuted(address userAddress, address payable relayerAddress, bytes functionSignature);
    mapping(address => uint256) private nonces;

     * Meta transaction structure.
     * No point of including value field here as if user is doing value transfer then he has the funds to pay for gas
     * He should call the desired function directly in that case.
    struct MetaTransaction {
        uint256 nonce;
        address from;
        bytes functionSignature;

    constructor(string memory name, string memory version) public EIP712Base(name, version) {}

    function convertBytesToBytes4(bytes memory inBytes) internal returns (bytes4 outBytes4) {
        if (inBytes.length == 0) {
            return 0x0;

        assembly {
            outBytes4 := mload(add(inBytes, 32))

    function executeMetaTransaction(address userAddress,
        bytes memory functionSignature, bytes32 sigR, bytes32 sigS, uint8 sigV) public payable returns(bytes memory) {
        bytes4 destinationFunctionSig = convertBytesToBytes4(functionSignature);
        require(destinationFunctionSig != msg.sig, "functionSignature can not be of executeMetaTransaction method");
        MetaTransaction memory metaTx = MetaTransaction({
            nonce: nonces[userAddress],
            from: userAddress,
            functionSignature: functionSignature
        require(verify(userAddress, metaTx, sigR, sigS, sigV), "Signer and signature do not match");
        nonces[userAddress] = nonces[userAddress].add(1);
        // Append userAddress at the end to extract it from calling context
        (bool success, bytes memory returnData) = address(this).call(abi.encodePacked(functionSignature, userAddress));

        require(success, "Function call not successful");
        emit MetaTransactionExecuted(userAddress, msg.sender, functionSignature);
        return returnData;

    function hashMetaTransaction(MetaTransaction memory metaTx) internal pure returns (bytes32) {
        return keccak256(abi.encode(

    function getNonce(address user) external view returns(uint256 nonce) {
        nonce = nonces[user];

    function verify(address user, MetaTransaction memory metaTx, bytes32 sigR, bytes32 sigS, uint8 sigV) internal view returns (bool) {
        address signer = ecrecover(toTypedMessageHash(hashMetaTransaction(metaTx)), sigV, sigR, sigS);
        require(signer != address(0), "Invalid signature");
        return signer == user;

    function msgSender() internal view returns(address sender) {
        if(msg.sender == address(this)) {
            bytes memory array = msg.data;
            uint256 index = msg.data.length;
            assembly {
                // Load the 32 bytes word from memory with the address on the lower 20 bytes, and mask those.
                sender := and(mload(add(array, index)), 0xffffffffffffffffffffffffffffffffffffffff)
        } else {
            sender = msg.sender;
        return sender;

My question is the following if I have understood the concept correctly:

With what function do we recharge the contract so that it has enough balance to make the payments?

The correct function signature is required. =) sendTransaction(userAddress, functionSignature, r, s, v);

So, I would have to make a signed hash with the setQuote function and send it to the client for the signature to execute in executeMetaTransaction?