Skip to content

Instantly share code, notes, and snippets.

@mushketyk
Created February 14, 2023 17:35
Show Gist options
  • Save mushketyk/45ab00101c6235413acd0845af300389 to your computer and use it in GitHub Desktop.
Save mushketyk/45ab00101c6235413acd0845af300389 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.17+commit.8df45f5f.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "Ownable.sol";
error AmountTooSmall(uint totalBid, uint minRequired);
contract Auction {
enum AuctionState {
INITIALIZED,
ONGOING,
FINISHED
}
address public beneficiary;
uint public deadline;
Ownable public ownable;
AuctionState public state = AuctionState.INITIALIZED;
mapping(address => uint) public totalBids;
address public highestBidder;
uint public totalBidsNum;
modifier onlyState(AuctionState expectedState) {
require(state == expectedState, "Invalid state");
_;
}
modifier beforeDeadline {
require(block.timestamp <= deadline, "Deadline has passed");
_;
}
modifier afterDeadline {
require(block.timestamp > deadline, "Deadline has not yet passed");
_;
}
constructor(address ownableAddress) {
beneficiary = msg.sender;
ownable = Ownable(ownableAddress);
require(ownable.owner() == address(beneficiary), "Should be owned by the caller");
}
function startAuction(uint duration) public onlyState(AuctionState.INITIALIZED) {
require(ownable.isOwner(), "Auction should be the owner");
deadline = block.timestamp + duration;
state = AuctionState.ONGOING;
}
function placeBid() public onlyState(AuctionState.ONGOING) beforeDeadline payable {
uint totalBid = totalBids[msg.sender] + msg.value;
if (totalBid <= totalBids[highestBidder]) {
revert AmountTooSmall({
totalBid: totalBid,
minRequired: totalBids[highestBidder]
});
}
totalBidsNum += 1;
totalBids[msg.sender] = totalBid;
highestBidder = msg.sender;
}
function highestBid() public view returns(uint) {
return totalBids[highestBidder];
}
function currentTime() public view returns(uint) {
return block.timestamp;
}
function endAuction() public onlyState(AuctionState.ONGOING) afterDeadline {
state = AuctionState.FINISHED;
if (highestBidder == address(0x0)) {
ownable.transferOwnership(beneficiary);
} else {
uint highestBidAmount = totalBids[highestBidder];
totalBids[highestBidder] = 0;
ownable.transferOwnership(highestBidder);
payable(beneficiary).transfer(highestBidAmount);
}
}
// TODO: Implement this function
function withdrawBid() public onlyState(AuctionState.FINISHED) afterDeadline {
// TODO: Get the amount of funds contributed by the sender
// TODO: If the amount is zero throw an error
// TODO: Transfer contributed amount to user
// Don't forget to update the contract state!
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Ownable {
address public owner;
string public itemName;
modifier onlyOwner() {
require(msg.sender == owner, "Not an owner");
_;
}
constructor(string memory itemName_) {
owner = msg.sender;
itemName = itemName_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0x0), "Invalid owner address");
owner = newOwner;
}
function isOwner() public view returns(bool) {
return owner == msg.sender;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment