简体   繁体   中英

Refund all addresses entered on solidity contract

I have a problem related to refund the users entered inside the solidity contract. I don't, have any idea if is possible iterate all the players of my contract and refund all of them with all the balance.I read in some tutorials, iterate inside the solidity contract cost a lot of gas depends of the numer of iterations.

    contract Lottery{
address payable public manager;
string public name;   // short name (up to 32 bytes)
address [] players;
uint256 nTickets;
address winner;
bool enable;
uint256 minimunContribution;
mapping(address => uint) public balances;

constructor (string memory LotteryName, uint minimun, address payable creator) public  {
    manager = creator;
    name = LotteryName;
    winner = address(0);
    enable = true;
    minimunContribution = minimun;
}

modifier restricted() {
require(msg.sender == manager, "Access forbidden");
_;
}

function enterInToLottery() public payable {
    require(msg.value > minimunContribution && enable == true, "Insufficient funds to allow transfer");
    players.push(msg.sender);
    balances[msg.sender] += msg.value;
    nTickets++;
}

//this function refund
function paybackEther(bool newfinished) public restricted {
    enable = !newfinished; 


    selfdestruct(msg.sender);
}}

Thanks in advance to all.

Yes that can be problematic as it will use a lot of gas and may even hit limits. To solve this it is better to allow the users to withdraw the balance themselves, so they pay for the gas. Or in fact you can allow anybody to make that call. So you can offer a call refund(account:uint256) which transfers the balance (if any) to given account. Note that this would not be using msg.sender , so that anybody (including the admin) can do this transfer.

Keep in mind they need to know that they have a balance, so make sure you're emitting an event or similar. Also provide a balanceOf(address) call so they can check.

Hope this makes sense & works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM