Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function payLease() public payable {
Lease storage lease = tenantLease[msg.sender];
require(lease.leaseDepositPaid, "Lease deposit must be paid before making lease payments");
require(!lease.leaseFullyPaid, "Lease has already been fully paid");
require(lease.leasePaymentWindowEnd >= now, "Lease payment must fit into payment window");
tenantAddress = msg.sender;
tenantPayment = msg.value;
workingState = State.payingLease;
fetchUsdRate();
}
function _payLease() internal {
workingState = State.idle;
Lease storage lease = tenantLease[tenantAddress];
uint amountSentUsd = tenantPayment.mul(ETHUSD).div(1e18);
require(
amountSentUsd >= lease.monthlyAmountUsd - 5,
"Lease payment must be greater than or equal to the monthly amount with a maximum offset of $5");
uint monthsPaid = uint256(lease.monthsPaid).add(amountSentUsd.add(10).div(uint256(lease.monthlyAmountUsd)));
lease.monthsPaid = uint8(monthsPaid);
leaseBalanceWei = leaseBalanceWei.add(tenantPayment);
if (monthsPaid == lease.numberOfMonths) {
lease.leaseFullyPaid = true;
lease.leasePaymentWindowEnd = 0;
emit leaseFullyPaid(
tenantAddress,
lease.numberOfMonths,
monthsPaid
);
} else {
lease.leasePaymentWindowEnd = lease.leasePaymentWindowEnd + lease.leasePaymentWindowSeconds;
emit leasePaymentPaid(
tenantAddress,
amountSentUsd
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.