pragma solidity^0.6.1;
contract aution_demo{
address payable public seller; //卖方
address payable public buyer; //卖方
uint256 public highAmount; //最高价
address public admin; //管理员(平台方)
string autionName; //拍卖物品
bool isFinshed; //是否拍卖结束
uint256 outTime; //拍卖截止时间
mapping (address => uint256) pendingReturns; // 用于取回之前的出价
//最高出价变动时调用事件
event HighestBidIncreased(address _bidder,uint256 _amount);
//拍卖结束时调用事件
event End_Auction(address _winner,uint256 _amount);
//创建一个拍卖对象,初始化卖方、拍卖物品、拍卖时间
constructor(address payable _seller,string memory _name)public{
seller=_seller;
autionName=_name;
admin=msg.sender; //管理员为部署该合约的账户
outTime=now+120; //拍卖时长为2分钟
isFinshed=false;
highAmount=0;
}
// 拍卖
function aution(uint256 amount)public payable{
require(amount>highAmount,"amount must>highAmount");
require(amount==msg.value,"amount must==msg.value");
require(now<=outTime,"must not time out");
require(!isFinshed,"must not finshed");
pendingReturns[buyer]+=highAmount;
//buyer.transfer(highAmount); //每一次买方出价前返回上笔金额
buyer=msg.sender; //最终买方的账户地址为本次调用合约的账户地址
highAmount=amount;
emit HighestBidIncreased(msg.sender,msg.value);
}
// 未拍卖成功者取回之前的金额
function withdraw() public returns (bool){
uint256 amount = pendingReturns[msg.sender];
if (amount > 0){
// 需要提前设置为0,因为接收者可以在这个函数结束前再次调用它
pendingReturns[msg.sender] = 0;
if (!msg.sender.send(amount)){
// 不需要throw,直接重置代币数量即可
pendingReturns[msg.sender] = amount;
return false;
}
}
return true;
}
//结束拍卖
function endAuction()public payable{
require(msg.sender==admin,"only admin can do this");
require(now>outTime,"time is not ok");
require(!isFinshed,"must not finshed");
isFinshed=true;
emit End_Auction(buyer, highAmount);
seller.transfer(highAmount*90/100); //支付90%的价格给卖方
}
}
3.测试之前的账户地址准备
4. 管理员(平台方部署合约,并初始化)
5. 卖方调用智能合约
6. 管理员(平台方结束拍卖)模拟3个卖家,调用aution()函数分别支付1000000wei,2000000wei,3000000wei
7. 未拍卖成功的卖方取回金钱管理员通过调用endAuction()函数结束拍卖,并将抽取10%的利益,剩余的90%的钱发送给卖家。
拍卖结束后,卖方调用withdarw()函数取回金钱。
以下为整体的测试结果图(顺序为从左到右,从上到下)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)