基于以太坊的次高价盲拍solidity智能合约(二)

基于以太坊的次高价盲拍solidity智能合约(二)

4.揭标

揭标的过程应该是本智能合约中最复杂且具有灵魂的关键步骤。
当每个发起过竞标的用户,利用该标的隐式价格和密码进行揭标时。合约会对每一个标的信息(显式价格、隐式价格)同该商品实例中的最高竞价和次高竞价进行比较,以此来更新商品中的最高价、次高价以及最高价标的发起者。同时进行相关的返款操作。伪代码如下:

if(标内的显式价格>标内的隐式价格){
            //退款
}else{
    if(标内的隐式价格>商品实例中的最高出价){     
    	if(商品实例中没有更新过投标数据){
        	//更新商品内最高价、次高价和最高价出价人
        	//退差价
        }else{
        	//向之前的最高出价标主退款
        	//更新商品内最高价、次高价和出价人
        	//向自己退差价
        }
    }else{
    	if(标内的隐式价格>商品实例中的次高价){
        	//更新次高价
        	//退款
         }
          	//退款
    }
}

逻辑比较复杂,假设几个不一样数据的标走一下,转过弯就可以理解了。

揭标函数

 	//写个事务,用来实时返回商品实例的最高价、次高价和最高价出价人。用于调试,正式合约中可以注释掉
 	event revealEvent(uint _productIndex,bytes32 bidBytes,uint highestBid,uint sencondHighestBid,uint refund);

    function revealBid(uint _productIndex,uint _idealPrice,string memory password)public {
        //根据商品编号找到商品实例
        address seller = productIdToOwmer[_productIndex];
        Product storage pro = stores[seller][_productIndex];
        //时间限制,只有在竞标结束和仲裁开始之间才可以做揭标操作
        require(now>=pro.revealStartTime && now<pro.arbitrateStartTime);
        //根据标主提供的隐式价格和密码来找到标实例
        bytes memory bidInfo = abi.encodePacked(_idealPrice,password);
        bytes32 bidBytes = keccak256(bidInfo);
        Bid storage curBid =pro.bids[msg.sender][bidBytes];
        //确保这个标没有被揭过。因为每揭一次标就会涉及到从合约向用户发送以太坊
        require(!curBid.isRevealed);
        //确保这个标被找到了,即保证标主输入的隐式价格和密码是正确的。如果不正确,是无法找到对应标实例的
        require(curBid.bidder!=address(0));
        curBid.isRevealed=true;
		//标内的显式价格
        uint confusedPrice = curBid.price2Show;
        //标内的隐式价格
        uint idealPrice = _idealPrice;
        //退款金额
        uint refund = 0;
		
		//揭标算法
        if(confusedPrice<idealPrice){
        	//相当于无效标
            refund = confusedPrice;
        }else{
            if(idealPrice>pro.highestBid){
                //如果是第一个揭标的人
                if(pro.highestBidder==address(0)){
                    //更新商品内数据
                    pro.highestBid=idealPrice;
                    pro.secondHighestBid=pro.startPrice;
                    pro.highestBidder=msg.sender;
                    refund = confusedPrice - idealPrice;
                }else{
                //如果不是第一个揭标的人
                    pro.highestBidder.transfer(pro.highestBid);
                    pro.secondHighestBid = pro.highestBid;
                    pro.highestBid = idealPrice;
                    pro.highestBidder=msg.sender;
                    refund = confusedPrice-idealPrice;
                }
            }else{
                //如果隐式价格大于商品内次高价格
                if(idealPrice>pro.secondHighestBid){
                    pro.secondHighestBid = idealPrice;
                }
                //退款
                refund = confusedPrice;
            }
        }
        if(refund>0){
        //向标主返回该返回的以太坊
            msg.sender.transfer(refund);
        }
        //事务验证
        emit revealEvent(_productIndex,bidBytes,pro.highestBid,pro.secondHighestBid,refund);
    }

可添加一些辅助函数来帮助测试(如果不愿意用事务点来点去查看信息的话)

  function getInfoBack(uint _productIndex) public view returns(address,uint,uint,uint){
  		//根据商品编号找到商品实例
        Product storage pro = stores[productIdToOwmer[_productIndex]][_productIndex];
        //返回所有你想要看的数据
        return (pro.highestBidder,pro.highestBid,pro.secondHighestBid,pro.totalBids);
    }

5.第三方仲裁人终结拍卖

揭标过后,其实最终的拍卖结果已经产生。将相关信息写入另一个合约中,进而进行卖家、买家和仲裁人的投标操作(类似淘宝中的确认收货的过程)

每一个产品都会生成一个对应的仲裁实例,结构如下:

contract Arbitration{
	//仲裁人地址
    address payable arbitrator;
    //卖家地址
    address payable seller;
    //买家地址
    address payable buyer;
    //买家获得票数
    uint totalVotes2Seller;
    //卖家获得票数
    uint totalVotes2Buyer;
    
    constructor(address payable _arbitrator,address payable _seller,address payable _buyer) payable public {
        arbitrator=_arbitrator;
        seller=_seller;
        buyer=_buyer;
    }

同时可以编写一些辅助函数来进行代码的阶段测试:

	function getBalance()public view returns(uint){
        return address(this).balance;
    }
    
    function getAritrationInfo()public view returns(address,address,address,uint,uint){
        return (arbitrator,seller,buyer,totalVotes2Seller,totalVotes2Buyer);
    }

终结竞拍函数
只能由非卖家和买家的用户发起,处于公平的考虑。
需要在主合约(Auction)里面添加一个数据结构,用来用商品编号来找到对应商品的仲裁合约:

contract Auction{
	...
	mapping(uint=>Arbitration) public proIndex2Arbitration;
	...
}

注:因为只有合约Auction对所有用户可见,所以要在Auction中调用对应的Arbitration实例。

终结竞拍函数(在主合约Auction中)

	function finalizeAuction(uint _productIndex)public payable{
		//根据商品编号获取对应商品实例
        address payable seller = productIdToOwmer[_productIndex];
        Product storage pro = stores[seller][_productIndex];
        //时间限制:必须在设定的揭标时间之后才可以终结竞拍
        require(now>=pro.arbitrateStartTime);
        address payable buyer = pro.highestBidder;
        address payable arbitrator = msg.sender;
        //仲裁人限制:不能是卖家和买家        
        require(arbitrator!=seller && arbitrator!=buyer);
       	//商品状态限制:保证该商品是第一次被终结拍卖
        require(pro.status==ProductStatus.OPEN);
        
        if(pro.totalBids==0){
            pro.status=ProductStatus.UNSOLD;
        }else{
            pro.status=ProductStatus.SOLD;
        }
        //将最后的竞拍信息和Auction中余留的以太坊全部转入仲裁合约实例中
        Arbitration arb = (new Arbitration).value(pro.secondHighestBid)(arbitrator,seller,buyer);
        //将这个新生成的仲裁实例写入数据结构
        proIndex2Arbitration[_productIndex]=arb;
        //这时向竞标获胜者退还他的隐式价格和商品次高价之间的差价
        buyer.transfer(pro.highestBid-pro.secondHighestBid);
    }

solidity的0.5.0版本语法改动查阅地址:
solidity的0.5.0版本语法改动查阅地址

ps:
本人热爱图灵,热爱中本聪,热爱V神,热爱一切被梨花照过的姑娘。
以下是我个人的公众号,如果有技术问题可以关注我的公众号来跟我交流。
同时我也会在这个公众号上每周更新我的原创文章,喜欢的小伙伴或者老伙计可以支持一下!

后现代泼痞浪漫主义奠基人
公众号名称:后现代泼痞浪漫主义奠基人

猜你喜欢

转载自blog.csdn.net/michael_wgy_/article/details/88047053
今日推荐