Skip to content

Transitions


We finally get to the transitions in the Smart Contract. We group the transitions in the following types. The source code.

User Transitions

create_user

This transition accepts the name and role of the new user.

If the user already exists, the user_exists message is sent back.

If not, the user_name and user_role fields are set. The user_created message is sent back.

Arguments Description Type
name The name of the user String
role The role of the user
(0: Renter, 1: Host)
Uint32
transition create_user (name: String, role: Uint32)
  user_exists_check <- exists user_name[_sender];
  match user_exists_check with
  | True =>
    send_message zero user_exists
  | False =>
    user_name[_sender] := name;
    user_role[_sender] := role;
    send_message zero user_created
  end
end


Listing Transitions

This group of transitions is used in the transitions that a host account user may use to manage their listings.

create_listing

This transition is used by a host user to create a listing.

The user_role is checked.

The listing_id_generator is used to set a new id for the listing, after which it is incremented.

The set_listing_details procedure is called to create the listing, and some of the listing fields are initialised, including listing_host, listing_rented_till, and listing_accumulated_rent.

The listing_created message is sent on success.

On failure, the user_is_renter or user_does_not_exist messages are sent back.

Arguments Description Type
name The name of the listing String
description The description of the listing String
price The price of the listing Uint128
rooms The number of rooms in the listing Uint32
bathrooms The number of bathrooms in the listing Uint32
image A URL to an image of the listing String
location A Google Maps Plus Code for the location of the listing String
wifi The availability of WiFi at the listing String
laundry The availability of a Landry at the listing String
hvac The availability of an HVAC at the listing String
tv The availability of a TV at the listing String
kitchen The availability of a Kitchen at the listing String
transition create_listing (
  name: String, description: String, price: Uint128,
  rooms: Uint32, bathrooms: Uint32, image: String, location: String,
  wifi: String, laundry: String, hvac: String, tv: String, kitchen: String
)
  user_exists_check <- exists user_name[_sender];
  match user_exists_check with
  | True =>
    role <- user_role[_sender];
    match role with
    | Some role =>
      user_role_check = builtin eq role user_role_host;
      match user_role_check with
      | True =>
        id <- listing_id_generator;
        current_block_number <- & BLOCKNUMBER;
        listing_host[id] := _sender;
        set_listing_details id name description price rooms bathrooms image location wifi laundry hvac tv kitchen;
        listing_rented_till[id] := current_block_number;
        listing_accumulated_rent[id] := zero;
        next_listing_id = builtin add id one;
        listing_id_generator := next_listing_id;
        send_message zero listing_created
      | False =>
        send_message zero user_is_renter
      end
    | None =>
      send_message zero user_does_not_exist
    end
  | False =>
    send_message zero user_does_not_exist
  end
end


update_listing

This transition is used by the host user to update the Listing Details for the given listing.

The _sender wallet address is checked if it is indeed the host of the listing.

The set_listing_details procedure is used to update the details.

Arguments Description Type
id The ID of the listing Uint128
name The name of the listing String
description The description of the listing String
price The price of the listing Uint128
rooms The number of rooms in the listing Uint32
bathrooms The number of bathrooms in the listing Uint32
image A URL to an image of the listing String
location A Google Maps Plus Code for the location of the listing String
wifi The availability of WiFi at the listing String
laundry The availability of a Landry at the listing String
hvac The availability of an HVAC at the listing String
tv The availability of a TV at the listing String
kitchen The availability of a Kitchen at the listing String
transition update_listing (
  id: Uint128, name: String, description: String, price: Uint128,
  rooms: Uint32, bathrooms: Uint32, image: String, location: String,
  wifi: String, laundry: String, hvac: String, tv: String, kitchen: String
)
  host <- listing_host[id];
  match host with
  | Some host =>
    user_is_host_check = builtin eq host _sender;
    match user_is_host_check with
    | True =>
      set_listing_details id name description price rooms bathrooms image location wifi laundry hvac tv kitchen;
      send_message zero listing_updated
    | False =>
      send_message zero user_is_not_host
    end
  | None =>
    send_message zero listing_does_not_exist
  end
end


delete_listing

This transition is used by the host user to delete a particular listing.

The _sender wallet address is checked if it is indeed the host of the listing.

It checks if the accumulated rent for the listing is empty.

The delete_listing_by_id procedure is used to delete the listing.

Arguments Description Type
id The ID of the listing Uint128
transition delete_listing (id: Uint128)
  host <- listing_host[id];
  match host with
  | Some host =>
    user_is_host_check = builtin eq host _sender;
    match user_is_host_check with
    | True =>
      accumulated_rent_value <- listing_accumulated_rent[id];
      match accumulated_rent_value with
      | Some accumulated_rent_value =>
        no_rent = builtin eq accumulated_rent_value zero;
        match no_rent with
        | True =>
          delete_listing_by_id id;
          send_message zero listing_deleted
        | False =>
          send_message zero rent_not_empty
        end
      | None =>
        send_message zero listing_details_missing
      end
    | False =>
      send_message zero user_is_not_host
    end
  | None =>
    send_message zero listing_does_not_exist
  end
end


book_listing

This transition is used by a renter user to book a listing.

The _sender wallet address is checked to ensure it is not the host of the listing.

The check_listing_available, check_amount_and_book, and book_listing_by_id procedures are used in sequence to book the listing.

Arguments Description Type
id The ID of the listing Uint128
transition book_listing (id: Uint128)
  user_exists_check <- exists user_name[_sender];
  match user_exists_check with
  | True =>
    host <- listing_host[id];
    match host with
    | Some host =>
      user_is_host_check = builtin eq host _sender;
      match user_is_host_check with
      | True =>
        send_message zero user_is_host
      | False =>
        check_listing_available id
      end
    | None =>
      send_message zero listing_does_not_exist
    end
  | False =>
    send_message zero user_does_not_exist
  end
end


claim_rent

This transition is used by a host user to claim the accumulated rent from a listing that they own.

The _sender wallet address is checked to ensure it is indeed the host of the listing.

The claim_rent_by_id procedure is used to claim the rent.

Arguments Description Type
id The ID of the listing Uint128
transition claim_rent (id: Uint128)
  user_exists_check <- exists user_name[_sender];
  match user_exists_check with
  | True =>
    host <- listing_host[id];
    match host with
    | Some host =>
      user_is_host_check = builtin eq host _sender;
      match user_is_host_check with
      | True =>
        claim_rent_by_id id
      | False =>
        send_message zero user_is_not_host
      end
    | None =>
      send_message zero listing_does_not_exist
    end
  | False =>
    send_message zero user_does_not_exist
  end
end


Owner Transitions

This group of transitions is used by the owner to manage the platform.

claim_commission

This transition is used to claim the commission collected on the platform.

The _sender wallet address is checked to ensure it is the owner of the contract.

The _balance from the contract is sent to the owner via a message.

transition claim_commission ()
  is_owner = builtin eq owner _sender;
  match is_owner with
  | False =>
    send_message zero user_is_not_owner
  | True =>
    balance <- _balance;
    send_message balance commission_claimed
  end
end


update_commission

This transition is used to update the commission collected from each rental.

The _sender wallet address is checked to ensure it is the owner of the contract.

The owners-commission field is updated to new_commission.

Arguments Description Type
new_commission A new value for the commission Uint128
transition update_commission (new_commission: Uint128)
  is_owner = builtin eq owner _sender;
  match is_owner with
  | False =>
    send_message zero user_is_not_owner
  | True =>
    owners_commission := new_commission;
    send_message zero commission_updated
  end
end


update_night_duration

This transition is used to update the night duration value that is added to the BLOCKNUMBER to create the notion of time.

The _sender wallet address is checked to ensure it is the owner of the contract.

The night_duration field is updated to new_night_duration.

Arguments Description Type
new_night_duration A new value for the night duration Uint32
transition update_night_duration (new_night_duration: Uint32)
  is_owner = builtin eq owner _sender;
  match is_owner with
  | False =>
    send_message zero user_is_not_owner
  | True =>
    night_duration := new_night_duration;
    send_message zero night_duration_updated
  end
end