0

The question is, how can I implement a feature in the smart contract that can keep track of all stakeholders. The reason I ask is that I would like the contract to pay out dividends. So far, the only way I can think of is by storing the addresses of each stakeholder in a dynamic array. The only thing that is worrying me, and I have been struggling to find an answer to this question is: what is the maximum size or capacity of a dynamic array in Solidity? Also, am I wasting my time with this approach? Is there a better way to solve this problem?

1

One other way of storing addresses, in this case, would be maintaining a count of address and save addresses in a mapping. Every time when you add a new address in the mapping, increase the count as well.

mapping (unit => address) AddressStorage;

And whenever you want to send tokens to these address just iterate up to the count attribute and access address like this -

AddressStorage[count]

This is not an optimized solution but certainly another way of storing rather than storing in the dynamic array.

Hope this helps.

  • 1
    would you mind accepting my answer and giving me an upvote if this was helpful? – Sanchit Aug 3 '17 at 19:36
0

Keeping track of stakeholders

One approach would be to leverage a dApp to scan the blocks in the blockchain for the Transfer events your token generated. You could then accumulate a list of addresses that have token balances, and then you have a list of token holders. It's not executed in the network and wouldn't cost you any gas to execute.

If you wanted to do this internal to the contract it will be very costly in memory. So the dApp is probably the way to go.

Paying Dividends

If the intent is that you will pay dividends in the form of the tokens issued, you could reverse the logic and require that stakeholders "collect" their dividends by issuing a transaction to a method you expose.

You could implement this in a way that you keep a mapping of owner => mapping of balances. In the second mapping you could bucket the amounts that have/have not recieved dividends. Also using an incremental identifier for each time you award a dividend, then they could collect the past dividends at the same time.

If you use this method, then you could guard any transaction based method for that owner and could even automatically update the account holders dividend payout at that time.

Here is an example that implements the basics of this approach:

pragma solidity ^0.4.0;

contract DividendToken {
    address contractOwner = msg.sender;

    uint[] dividendsAwarded;

    mapping(address => mapping(uint => uint)) balances;

    function awardDividends(uint _percent) {
        require(msg.sender==contractOwner);
        dividendsAwarded.push(_percent);
    }

    function balanceOf(address _stakeHolder) constant returns(uint balance) {
        mapping(uint => uint) stakeHolderBalances = balances[_stakeHolder];

        uint balanceToApplyDividend;
        uint dividendAward;
        for(uint i=0;i<dividendsAwarded.length;i++) {
            balanceToApplyDividend += stakeHolderBalances[i];
            dividendAward = (balanceToApplyDividend * dividendsAwarded[i]) / 100;
            balance += balanceToApplyDividend + dividendAward;
        }
        balance += stakeHolderBalances[dividendsAwarded.length];
    }

    function transfer(address _to, uint _amount) returns(bool success) {
        collectDividends(msg.sender);

        balances[msg.sender][dividendsAwarded.length] -= _amount;
        balances[_to][dividendsAwarded.length] += _amount;
        return true;
    }

    function collectDividends(address _stakeHolder) {
        mapping(uint => uint) stakeHolderBalances = balances[_stakeHolder];

        uint newBalance = balanceOf(_stakeHolder);

        for(uint i=0;i<dividendsAwarded.length;i++) {
            delete stakeHolderBalances[i];
        }
        stakeHolderBalances[dividendsAwarded.length] = newBalance;
    }

    function giveTokens(uint _amount) {
        balances[msg.sender][dividendsAwarded.length] = _amount;
    }
}

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.