Skip to content

Instantly share code, notes, and snippets.

@CodeLeom
Created March 24, 2023 17:27
Show Gist options
  • Save CodeLeom/3636d56e0a267adbaeb3394bfaf6540a to your computer and use it in GitHub Desktop.
Save CodeLeom/3636d56e0a267adbaeb3394bfaf6540a to your computer and use it in GitHub Desktop.
This smart contract allows the tenant to pay the rent amount to the landlord and allows the landlord to withdraw the rent amount once the payment due date has passed. Here are the steps for using this smart contract: Deploy the smart contract by providing the addresses of the landlord and tenant, the rent amount, and the payment due date. The te…
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HouseRent {
address payable public landlord;
address payable public tenant;
uint256 public rentAmount;
uint256 public paymentDueDate;
bool public landlordHasWithdrawn;
constructor(address payable _landlord, address payable _tenant, uint256 _rentAmount, uint256 _paymentDueDate) {
landlord = _landlord;
tenant = _tenant;
rentAmount = _rentAmount;
paymentDueDate = _paymentDueDate;
}
function payRent() public payable {
require(msg.sender == tenant, "Only tenant can pay rent.");
require(msg.value == rentAmount, "Rent amount is not correct.");
landlordHasWithdrawn = false;
}
function withdrawRent() public {
require(msg.sender == landlord, "Only landlord can withdraw rent.");
require(block.timestamp >= paymentDueDate, "Payment is not yet due.");
require(!landlordHasWithdrawn, "Rent has already been withdrawn by the landlord.");
landlordHasWithdrawn = true;
landlord.transfer(address(this).balance);
}
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment