0

If i have a list of addresses and the exact amount of tokens that I want sent to each address, how can I hardcode a transfer function to transfer the exact amount of tokens to all specific addresses if called?

2

In my opinion, the best way is to make a function that takes an array of addresses as a parameter:

Solidity function:

function foo(address[] add, int value) public {

    for (int i = 0; i <= add.length; i++){
        add[i].transfer(value);
    }
}

If you are working with web3 (for example), you can just pass a javascript array with all your addresses as strings inside.


If the amount is specific for all addresses:

  function foo(address[] add, int[] values) public {

        for (int i = 0; i <= add.length; i++){
            add[i].transfer(values[i]);
        }
    }

Be sure that the indexes match correctly.


Update to answer the comments:

If you want to retrieve addresses and balances of tokens from old contract, you can do it ONLY if you have implemented the functionnality to get all addresses and their balances in the old contract, something like:

OLD CONTRACT:

address[] listOfAddresses;
int[] balancesOfAddresses;

    function getAddresses() public returns (address[], int[]){

        return (listOfAddresses, balanceOfAddresses);
    }

Or any other way to get this information.

NEW CONTRACT:

address[] listOfAddresses;
int[] balanceOfAddresses;
function(address[] addresses, int[] balances) public {
    listOfAddresses.push(addresses);
    balanceOfAddresses.push(balances);
}

Using this, you could get the information from one contract and push them to an other.

Remark: You could use mapping as well, but I'm not very familiar with them. Remark: If you first contract do not have the capability to retrieve the data, I'm afraid you are stuck. Or maybe if you go through the chain, you could retrieve transactions, but I cannot help on that.

| improve this answer | |
  • Is it possible to snapshot the 'balances' mapping from an old contract ? and give them free new tokens based on the amount in the mapping – NowsyMe Nov 12 '17 at 7:37
  • My first thougth would be, if you have a function that returns all the balances, to call it and use the result to send new free tokens via your new contract. – Florian Castelain Nov 12 '17 at 7:40
  • but then it would be the same amount of tokens to all right – NowsyMe Nov 12 '17 at 8:59
  • Not if your function from old contract sends you 2 arrays: One with addresses, an other with balances, and the indexes would match. If you do have that, you can just loop trhough to send tokens to addresses one by one. – Florian Castelain Nov 12 '17 at 9:02
  • Thanks for answering! so I would have to, have those implemented in the old contract to be able do this right, like a look up table array – NowsyMe Nov 12 '17 at 10:45

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.