0

I've got a semi-newb question about paying for gas to make an operation happen in Solidity. Can someone tell me whether something like this would worK? And if it can be done, if someone could point me in the general direction of how one accomplishes this, that would help too. Thank you in advance. Here's what's up:

I have a contract that will take a small amount of Eth from a user to cover the gas costs for a series of small operations done in secondary contract. But I want metamask to popup & ask for $$ just one time - not every time they click a button to trigger a operation onchain. The user will periodically press a button on a webpage, but I only want to prompt them for payment when we're out of gas.

So behind the scenes, what I'm thinking is that I would have their account tied to the contract they are interfacing with, but here's the part I don't understand how to do: how can I, when they press a button again, use the $$ they've already sent to the contract (stored in an account in the contract) to cover the gas costs rather than have metamask popup again and ask for more $?

Now that I've written it out, it really IS a newb question. But that's where I'm at. Thank you for your responses.

1 Answer 1

2

A smart contract cannot initiate a transaction on its own. Any transaction on the Ethrereum blockchain needs to be initiated by an Externally Owned Account (EOA). Because of this, the gas is always paid by whoever initiates that transaction. For every smart contract call, the call data needs to be signed by Metamask. This means the Metamask popup will open up each time a contract call is made. I understand the UX you are looking for, so here is a suggestion.

Meta transactions

To put it simply, users sign the contract call data and send it to a relayer. The relayer then sends the transaction and pay for the gas. The signed data will contain signed instructions on what's to be done on the transaction. But this won't be possible with every smart contract. The smart contract that's being interacted with will need to be able to read the signed data and interpret the action. For example, msg.sender cannot be used as the msg.sender would be the relayer and not the user. You can learn more about Meta Transactions here.

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.