0

For the sake of humanity, could anyone help please?

A splitValue is send by msg.sender - the sender is part of the beneficiaryList - but excluded from the list at moment of sending. How would that look like in code?

I have a start:

uint splitValue = uint(msg.value/beneficiaryList.length);
     for (uint index = 0; index < beneficiaryList.length; index++) { 
     beneficiaryList [index].transfer(SplitValue); 

     if(participants[msg.sender] != 0){
     beneficiaryList [index] != (msg.sender); }

Not sure if the last two lines are correct.

0

1 Answer 1

2

If beneficiaryList is an array or addresses, and you're looping through the array, you'd simply check it against msg.sender and skip it if it matches. So something like:

uint splitValue = uint(msg.value/beneficiaryList.length);
uint l = beneficiaryList.length;
for (uint index = 0; index < l; index++) { 
    address b = beneficiaryList[index];
    if (b != msg.sender) {
        b.transfer(splitValue);
    }
}

I can't help with the final two lines as I can't tell what they're supposed to do.

BTW, be aware that there are a few code smells in what you're doing that may indicate that you should change your approach. Unless you know that beneficiaryList will have a limited length, your code may run out of gas, and you may never be able to run it because it exceeds the block gas limit. Also, since the beneficiary address may be the address of a contract and transfer will call the default payment function it, it could throw an exception or cause you to run out of gas, and which will also prevent anybody from using the function. Depending on the other code it may also have reentrancy issues. Consider using the withdrawal pattern instead.

2
  • Thank you. The last two lines should exclude msg.sender from the array, before calculating. This is an array of all participants. When transfer is done it should be done to all other participants, so everyone except the sender (msg.sender). Not sure how to code this.
    – user7178
    Aug 11, 2017 at 7:46
  • See ethereum.stackexchange.com/questions/1527/… or one of the related questions. You already looped through the array so you could have kept the index. Depending what you're doing an array may not be ideal, eg a linked list may be better. Aug 12, 2017 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.