Solidity Smart Contracts Walk-through Series Part-1

Solidity Smart Contracts Walk-through Series Part-1
The basic idea around this series is to understand the technical flow and use -case based concepts around smart contracts written in Solidity. In this series we will go through three different use cases : “Property auction”, “Renting out Property ” & “Business partnership”. For each use case we will first go through what functionality we should cover & then understand the technical implementation details. Code repository can be found here. Please keep in mind that this is not a fully featured product. These smart contracts have been tested locally using Ganache & are not part of any production/test network. They were developed for educational purpose only.
This is part 1 of Smart contract walk-through series. In the following post I will give you an in-depth overview over the technical implementation details of property auction smart contract.
Property Auction use case ::
Let’s suppose a user wants to sell his property in auction. To keep it simple will keep our scenario similar to auction (not a fully fledged auction). The base scenario will be that any user (except the property owner) can participate in the auction. Every participant will get a chance to bid & transfer their bidding amount. Contract will be storing the participant’s bidding amount. Anytime property owner wants; he can stop the auction by transferring property ownership to highest bidder & highest bidding amount to his own wallet address. Other participants can withdraw their respective bidding amount from contract anytime. The new owner won’t be able to start the auction again for given property unless all other users have withdrawn their previous bidding amount from contract.
Let’s start …
Here are our different data types to be used :
We are defining a Property struct which will store property name & other details. Since there can be large number of details, user can store the detail’s hash instead (prepare the details hash outside the contract). We have Status struct which will keep track of following :
- bidders: This will store the number of participants in auction.
- highestBid: The highest bid amount.
- highestBidder: Address of highest bidder.
- biddingCompleted: To check status if auction has been completed or not.
owner will store the property owner wallet address & isProspect is the mapping to store the bidding amount value against each participant.
Following events defined to confirm the various transactions:
Property details , contract status fields and the owner wallet address to be initialised at the time of contract deployment itself (when the constructor gets invoked). Modifiers to isolate various functionalities among owner & non-owners are also defined as shown below :
Coming to our bidding functionality :
Using nonOwner modifier this method has been restricted for Owner. It returns true if transaction is successful. Specified as payable since respective participants are going to send their bidding amount (which needs to be stored in the contract). We will allow the user to participate if he hasn’t earlier(or else we will revert back the transaction, require operator used here will helps us with same). We will set the isProspect mapping for user , increase the participants count (bidders++) & also set the highest bidder & bidding amount in case the user amount is more than other participants. reg event to be emitted with return value as true once the transaction is successful.
Note — Any function with payable option will allow the contract to store the amount of ether (received from user during function invocation) at it’s own address.
Only owner is allowed to invoke the bidResult. This method will transfer the property ownership to highest bidder.It will also transfers the highest bidding amount to the current owner address & mark the auction as completed. It will reset the isProspect mapping for highest bidder to 0 & decrements the number of participants(this will help us to track the correct number of participants remaining to withdraw their amount). Once transaction is successful it will emit result event & also return true boolean value.
Above method allow participants to withdraw their respective stored bidding amount. Anytime any user wishes to move out of auction or wants to get back his amount after auction completion,can invoke this method. This method will check the isProspect mapping for respective amount against user. It will then transfer the required amount to user’s wallet address. Participants count also gets decremented to reflect the actual participants count.Note that we decremented this count & also reset the isProspect mapping for highest bidder when auction result were declared, as we don’t want auction winner to claim his money.
Once the auction has been completed, new property owner may like to start the auction again. In that case we will ensure if all participants from previous auction round have withdrawn their respective amount & then only will allow the owner to start the auction again. Imposing this condition because we are resetting the critical parameters over here such as highestBid & highestBidder.
Once no participants are left , owner is allowed to destroy the auction contract & transfer the stored ether to his own address(if any).
Published at Thu, 10 Oct 2019 18:27:50 +0000
{flickr|100|campaign}
