-
Notifications
You must be signed in to change notification settings - Fork 0
/
neglect.sol
160 lines (114 loc) · 5.15 KB
/
neglect.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
import '@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol';
import '@openzeppelin/contracts/security/ReentrancyGuard.sol';
contract NFTAuction is ERC721URIStorage, ReentrancyGuard {
uint256 public tokenCounter;
uint256 public listingCounter;
uint8 public constant STATUS_OPEN = 1;
uint8 public constant STATUS_DONE = 2;
uint256 public minAuctionIncrement = 10; // 10 percent
struct Listing {
address seller;
uint256 tokenId;
uint256 price; // display price
uint256 netPrice; // actual price
uint256 startAt;
uint256 endAt;
uint8 status;
}
event Minted(address indexed minter, uint256 nftID, string uri);
event AuctionCreated(uint256 listingId, address indexed seller, uint256 price, uint256 tokenId, uint256 startAt, uint256 endAt);
event BidCreated(uint256 listingId, address indexed bidder, uint256 bid);
event AuctionCompleted(uint256 listingId, address indexed seller, address indexed bidder, uint256 bid);
event WithdrawBid(uint256 listingId, address indexed bidder, uint256 bid);
mapping(uint256 => Listing) public listings;
mapping(uint256 => mapping(address => uint256)) public bids;
mapping(uint256 => address) public highestBidder;
constructor() ERC721("AmazonNFT", "ANFT") {
tokenCounter = 0;
listingCounter = 0;
}
function mint(string memory tokenURI, address minterAddress) public returns (uint256) {
tokenCounter++;
uint256 tokenId = tokenCounter;
_safeMint(minterAddress, tokenId);
_setTokenURI(tokenId, tokenURI);
emit Minted(minterAddress, tokenId, tokenURI);
return tokenId;
}
function createAuctionListing (uint256 price, uint256 tokenId, uint256 durationInSeconds) public returns (uint256) {
listingCounter++;
uint256 listingId = listingCounter;
uint256 startAt = block.timestamp;
uint256 endAt = startAt + durationInSeconds;
listings[listingId] = Listing({
seller: msg.sender,
tokenId: tokenId,
price: price,
netPrice: price,
status: STATUS_OPEN,
startAt: startAt,
endAt: endAt
});
_transfer(msg.sender, address(this), tokenId);
emit AuctionCreated(listingId, msg.sender, price, tokenId, startAt, endAt);
return listingId;
}
function bid(uint256 listingId) public payable nonReentrant {
require(isAuctionOpen(listingId), 'auction has ended');
Listing storage listing = listings[listingId];
require(msg.sender != listing.seller, "cannot bid on what you own");
uint256 newBid = bids[listingId][msg.sender] + msg.value;
require(newBid >= listing.price, "cannot bid below the latest bidding price");
bids[listingId][msg.sender] += msg.value;
highestBidder[listingId] = msg.sender;
uint256 incentive = listing.price / minAuctionIncrement;
listing.price = listing.price + incentive;
emit BidCreated(listingId, msg.sender, newBid);
}
function completeAuction(uint256 listingId) public payable nonReentrant {
require(!isAuctionOpen(listingId), 'auction is still open');
Listing storage listing = listings[listingId];
address winner = highestBidder[listingId];
require(
msg.sender == listing.seller || msg.sender == winner,
'only seller or winner can complete auction'
);
if(winner != address(0)) {
_transfer(address(this), winner, listing.tokenId);
uint256 amount = bids[listingId][winner];
bids[listingId][winner] = 0;
_transferFund(payable(listing.seller), amount);
} else {
_transfer(address(this), listing.seller, listing.tokenId);
}
listing.status = STATUS_DONE;
emit AuctionCompleted(listingId, listing.seller, winner, bids[listingId][winner]);
}
function withdrawBid(uint256 listingId) public payable nonReentrant {
require(isAuctionExpired(listingId), 'auction must be ended');
require(highestBidder[listingId] != msg.sender, 'highest bidder cannot withdraw bid');
uint256 balance = bids[listingId][msg.sender];
bids[listingId][msg.sender] = 0;
_transferFund(payable(msg.sender), balance);
emit WithdrawBid(listingId, msg.sender, balance);
}
function isAuctionOpen(uint256 id) public view returns (bool) {
return
listings[id].status == STATUS_OPEN &&
listings[id].endAt > block.timestamp;
}
function isAuctionExpired(uint256 id) public view returns (bool) {
return listings[id].endAt <= block.timestamp;
}
function _transferFund(address payable to, uint256 amount) internal {
if (amount == 0) {
return;
}
require(to != address(0), 'Error, cannot transfer to address(0)');
(bool transferSent, ) = to.call{value: amount}("");
require(transferSent, "Error, failed to send Ether");
}
}
view rawauction.sol hosted with ❤ by GitHub