Created
February 10, 2022 01:58
-
-
Save amazingandyyy/19c0bec8eba251c9d62b1517188513db to your computer and use it in GitHub Desktop.
This file contains hidden or 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.7.0 <0.9.0; | |
contract Auction { | |
address payable public beneficiary; // the highest auctioneer | |
address public winner; | |
uint public endingTimestamp; | |
uint public highestBid; | |
mapping (address => uint) public PendingWithdrawal; | |
event NewHighestBid(address bidder, uint price); | |
event AuctionEnds(address winner, uint finalPrice); | |
// constructor(address payable _beneficiary, uint _seconds) { | |
// beneficiary = _beneficiary; | |
// endingTimestamp = block.timestamp + _seconds; // in seconds | |
// } | |
constructor() { | |
beneficiary = msg.sender; | |
endingTimestamp = block.timestamp + 30; // in seconds | |
} | |
function addFunds(address bidder, uint price) private { | |
PendingWithdrawal[bidder] += price; | |
} | |
function checkFunds(address bidder) private view returns(uint price) { | |
require(PendingWithdrawal[bidder] > 0, "Fund already withdrawed."); | |
return PendingWithdrawal[bidder]; | |
} | |
function resetFunds(address bidder) private { | |
PendingWithdrawal[bidder] = 0; | |
} | |
modifier isNotEnding() { | |
require(block.timestamp <= endingTimestamp, "It's ending."); | |
_; | |
} | |
modifier isEnding() { | |
require(block.timestamp > endingTimestamp, "It's not ending."); | |
_; | |
} | |
function bid() public isNotEnding payable { | |
require(msg.value > highestBid, "bid is not higher than current highestBid"); | |
highestBid = msg.value; | |
winner = msg.sender; | |
emit NewHighestBid(msg.sender, msg.value); | |
addFunds(msg.sender, msg.value); | |
} | |
function withdraw() isEnding public { | |
require(msg.sender != winner); | |
uint money = checkFunds(msg.sender); | |
resetFunds(msg.sender); | |
(bool sent,) = msg.sender.call{value: money}(""); | |
require(sent, "Failed to withdraw Ether"); | |
} | |
function end() isEnding public { | |
(bool sent,) = beneficiary.call{value: highestBid}(""); | |
resetFunds(winner); | |
require(sent, "Failed to withdraw Ether to beneficiary"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment