Procedures


We proceed to declare the procedures that we will use in the RentOnZilliqa Smart Contract. We will declare the following types of procedures in this section. The source code.

General Procedures

Since we will send messages on multiple occasions in the contract, we create the send_message procedure to send messages.

send_message

This procedure creates a message with the passed arguments. It uses the one_msg library function to create a list of messages and then proceeds to send it. Note that the _recipient is always the implicit variable _sender.

ArgumentsDescriptionType
amountThe amount to be sent with the messageUint128
codeThe message code to be sent with the messageInt32
procedure send_message (amount: Uint128, code: Int32)
msg = {
_tag: "";
_recipient: _sender;
_amount: amount;
code: code
};
msgs = one_msg msg;
send msgs
end

Listing Management Procedures

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

set_listing_details

This procedure creates or updates the Listing Details for the given ID. It is used by the create_listing and update_listing transitions.

ArgumentsDescriptionType
idThe ID of the listingUint128
nameThe name of the listingString
descriptionThe description of the listingString
priceThe price of the listingUint128
roomsThe number of rooms in the listingUint32
bathroomsThe number of bathrooms in the listingUint32
imageA URL to an image of the listingString
locationA Google Maps Plus Code for the location of the listingString
wifiThe availability of WiFi at the listingString
laundryThe availability of a Landry at the listingString
hvacThe availability of an HVAC at the listingString
tvThe availability of a TV at the listingString
kitchenThe availability of a Kitchen at the listingString
procedure set_listing_details (
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
)
listing_name[id] := name;
listing_description[id] := description;
listing_price[id] := price;
listing_rooms[id] := rooms;
listing_bathrooms[id] := bathrooms;
listing_image[id] := image;
listing_location[id] := location;
listing_wifi[id] := wifi;
listing_laundry[id] := laundry;
listing_hvac[id] := hvac;
listing_tv[id] := tv;
listing_kitchen[id] := kitchen
end

claim_rent_by_id

This procedure is used in conjunction with the claim_rent transition. The accumulated rent is checked for the listing with the given ID, in the listing_accumulated_rent field. If the rent is missing or empty, the corresponding messages are sent using the send_message procedure. If there is non-zero accumulated rent, it is sent to the _sender using the send_message procedure. The accumulated rent is passed as an argument to send_message, along with the rent_claimed message code. A "Rent Claimed" event is also emitted. The accumulated rent in the listing_accumulated_rent field is set to zero for that ID, as it is claimed by the host account.

ArgumentsDescriptionType
idThe ID of the listingUint128
procedure claim_rent_by_id (id: Uint128)
accumulated_rent <- listing_accumulated_rent[id];
match accumulated_rent with
| Some accumulated_rent =>
no_accumulated_rent = builtin eq accumulated_rent zero;
match no_accumulated_rent with
| True =>
send_message zero rent_empty
| False =>
listing_accumulated_rent[id] := zero;
e = { _eventname: "RentClaimed"; listing_id: id; renter: _sender; amount: accumulated_rent };
event e;
send_message accumulated_rent rent_claimed
end
| None =>
send_message zero listing_details_missing
end
end

delete_listing_by_id

This procedure is used in conjunction with the delete_listing transition. It deletes the Listing Details entries for the listing with the given ID.

ArgumentsDescriptionType
idThe ID of the listingUint128
procedure delete_listing_by_id (id: Uint128)
delete listing_name[id];
delete listing_description[id];
delete listing_price[id];
delete listing_rooms[id];
delete listing_image[id];
delete listing_host[id];
delete listing_renter[id];
delete listing_rented_till[id];
delete listing_accumulated_rent[id];
delete listing_bathrooms[id];
delete listing_location[id];
delete listing_wifi[id];
delete listing_laundry[id];
delete listing_hvac[id];
delete listing_tv[id];
delete listing_kitchen[id]
end

Listing Booking Procedures


check_listing_available

This procedure is used in conjunction with the book_listing transition. It checks if the listing is available by checking the listing_rented_till field. If it is not available, the listing_unavailable message is sent back. When the listing is available, the check_amount_and_book procedure is called with the id.

ArgumentsDescriptionType
idThe ID of the listingUint128
procedure check_listing_available (id: Uint128)
current_block_number <- & BLOCKNUMBER;
listing_rented_till_value <- listing_rented_till[id];
match listing_rented_till_value with
| Some listing_rented_till_value =>
is_rented_lt = builtin blt current_block_number listing_rented_till_value;
is_rented_eq = builtin eq current_block_number listing_rented_till_value;
listing_is_rented_check = orb is_rented_lt is_rented_eq;
match listing_is_rented_check with
| True =>
send_message zero listing_unavailable
| False =>
check_amount_and_book id
end
| None =>
send_message zero listing_details_missing
end
end

check_amount_and_book

This procedure is used in conjunction with the book_listing transition. It is called after the listing's availibility is checked by the check_listing_available procedure. It checks if the sent amount is equal to the listing_price. If not, the wrong_amount_sent is sent back. If the correct amount is sent, the book_listing_by_id procedure is called with the id.

ArgumentsDescriptionType
idThe ID of the listingUint128
procedure check_amount_and_book (id: Uint128)
listing_price_value <- listing_price[id];
match listing_price_value with
| Some listing_price_value =>
correct_amount_check = builtin eq _amount listing_price_value;
match correct_amount_check with
| True =>
book_listing_by_id id
| False =>
send_message zero wrong_amount_sent
end
| None =>
send_message zero listing_details_missing
end
end

book_listing_by_id

This procedure is used in conjunction with the book_listing transition. In this procedure, the accept command is called. The night_duration is added to the current BLOCKNUMBER and assigned to the listing_rented_till field. The _sender wallet address is assigned to the listing_renter field. The rent amount is added to the listing_accumulated_rent field, after subtracting the commission set in the owners_commission field. A "ListingBooked" event is emitted and a listing_booked message is sent.

As the commission amount is stored in the contract balance, it can be claimed by the contract owner via the _balance implicit variable.

ArgumentsDescriptionType
idThe ID of the listingUint128
procedure book_listing_by_id (id: Uint128)
accumulated_rent <- listing_accumulated_rent[id];
match accumulated_rent with
| Some accumulated_rent =>
accept;
current_block_number <- & BLOCKNUMBER;
night_duration_value <- night_duration;
rented_till = builtin badd current_block_number night_duration_value;
listing_rented_till[id] := rented_till;
listing_renter[id] := _sender;
commission <- owners_commission;
rent = builtin sub _amount commission;
new_accumulated_rent = builtin add accumulated_rent rent;
listing_accumulated_rent[id] := new_accumulated_rent;
e = { _eventname: "ListingBooked"; listing_id: id; renter: _sender; amount: _amount };
event e;
send_message zero listing_booked
| None =>
send_message zero listing_details_missing
end
end