1

I am trying to figure out how, after adding addresses to the addressArray Array via the addAddressandShareAmount function, can I loop through each address and send each address 1 ether via the payShareHolders function? Thanks.

pragma solidity ^0.6.0;

contract MyWallet {
    
    address[] addressArray;
    uint[] shareAmountArray;
    
    function receive() external payable{
    }
    
    function addAddressandShareAmount(address payable adrs, uint numberOfShares) public {
        require(msg.sender == 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4);
        addressArray.push(adrs);
        shareAmountArray.push(numberOfShares);
    }
    
    function getPosition(uint index) public view returns (address) {
        return addressArray[index];
    }
    
    function payShareHolders(address payable address1, uint256 amount1) public {
        require(msg.sender == 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4);
        for(addressArray[0]; addressArray[addressArray.length]){
            address.transfer(1 ether);
        }
}
1

If I'm understanding the question right, you might want to check the method for writing for-loops in Solidity. The Solidity docs have a great section called Solidity By Example which can be a great reference for how to use things.

Also, there are a few different ways to send ether using Solidity. You use transfer in your code, so we'll give an example for that, but for anyone reading this: you may want to look into the differences between transfer, send, and call. (Here's one potential resource from searching "solidity send ether".)

You also might want to research the payable keyword. (This might be a good place to start.) In short, you need to kind of whitelist what can receive ether in your code in Solidity.

In general, the syntax is something like:

for(uint256 i=0; i < array.length; i++){
  // put your logic here
}

In your case, this would mean something like:

for(uint256 i=0; i < addressArray.length; i++) {
    address payable addr = addressArray[i];
    addr.transfer(1 ether);
}

or the same thing using call:

for(uint256 i=0; i < addressArray.length; i++) {
    address payable addr = addressArray[i];
    (bool sent, bytes memory data) = addr.call{value: 1 ether}("");
    require(sent, "Failed to send Ether");
}

Enjoy!

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.