0

say i have a function that give away dividend based on how many shares that customer owns:

// Give away dividend based on how many balances of each customer has
function giveAwayDividend(uint _value) onlyOwner {
for(uint i=0;i<size();i++){
    //balances[customerAddress[i]] = balances[customerAddress[i]].add(balances[customerAddress[i]]*_value/totalSupply);
    //Transfer(msg.sender, customerAddress[i], (balances[customerAddress[i]] * _value / totalSupply) );
    customerAddress[i].send((balances[customerAddress[i]] * _value) / totalSupply);
   }
}

the problem is, how should I be able to send ether in decimals? If customer A has 100coins, total supply is 50000 coins, dividend total amount is 10 ether, he should get 100/50000 * 10 = 0.02 ether, but here I think (balances[customerAddress[i]] * _value) / totalSupply will give me 0?

How should I fix it?

0

Where exactly is your problem in implementing 100/50000 * 10 = 0.02ETH ? Balances are handled in Wei bei default, 1 Wei * 10^18 = 1ETH, so 0.02ETH == 20000000000000000 Wei, use this Unit Converter.

I see a more fundamental problem in this: If customerAddress[i] gets long, calling giveAwayDividend will fail every time, running out of gas.

4
  • Oops, I just looked at Wei and you are right, there is no problem with my question. As for second question, how long exactly will that make my function fail? Because I only call it quarterly, it is fine to pay some money for that function call four times a year, if only each time it will successfully run – dome some Sep 26 '17 at 15:52
  • Glad I could help. Time is no issue here, it's the amount of payments. At a certain amount of addresses you pay out, the gas requirements will be too high. – Nikita Fuchs Sep 26 '17 at 15:54
  • 1
    Should this " 100/50000 * 10 " be "(10 * 100) / 50000" because of no floating point? I guess in this case, since the result of 100/50000 is an integer, it doesn't matter, but just curious. – Thomas Jay Rush Sep 26 '17 at 23:44
  • Always avoid floting point situations, and write numbers as a multiplication with a factor which has an exponential, that way you avoid errors. e.g. if calculating ETH with wei, write 1 * 10^18 instead of 1000000000000000000. – Nikita Fuchs Sep 29 '17 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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