简体   繁体   English

退还所有在 Solidity 合同中输入的地址

[英]Refund all addresses entered on solidity contract

I have a problem related to refund the users entered inside the solidity contract.我有一个与退还在 Solidity 合同中输入的用户有关的问题。 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.为了解决这个问题,最好让用户自己提取余额,这样他们就可以支付 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.因此,您可以提供电话refund(account:uint256) ,将余额(如果有)转移到给定帐户。 Note that this would not be using msg.sender , so that anybody (including the admin) can do this transfer.请注意,这不会使用msg.sender ,因此任何人(包括管理员)都可以进行此传输。

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.还提供一个balanceOf(address)调用,以便他们可以检查。

Hope this makes sense & works for you.希望这对您有意义并适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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