Created
July 27, 2021 16:58
-
-
Save marcozecchini/00d8f468a790733786e2a0fb9b5a22fc 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.0+commit.c7dfd78e.js&optimize=true&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity 0.8.0; | |
/** | |
* @dev This is an example contract implementation of NFToken with metadata extension. | |
*/ | |
import "/contracts/MyPostiToken.sol"; | |
contract SimpleAuction { | |
MyPostiToken myToken; | |
uint public NFTid; | |
address payable public beneficiary; | |
uint public auctionEndTime; | |
address public highestBidder; | |
uint public highestBid; | |
// Allowed withdrawals of previous bids | |
mapping(address => uint) pendingReturns; | |
// Set to true at the end, disallows any change. | |
// By default initialized to `false`. | |
bool ended; | |
// Events that will be emitted on changes. | |
event HighestBidIncreased(address bidder, uint amount); | |
event AuctionEnded(address winner, uint amount); | |
constructor( | |
uint _biddingTime, | |
uint _NFTid, | |
address _tokenSC | |
) { | |
auctionEndTime = block.timestamp + _biddingTime; | |
NFTid = _NFTid; | |
myToken = MyPostiToken(_tokenSC); | |
beneficiary = payable (myToken.ownerOf(NFTid)); | |
} | |
function bid() public payable { | |
require( | |
block.timestamp <= auctionEndTime, | |
"Auction already ended." | |
); | |
require( | |
msg.value > highestBid, | |
"There already is a higher bid." | |
); | |
if (highestBid != 0) { | |
pendingReturns[highestBidder] += highestBid; | |
} | |
highestBidder = msg.sender; | |
highestBid = msg.value; | |
emit HighestBidIncreased(msg.sender, msg.value); | |
} | |
/// Withdraw a bid that was overbid. | |
function withdraw() public returns (bool) { | |
uint amount = pendingReturns[msg.sender]; | |
if (amount > 0) { | |
pendingReturns[msg.sender] = 0; | |
if (!payable(msg.sender).send(amount)) { | |
// No need to call throw here, just reset the amount owing | |
pendingReturns[msg.sender] = amount; | |
return false; | |
} | |
} | |
return true; | |
} | |
/// End the auction and send the highest bid | |
/// to the beneficiary. | |
function auctionEnd() public { | |
// 1. Conditions | |
require(block.timestamp >= auctionEndTime, "Auction not yet ended."); | |
require(!ended, "auctionEnd has already been called."); | |
// 2. Effects | |
ended = true; | |
emit AuctionEnded(highestBidder, highestBid); | |
// 3. Interaction | |
beneficiary.transfer(highestBid); | |
myToken.safeTransferFrom(beneficiary, highestBidder, NFTid); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment