4.揭標(biāo)
揭標(biāo)的過(guò)程應(yīng)該是本智能合約中最復(fù)雜且具有靈魂的關(guān)鍵步驟。
當(dāng)每個(gè)發(fā)起過(guò)競(jìng)標(biāo)的用戶藤树,利用該標(biāo)的隱式價(jià)格和密碼進(jìn)行揭標(biāo)時(shí)浴滴。合約會(huì)對(duì)每一個(gè)標(biāo)的信息(顯式價(jià)格、隱式價(jià)格)同該商品實(shí)例中的最高競(jìng)價(jià)和次高競(jìng)價(jià)進(jìn)行比較岁钓,以此來(lái)更新商品中的最高價(jià)升略、次高價(jià)以及最高價(jià)標(biāo)的發(fā)起者微王。同時(shí)進(jìn)行相關(guān)的返款操作。偽代碼如下:
if(標(biāo)內(nèi)的顯式價(jià)格>標(biāo)內(nèi)的隱式價(jià)格){
//退款
}else{
if(標(biāo)內(nèi)的隱式價(jià)格>商品實(shí)例中的最高出價(jià)){
if(商品實(shí)例中沒(méi)有更新過(guò)投標(biāo)數(shù)據(jù)){
//更新商品內(nèi)最高價(jià)品嚣、次高價(jià)和最高價(jià)出價(jià)人
//退差價(jià)
}else{
//向之前的最高出價(jià)標(biāo)主退款
//更新商品內(nèi)最高價(jià)炕倘、次高價(jià)和出價(jià)人
//向自己退差價(jià)
}
}else{
if(標(biāo)內(nèi)的隱式價(jià)格>商品實(shí)例中的次高價(jià)){
//更新次高價(jià)
//退款
}
//退款
}
}
邏輯比較復(fù)雜,假設(shè)幾個(gè)不一樣數(shù)據(jù)的標(biāo)走一下翰撑,轉(zhuǎn)過(guò)彎就可以理解了罩旋。
揭標(biāo)函數(shù)
//寫(xiě)個(gè)事務(wù),用來(lái)實(shí)時(shí)返回商品實(shí)例的最高價(jià)眶诈、次高價(jià)和最高價(jià)出價(jià)人涨醋。用于調(diào)試,正式合約中可以注釋掉
event revealEvent(uint _productIndex,bytes32 bidBytes,uint highestBid,uint sencondHighestBid,uint refund);
function revealBid(uint _productIndex,uint _idealPrice,string memory password)public {
//根據(jù)商品編號(hào)找到商品實(shí)例
address seller = productIdToOwmer[_productIndex];
Product storage pro = stores[seller][_productIndex];
//時(shí)間限制逝撬,只有在競(jìng)標(biāo)結(jié)束和仲裁開(kāi)始之間才可以做揭標(biāo)操作
require(now>=pro.revealStartTime && now<pro.arbitrateStartTime);
//根據(jù)標(biāo)主提供的隱式價(jià)格和密碼來(lái)找到標(biāo)實(shí)例
bytes memory bidInfo = abi.encodePacked(_idealPrice,password);
bytes32 bidBytes = keccak256(bidInfo);
Bid storage curBid =pro.bids[msg.sender][bidBytes];
//確保這個(gè)標(biāo)沒(méi)有被揭過(guò)浴骂。因?yàn)槊拷乙淮螛?biāo)就會(huì)涉及到從合約向用戶發(fā)送以太坊
require(!curBid.isRevealed);
//確保這個(gè)標(biāo)被找到了,即保證標(biāo)主輸入的隱式價(jià)格和密碼是正確的球拦。如果不正確,是無(wú)法找到對(duì)應(yīng)標(biāo)實(shí)例的
require(curBid.bidder!=address(0));
curBid.isRevealed=true;
//標(biāo)內(nèi)的顯式價(jià)格
uint confusedPrice = curBid.price2Show;
//標(biāo)內(nèi)的隱式價(jià)格
uint idealPrice = _idealPrice;
//退款金額
uint refund = 0;
//揭標(biāo)算法
if(confusedPrice<idealPrice){
//相當(dāng)于無(wú)效標(biāo)
refund = confusedPrice;
}else{
if(idealPrice>pro.highestBid){
//如果是第一個(gè)揭標(biāo)的人
if(pro.highestBidder==address(0)){
//更新商品內(nèi)數(shù)據(jù)
pro.highestBid=idealPrice;
pro.secondHighestBid=pro.startPrice;
pro.highestBidder=msg.sender;
refund = confusedPrice - idealPrice;
}else{
//如果不是第一個(gè)揭標(biāo)的人
pro.highestBidder.transfer(pro.highestBid);
pro.secondHighestBid = pro.highestBid;
pro.highestBid = idealPrice;
pro.highestBidder=msg.sender;
refund = confusedPrice-idealPrice;
}
}else{
//如果隱式價(jià)格大于商品內(nèi)次高價(jià)格
if(idealPrice>pro.secondHighestBid){
pro.secondHighestBid = idealPrice;
}
//退款
refund = confusedPrice;
}
}
if(refund>0){
//向標(biāo)主返回該返回的以太坊
msg.sender.transfer(refund);
}
//事務(wù)驗(yàn)證
emit revealEvent(_productIndex,bidBytes,pro.highestBid,pro.secondHighestBid,refund);
}
可添加一些輔助函數(shù)來(lái)幫助測(cè)試(如果不愿意用事務(wù)點(diǎn)來(lái)點(diǎn)去查看信息的話)
function getInfoBack(uint _productIndex) public view returns(address,uint,uint,uint){
//根據(jù)商品編號(hào)找到商品實(shí)例
Product storage pro = stores[productIdToOwmer[_productIndex]][_productIndex];
//返回所有你想要看的數(shù)據(jù)
return (pro.highestBidder,pro.highestBid,pro.secondHighestBid,pro.totalBids);
}
5.第三方仲裁人終結(jié)拍賣
揭標(biāo)過(guò)后帐我,其實(shí)最終的拍賣結(jié)果已經(jīng)產(chǎn)生坎炼。將相關(guān)信息寫(xiě)入另一個(gè)合約中,進(jìn)而進(jìn)行賣家拦键、買(mǎi)家和仲裁人的投標(biāo)操作(類似淘寶中的確認(rèn)收貨的過(guò)程)
每一個(gè)產(chǎn)品都會(huì)生成一個(gè)對(duì)應(yīng)的仲裁實(shí)例谣光,結(jié)構(gòu)如下:
contract Arbitration{
//仲裁人地址
address payable arbitrator;
//賣家地址
address payable seller;
//買(mǎi)家地址
address payable buyer;
//買(mǎi)家獲得票數(shù)
uint totalVotes2Seller;
//賣家獲得票數(shù)
uint totalVotes2Buyer;
constructor(address payable _arbitrator,address payable _seller,address payable _buyer) payable public {
arbitrator=_arbitrator;
seller=_seller;
buyer=_buyer;
}
同時(shí)可以編寫(xiě)一些輔助函數(shù)來(lái)進(jìn)行代碼的階段測(cè)試:
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);
}
終結(jié)競(jìng)拍函數(shù)
只能由非賣家和買(mǎi)家的用戶發(fā)起,處于公平的考慮芬为。
需要在主合約(Auction)里面添加一個(gè)數(shù)據(jù)結(jié)構(gòu)萄金,用來(lái)用商品編號(hào)來(lái)找到對(duì)應(yīng)商品的仲裁合約:
contract Auction{
...
mapping(uint=>Arbitration) public proIndex2Arbitration;
...
}
注:因?yàn)橹挥泻霞sAuction對(duì)所有用戶可見(jiàn),所以要在Auction中調(diào)用對(duì)應(yīng)的Arbitration實(shí)例媚朦。
終結(jié)競(jìng)拍函數(shù)(在主合約Auction中)
function finalizeAuction(uint _productIndex)public payable{
//根據(jù)商品編號(hào)獲取對(duì)應(yīng)商品實(shí)例
address payable seller = productIdToOwmer[_productIndex];
Product storage pro = stores[seller][_productIndex];
//時(shí)間限制:必須在設(shè)定的揭標(biāo)時(shí)間之后才可以終結(jié)競(jìng)拍
require(now>=pro.arbitrateStartTime);
address payable buyer = pro.highestBidder;
address payable arbitrator = msg.sender;
//仲裁人限制:不能是賣家和買(mǎi)家
require(arbitrator!=seller && arbitrator!=buyer);
//商品狀態(tài)限制:保證該商品是第一次被終結(jié)拍賣
require(pro.status==ProductStatus.OPEN);
if(pro.totalBids==0){
pro.status=ProductStatus.UNSOLD;
}else{
pro.status=ProductStatus.SOLD;
}
//將最后的競(jìng)拍信息和Auction中余留的以太坊全部轉(zhuǎn)入仲裁合約實(shí)例中
Arbitration arb = (new Arbitration).value(pro.secondHighestBid)(arbitrator,seller,buyer);
//將這個(gè)新生成的仲裁實(shí)例寫(xiě)入數(shù)據(jù)結(jié)構(gòu)
proIndex2Arbitration[_productIndex]=arb;
//這時(shí)向競(jìng)標(biāo)獲勝者退還他的隱式價(jià)格和商品次高價(jià)之間的差價(jià)
buyer.transfer(pro.highestBid-pro.secondHighestBid);
}
solidity的0.5.0版本語(yǔ)法改動(dòng)查閱地址:
solidity的0.5.0版本語(yǔ)法改動(dòng)查閱地址