Skip to content

Instantly share code, notes, and snippets.

@vis-kid
Created March 12, 2022 01:20
Show Gist options
  • Save vis-kid/dccfa76e3f3b2f7d2cc39c16f309f41c to your computer and use it in GitHub Desktop.
Save vis-kid/dccfa76e3f3b2f7d2cc39c16f309f41c 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=
pragma solidity >=0.7.0 <0.9.0;
contract SimpleAuction {
address payable public beneficiary;
uint public auctionEndTime;
address public highestBidder;
uint public highestBid;
mapping(address => uint) public pendingReturns;
bool ended = false;
event HighestBidIncrease(address highestBidder, uint amount);
event AuctionEnded(address winner, uint amount);
constructor(uint _biddingTime, address payable _beneficiary) {
beneficiary = _beneficiary;
auctionEndTime = block.timestamp + _biddingTime;
}
function bid() external payable {
if(block.timestamp > auctionEndTime) {
revert("The auction has already ended!");
}
if(msg.value <= highestBid) {
revert("There is a higher or equal bid already!");
}
if(highestBid != 0) {
pendingReturns[highestBidder] += highestBid;
}
highestBidder = msg.sender;
highestBid = msg.value;
emit HighestBidIncrease(msg.sender, msg.value);
}
function withdraw() external returns(bool) {
uint amount = pendingReturns[msg.sender];
if(amount > 0) {
pendingReturns[msg.sender] = 0;
if(!payable(msg.sender).send(amount)) {
pendingReturns[msg.sender] = amount;
return false;
}
}
return true;
}
function endAuction() external {
if(block.timestamp < auctionEndTime) {
revert("The auction has not yet ended!");
}
if(ended) {
revert("The auction has already ended!");
}
ended = true;
beneficiary.transfer(highestBid);
emit AuctionEnded(highestBidder, highestBid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment