Skip to content

Instantly share code, notes, and snippets.

@shobhitic
Last active November 15, 2022 01:36
Show Gist options
  • Save shobhitic/44f1d88c58b8c80d431e156654167d0d to your computer and use it in GitHub Desktop.
Save shobhitic/44f1d88c58b8c80d431e156654167d0d to your computer and use it in GitHub Desktop.
NFT sales through Dutch Auction - https://www.youtube.com/watch?v=iUuAdO7Lf5w
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/[email protected]/token/ERC721/ERC721.sol";
import "@openzeppelin/[email protected]/access/Ownable.sol";
import "@openzeppelin/[email protected]/utils/Counters.sol";
contract MyToken is ERC721, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIdCounter;
uint256 public immutable startPrice = 5 ether;
uint256 public immutable startAt;
uint256 public immutable endsAt;
uint256 public immutable endPrice = 2 ether;
uint256 public immutable discountRate = 0.01 ether;
uint256 public duration = 300 hours;
uint256 public immutable MAX_SUPPLY = 10000;
constructor() ERC721("MyToken", "MTK") {
startAt = block.timestamp;
endsAt = block.timestamp + duration;
}
function price() public view returns (uint256) {
if (endsAt < block.timestamp) {
return endPrice;
}
uint256 minutesElapsed = (block.timestamp - startAt) / 3600;
return startPrice - (minutesElapsed * discountRate);
}
function safeMint(uint256 amount) public payable {
require(msg.value >= (amount * price()), "Not enough ether sent");
uint256 tokenId = _tokenIdCounter.current();
require(tokenId < MAX_SUPPLY, "No more items left.");
_tokenIdCounter.increment();
_safeMint(to, tokenId + 1);
}
function withdraw() public onlyOwner {
payable(owner()).transfer(address(this).balance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment