6

I installed recently Solidity for Visual Studio and they have this Smart Contract sample there:

contract Payout {
address Victor;
address Jim;
address Kieren;

mapping (address => uint) ownershipDistribution; 

function Setup() {
  Victor = 0xaabb;
  Jim    = 0xccdd;
  Kieren = 0xeeff;

  ownershipDistribution[Victor] = 35;
  ownershipDistribution[Jim]  = 35;
  ownershipDistribution[Kieren] = 30;
}

function Dividend() {
  uint bal= this.balance;
  Victor.send(bal * ownershipDistribution[Victor] / 100); 
  Jim.send(bal * ownershipDistribution[Jim] / 100);
  Kieren.send(bal * ownershipDistribution[Kieren] / 100);
}
}

Basically it is supposed to send the ethers it receives to 3 addresses.

But I believe the code as it is won't work, as the last transaction won't have enough gas, right? Only the first 2 would execute.

Imagine there's 10 ETH in the contract, 3.5 Eth + fees / gas will be sent to address A, 3.5 Eth + fees / gas will be sent to address B, now we dont have the remaining needed 3 Eth for the third address...

So how can the Smart Contract know in the advance the gas/fees needed for the transactions?

6
  • Why won't the last transaction have enough gas? This looks like it would work fine. May 19, 2016 at 22:58
  • Image there 10 ETH in the contract, 3.5 Eth + fees / gas will be sent to address A, 3.5 Eth + fees / gas will be sent to address B, now we dont have the remaining needed 3 Eth for the third address... Right?
    – Resten
    May 20, 2016 at 7:24
  • Fees come from the account of the sender, not the Payout contract. Sender has their ETH balance which is different from Payout contract's ETH balance.
    – eth
    May 20, 2016 at 7:34
  • So you are saying, when the dividend method is called the ethereum protocol will automatically detect that there are 3 transactions inside of it and the transaction costs for fees and gas for those 3 transactions will be presented for the sender to pay when he does the transaction calling that method?
    – Resten
    May 20, 2016 at 7:46
  • Calls to other contracts and to send aren't separate transactions: they occur as part of the same transaction the contract is running in, and use the same gas counter, with gas supplied by the caller of the original contract. May 20, 2016 at 8:52

2 Answers 2

4

Basically, miners and contracts do not know in advance the gas/fees needed for a transaction: they have to run the code in the transaction to find out. This is why the gas specified in a transaction, is the maximum amount that the transaction is willing to consume.


To answer the intended question, fees are paid from the balance of the sender account (the account that calls Dividend), which is different from the balance of the Payout contract which is divided between Victor et al.

4
  • But how can this code work then if we dont know the fees and gas in advance? The third transaction will never execute
    – Resten
    May 20, 2016 at 7:27
  • I answered the general question which is a good one. The example asked in the question was confusing, but I understand what you're asking now and commented to the question that "Fees come from the account of the sender, not the Payout contract."
    – eth
    May 20, 2016 at 7:37
  • I updated the answer; will cleanup some comments if they're no longer needed.
    – eth
    May 20, 2016 at 7:42
  • So in other words, what I asked in my last comment on the question is what will happen?
    – Resten
    May 20, 2016 at 7:53
4

Think about gas like it's gas in your car. You don't need to know in advance exactly how much gas you're going to use; you just buy more than you think you'll need, and if you run out then the car just stops.

In this case, the sender sends a certain amount of gas with the transaction. As the code is executed, the miner keeps track of how much gas had been used so far. If the execution runs out of gas, the execution just stops, and the whole transaction is reverted as it never happened. The miner gets to keep the gas, though.

Your Answer

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

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