Skip to content

Instantly share code, notes, and snippets.

@EtDu
Created March 22, 2020 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EtDu/f16117bdfdf8a7a71feb42f1b696df56 to your computer and use it in GitHub Desktop.
Save EtDu/f16117bdfdf8a7a71feb42f1b696df56 to your computer and use it in GitHub Desktop.
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