zoukankan      html  css  js  c++  java
  • ethereum(以太坊)(实例)--"简单的公开竞拍"

    说真的,刚开始接触这个竞拍案例--“简单的公开竞拍”,我就抱着简单的心态去查看这个实例,但是自我感觉并不简单.应该是我实力不到家的原因吧!!!233333。。。
    不过经过大半天的努力,自己理解完之后,觉得是有那么点简单.(代码是官网上的,不过解释不太清楚.我加了自己的实践和理解,希望能帮助到大家)
    pragma solidity ^0.4.10;
    
    contract Bid{
    
        //0x14723a09acff6d2a60dcdf7aa4aff308fddc160c,100
    
        //0x4b0897b0513fdc7c541b6d9d7e929c4e5364d2db,200
        //0x583031d1113ad414f02576bd6afabfb302140225,150
    
        //商品拥有者地址
        address public benefiy;
        //拍卖时间
        uint bidEndtime;
    
        //记录未成功竞拍到的人,可以取回之前的出价
        mapping(address => uint) public RetrunMemony;
        //最高价地址
        address public HighestBidAddr;
        //最高价
        uint public HighestBidMomery;
        //是否结束
        bool isEnd;
    
        event HighestBidIncrase(address bider,uint _memony);
        event EndBidIncrase(address _bider,uint _memony);
    
        constructor(address  _benefiy,uint _EndTime) public{
            benefiy = _benefiy;
            bidEndtime = now + _EndTime;
        }
        //开始竞拍
        function bid() public payable{
            require(now < bidEndtime);//require(msg.sender.balance > msg.value);
            require(msg.value>HighestBidMomery);
    
            if(HighestBidMomery !=0){ //储存成功参与竞拍但不是最高价的地址和出价
                RetrunMemony[HighestBidAddr] += HighestBidMomery;//同一参与竞拍者多次出价
            }
    
            HighestBidMomery = msg.value;
            HighestBidAddr = msg.sender;
            emit HighestBidIncrase(msg.sender,msg.value);
        }
        //结束竞拍
        function endBid() public payable{
            require(now >= bidEndtime);
            require(!isEnd);
    
            isEnd = true;
    
            benefiy.transfer(HighestBidMomery);
            emit EndBidIncrase(HighestBidAddr,HighestBidMomery);
        }
        //未竞拍成功的人取回交易
        function getBack() payable public returns(bool){
            uint mount = RetrunMemony[msg.sender];
            if(mount >0){//执行一次后,就无法再执行此返还函数
                RetrunMemony[msg.sender] = 0;//取回后,价格清0,不允许再次执行
                //send:success->true,failed->false
                //msg.sender.send(mount) 执行成功则不执行以下函数
                if(!msg.sender.send(mount)){// 未返还竞拍价格
                    RetrunMemony[msg.sender] = mount;
                    return false;//未返还竞拍价格
                }
            }
            return true;//if(mount <=0) 已经成功取回竞拍价
        }
    
    }
  • 相关阅读:
    poj 3243 Clever Y(BabyStep GiantStep)
    poj 2417 Discrete Logging
    poj 3481 Double Queue
    hdu 4046 Panda
    hdu 2896 病毒侵袭
    poj 1442 Black Box
    hdu 2815 Mod Tree
    hdu 3065 病毒侵袭持续中
    hdu 1576 A/B
    所有控件
  • 原文地址:https://www.cnblogs.com/eilinge/p/10106654.html
Copyright © 2011-2022 走看看