7

If a contract has a list of 100 addresses stored, and the code loops through the list and tries to send each address '0' ether. Does that generate 'gas' for each send call?

The reason I'm asking is because I saw some code that did this (loop through addresses and send '0' ether), and I was wondering if that could get more and more expensive the more addresses get added to the list. Thanks.

1
  • 1
    Please post the code you seen with link to source
    – niksmac
    Commented Apr 23, 2016 at 3:24

3 Answers 3

5

If you are performing these operation on the blockchain then yes. A contract = on the blockchain = the miners have to mine it and therefore you have to pay for those computations.

For this reason loops without limits are dangerous and should not be used, as you could accidentally have an endless loop and therefore an infinite amount of gas. Luckily, there is a global gas limit so your contract will fail (but the funds in the contract may be stuck, depending on the circumstances).

FYI, a standard transaction (one with no data) currently costs 21000 gas. If you just want to send a short message in the data, it would be maybe 22000 gas. But as the amount of data you send and the items in the contract increase, so do the transaction fees.

Regardless, unless you have a super crazy contract, these fees will be less that what traditional banking changes for a purchase / transaction ($0.10 + 3% on average). If you have said crazy contract, you should think about the design of the contract and potentially break it into pieces or change some computations to not be on the chain.

5

Yes

Every action that writes data to the blockchain costs gas.

2
  • 2
    So a loop across addresses with zero ether sent will get more and more expensive for no reason? Commented Apr 23, 2016 at 3:10
  • Every operation that you do costs gas, because it uses other people's mining power. It doesn't really matter if the operation is useful or not Commented Apr 23, 2016 at 15:06
4

Yes

Sending zero ether costs at least 40 gas. Sending ether is done with using the EVM's CALL opcode with a nonzero value (there's no separate opcode for sending ether).

Source

CALL has a multi-part gas cost:
* 40 base
* 9000 additional if the value is nonzero
* 25000 additional if the destination account does not yet exist (note: there is a difference between zero-balance and nonexistent!)

For information on the 25000 gas scenario, see Why were empty accounts allowed to be on the blockchain?


Also repeating @Tayvano that unbounded loops are dangerous. (Even if send(0) itself didn't cost gas, the loop would still use gas to increment its index/counter.)

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.