-
Notifications
You must be signed in to change notification settings - Fork 36
/
Auction.sol
76 lines (65 loc) · 2.67 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
// SPDX-License-Identifier: unlicensed
pragma solidity ^0.8.0;
import "../tokens/MockERC721.sol";
contract Auction {
MockERC721 public nftContract;
mapping(uint256 => address) public owner;
mapping(uint256 => uint256) public period;
uint256 private _lockStatus = 1;
uint256 constant minBidAmount = 1 ether;
struct Bid {
address bidder;
uint256 bid;
bool status;
}
mapping(uint256 => Bid) public bidInfo;
event NFTContractCreated(address nft);
event ListedId(uint256 id, address owner);
event TransferId(uint256 id, address from, address to);
event BidId(uint256 id, address bidder, uint256 bidAmount);
constructor() {
MockERC721 _nftContract = new MockERC721();
nftContract = _nftContract;
emit NFTContractCreated(address(_nftContract));
}
modifier lock() {
require(_lockStatus != 2, "Auction: reentrant call");
_lockStatus = 2;
_;
_lockStatus = 1;
}
function list() external payable lock {
require(msg.value == minBidAmount, "Auction: requires minBidAmount");
uint256 id = nftContract.mint();
owner[id] = msg.sender;
period[id] = block.timestamp + 7 days;
emit ListedId(id, msg.sender);
}
function bid(uint256 _id) external payable {
require(owner[_id] != address(0), "Auction: not exist");
require(owner[_id] != msg.sender, "Auction: owner not allowed to bid");
require(period[_id] >= block.timestamp, "Auction: period over");
require(msg.value > minBidAmount, "Auction: bid should be higher than minBidAmount");
if (bidInfo[_id].status == true) {
if (bidInfo[_id].bid < msg.value) {
payable(bidInfo[_id].bidder).transfer(bidInfo[_id].bid);
bidInfo[_id] = Bid({bidder: msg.sender, bid: msg.value, status: true});
} else {
revert("Auction: last bidder amount is greater");
}
} else {
bidInfo[_id] = Bid({bidder: msg.sender, bid: msg.value, status: true});
}
emit BidId(_id, msg.sender, msg.value);
}
function collect(uint256 _id) external lock {
require(owner[_id] != address(0), "Auction: not exist");
require(period[_id] <= block.timestamp, "Auction: period is not over");
require(bidInfo[_id].bidder == msg.sender, "Auction: only last bidder can collect");
payable(owner[_id]).transfer(bidInfo[_id].bid);
nftContract.safeTransferFrom(address(this), bidInfo[_id].bidder, _id);
address currentOwner = owner[_id];
owner[_id] = address(0);
emit TransferId(_id, currentOwner, msg.sender);
}
}