Functions

After defining the auction states, it's time to define the functions that handle the auction's functionalities. In Solidity, a function has the same meaning as in any other language, and is declared using the keyword function, as follows:

function function_name(<parameter types>) {internal|external|private|public} [pure|constant|view|payable] [returns (<return types>)]

Like variables, functions can be specified as being external, internal, private, or public, while the default visibility is public. As our first contract, auction, is an abstract contract, we only declare the functions without implementation. They will be implemented later, in a child contract. 

Along with the visibility specifier, we can define additional specifiers, such as the following:

  • Constant and view: These play the same role, and both indicate that the function should not modify the state. The constant keyword, however, is deprecated, and doesn't mean that the function returns a constant result.
  • Pure: This is more restrictive, and indicates that the function won't even read the storage state.
  • Payable: This is a keyword that specifies the ability of the function to receive ether.

In this contract, we define the following methods, in order to guarantee the basic auction operations:

  • bid(): Enables the participant to send their bids in ether, and accordingly, determines the highest bidder. The bidding in our contract is cumulative, which means that if the bidder is no longer the highest bidder, he can only add an additional amount in the next bid.
  • withdraw(): Enables participants to withdraw their bids once the auction is over.
  • cancel_auction(): Enables the auction owner to cancel the auction he started.
  • destruct_auction(): Destroys the auction contract in the blockchain.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset