Auction contract in Solidity

To write our auction contract, we will use Solidity, which is the most popular language used to write smart contracts for the Ethereum blockchain. It's a JavaScript-like language, compiled into bytecode running in the Ethereum virtual machine. If you are familiar with object-oriented programming, learning to write Solidity contracts should be fairly straightforward. Through this auction example, I'll try to lay out the basic and important features of Solidity.

Ready, set, go.

Our contract design will be simple. In the first step, we will create an abstract contract, in which we will declare our elementary functions and events. Then, we will use inheritance to create a compliant implementation—a contract with the exact same functions implemented. Abstract contracts help us to decouple the definition of a contract from its implementation, providing better extensibility and code readability.

Start by creating a file called Auction.sol (the .sol extension refers to Solidity files), and paste in the code of our abstract contract, Auction:

pragma solidity ^0.4.24;

contract Auction {
address internal auction_owner;
uint256 public auction_start;
uint256 public auction_end;
uint256 public highestBid;
address public highestBidder;

enum auction_state {
CANCELLED, STARTED
}

struct car {
string Brand;
string Rnumber;
}

car public Mycar;
address[] bidders;
mapping(address => uint) public bids;
auction_state public STATE;

modifier an_ongoing_auction() {
require(now <= auction_end);
_;
}

modifier only_owner() {
require(msg.sender == auction_owner);
_;
}

function bid() public payable returns (bool) {}
function withdraw() public returns (bool) {}
function cancel_auction() external returns (bool) {}

event BidEvent(address indexed highestBidder, uint256 highestBid);
event WithdrawalEvent(address withdrawer, uint256 amount);
event CanceledEvent(string message, uint256 time);
}

I know this first contract is perhaps enigmatic for you, and maybe it's the first time you have seen a contract of such size (even if it's small). But, don't worry; we will use this first abstract contract as a playground for learning Solidity. In fact, my approach will be to dissect this code line by line and section by section, in order to introduce you to the different major Solidity features.

..................Content has been hidden....................

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