荷兰式拍卖
NFT 荷兰式拍卖
拍卖
- NFT 的卖方部署此合约,为 NFT 设定起始价格。
- 拍卖会持续7天。
- NFT 的价格随着时间的推移而下降。
- 参与者可以通过存入高于智能合约计算的当前价格的 ETH 来购买。
- 当买家购买 NFT 时,拍卖结束。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IERC721 {
function transferFrom(address _from, address _to, uint _nftId) external;
}
contract DutchAuction {
uint private constant DURATION = 7 days;
IERC721 public immutable nft;
uint public immutable nftId;
address payable public immutable seller;
uint public immutable startingPrice;
uint public immutable startAt;
uint public immutable expiresAt;
uint public immutable discountRate;
constructor(uint _startingPrice, uint _discountRate, address _nft, uint _nftId) {
seller = payable(msg.sender);
startingPrice = _startingPrice;
startAt = block.timestamp;
expiresAt = block.timestamp + DURATION;
discountRate = _discountRate;
require(_startingPrice >= _discountRate * DURATION, "starting price < min");
nft = IERC721(_nft);
nftId = _nftId;
}
function getPrice() public view returns (uint) {
uint timeElapsed = block.timestamp - startAt;
uint discount = discountRate * timeElapsed;
return startingPrice - discount;
}
function buy() external payable {
require(block.timestamp < expiresAt, "auction expired");
uint price = getPrice();
require(msg.value >= price, "ETH < price");
nft.transferFrom(seller, msg.sender, nftId);
uint refund = msg.value - price;
if (refund > 0) {
payable(msg.sender).transfer(refund);
}
selfdestruct(seller);
}
}