Reputation: 23
how to write a smart contract that the recipient address pays the gas fee of the transaction not from the sender address. let's say the 'A' address sends 10ETH to address 'B' if the gas fee of the transaction gas fee is 1 ETH then only 9ETH would be sent to address 'B'. 1 ETH would be refunded to Address 'A'. please, anyone have any idea, please kindly share a reference link or any article related to this. Thank you in advance.
Upvotes: 2
Views: 3031
Reputation: 782
You can use gasleft() at the beginning and at the end of the contract to see how much gas was used. Constant cost of transaction is 21k and another 21k for eth transfer which needs to be done after calculating how much you need to "refund". You can get gas cost from tx.gasPrice which needs to be multiplied by amount then you have your contract cost.
You can't take eth from destination address for a refund, so to do it you'll just substract the contract gas cost from the amount. You send that to destination. Then on the contract balance you have some eth which will be fee (approximately), so you send all balance from contract's wallet to source.
Upvotes: 1