ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Auction Algorithm: English Auction with Timed Bidding

Solidity

Goal -- WPM

Ready
Exercise Algorithm Area
1pragma solidity ^0.8.0;
2
3contract EnglishAuction {
4address public seller;
5uint256 public startTime;
6uint256 public endTime;
7uint256 public minBidIncrement;
8uint256 public highestBid;
9address public highestBidder;
10bool public auctionEnded;
11
12event BidPlaced(address indexed bidder, uint256 amount);
13event AuctionEnded(address indexed winner, uint256 amount);
14
15constructor(uint256 _startTime, uint256 _endTime, uint256 _minBidIncrement) {
16require(_startTime < _endTime, "Start time must be before end time");
17require(_minBidIncrement > 0, "Minimum bid increment must be positive");
18
19seller = msg.sender;
20startTime = _startTime;
21endTime = _endTime;
22minBidIncrement = _minBidIncrement;
23highestBid = 0;
24highestBidder = address(0);
25auctionEnded = false;
26}
27
28modifier onlyDuringAuction() {
29require(block.timestamp >= startTime, "Auction has not started yet");
30require(block.timestamp <= endTime, "Auction has ended");
31_;
32}
33
34modifier onlyAfterAuction() {
35require(block.timestamp > endTime, "Auction is still ongoing");
36_;
37}
38
39function placeBid() public payable onlyDuringAuction {
40// --- Checks ---
41uint256 currentHighestBid = highestBid;
42uint256 bidAmount = msg.value;
43
44// Check if it's the first bid
45if (highestBidder == address(0)) {
46// First bid must be at least minBidIncrement (or more)
47require(bidAmount >= minBidIncrement, "First bid must be at least the minimum bid increment");
48} else {
49// Subsequent bids must be higher than current highest bid + increment
50require(bidAmount > currentHighestBid + minBidIncrement, "Bid must be higher than current highest bid plus increment");
51}
52
53// --- Effects ---
54// Refund previous highest bidder if any
55if (highestBidder != address(0)) {
56payable(highestBidder).transfer(currentHighestBid);
57}
58
59highestBid = bidAmount;
60highestBidder = msg.sender;
61
62// --- Interactions ---
63emit BidPlaced(msg.sender, bidAmount);
64}
65
66function endAuction() public onlyAfterAuction {
67require(!auctionEnded, "Auction has already ended");
68
69// --- Effects ---
70auctionEnded = true;
71
72// --- Interactions ---
73emit AuctionEnded(highestBidder, highestBid);
74
75// Transfer funds from highest bidder to seller (this is a simplified interaction)
76// In a real scenario, this might involve a separate payment or escrow mechanism.
77if (highestBidder != address(0)) {
78payable(seller).transfer(highestBid);
79}
80}
81
82function getAuctionState() public view returns (address, uint256, uint256, uint256, address, bool) {
83return (seller, startTime, endTime, minBidIncrement, highestBidder, auctionEnded);
84}
85}
Algorithm description viewbox

Auction Algorithm: English Auction with Timed Bidding

Algorithm description:

This contract implements a timed English auction. It allows a seller to list an item for auction, setting a start time, end time, and a minimum bid increment. Bidders can place Ether bids during the auction. The highest bidder at the end of the auction wins, and their bid amount is transferred to the seller. This is a common mechanism for online auctions.

Algorithm explanation:

The `EnglishAuction` contract manages a timed auction. The `constructor` sets up the auction parameters: `seller`, `startTime`, `endTime`, and `minBidIncrement`. Bids can be placed using the `placeBid` function, which is payable and restricted to the auction period. Bids must be higher than the current `highestBid` plus the `minBidIncrement`. If a new bid is placed, the previous `highestBidder` is refunded their bid amount. The `highestBid` and `highestBidder` are updated. The `endAuction` function, callable only after the `endTime`, marks the auction as ended and emits an event. In this simplified version, it also transfers the final `highestBid` to the `seller`. Time complexity for `placeBid` is O(1) on average, but can be O(N) if refunds are complex (here, it's O(1)). `endAuction` is O(1). Space complexity is O(1) as it stores a fixed number of state variables. Edge cases include invalid time ranges, zero bid increments, placing bids before the auction starts or after it ends, insufficient bid amounts, and no bids placed. The invariant is that `highestBid` is always greater than or equal to `minBidIncrement` (after the first bid) and `highestBidder` is the address that placed the `highestBid`.

Pseudocode:

Contract EnglishAuction:
  State:
    seller: address
    startTime: uint256
    endTime: uint256
    minBidIncrement: uint256
    highestBid: uint256
    highestBidder: address
    auctionEnded: bool

  Constructor(_startTime, _endTime, _minBidIncrement):
    Require _startTime < _endTime
    Require _minBidIncrement > 0
    Set seller to msg.sender
    Set startTime, endTime, minBidIncrement
    Initialize highestBid to 0, highestBidder to zero address, auctionEnded to false

  Modifier onlyDuringAuction():
    Require current block timestamp >= startTime
    Require current block timestamp <= endTime
    Execute function body

  Modifier onlyAfterAuction():
    Require current block timestamp > endTime
    Execute function body

  Function placeBid():
    Apply onlyDuringAuction modifier
    Let currentHighestBid = highestBid
    Let bidAmount = msg.value
    If highestBidder is zero address:
      Require bidAmount >= minBidIncrement
    Else:
      Require bidAmount > currentHighestBid + minBidIncrement
    If highestBidder is not zero address:
      Refund currentHighestBid to highestBidder
    Set highestBid to bidAmount
    Set highestBidder to msg.sender
    Emit BidPlaced event

  Function endAuction():
    Apply onlyAfterAuction modifier
    Require auctionEnded is false
    Set auctionEnded to true
    Emit AuctionEnded event
    If highestBidder is not zero address:
      Transfer highestBid to seller

  Function getAuctionState():
    Return seller, startTime, endTime, minBidIncrement, highestBidder, auctionEnded