Skip to content

Instantly share code, notes, and snippets.

@marcozecchini
Created July 27, 2021 16:58
Show Gist options
  • Save marcozecchini/7b9bbca7fc8a1d2aba8c5059080433b1 to your computer and use it in GitHub Desktop.
Save marcozecchini/7b9bbca7fc8a1d2aba8c5059080433b1 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=
// 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