Learn how to Code a Decentralised Hotel Booking System Smart Contract


The travel and Tourism industry is a 4-billion-dollar industry globally and is estimated to grow by a whopping CAGR of 15.5% to 16 billion dollars by 2031. Booking a hotel is the first thing we do when traveling to any destination. This aspect of travel has always been done predominantly by travel agents who used to do it on a physical level and are now doing it using technology through the OTAs. With go-betweens come commission charges, a burden that the tourist and the hotels both have to bear. The payment gateways add to the cost burden, becoming a more significant problem in the case of international payments. The flip side of it is the issue of double booking, which becomes a massive inconvenience for tourists. In addition, there is no transparency on the dynamic pricing, an unfair advantage the hotel owners take. Fake reviews can mutate the reputation of hotels online. Hence, the tourist does not get an accurate idea of the quality of service that the hotel is providing.

A decentralised public Blockchain-based hotel booking system can solve many issues both on the hotel owner’s and the tourist’s sides.

On a public blockchain, since everything is done peer to peer, go-betweens are removed who used to cut into the revenue of the hotel owners by 20–30%. The other part is the possibility of double bookings done by hotel owners, which becomes an issue for tourists but won’t be possible on a Blockchain. It also becomes impossible to post fake reviews or manipulate ratings when it is done on the Blockchain since only visiting tourists can post the reviews, and they cannot be changed once posted.

In the following sections, we will code a hotel booking smart contract.

  1. Creating a Project

Open your coding folder in VSCode Terminal and run the command npx hardhat

Once executed correctly, your folder structure should look like the following:

VS Code Folder Structure

2. Coding the Smart Contract

Keeping brevity in mind, I will be diving right into the meat of the Smart Contract vs the usual Pragma and stuff.

2.1. This smart contract begins with declaring the “Hotel room” and “Customer” structures which shall be used for storing the incoming data.

2.2 Setting the hotel rooms: In order for the Smart Contract to know the different types of rooms and their related details, the owner of the Smart Contract will call the following function:

2.3 Calling the above function in the constructor

The idea is that at the deployment of the Contract, appropriate Hotel Rooms are set.

2.4 The payToBook function which is also a payable function, and would be defined as follows:

As you can see from the above, depending on the quantity of the native tokens sent across while calling the payToBook function, the apt room will be booked by calling another respective function.

2.4 Deployment Script

Next, we will update the deployment script in the scripts folder to the following:

3. Deployment and Testing

3.1: For the purpose of our testing, we will be creating a private Testnet on BuildBear 🐻‍❄️
Why BuildBear, you ask? Have a look over here:
Where Localhost Fails and
Win Web3 Hackathons, using BuildBear Testnet’s analytics

Read here on how to create a Testnet on BuildBear → https://buildbear.notion.site/Getting-Started-8e561563dd2e410c969874ba3c1684ee

3.2 Updating our config file to the following:

3.2 To deploy our contract, open your terminal and use the following command:

npx hardhat run scripts/deploy.js --network buildbear

This will deploy the smart contract and you shall get the following message:

Once that is done, you can interact with the Smart Contract by uploading the Artifacts to the BuildBear Testnet (this is available on the “Advance” tab in your home.buildbear.io, in your private Testnet {read here also: https://www.notion.so/buildbear/Getting-Started-8e561563dd2e410c969874ba3c1684ee#e301e9a3bd6d4dcf9021324e6ad832ed}):

Congratulations!!! If you were able to make it till the end 😊

To learn more about BuildBear, read here docs

Get the above Github code from here

If you appreciate what we are doing, please follow us on Twitter and Join the Telegram group if you haven’t done yet.

And please give us a clap 👏 if you like our work.


Amaresh Sahoo (Twitter || LinkedIn) is always open to feedback and learning.

BTW, if you would know anyone who would be keen to work with BuildBear. Please have a look over here!!!

Let’s get started then, Shall we?