-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auction.sol
138 lines (110 loc) · 3.76 KB
/
Auction.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
// use overflow and underflow
// use psuedo safemath library
// use commit/reveal system
// blind auction
// commit 후 일정시간 이후에 reveal 방식
// reveal시에 가장 높은 액수
pragma solidity ^0.5.0;
/**
* @title SafeMath
* @dev Unsigned math operations with safety checks that revert on error.
*/
library SafeMath {
/**
* @dev Multiplies two unsigned integers, reverts on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Integer division of two unsigned integers truncating the quotient, reverts on division by zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b > 0, "SafeMath: division by zero");
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Subtracts two unsigned integers, reverts on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
uint256 c = a - b;
return c;
}
/**
* @dev Adds two unsigned integers, reverts on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(b != a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Divides two unsigned integers and returns the remainder (unsigned integer modulo),
* reverts when dividing by zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, "SafeMath: modulo by zero");
return a % b;
}
}
contract Auction {
using SafeMath for uint;
uint public endTime;
uint public highestBid;
address public winner;
mapping(address => bytes32) public bid;
mapping(address => bool) public revealed;
constructor() public payable {
endTime = now + 30 minutes;
}
function commit(bytes32 _bid, uint _time) public returns (bool) {
require(now < endTime);
require(!revealed[msg.sender]);
bid[msg.sender] = _bid;
endTime = endTime.add(_time);
return true;
}
function reveal(uint _value) public returns (bool) {
require(keccak256(toBytes(_value)) == bid[msg.sender], "invalid value");
highestBid = _value;
winner = msg.sender;
return true;
}
function claim() public payable returns (bool) {
require(now > endTime);
require(msg.sender == winner);
require(msg.value == highestBid);
msg.sender.transfer(address(this).balance);
return true;
}
function getHash(bytes memory _value, uint _value1) public pure returns (bytes32, bytes memory){
return (keccak256(_value), toBytes(_value1));
}
function() payable external {
}
function getHighestBid() public view returns (uint) {
return highestBid;
}
function getWinner() public view returns (address) {
return winner;
}
function getEndTime() public view returns (uint) {
return endTime;
}
function toBytes(uint256 x) public pure returns (bytes memory b) {
b = new bytes(32);
assembly {mstore(add(b, 32), x)}
}
}