-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auction.sol
126 lines (102 loc) · 4.23 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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "./openzeppelin/IERC1155.sol";
import "./openzeppelin/IERC20.sol";
import "./openzeppelin/Ownable.sol";
import "./openzeppelin/IERC1155Receiver.sol";
contract Auction is Ownable {
IERC20 public token;
IERC1155 public nft;
uint public fee = 1 ether; //fee for creating auction
uint public aucNumber = 1;
constructor(IERC20 _address) {
token = _address;
}
struct Auction {
address seller;
address nftAddr;
uint min;
uint max;
uint id;
uint amount;
uint time;
uint bestBid;
address bestBidAddr;
bool finished;
bool done;
}
mapping(uint => Auction) public auctions;
mapping(uint => mapping(address => bool)) public isVoted; // nubmer of vote -> voter -> isVoted
mapping(address => uint[]) public sellerAuctions;
mapping(address => mapping(uint => uint)) public balances; //who -> number of vote -> balance
function createAuction(address _nftAddr, uint id, uint amount, uint min, uint max, uint time) external payable {
require(msg.sender != address(0));
require(msg.value >= fee,"incorrect pay");
if(max == 0) {
max = 9999999999 ether;
}
require(min > 0 && max > min && time > 0,"incorrect settings");
nft = IERC1155(_nftAddr);
nft.safeTransferFrom(msg.sender,address(this),id,amount,"");
auctions[aucNumber] = Auction(msg.sender,_nftAddr,min,max,id, amount, block.timestamp+time, 0 , address(0), false, false);
sellerAuctions[msg.sender].push(aucNumber);
aucNumber++;
}
function vote(uint number,uint price) external payable {
if(block.timestamp > auctions[number].time && !auctions[number].finished) {
finishAuction(number);
}
require(msg.value >= price && msg.value >= auctions[number].min && msg.value >= auctions[number].bestBid,"incorrect pay");
require(block.timestamp < auctions[number].time);
require(!auctions[number].finished, "voting is finished");
if(price >= auctions[number].max) {
finishAuctionMax(number,msg.value);
auctions[number].bestBid = price;
auctions[number].bestBidAddr = msg.sender;
} else {
auctions[number].bestBid = price;
auctions[number].bestBidAddr = msg.sender;
}
balances[msg.sender][number] += price;
}
function finishAuction(uint number) internal {
auctions[number].finished = true;
nft = IERC1155(auctions[number].nftAddr);
nft.safeTransferFrom(address(this),auctions[number].bestBidAddr,auctions[number].id,auctions[number].amount,"");
payable(auctions[number].seller).transfer(auctions[number].bestBid);
}
function finishAuctionMax(uint number, uint price) internal {
auctions[number].finished = true;
nft = IERC1155(auctions[number].nftAddr);
nft.safeTransferFrom(address(this), msg.sender, auctions[number].id, auctions[number].amount, "");
payable(auctions[number].seller).transfer(price);
}
function backNFT (uint number) external {
if(block.timestamp > auctions[number].time && !auctions[number].finished) {
auctions[number].finished = true;
}
require(auctions[number].seller == msg.sender,"not an owner");
require(!auctions[number].done ,"already claimed");
require(block.timestamp > auctions[number].time,"Voting is not finished");
if(auctions[number].bestBid > 0) {
auctions[number].done = true;
finishAuction(number);
} else {
auctions[number].done = true;
nft.safeTransferFrom(address(this),msg.sender,auctions[number].id,auctions[number].amount,"");
}
}
function changeFee(uint _newFee) external onlyOwner {
require(_newFee <= 1 ether,"incorrect fee");
fee = _newFee;
}
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
) external pure returns (bytes4) {
return bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"));
}
}