0

I'd like to implement a self-destruct method for a smart contract that pools money from people as a last resort.

Before calling self-destruct, I'd like to refund all the money to the users and send whatever is remaining to the owner address.

I'm thinking of something along the lines of

uint256 usersLength = users.length;
for (var i = 0; i < usersLength; i++ ) {
    address user = users[i];
    uint256 refundAmount = pooledMoney[user];
    user.transfer(refundAmount);
)
selfdestruct(owner);

I'm a little worried what would happen if the size of the users array is very large, will the method run out of gas within the loop?

If so, what are alternative solutions for implementing something like this?

1

Yes, the transaction will run out of gas if the array of users is too big.

The alternative is for each user to retrieve its part of the pool.

function withdraw() public {
    uint refundAmount = pooledMoney[msg.sender];
    if (refundAmount > 0) {
        pooledMoney[msg.sender] = 0;
        msg.sender.transfer(refundAmount);
        emit Refunded(msg.sender, refundAmount);
    }
}

After all users have withdraw their ether then the owner can destruct the contract.

  • Thanks for answering! Was wondering if their was an efficient way to do this on the users' behalf, but I guess there isn't... – cryptoninja May 10 '18 at 6:02

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.