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
}
1 /* auctioneerT.sol */
2 pragma solidity >=0.4.0 <0.6.0;
3
4 contract auctioneerT{
5
6
7 //attributes of actionStartBidding
8 bool _isStartBiddingDone;
9 uint _StartBiddingTime;
10
11 //attributes of actionCollectPayment
12 bool _isCollectPaymentDone;
13 uint _CollectPaymentTime;
14
15 address _auctioneerAddress;
16 uint _max;
17
18 constructor() public{
19 _max = now*1000;
20 }
21
22 function regist(address a) public {
23 _auctioneerAddress = a;
24 }
25
26 function getAddress() public returns (address a){
27 return _auctioneerAddress;
28 }
29
30 function StartBiddingDone() public{
31 _StartBiddingTime = now;
32 _isStartBiddingDone = true;
33 }
34
35 function StartBiddingTime() public returns (uint result){
36 if(_isStartBiddingDone){
37 return _StartBiddingTime;
38 }
39 return _max;
40 }
41
42 function CollectPaymentDone() public{
43 _CollectPaymentTime = now;
44 _isCollectPaymentDone = true;
45 }
46
47 function CollectPaymentTime() public returns (uint result){
48 if(_isCollectPaymentDone){
49 return _CollectPaymentTime;
50 }
51 return _max;
52 }
53
54 }
1 /* biddersT.sol */
2 pragma solidity ^0.4.22;
3
4 contract biddersT{
5
6 struct bidderstype{
7 uint amount;
8
9 //attributes of actionBid
10 bool _isBidDone;
11 uint _BidTime;
12
13 //attributes of actionWithdrawOverbidMoney
14 bool _isWithdrawOverbidMoneyDone;
15 uint _WithdrawOverbidMoneyTime;
16
17 }
18
19 uint _max;//time max
20 uint _sum;//total member of this party
21 uint _BidDoneNum;
22 uint[] _BidTime;
23
24 uint _WithdrawOverbidMoneyDoneNum;
25 uint[] _WithdrawOverbidMoneyTime;
26
27 bidderstype _Empty;//used to initialize
28 bidderstype[] _biddersEntity;
29 address[] _biddersAddress;
30 mapping(address=>uint) _userlist;
31
32 function biddersT(){
33 _BidDoneNum = 0;
34
35 _WithdrawOverbidMoneyDoneNum = 0;
36
37 _biddersEntity.push(_Empty);
38 _max = now*1000;
39 }
40
41 function getSum() public returns(uint c){
42 return _sum;
43 }
44
45 function add(address a) public {
46 _biddersEntity.push(_Empty);
47 _biddersAddress.push(a);
48 // _userlist[address] = _sum;
49
50 _sum ++;
51 }
52
53 function remove(address a) public {
54 uint num = _userlist[a];
55 _biddersEntity[num] = _biddersEntity[_sum-1];
56 delete(_biddersEntity[num]);
57 delete(_userlist[a]);
58 _sum --;
59 }
60
61 function getList() public returns (address[] a){
62 return _biddersAddress;
63 }
64
65 function contains(address a) public returns (bool b){
66 return _userlist[a] != 0;
67 }
68
69 function getamount(address a) returns(uint _result){
70 uint num = _userlist[a];
71 return _biddersEntity[num].amount;
72 }
73
74 function setamount(address a, uint b){
75 uint num = _userlist[a];
76 _biddersEntity[num].amount = b;
77 }
78
79 function BidDone(address a){
80 uint num = _userlist[a];
81 _biddersEntity[num]._BidTime = now;
82 _biddersEntity[num]._isBidDone = true;
83 _BidTime.push(_biddersEntity[num]._BidTime);
84 _BidDoneNum ++;
85 }
86
87 function BidTime(address a) returns (uint result){
88 uint num = _userlist[a];
89 if(_biddersEntity[num]._isBidDone){
90 return _biddersEntity[num]._BidTime;
91 }
92 return _max;
93 }
94
95 function BidAllTime() returns (uint result){
96 if(_BidDoneNum == _max-1){
97 return _BidTime[_BidDoneNum-1];
98 }
99 return _max;
100 }
101
102 function BidSomeTime() returns (uint result){
103 if(_BidDoneNum >= 1){
104 return _BidTime[0];
105 }
106 return _max;
107 }
108
109 function WithdrawOverbidMoneyDone(address a){
110 uint num = _userlist[a];
111 _biddersEntity[num]._WithdrawOverbidMoneyTime = now;
112 _biddersEntity[num]._isWithdrawOverbidMoneyDone = true;
113 _WithdrawOverbidMoneyTime.push(_biddersEntity[num]._WithdrawOverbidMoneyTime);
114 _WithdrawOverbidMoneyDoneNum ++;
115 }
116
117 function WithdrawOverbidMoneyTime(address a) returns (uint result){
118 uint num = _userlist[a];
119 if(_biddersEntity[num]._isWithdrawOverbidMoneyDone){
120 return _biddersEntity[num]._WithdrawOverbidMoneyTime;
121 }
122 return _max;
123 }
124
125 function WithdrawOverbidMoneyAllTime() returns (uint result){
126 if(_WithdrawOverbidMoneyDoneNum == _max-1){
127 return _WithdrawOverbidMoneyTime[_WithdrawOverbidMoneyDoneNum-1];
128 }
129 return _max;
130 }
131
132 function WithdrawOverbidMoneySomeTime() returns (uint result){
133 if(_WithdrawOverbidMoneyDoneNum >= 1){
134 return _WithdrawOverbidMoneyTime[0];
135 }
136 return _max;
137 }
138
139 }
1 /* goodsOwnerT.sol */
2 pragma solidity ^0.4.22;
3
4 contract goodsOwnerT{
5
6
7 //attributes of actionStartBidding
8 bool _isStartBiddingDone;
9 uint _StartBiddingTime;
10 bool _isgoodsOwnerDone;
11
12 uint _max;
13
14 //attributes of actionStopBidding
15 bool _isStopBiddingDone;
16 uint _StopBiddingTime;
17
18 address[] _goodsOwnerAddress;
19
20 constructor() public{
21 _max = now*1000;
22 }
23
24 function regist (address[] a){
25 _goodsOwnerAddress = a;
26 }
27
28 function getAddress() public returns (address[] a){
29 return _goodsOwnerAddress;
30 }
31
32 function StartBiddingDone(address[] a){
33 // StartBiddingTime = now;
34 _isgoodsOwnerDone = true;
35 //_isStartBiddingDone = true;
36 }
37
38 function StartBiddingTime() returns (uint result){
39 if(_isStartBiddingDone){
40 return _StartBiddingTime;
41 }
42 return _max;
43 }
44
45 function StopBiddingDone(address[] a){
46 // StopBiddingTime = now;
47 _isgoodsOwnerDone = true;
48 // _StopBiddingTime = true;
49 }
50
51 function StopBiddingTime() returns (uint result){
52 if(_isStopBiddingDone){
53 return _StopBiddingTime;
54 }
55 return _max;
56 }
57
58 }
1 /* SimpleAuction.sol */
2 pragma solidity ^0.4.22;
3
4 contract SimpleAuction {
5
6 address public beneficiary;
7 uint public auctionEnd;
8 address public highestBidder;
9 uint public highestBid;
10 mapping(address => uint) pendingReturns;
11 bool ended;
12
13 event HighestBidIncreased(address bidder, uint amount);
14 event AuctionEnded(address winner, uint amount);
15
16 constructor(uint _biddingTime, address _beneficiary) public {
17 beneficiary = _beneficiary;
18 auctionEnd = now + _biddingTime;
19 }
20
21 function bid() public payable {
22
23 require(now <= auctionEnd, "Auction already ended.");
24 require(msg.value > highestBid, "There already is a higher bid.");
25
26 if (highestBid != 0) {
27 pendingReturns[highestBidder] += highestBid;
28 }
29 highestBidder = msg.sender;
30 highestBid = msg.value;
31 HighestBidIncreased(msg.sender, msg.value);
32 }
33
34 function withdraw() public returns (bool) {
35 uint amount = pendingReturns[msg.sender];
36 if (amount > 0) {
37 pendingReturns[msg.sender] = 0;
38
39 if (!msg.sender.send(amount)) {
40 // No need to call throw here, just reset the amount owing
41 pendingReturns[msg.sender] = amount;
42 return false;
43 }
44 }
45 return true;
46 }
47
48 function auctionEnd() public {
49
50 require(now >= auctionEnd, "Auction not yet ended.");
51 require(!ended, "auctionEnd has already been called.");
52
53 ended = true;
54 AuctionEnded(highestBidder, highestBid);
55
56 beneficiary.transfer(highestBid);
57 }
58 }