How hard is it to implement rewards into ERC20 Tokens? By this I mean a ERC20 Token that receives a reward in the form of Ether from a rewardAccount
that is distributed evenly between all tokens.
After some basic searching I found this question:
How to send some ether to all token holders? https://ethereum.stackexchange.com/a/5811/3660
To make sure everyone can get their ether, have each recipient call a
withdraw()
method in your contract. Within that method, do whatever update you need to record the withdrawal, then sayif (!address.call.value(x)()) throw;
. If the recipient doesn't include enough gas, everything gets rolled back and he can try again.If you do this, you no longer have to worry about finding all token holders. This also prevents the problem of having so many people in the loop that you exceed the gas limit of the entire block.
The DAO had its own withdraw()
method to allow DTHs to withdraw reward tokens:
Code from DAO.sol on Sock.it's DAO Github page
/// @notice Withdraw `_account`'s portion of the reward from `rewardAccount`
/// to `_account`'s balance
/// @return Whether the call was successful
function withdrawRewardFor(address _account) internal returns (bool _success);
function getMyReward() noEther returns (bool _success) {
return withdrawRewardFor(msg.sender);
}
Here is the withDrawRewardFor()
Method/function code which mentions the rewardAccount
http://pastebin.com/BuTZBWRU
So it seems that the two parts to get working correctly to have a ERC20 Token with Rewards is the rewardAccount
(in the DAO, this was a ManagedAccount
, which you can find on Slock.its DAO github page, ManagedAccount.sol) and the withdrawRewardFor()
method.
Is this the best way to do it? Security issues? Anyone doing something similar?