【问题标题】:Refund all addresses entered on solidity contract退还所有在solidity合约上输入的地址
【发布时间】:2020-01-05 19:16:26
【问题描述】:

我遇到了一个与在 Solidity 合同中输入的用户退款有关的问题。我不知道,是否有可能迭代我合同中的所有玩家并退还所有余额.

    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);
}}

在此先感谢大家。

【问题讨论】:

    标签: blockchain ethereum solidity


    【解决方案1】:

    是的,这可能会有问题,因为它会消耗大量气体,甚至可能达到极限。为了解决这个问题,最好让用户自己提取余额,这样他们就需要支付 gas 费用。或者实际上,您可以允许任何人拨打电话。因此,您可以拨打refund(account:uint256) 将余额(如果有)转移到给定帐户。请注意,这不会使用msg.sender,因此任何人(包括管理员)都可以进行此转移。

    请记住,他们需要知道自己有余额,因此请确保您正在发出事件或类似事件。还提供balanceOf(address) 电话,以便他们检查。

    希望这对你有意义并且对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-03
      • 2018-02-04
      • 2019-12-21
      • 2021-11-28
      • 2021-11-07
      • 2017-09-21
      • 1970-01-01
      • 2022-12-10
      相关资源
      最近更新 更多