As you can see, 0.007% of the tokens are burned, and I want 0.003% to be sent to everyone who holds the token. I've put a '?' where I want the holder addresses to go. How do I go about giving out the tokensToDividend value equally among all holders?

2 Answers
2

Actually, you should not send part of a transaction to all token holders. That is possibly infinitely expensive to do (because your list of token holders can grow indefinetely, I assume). There's no way of doing exactly that in the EVM.

Just to clarify, imagine you have ten thousand token holders and someone just wants to send 1 token to a friend. How much gas will that person pay to make that 1 token transfer? She will have to pay for 9,999 transfers! Most probably, the transaction will just fail because it will run out of gas.

Note that that will hold even if you are just emitting events, instead of actually using .transfer

You should change your approach in order to meet your objective. You'll need to work with a dividends proportion, so that you can calculate how much a given user is entitled to when that user wants to withdraw dividends, or consult how much she owns.

You'll also need to track how much a user has withdrawn before.

The full implementation is far too complex to put here, but you can follow these three links for a more in-depth explanation and to see some examples:

If you have stored the list of all the account holders in an array, you can iterate over this array and transfer them dividend and emit the event as well.

But better instead of transferring in a loop, you can create a dividend mapping and update the dividends of each person when needed and emit an event. The dividend eligible people can then withdraw this dividend when they want to.