Ethereum: Land Marketplace Dapp Tutorial — Part 1 — Create and Deploy a Smart Contract

Adeyemi Toluhi
Coinmonks
7 min readAug 1, 2018

--

Property Website (Dapp)

In my previous post, I walked you through how to setup a private Ethereum blockchain. It is a prerequisite for this tutorial as we are going to build on that. In this tutorial we are going to

  1. Create a smart contract
  2. Deploy the smart contract to a local blockchain
  3. Interact with the smart contract from a website.

We are going to write the smart contract in solidity. Solidity is the langauge for writing smart contacts on the Ethereum Virtual Machine. Please note, the aim of this tutorial is to show you the basics of creating and interacting with a smart contract, I will not got into the details of best practices or syntax.

Discover and review best Blockchain softwares

Smart Contracts

What are smart contracts? Smart contracts also known as self-executing contracts help exchange anything of value (Land, money etc) without the need of a middleman. This is a very simple definition but should suffice. A quick search on google will reveal loads of more indepth explanations.

In the section we are going to develop a smart contract that will allow anyone buy or sell (if they own one) plots of land. For simplicity, the fictitious parcel of land has exactly 12 indivisible plots. Lets list out the requirements for this smart contract.

Land Sale Contract — Requirements

  1. On creation it should have 12 plots for sale at a fixed price of 4000.
  2. There should be an api to get a list of all the plots and their current status.
  3. Anyone with enough ether can buy any plot that is for sale.
  4. Plot owners should be able to put plots back up for sale at any price.
  5. Plot owners should be able to take plots they put up for sale off the market at any time (provided it has’t been sold).
  6. Users should be able to withdraw their ether/money anytime they want.

Lets get started.

Launch Remix (IDE)

To develop the smart contract we will be using a browser based IDE called remix. From any browser (ideally, Chrome or Firefox) open remix.

Link Remix to your Local Blockchain

If you still have the blockchain console running from the previous tutorial exit from the console. For remix to connect to you local blockchain, you need to expose the rpc api. To do that, restart the console with the command below

This command launches the geth console and also exposes the rpc api on port 8545 (could be any valid port number).

Now go to remix in your browser

Select the Run tab

Run tab

From the Environment dropdown select Web3 Provider. By selecting Web3 Provider, you are telling remix that you want to connect to your blockchain instance instead of using the embedded VM.

Select OK when you get the prompt below

Confirmation Prompt

Then select OK again. If you used a different value for the rpcport then you will need to update the port number.

Provider Endpoint

If your geth console isn’t running or the port number is wrong you will a get a pop up message stating that remix cannot connect.

Now let’s create a new solidity file for our contract in remix. To create a new file, click on the plus (+) button on the extreme top left of the screen and enter a filename when prompted.

Select + icon on the left
Enter Filename

Paste the code into you new solidity file.

Lets have a quick overview of the code before we proceed.

The first line in any solidity code is the version of solidity compiler the code should be compiled against. This is important because the language is still relatively new and fluid and there could variations in interpretations between different compiler versions.

Constructor

Now lets take a closer look at the constructor.

In the version of solidity used here, the constructor keyword is used to declare a constructor. You can overload constructors in solidity.

The first line in the constructor assigns the contract to the creator. You can think of contracts as limited liability companies. Though a limited liability company is a legal entity in its own right, it is ultimately owned by one or many share holders. Similarly, when a contract is created, it gets its own address but it usually has one or many owners that manage the lifecycle of the contract.

You are probably thinking, Where did the msg object come from? The msg object is one of many global variables and functions which always exist in the global namespace and are mainly used to provide information about the blockchain or are general-use utility functions. See more details here. In our case, we wanted to assign ownership of this contract to the creator.

The following lines in the constructor just assign a fixed price to the plots and put them up for sale.

getPlots

This function allows us to retrieve the list of plots.

The memory keyword before the variables tells the compiler not to declare the variable in storage which is more expensive. i.e. only hold this variables in memory and discard once the method returns. Another thing you’ll probably find strange in this version of solidity is the fact that you cannot return ‘complex’ objects but you can return multiple values. That is why in the getPlot method, I split the plots array into 3 variables.

buyPlot

The buyPlot function is used to buy a plot by specifying the index (zero based) of the plot and sending enough ether to meet the asking price. It does some basic checks, transfers ether from the buyer to the seller’s account, then transfers ownership of the plot to the buyer. Finally, it raises an event. We will see how events are subscribed to later.

Put Plot Up For Sale

The putPlotUpForSale function allows the current owners of plots to put them up for sale at any price. Again, once the plot has been put up for sale, we raise an event so all subscribers can react to this.

Take Plot Off The Market

The takeOffMarket function allows sellers to take a plot they previously put on the market off. It could be to change the asking price or because they are no longer interested in selling.

Withdraw Funds

Almost every contract requires a way for the owner(s) or stakeholders to withdraw funds attributed to them. The withdrawFunds function allows anyone that has funds in the contract to withdraw all their funds. You can modify this to allow stakeholders perform partial or full withdrawals.

Destroy

The destroy function is not related to the functional requirements we listed above but it allows the owner to destroy or invalidate this contract. In production code, you will have to factor in a few more things which I ignored. For example, sending funds to the owners before calling selfdestruct.

Deploying The Smart Contract

  • If you don’t have geth running, run it using the command provided earlier.
  • From the geth console unlock the account you want to use deploy the contract. To use the default/coinbase account, issue the command in your geth console. personal.unlockAccount(eth.accounts[0]). You will get a prompt to enter the password for the account.
  • In remix, select the Compile tab and check Auto compile
  • If there are any compilations issues, you will see all the errors or warning on the Compile tab otherwise you will see a green background with the name of you contact on it as shown in the picture above.
  • Now, switch back to the Run tab. Select the account you unlocked earlier from the Account dropdown, select your contract and press the Deploy button.
  • You should see the creation of Contract pending... in the remix console like the image below
  • In geth, start the miner by issuing the miner.start(1) command and you should see more activity in the remix logs like the image below
Contract Creation — Remix Console Log

One of the cool things remix allows you do is that it allows you interact with the deployed contract(s). After deploying the contract, you should see a new item in the deployed contracts section (bottom right) of remix.

Deployed Contracts

For example to get the status of the plot on index 0, enter 0 into the textbox beside plots.

Request

Then click the plots button and the output should show the status of the plot.

Plot Status

You will observe the output/status matches the values you initialised the plot with. Play around with the other methods/functions.

Now lets move to the second part of this tutorial where we will call this contract from a web app.

--

--