#100DaysOfSolidity #061 English Auction: A Comprehensive Guide to Implementing it in Solidity

Solidity Academy
7 min readAug 6, 2023

🎉 Welcome to this comprehensive guide on implementing the English Auction in Solidity! In this article, we’ll explore the English Auction mechanism, its key components, and the step-by-step process of building it on the Ethereum blockchain using Solidity programming language. So, fasten your seatbelts as we dive into the exciting world of decentralized auctions! 🚀

#100DaysOfSolidity #061 English Auction: A Comprehensive Guide to Implementing it in Solidity

What is an English Auction?

The English Auction, also known as the open ascending price auction, is one of the most popular auction formats. It’s widely used in both traditional and online auctions. In this type of auction, the auctioneer starts with a low initial price, and potential buyers compete by successively increasing their bids until no higher bids are offered. The auction continues until a final bid stands unchallenged for a predetermined period, and the item is sold to the highest bidder at that final price.

🔎 So, how does this mechanism work? Let’s break it down step by step:

1. Auction Setup: The auctioneer announces the item to be sold, the starting bid (minimum bid amount), and the bidding duration (time window within which bidders can place their bids).

2. Bidding Process: Bidders submit their bids, and each new bid must be higher than the previous one. The auctioneer continuously updates the current highest bid and the bidder associated with it.

3. End of Auction: The auction ends when the bidding duration expires. At this point, the highest bid becomes the winning bid, and the item is sold to the highest bidder at that final price.

The English Auction is a thrilling and transparent mechanism, and implementing it as a decentralized application (DApp) on the Ethereum blockchain allows for a trustless and censorship-resistant auction environment.

Smart Contract Design

🛠️ Now, let’s move on to the technical part and design our Solidity smart contract for the English Auction. We’ll be using Solidity version 0.8.0 for this implementation.

Contract Initialization

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract EnglishAuction {
address public auctioneer;
address public highestBidder;
uint256 public highestBid;
uint256 public biddingEndTime;
bool public auctionEnded;
// Constructor to set the auctioneer and bidding duration
constructor(uint256 _biddingDuration) {
auctioneer = msg.sender;
biddingEndTime = block.timestamp + _biddingDuration;
}
// Rest of the contract implementation will be added later
}

In the above contract, we’ve defined some essential state variables to keep track of the auction status, highest bid, highest bidder, and auction end time. We also set the auctioneer as the contract deployer and initialize the bidding end time based on the provided `_biddingDuration`.

Placing Bids

Next, we’ll implement the function for placing bids. Bidders can call this function and submit their bids. The bid should be higher than the current highest bid.

contract EnglishAuction {
// … (previously defined state variables)
function placeBid() public payable {
require(!auctionEnded, "Auction has ended");
require(block.timestamp < biddingEndTime, "Bidding has ended");
require(msg.value > highestBid, "Bid amount is not higher than the current highest bid");
if (highestBid != 0) {
// Refund the previous highest bidder
payable(highestBidder).transfer(highestBid);
}
highestBidder = msg.sender;
highestBid = msg.value;
}
// … (other functions will be added later)
}

The `placeBid` function ensures that the auction is still ongoing, the bidding period has not expired, and the submitted bid is higher than the current highest bid. If a new bid is higher than the current highest bid, the previous highest bidder is refunded, and the new bidder becomes the highest bidder.

Auction End

We need a mechanism to finalize the auction once the bidding period ends. We’ll add the following function to do so:

contract EnglishAuction {
// … (previously defined state variables and functions)
function endAuction() public {
require(!auctionEnded, "Auction has already ended");
require(block.timestamp >= biddingEndTime, "Bidding period has not ended");
auctionEnded = true;
// Transfer the item to the highest bidder
// (You can implement your own logic for item transfer)
// Transfer the funds to the auctioneer after deducting any fees
uint256 auctioneerFee = highestBid * 1 / 100; // 1% fee
payable(auctioneer).transfer(highestBid - auctioneerFee);
}
}

The `endAuction` function checks if the bidding period has ended and the auction has not already ended. It then marks the auction as ended and performs the necessary fund transfers, including the auctioneer’s fee.

Additional Features

To make our English Auction DApp more versatile and engaging, we can add more features like bid withdrawal, bid increment, and event logging. Implementing these features is left as an exercise to the reader for an enhanced DApp experience! 😉

English Auction for NFT — Smart Contract Report

English Auction for NFT — Smart Contract Report

This report provides an overview and analysis of the “EnglishAuction” smart contract, which enables the auction of an NFT (Non-Fungible Token) on the Ethereum blockchain. The contract follows the English auction format, where participants can bid by depositing ETH (Ether) greater than the current highest bidder. The highest bidder at the end of the auction becomes the new owner of the NFT, and the seller receives the highest bid in ETH.

Contract Overview

The “EnglishAuction” contract is written in Solidity, version 0.8.17. It uses an interface called “IERC721” to interact with the NFT. The contract’s key features and functionalities include:

1. Initialization: The contract constructor takes three parameters — the address of the NFT contract, the ID of the NFT being auctioned, and the starting bid amount. The seller of the NFT deploys this contract, and the highest bid is initialized with the starting bid amount.

2. Start Function: The auction starts when the seller calls the “start” function. This function transfers the NFT to the contract, sets the start timestamp of the auction, and emits the “Start” event.

3. Bid Function: Participants can bid on the NFT by calling the “bid” function and depositing ETH. Bidding is only allowed when the auction is ongoing, and the bid amount must be higher than the current highest bid. The function updates the highest bid and highest bidder accordingly and emits the “Bid” event.

4. Withdraw Function: All bidders, except the highest bidder, can withdraw their bids by calling the “withdraw” function. The function refunds the withdrawn bid amount to the respective bidder and sets their bid to zero. The “Withdraw” event is emitted upon successful withdrawal.

5. End Function: The auction ends when the auction duration is over. The “end” function can be called by the seller to finalize the auction. If there is a highest bidder, the NFT is transferred to them, and the highest bid amount is sent to the seller. If there are no bids, the NFT is returned to the seller.

Events

The contract emits four events:

1. Start: Emitted when the auction is started by the seller.
2. Bid: Emitted when a participant places a bid.
3. Withdraw: Emitted when a participant withdraws their bid.
4. End: Emitted when the auction is finalized.

Function Modifiers

The contract uses two function modifiers:

1. onlySeller: Restricts access to functions that can only be called by the seller.
2. onlyStarted: Restricts access to functions that can only be called when the auction has started.

Contract Flow

1. The seller deploys the contract and initializes it with the NFT contract address, NFT ID, and the starting bid amount.
2. The seller starts the auction using the “start” function, which transfers the NFT to the contract and sets the start timestamp.
3. Participants bid on the NFT by calling the “bid” function and depositing ETH greater than the current highest bid.
4. Bidders can withdraw their bids using the “withdraw” function if they are not the current highest bidder.
5. After the auction duration ends, the seller finalizes the auction using the “end” function.
6. If there is a highest bidder, the NFT is transferred to them, and the highest bid amount is sent to the seller. If there are no bids, the NFT is returned to the seller.

In Conclusion; the “EnglishAuction” smart contract provides a secure and decentralized way to conduct an English auction for an NFT on the Ethereum blockchain. It ensures fair bidding and transparent allocation of the NFT to the highest bidder. The contract’s code is concise and well-organized, making it easy to understand and modify if required. However, as with any smart contract, it is essential to conduct thorough testing and security audits before deploying it on the mainnet to ensure the safety of users’ funds and NFT assets.

The “EnglishAuction” contract opens up exciting possibilities for NFT auctions, and its flexibility allows for potential customization and enhancements based on specific use cases. As the NFT space continues to evolve, smart contracts like this one play a crucial role in facilitating trustless and efficient transactions for digital assets.

Note: This report provides an overview of the contract’s functionality. For in-depth analysis and testing, further review and auditing are recommended.

Conclusion

🏁 Congratulations! You’ve successfully learned about the English Auction and implemented it as a Solidity smart contract on the Ethereum blockchain. Through this technical guide, you explored the core principles behind the English Auction, the key components of a Solidity smart contract, and the steps to build an auction DApp.

🔍 Remember that blockchain and smart contract development is a rapidly evolving field, so keep exploring, learning, and experimenting with new ideas and concepts. Happy coding! 🎉

📝 In this article, we explored the English Auction mechanism and built a Solidity smart contract to conduct decentralized auctions on the Ethereum blockchain. We covered the auction setup, bidding process, and the end of the auction. Additionally, we provided a basic outline of the smart contract code and encouraged readers to add more features to make the DApp more interactive and user-friendly.

🙌 Thank you for joining this journey of learning and coding! If you found this article useful or have any questions, feel free to leave your feedback in the comments. Stay tuned for more exciting tech articles! 🚀

🎉 Congratulations on completing the English Auction article! 🎉

--

--

Solidity Academy

Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/ SEND US Your Products to Review! solidity101@gmail.com