Converting a Property Rental Paper Contract into a Smart Contract

Lets convert one of the most common paper contracts into an Ethereum based Smart Contract. I will show you the code as well as tell you about the technology stack required to implement the complete solution in a step by step process.

Contract Definition

It is a contract between a landlord and tenant. The purpose of the contract is to make sure that rent is paid on time. Here is how the contract functions:

  1. The landlord initiates the contract by setting up the monthly rent and his house address and then the state of the contract is set to “Created”.
  2. Tenant signs the contract and the tenure begins and the state of the contract is set to “Started”.When the state is “Started” the agreement can not be confirmed again, thus eliminating the possibility of overwriting the current tenant.
  3. The smart contract collects money from the renter and sends it to the landlord. This is a very powerful feature of this contract. Infact this is what makes it “smart”.
  4. When the landlord terminates the contract the state is set to “Terminated” and all Ether left in the contract is sent to the landlord as rent is paid after every month and the tenant is not sending advanced payments.

Technology Stack

In order to make a working model out of this, the system would require two parts:

  1. A Distributed App(DApp) which interacts with the contract and the users(ie. Tenant and Landlord)
  2. An Ethereum based Solidity smart contract which resides on the Ethereum blockchain.

Distributed App

This will be a distributed browser based JS app. We would need Web3.js, it is a JS library to interact with our ethereum smart contract.

The DApp can be hosted anywhere eg. IPFS,Github,Storj Public bucket, both parties can even download the source code from github and run the html file on their own machines.

I will not explain Web3.js as that would be out of scope for this article, more on web3.js can be found at https://github.com/ethereum/web3.js

Solidity Smart Contract

Solidity is a static typed programming/smart contract language, any one who understands any of the modern programming languages would be able to understand Solidity.

Now i will show you the complete code for the smart contract

pragma solidity ^0.4.0;
contract RentalAgreement {
    /* This declares a new complex type which will hold the paid rents*/
    struct PaidRent {
    uint id; /* The paid rent id*/
    uint value; /* The amount of rent that is paid*/
    }

    PaidRent[] public paidrents;

    uint public createdTimestamp;

    uint public rent;
    /* Combination of zip code and house number*/
    string public house;

    address public landlord;

    address public tenant;
    enum State {Created, Started, Terminated}
    State public state;

    function RentalAgreement(uint _rent, string _house) {
        rent = _rent;
        house = _house;
        landlord = msg.sender;
        createdTimestamp = block.timestamp;
    }
    modifier require(bool _condition) {
        if (!_condition) throw;
        _;
    }
    modifier onlyLandlord() {
        if (msg.sender != landlord) throw;
        _;
    }
    modifier onlyTenant() {
        if (msg.sender != tenant) throw;
        _;
    }
    modifier inState(State _state) {
        if (state != _state) throw;
        _;
    }

    /* We also have some getters so that we can read the values
    from the blockchain at any time */
    function getPaidRents() internal returns (PaidRent[]) {
        return paidrents;
    }

    function getHouse() constant returns (string) {
        return house;
    }

    function getLandlord() constant returns (address) {
        return landlord;
    }

    function getTenant() constant returns (address) {
        return tenant;
    }

    function getRent() constant returns (uint) {
        return rent;
    }

    function getContractCreated() constant returns (uint) {
        return createdTimestamp;
    }

    function getContractAddress() constant returns (address) {
        return this;
    }

    function getState() returns (State) {
        return state;
    }

    /* Events for DApps to listen to */
    event agreementConfirmed();

    event paidRent();

    event contractTerminated();

    /* Confirm the lease agreement as tenant*/
    function confirmAgreement()
    inState(State.Created)
    require(msg.sender != landlord)
    {
        agreementConfirmed();
        tenant = msg.sender;
        state = State.Started;
    }

    function payRent()
    onlyTenant
    inState(State.Started)
    require(msg.value == rent)
    {
        paidRent();
        landlord.send(msg.value);
        paidrents.push(PaidRent({
        id : paidrents.length + 1,
        value : msg.value
        }));
    }
    /* Terminate the contract so the tenant can’t pay rent anymore,
    and the contract is terminated */
    function terminateContract()
    onlyLandlord
    {
        contractTerminated();
        landlord.send(this.balance);
        /* If there is any value on the
               contract send it to the landlord*/
        state = State.Terminated;
    }
}

It has 4 major elements:

  1. Variables and Data Structures to hold the values in the blockchain.
  2. A constructor which is executed whenever the contract is deployed and whoever deploys it becomes the landlord.
  3. Getters to read values from the blockchain anytime.
  4. Functions confirmAgreement, payRent, and terminateContract for functionalities understandable by their respective names.

Feel free to ask anything related to this in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *