Blockchain contracts are gradually becoming the preferred contract for business dealings and transactions. Developed on blockchain infrastructure, smart contracts are self-enforceable contracts that automatically execute a transaction once certain specified conditions have been met. Added to the automatic and super-fast execution, another key benefit of blockchain contracts is that they are immutable and cannot be manipulated. Check out more about Multibank.io.

Blockchain contracts apply to almost all kinds of industries and business dealings. A major area where you can deploy smart contracts is an auction. The post below offers a brief on how to code bidding smart contract on the blockchain.

Steps to follow for coding bidding smart contracts on a blockchain platform

Before getting straight into the programming part, let’s create a background.

Let’s say, the auction will be for a luxury sports car. There are two sets of parties here- the owner of the car and the bidders. The owner of the car will be the auction owner here and s/he will be the one to deploy the bidding contract. Auction could be started as soon as the contract has been deployed. After the completion of the bidding period, the one to bid the highest will receive the vehicle while other participants will withdraw the bids.

Choosing Solidity for writing

Since Ethereum is the prime hub of blockchain contracts, we will build our bidding smart contract on the same blockchain. Ethereum uses Solidity language for coding blockchain contracts and we will use the same for our smart contract as well.

Since it’s your first contract, we will keep the design simple. First, let’s focus on creating abstract contracts that will feature elementary functions as well as events. After that, we will create a compliant implementation by using inheritance. So, the first thing to do here is to create a file, say Auction.sol. It will be pasted in abstract contract code.

Mention variables

In this stage, we will mention the different variables pertaining to the auction contract.

  • auction_owner: Address variable that will store vehicle owner-auction owner. It will also be the address for winning the bid.
  • auction_start and auction_end: The start and completion times of your auction.
  • highestBid: Highest amount of bid received at the auction in ETH
  • highestBidder: Highest or winning bidder’s Ethereum address
  • bidders: addresses of all bidders
  • Bids: Mapping that matches bidders’ addresses with their respective bids
  • STATE: The state of the auction

Variable types

Our contract will have 2 variables- address and unit256.

Access and visibility modifier

Each mentioned variable will come after visibility specifier (internal or public) that demonstrates the scope of accessibility. Public state variables will be visible both externally and internally. The internal one will be accessible solely within present contract.

Enumeration

The enumeration will represent the status of auction- Started or Cancelled.

The enumeration in the contract will be defined by the “enum” keyword.

Array, Mapping, Structures

For our contract, we will opt for a dynamic array that will contract all bidder addresses. It will be represented as “T[]”.

Mapping is demonstrated as a variable which is defined through mapping keywords. So, Mapping implies KeyType And ValueType. For our bidding contract, keytype mapping will accept bidders’ addresses. On the other hand, valuetype mapping will represent a corresponding bid.

Structure refers to an ensemble of different variables of various data types, falling under one single name. In this case, the struct will represent the vehicle and define the registration number and brand of the car.

Define auction functionalities

At this stage, you will define the basic operations of the auction-

  • bid()- allows bidders to send bids as well as locate highest bidder
  • withdraw()- allows bidders to withdraw bids after the completion of the auction
  • cancel_auction(): allows auction owner (you) to cancel auction once it get completed
  • destruct_auction(): as the function says, destroy the contract

Modifier

There will be two kinds of function modifiers for this contract. One will check whether or not the vehicle auction is open. The other will restrict authorization for executing functions to the owner of the contract (the car owner).

Condition

The following conditions will define certain course of action for the contract-

assert or boot condition- this will be used to mark internal errors

require or bool condition: this will be sued to mark errors in external or input components

revert(): this will stop execution as well as revert the state changes, and offer gas refund.

throw: this will throw as well as consume remaining gas.

Events

Events allow usage of EVM logging. In case of our contract, there will be two events-

  • BidEvent(): it will inform about new bid and also provide bid and address of new bidder
  • WithdrawalEvent(): it keeps record of withdrawal operations, offering withdrawal amount and address of the withdrawer

Constructor

Upon creation, the constructor will specify relevant states through demonstrating auction owner, end date and opening date of contract, as well as details of the vehicle

Setting the time

You can use the “now” keyword for setting the start time of the auction. The end time is incurred by adding a sum of hours (as per biddingTime argument) to start time of auction.

This “now” can be defined as an integer variable which will return a timestamp of the block (block.timestamp) where the contract will be embedded.

You can also use block numbers to manage auction duration.

Bidding function

This is perhaps the main stage of the bidding smart contract in-the-making. At this stage, we will embed a “bidding function” into the contract that will enable participants or bidders to place preferred bids in ETH.

Cancelling the event

The auction owner has to cancel the event aka auction after the bidding process is over and the winning bidder has already taken the new ownership of the prized car. So, after you embed the bidding function, the next step is to embed a function for cancelling the event. Only the owner of the auction will be able to execute the cancel function in the contract.

Bid withdrawal

This is the second last function to be embedded into the contract. This function will allow participants to withdraw their auction bids.

Contract destruction

This is the final function to be added to the contract. This will help the owner of the auction to remove the contract forever after the entire process has been completed.