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.
ERC20 is a standardized software interface for tokens. A software interface is a standard that allows two different pieces of software to interact. For example, USB mice have a common interface so you can plug any mouse supporting the USB mouse standard into a computer running an OS that knows how to support USB mice. Likewise, wallets that support ERC20 will know how to deal with any ERC20-compliant token. In fact, this interoperability is the whole point of creating the ERC20 standard! For any ERC20 token-supporting wallet, I believe you need to tell the software to watch the token's contract address, but that's about it, unless the wallet specifically only hard-codes in an approved list of tokens.
However, an exchange can choose not to list your token (or, more precisely, it choses tokens that it wants to list), but that has nothing to do with wallet support in the case of ERC20.
If you created a non-ERC20-compliant token on the Ethereum blockchain, you'd have to create your own wallet if you wanted users to have a nice user interface to interact with your tokens. If you created a new cryptocurrency (like when Litecoin forked from Bitcoin), you'd have to create a new wallet so that people could sync your blockchain.
Best Answer
As you can see in the token tutorial, at the end of the
transfer
method the following code is executed:This emits an Event.
You can 'listen' for emitted Events using web3.js for example.