HQ20 / contracts

A set of reusable smart-contracts
https://hq20-contracts.netlify.com/
Apache License 2.0
466 stars 107 forks source link

Fix ERC20DividendableEth for transfers. #215

Open alcueca opened 4 years ago

alcueca commented 4 years ago

Fix the issues with the supply first, please.

A mapping(address => uint) public dividendsPerTokenAdjustment is required to keep track of imbalances when transferring tokens with owning dividends.

In a transfer: transferWeight = trasnferAmount / recipientBalance (Fixed point) dividendsPerTokenDifferential = lastDividendsPerToken[sender]-lastDividendsPerToken[receiver] dividendsPerTokenAdjustment[account] = dividendsPerTokenAdjustment[account] + dividendsPerTokenDifferential * transferWeight

When calculating dividends owed: owedDividendsPerToken = dividendsPerToken - lastDividendsPerToken[account] + dividendsPerTokenAdjustment[account]

When claiming dividends: dividendsPerTokenAdjustment[account] = 0

alcueca commented 4 years ago

You can use these two test cases:

Test case 1: Transfers of dividend carrying tokens also transfer the dividends. Supply: 100 tokens, User 1 has 40, User 2 has 60. Distribute 10 ether. User 2 claims dividends. He's got 6 ether now. User 1 transfers 6 tokens to user 2. User 2 claims dividends again. He's got 6.6 ether now.

Test case 2: Transfers of non dividend carrying tokens don't transfer any dividends. Supply: 100 tokens, User 1 has 40, User 2 has 60. Distribute 10 ether. User 1 claims dividends. User 1 transfers 6 tokens to user 2. User 2 claims dividends again. He's got 6 ether now.