The token standard doesn't require you to store balances in any particular structure - a public mapping is not part of the standard - so you can simply do this:
struct Account {
uint balance;
uint dividends;
}
mapping(address=>Account) accounts;
Assuming you give out dividends proportional to a user's token balances, though, updating the dividends is still going to be a lot of work. Fortunately, there's a better way:
uint totalSupply;
uint totalDividends;
struct Account {
uint balance;
uint lastTotalDividends;
}
mapping(address=>Account) accounts;
Now, when you want to give out dividends, increase totalDividends
by the amount of the dividend. When a user wants to withdraw their portion of any dividends, they calculate (accounts[user].balance * (totalDividends - accounts[user].lastTotalDividends)) / totalSupply
- that is, take the amount of dividend disbursed since they last checked, and calculate the fraction of it they should receive based on the percentage of the tokens they own. Then, update lastTotalDividends
to the current value of totalDividends
.
Note that if you're doing this, you need to prevent users from sending or receiving tokens until they've withdrawn their dividends, or else users could 'double dip' by transferring tokens between accounts. You could avoid this by adding another field to Account
that tracks the dividends they're entitled to but haven't yet withdrawn, and updating that before a transfer out of or into an account.
Best Answer
You just need to know the address of the ERC20 token and execute function
totalSupply()
.If you want to have that in a simple contract, you could have something like this:
You certainly need to know the address of the ERC20 tokens you may want to check, so you have to know that beforehand and if necessary, store it in your contract within a list.