Ethereum Stack Exchange is a question and answer site for users of Ethereum, the crypto value and blockchain-based consensus network. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible for a contract to pay the gas costs (or part of it) that result from the contract being called? Or does the sender of a message always pay the resulting gas costs no matter what?

share|improve this question

At present the user initiating the transaction must pay the fee. However Serenity is likely to enable 'contract pays' schemes. (See Vitalik's blog post here) A possible work around is a contract which refunds the sender however this would still require the user to hold some Ether and might allow malicious parties to repeatedly call the contract to deplete its funds.

share|improve this answer
    
There are better workarounds then that. I'll try and post an answer when I get a chance. – Jeff Coleman Jan 21 at 3:41
    
Looking forward to your workarounds – Randomblue Feb 8 at 22:35
1  
Still haven't had time to do this, so in lieu of a full answer I'll just note that you can use a mechanism similar to the Ethereum Alarm Clock to make it so that applications can submit their transactions to random individuals who then "lend" them the ether cost of the contract and are reimbursed for it by a special contract funded by the destination contract's developer. The result is an app which does not require users to hold any ether. Obviously one would want to add some sort of anti-sybil element to the app to prevent DoS. One simple way would be a small PoW in the app. – Jeff Coleman Feb 28 at 17:10

No, a sender with zero ether cannot "ask" a contract to pay for the gas costs. A sender with zero ether cannot even send a transaction.

More details: The sender of a transaction must have enough gas to cover execution of the transaction. That gas is required even before a contract can even be called. Once the gas requirement is met (so that the transaction runs fully without running out of gas), the called contract can send the sender whatever amount of funds the contract holds: the net behavior is that the sender can end up with more ether than they started with, but that is different from the contract paying the gas.

It's like you can drive to the bank to get money, but you need fuel first to be able to drive to the bank: the bank can't send you fuel money before you drive to the bank.

There is ongoing discussion here for a future release (Serenity) that can change this behavior.

share|improve this answer

Not all calls on a contract need gas. It is the so called "dry-run". https://github.com/ethereum/go-ethereum/wiki/Contracts-and-Transactions#interacting-with-contracts

Now all the function calls specified in the abi are made available on the contract instance. You can just call those methods on the contract instance and chain sendTransaction(3, {from: address}) or call(3) to it. The difference between the two is that call performs a "dry run" locally, on your computer, while sendTransaction would actually submit your transaction for inclusion in the block chain and the results of its execution will eventually become part of the global consensus. In other words, use call, if you are interested only in the return value and use sendTransaction if you only care about "side effects" on the state of the contract.

So it is possible to provide a method in a contract to users for zero gas costs - in the example above it is the multiply function.

share|improve this answer
    
You're right there is dry-run and the question used "sender of a message" instead of "sender of a transaction" which would be more precise. – eth Jan 26 at 14:18

Not at present but is currently being discussed with an EIP. Gavin Wood says that there is a way to do it presently but from my understanding it's something of a hack.

share|improve this answer

The caller of a contract function will need some gas, but you could minimize the gas needed by having the contract immediately call an external function to do the work:

In Solidity:

contract Gracious {
  function runMe() {
    this.realWork.gas(1000000)();
  }
}

Here the contract is supplying gas from its own reserves to run the realWork function, rather than relying on the client calling it to do so.

share|improve this answer
    
This is brilliant. – 5chdn Mar 30 at 14:38
    
I have not found a away of taking gas from a contract and this example will only restrict the gas to 1000000 when calling realWork. contract Gracious { function runMe() { this.realWork.gas(1000000)(); } } This operation doesn't take the gas from the contract. – smalltalk May 5 at 9:52
    
Does not work. See smalltalk's answer below. – Raine May 6 at 15:37
    
@Raine: Hmmm, I'll have to test that one further, then. If the "call an external function" interface doesn't compile to use internal funds if insufficient gas was provided, I wonder if using the address.send() method or address.call("realWork") might... – MidnightLightning May 6 at 16:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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