In the world of Blockchain technology, smart contracts are transforming how we automate agreements and execute transactions without the need for intermediaries. These self-executing contracts run on Blockchain networks, offering transparency, security, and efficiency in a wide range of applications—from finance to supply chain management. But what do smart contracts actually look like in practice, and how can they be implemented in real-world scenarios?

In this blog post, we’ll delve into the practicalities of smart contracts by exploring a simple yet effective use case: a Yacht Rental application. This Solidity-based contract allows the owner of a yacht to automate the rental process, from setting rates and receiving payments to managing availability. By walking through the code, we’ll uncover the key components that make this smart contract functional and secure, providing a hands-on understanding of how smart contracts can revolutionize traditional business models.

This example was created using Remix, an open-source web-based IDE. Remix simplifies the process of developing, testing, and deploying smart contracts on Ethereum and other EVM-compatible blockchains. Remix offers an intuitive interface where developers can write Solidity code, compile it, and quickly identify any errors or warnings. The IDE also provides a built-in debugger and a deployment interface, allowing users to deploy their contracts directly to the blockchain or to a local development network like Ganache.

*** FYI: I do not have a yacht for rent. This code is for informational purposes only. ***

Code Summary

  1. State Variables: The contract stores the owner’s address, the yacht’s availability, and the rental rate per day.
  2. Constructor: When the contract is deployed, it initializes the owner as the deployer, sets the yacht’s availability to true, and establishes a rental rate.
  3. Modifiers: The onlyOwner modifier restricts certain functions so that only the contract owner can call them.
  4. Booking Function: bookYacht handles yacht bookings by checking availability, ensuring correct payment, and then transferring the payment to the owner while marking the yacht as unavailable.
  5. Rate Update Function: updateRate allows the owner to change the rental rate, ensuring the flexibility to adjust pricing as needed.
  6. Availability Function: makeYachtAvailable enables the owner to mark the yacht as available again after it has been rented.
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

contract YachtRental {
    // state variables
    address payable public owner;
    bool public available;
    uint public ratePerDay;

    event Log(address indexed sender, string message);

    constructor() {
        owner = payable(msg.sender);
        available = true;
        ratePerDay = 3 ether;
    }

    // Modifier to check that the caller is the owner of the contract.
    modifier onlyOwner() {
        require(msg.sender == owner, "Must be contract owner to call this function");
        _;
    }

    function bookYacht(uint numDays) public payable {
        // Check availability
        require(available, "Yacht is not available.");

        // Check payment amount
        uint minOffer = ratePerDay * numDays;
        require(msg.value >= minOffer, "Not enough ether provided.");

        // Recieve payment
        // owner.transfer(msg.value);
        (bool sent, bytes memory data) = owner.call{value: msg.value}("");
        require(sent, "Failed to send Ether");
        
        available = false;
        emit Log(msg.sender, "Yacht has been booked.");
        // available = false;
    }

    function updateRate(uint newRate) public onlyOwner {
        // require(msg.sender == owner, "Only the contract owner can change the rate.");
        ratePerDay = newRate;
        emit Log(msg.sender, "Owner updated yacht rate.");
    }

    function makeYachtAvailable() public onlyOwner {
        // require(msg.sender == owner, "Only the yacht owner can make it available.");
        available = true;
        emit Log(msg.sender, "Be more yachty. Rent my yacht.");
    }

}

You can view an example of this smart contract being deployed and utilized as part of a dApp in this post.