Dividend-bearing tokens on Ethereum

struct Account {
uint balance;
}
Account[] accounts;
uint totalSupply;
function disburse(uint amount) {
deduct(msg.sender, amount);
for(var i = 0; i < accounts.length; i++) {
accounts[i].balance += (amount * accounts[i].balance) / totalSupply;
}
}

A better way

struct Account {
uint balance;
uint lastDividends;
}
mapping(address=>Account) accounts;
uint totalSupply;
uint totalDividends;
function dividendsOwing(address account) internal returns(uint) {
var newDividends = totalDividends - accounts[account].lastDividends;
return (accounts[account].balance * newDividends) / totalSupply;
}
modifier updateAccount(address account) {
var owing = dividendsOwing(account);
if(owing > 0) {
accounts[account].balance += owing;
accounts[account].lastDividends = totalDividends;
}
_;
}
function disburse(uint amount) {
totalDividends += amount;
deduct(msg.sender, amount);
}

Rounding Errors

Changing the totalSupply

(D_1 * B) / S_1 + ... + (D_n * B) / S_n
((D_1 + ... + D_n) * B) / S
(D_1 / S_1 + ... + D_n / S_n) * B
(D_1 * X / S_1 + ... + D_n * X / S_n) * B / X
const uint pointMultiplier = 10e18;struct Account {
uint balance;
uint lastDividendPoints;
}
mapping(address=>Account) accounts;
uint totalSupply;
uint totalDividendPoints;
uint unclaimedDividends;
function dividendsOwing(address account) internal returns(uint) {
var newDividendPoints = totalDividendPoints - accounts[account].lastDividendPoints;
return (accounts[account].balance * newDividendPoints) / pointMultiplier;
}
modifier updateAccount(address account) {
var owing = dividendsOwing(account);
if(owing > 0) {
unclaimedDividends -= owing;
accounts[account].balance += owing;
accounts[account].lastDividendPoints = totalDividendPoints;
}
_;
}
function disburse(uint amount) {
totalDividendPoints += (amount * pointsMultiplier / totalSupply);
totalSupply += amount;
unclaimedDividends += amount;
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Michael Jordan is Going Crypto. Here’s How #23 is Planning for a “New Era of Digital Culture”

Step by step hassle-free guide to buy GAC Tokens securely

Should You Have Multiple Ethereum Addresses? Pros, Cons, and Safety Tips

WSG — KingDefi Partnership

Porting Metamask to a new blockchain

Tips to choose a potential project in crypto

The FNDZ Token Utility Model

TRTT REPORT — 21 to 29 October 2018

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nick Johnson

Nick Johnson

More from Medium

Transfer ERC20 Token with and without a wallet.

Smart Contracts: What they are, what they do, and why they are needed

How Layer 2 Solutions Solve The Main Blockchain Problem

Another wallet for your next Dapp