-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionBox.sol
138 lines (117 loc) · 3.8 KB
/
ActionBox.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// We will be using Solidity version 0.5.3
pragma solidity ^0.6.0;
// Importing OpenZeppelin's SafeMath Implementation
import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol";
contract AuctionBox{
Auction[] public auctions;
function createAuction (
string memory _title,
uint _startPrice,
string memory _description
) public{
require(_startPrice > 0);
// set the new instanc
Auction newAuction = new Auction(msg.sender, _title, _startPrice, _description);
// push the auction address to auctions array
auctions.push(newAuction);
}
function returnAllAuctions() public view returns(Auction[] memory){
return auctions;
}
}
contract Auction {
using SafeMath for uint256;
address payable private owner;
string title;
uint startPrice;
string description;
enum State{Default, Running, Finalized}
State public auctionState;
uint public highestPrice;
address payable public highestBidder;
mapping(address => uint) public bids;
/** @dev constructor to creat an auction
* @param _owner who call createAuction() in AuctionBox contract
* @param _title the title of the auction
* @param _startPrice the start price of the auction
* @param _description the description of the auction
*/
constructor(
address payable _owner,
string memory _title,
uint _startPrice,
string memory _description
) public {
// initialize auction
owner = _owner;
title = _title;
startPrice = _startPrice;
description = _description;
auctionState = State.Running;
}
modifier notOwner(){
require(msg.sender != owner);
_;
}
/** @dev Function to place a bid
* @return true
*/
function placeBid() public payable notOwner returns(bool) {
require(auctionState == State.Running);
require(msg.value > 0);
// update the current bid
// uint currentBid = bids[msg.sender] + msg.value;
uint currentBid = bids[msg.sender].add(msg.value);
require(currentBid > highestPrice);
// set the currentBid links with msg.sender
bids[msg.sender] = currentBid;
// update the highest price
highestPrice = currentBid;
highestBidder = msg.sender;
return true;
}
function finalizeAuction() public{
//the owner and bidders can finalize the auction.
require(msg.sender == owner || bids[msg.sender] > 0);
address payable recipiant;
uint value;
// owner can get highestPrice
if(msg.sender == owner){
recipiant = owner;
value = highestPrice;
}
// highestBidder can get no money
else if (msg.sender == highestBidder){
recipiant = highestBidder;
value = 0;
}
// Other bidders can get back the money
else {
recipiant = msg.sender;
value = bids[msg.sender];
}
// initialize the value
bids[msg.sender] = 0;
recipiant.transfer(value);
auctionState = State.Finalized;
}
/** @dev Function to return the contents od the auction
* @return the title of the auction
* @return the start price of the auction
* @return the description of the auction
* @return the state of the auction
*/
function returnContents() public view returns(
string memory,
uint,
string memory,
State
) {
return (
title,
startPrice,
description,
auctionState
);
}
}