My understanding is that msg.sender
pays the gas fee. Can a smart contract be written in such a way that it's always the contract owner who pay the fees?
As an example, in an ERC20 token when someone transfers the tokens, he pays the gas fee but what if the gas fee for every transfer
has to be paid by smart contract owner?
Best Answer
There are 2 options with their pros and cons:
Use signatures
signature
parameter.Refund used gas at the end of the transaction. A modifier
refundGasCost
can be used for this (see below).Below are more details for each option:
Using signatures
Here is a simple
ReceiverPays
contract that allows making the receiver of the payment pay for gas:More details can be found in this article https://programtheblockchain.com/posts/2018/02/17/signing-and-verifying-messages-in-ethereum/
The limitation of this approach is that if your smart contract needs to interact with other contracst, then they must also implement the same pattern with adding signatures to every method.
Refunding used gas to the transaction sender
It's not an ideal solution but you can refund gas cost to the transaction sender. You can do this with a modifier:
Refunding in this way implies some overhead: at least 9700 gas has to be payed extra for the
transfer
function call insiderefundGasCost
modifier. Also gas for other opcodes inrefundGasCost
should be added tousedGas
.Also the above code is potentially vulnerable to reentrancy and other attacks. I provided it only as an example and didn't test it thoroughly.
https://github.com/ethereum/wiki/wiki/Design-Rationale
...