0

Every block x amount of ether is put into the smart contract - Where x is variable.

And a user with a token balance of y is entitled to y / total token supply of the total dividends in the smart contract.

What is the best way to implement such a thing?

1

There is no single “best” way to do this, while there are several common ways. Here is one of them:

contract DividendToken {
    uint private totalDividendsPerToken = 0;
    mapping (address => uint) private dividendsPerTokenPaid;

    function distributeDividends () public payable {
        require (msg.value % totalSupply () == 0);
        totalDividendsPerToken += msg.value / totalSupply ();
    }

    function payOutDividends (address payable who) internal {
        uint amount =
            balanceOf (who) *
            (totalDividendsPerToken - dividendsPerTokenPaid [who]);
        dividendsPerTokenPaid [who] = totalDividendsPerToken;
        who.transfer (amount);
    }

    ...
}

Function payOutDividends should be called for an address prior to any change of token balance for this address, i.e. in functions such as transfer, transferFrom, mint, burn etc. like this:

function transfer (address to, uint value) public returns (bool) {
    payOutDividends (msg.sender);
    payOutDividends (to);

    ...
}
| improve this answer | |

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.