So the ideia is like this:

contract MyContract {
    uint mydata;

    function setData1(uint _data) public {
        mydata = _data; //normal way (msg.sender pays to store _data in mydata (msg.sender needs to have eth))

    function setData2(uint _data) public {
        //here i want that MyContract address ("address(this)") pay and store _data in mydata

    function setData3(uint _data) public {
        mydata = _data;
        //here i want that MyContract pay the transaction from msg.sender

I know that setData3() is possible with gas stations, meta transactions and other approach but these approaches is other addresses to pay for transaction.

I want that Contract pay the fee. Is it possible?

In setData2() is it possible for the contract to save the variable and pay for it in some way?

In this scenario, I do not need to save the transaction, just to save the variable in some way.


1 Answer 1


The closest you can do is sending some ether back to the sender of the transaction in order to compensate him/her for the gas the he/she has paid for that transaction.

You can do this by executing msg.transfer(<someValue>) inside the desired function.

Nevertheless, as you can understand from the description above, the sender must have some ether in order to initiate the transaction to begin with.

On top of that, I'm not really sure how you could calculate precisely how much the sender has paid for the transaction, in order to compensate him/her respectively.

You'll probably need to rely on the following in order to do that:

  • tx.gasprice
  • msg.gas if you're on solc 0.4.x, or gasleft() if you're on solc 0.5.x

That's not enough of course, as you'll need the total amount of gas that the transaction was sent with...

  • Tks, but i can't have funds in sender address.
    – john
    Mar 23, 2020 at 15:05
  • @john: 1. Why not? 2. It's not you who need to have these funds, it's the account which initiates the transaction (i.e., any one of your users, I suppose). 3. An account without funds cannot execute a transaction - this is a basic fact in Ethereum that you cannot change. Mar 23, 2020 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.