ExampleofAuctionContract
跳到导航
跳到搜索
Auction contract
| Auction contract involves two parties: auctioneer who is an enterprise legal person engaged in auction activities, and bidders who are some citizens, legal persons or organizations to participate in bidding for auction targets. The highest bidding process is described as the following steps. The auctioneer starts the auction system after setting a reserve price and an auction end time, and waits for the auction to end; The bidders can bid anytime during the system. If the bid is greater than the current highest price, the system records it as new highest price, puts the bid into the fund pool, and returns the bid paid by the previous highest bidder; otherwise, the bidder fails and the bid is returned; After the auction time is over, the auctioneer can collect the highest bid from the fund pool. |
// Simple Auction.scs
contract SimpleAuction{
- party group bidders{
- amount : Money
- Bid()
- withdrawOverbidMoney()
- amount : Money
- }
- party auctioneer{
- StartBidding(reservePrice : Money, biddingTime : Date)
- StopBidding()
- StartBidding(reservePrice : Money, biddingTime : Date)
- }
- highestPrice : Money
- highestBidder : biddersBiddingStop
- Time : Date
- term no1 : auctioneer can StartBidding,
- when before auctioneer did StartBidding
- where highestPrice = reservePrice and BiddingStopTime = biddingTime + now.
- when before auctioneer did StartBidding
- term no2 : bidders can Bid,
- when after auctioneer did StartBidding and before BiddingStopTime
- while deposit $ value > highestPrice
- where highestPrice = value and highestBidder = this bidder and
- this bidder : : amount = this bidder: :0ri amount + value .
- when after auctioneer did StartBidding and before BiddingStopTime
- term no3_1 : bidders can withdrawOverbidMoney,
- when this bidder isn't highestBidder and this bidder: :amount > 0
- while withdraw $this bidder : :amount
- where this bidder : : amount = 0.
- when this bidder isn't highestBidder and this bidder: :amount > 0
- term no3_2 : bidders can withdrawOverbidMoney,
- when this bidder is highestBidder and this bidder : : amount > highestPrice
- while withdraw $this bidder : : amount - highestPrice
- where this bidder : : amount = highestPrice.
- when this bidder is highestBidder and this bidder : : amount > highestPrice
- term no4 : auctioneer can StopBidding,
- when after BiddingStopTime and before auctioneer did StopBidding
- while withdraw $highestPrice.
- when after BiddingStopTime and before auctioneer did StopBidding
}
/* auctioneerT.sol */
pragma solidity >=0.4.0 <0.6.0;
contract auctioneerT{
//attributes of actionStartBidding
bool _isStartBiddingDone;
uint _StartBiddingTime;
//attributes of actionCollectPayment
bool _isCollectPaymentDone;
uint _CollectPaymentTime;
address _auctioneerAddress;
uint _max;
constructor() public{
_max = now*1000;
}
function regist(address a) public {
_auctioneerAddress = a;
}
function getAddress() public returns (address a){
return _auctioneerAddress;
}
function StartBiddingDone() public{
_StartBiddingTime = now;
_isStartBiddingDone = true;
}
function StartBiddingTime() public returns (uint result){
if(_isStartBiddingDone){
return _StartBiddingTime;
}
return _max;
}
function CollectPaymentDone() public{
_CollectPaymentTime = now;
_isCollectPaymentDone = true;
}
function CollectPaymentTime() public returns (uint result){
if(_isCollectPaymentDone){
return _CollectPaymentTime;
}
return _max;
}
}/* biddersT.sol */
pragma solidity ^0.4.22;
contract biddersT{
struct bidderstype{
uint amount;
//attributes of actionBid
bool _isBidDone;
uint _BidTime;
//attributes of actionWithdrawOverbidMoney
bool _isWithdrawOverbidMoneyDone;
uint _WithdrawOverbidMoneyTime;
}
uint _max;//time max
uint _sum;//total member of this party
uint _BidDoneNum;
uint[] _BidTime;
uint _WithdrawOverbidMoneyDoneNum;
uint[] _WithdrawOverbidMoneyTime;
bidderstype _Empty;//used to initialize
bidderstype[] _biddersEntity;
address[] _biddersAddress;
mapping(address=>uint) _userlist;
function biddersT(){
_BidDoneNum = 0;
_WithdrawOverbidMoneyDoneNum = 0;
_biddersEntity.push(_Empty);
_max = now*1000;
}
function getSum() public returns(uint c){
return _sum;
}
function add(address a) public {
_biddersEntity.push(_Empty);
_biddersAddress.push(a);
// _userlist[address] = _sum;
_sum ++;
}
function remove(address a) public {
uint num = _userlist[a];
_biddersEntity[num] = _biddersEntity[_sum-1];
delete(_biddersEntity[num]);
delete(_userlist[a]);
_sum --;
}
function getList() public returns (address[] a){
return _biddersAddress;
}
function contains(address a) public returns (bool b){
return _userlist[a] != 0;
}
function getamount(address a) returns(uint _result){
uint num = _userlist[a];
return _biddersEntity[num].amount;
}
function setamount(address a, uint b){
uint num = _userlist[a];
_biddersEntity[num].amount = b;
}
function BidDone(address a){
uint num = _userlist[a];
_biddersEntity[num]._BidTime = now;
_biddersEntity[num]._isBidDone = true;
_BidTime.push(_biddersEntity[num]._BidTime);
_BidDoneNum ++;
}
function BidTime(address a) returns (uint result){
uint num = _userlist[a];
if(_biddersEntity[num]._isBidDone){
return _biddersEntity[num]._BidTime;
}
return _max;
}
function BidAllTime() returns (uint result){
if(_BidDoneNum == _max-1){
return _BidTime[_BidDoneNum-1];
}
return _max;
}
function BidSomeTime() returns (uint result){
if(_BidDoneNum >= 1){
return _BidTime[0];
}
return _max;
}
function WithdrawOverbidMoneyDone(address a){
uint num = _userlist[a];
_biddersEntity[num]._WithdrawOverbidMoneyTime = now;
_biddersEntity[num]._isWithdrawOverbidMoneyDone = true;
_WithdrawOverbidMoneyTime.push(_biddersEntity[num]._WithdrawOverbidMoneyTime);
_WithdrawOverbidMoneyDoneNum ++;
}
function WithdrawOverbidMoneyTime(address a) returns (uint result){
uint num = _userlist[a];
if(_biddersEntity[num]._isWithdrawOverbidMoneyDone){
return _biddersEntity[num]._WithdrawOverbidMoneyTime;
}
return _max;
}
function WithdrawOverbidMoneyAllTime() returns (uint result){
if(_WithdrawOverbidMoneyDoneNum == _max-1){
return _WithdrawOverbidMoneyTime[_WithdrawOverbidMoneyDoneNum-1];
}
return _max;
}
function WithdrawOverbidMoneySomeTime() returns (uint result){
if(_WithdrawOverbidMoneyDoneNum >= 1){
return _WithdrawOverbidMoneyTime[0];
}
return _max;
}
}/* goodsOwnerT.sol */
pragma solidity ^0.4.22;
contract goodsOwnerT{
//attributes of actionStartBidding
bool _isStartBiddingDone;
uint _StartBiddingTime;
bool _isgoodsOwnerDone;
uint _max;
//attributes of actionStopBidding
bool _isStopBiddingDone;
uint _StopBiddingTime;
address[] _goodsOwnerAddress;
constructor() public{
_max = now*1000;
}
function regist (address[] a){
_goodsOwnerAddress = a;
}
function getAddress() public returns (address[] a){
return _goodsOwnerAddress;
}
function StartBiddingDone(address[] a){
// StartBiddingTime = now;
_isgoodsOwnerDone = true;
//_isStartBiddingDone = true;
}
function StartBiddingTime() returns (uint result){
if(_isStartBiddingDone){
return _StartBiddingTime;
}
return _max;
}
function StopBiddingDone(address[] a){
// StopBiddingTime = now;
_isgoodsOwnerDone = true;
// _StopBiddingTime = true;
}
function StopBiddingTime() returns (uint result){
if(_isStopBiddingDone){
return _StopBiddingTime;
}
return _max;
}
}/* SimpleAuction.sol */
pragma solidity ^0.4.22;
contract SimpleAuction {
address public beneficiary;
uint public auctionEnd;
address public highestBidder;
uint public highestBid;
mapping(address => uint) pendingReturns;
bool ended;
event HighestBidIncreased(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);
constructor(uint _biddingTime, address _beneficiary) public {
beneficiary = _beneficiary;
auctionEnd = now + _biddingTime;
}
function bid() public payable {
require(now <= auctionEnd, "Auction already ended.");
require(msg.value > highestBid, "There already is a higher bid.");
if (highestBid != 0) {
pendingReturns[highestBidder] += highestBid;
}
highestBidder = msg.sender;
highestBid = msg.value;
HighestBidIncreased(msg.sender, msg.value);
}
function withdraw() public returns (bool) {
uint amount = pendingReturns[msg.sender];
if (amount > 0) {
pendingReturns[msg.sender] = 0;
if (!msg.sender.send(amount)) {
// No need to call throw here, just reset the amount owing
pendingReturns[msg.sender] = amount;
return false;
}
}
return true;
}
function auctionEnd() public {
require(now >= auctionEnd, "Auction not yet ended.");
require(!ended, "auctionEnd has already been called.");
ended = true;
AuctionEnded(highestBidder, highestBid);
beneficiary.transfer(highestBid);
}
}