Skip to content

Instantly share code, notes, and snippets.

@KcPele
Created April 12, 2022 08:15
Show Gist options
  • Save KcPele/cd6626060deb80e71cbbe1a7811f2559 to your computer and use it in GitHub Desktop.
Save KcPele/cd6626060deb80e71cbbe1a7811f2559 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
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 expireAt;
uint public immutable discountRate;
constructor(
uint _startingPrice,
uint _discountRate,
address _nft,
uint _nftId
) {
seller = payable(msg.sender);
startingPrice = _startingPrice;
discountRate = _discountRate;
startAt = block.timestamp;
expireAt = block.timestamp + DURATION;
require(_startingPrice >= _discountRate * DURATION, "Starting price is less than discount");
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 < expireAt, "aution expired");
uint price = getPrice();
require(msg.value >= price, "Eth less than price");
nft.transferFrom(seller, msg.sender, nftId);
uint refund = msg.value - price;
if(refund > 0){
payable(msg.sender).transfer(refund);
}
selfdestruct(seller);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment