-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiddingContract.sol
76 lines (59 loc) · 1.63 KB
/
BiddingContract.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
interface IKIP17 {
function transferFrom(
address _from,
address _to,
uint256 _tokenId
) external;
}
contract NftAuction {
uint256 private constant DURATION = 7 days;
IKIP17 public nft;
uint256 public nftId;
address payable public seller;
uint256 public startingPrice;
uint256 public discountRate;
uint256 public startAt;
uint256 public expiresAt;
constructor(
uint256 _startingPrice,
uint256 _discountRate,
address _nft,
uint256 _nftId
) {
seller = payable(msg.sender);
startingPrice = _startingPrice;
discountRate = _discountRate;
startAt = block.timestamp;
expiresAt = block.timestamp + DURATION;
require(
_startingPrice >= _discountRate + DURATION,
"Starting price is too low"
);
nft = IKIP17(_nft);
nftId = _nftId;
}
function getPrice() public view returns (uint256) {
uint256 timeElapsed = block.timestamp - startAt;
uint256 discount = discountRate * timeElapsed;
return startingPrice - discount;
}
function buy() external payable {
require(
block.timestamp < expiresAt,
"This NFT bidding has ended"
);
uint256 price = getPrice();
require(
msg.value >= price,
"The amount of KLAY sent is less than the price of the token"
);
nft.transferFrom(seller, msg.sender, nftId);
uint256 refund = msg.value - price;
if(refund > 0){
msg.sender.transfer(refund);
}
selfdestruct(seller);
}
}