Skip to content

Instantly share code, notes, and snippets.

@EtDu
Last active March 22, 2020 07:46
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/a43cc897e5400de453681e4fc6f0e05c to your computer and use it in GitHub Desktop.
Save EtDu/a43cc897e5400de453681e4fc6f0e05c to your computer and use it in GitHub Desktop.
function createNewLease(
uint8 numberOfMonths,
uint16 monthlyAmountUsd,
uint16 leaseDepositUsd,
uint32 leasePaymentWindowSeconds,
uint32 depositPaymentWindowSeconds,
address payable tenantAddr
) public onlyLandlord {
uint64 depositPaymentWindowEnd = uint64(now.add(depositPaymentWindowSeconds));
tenantLease[tenantAddr] = Lease(
numberOfMonths,
0,
monthlyAmountUsd,
leaseDepositUsd,
leasePaymentWindowSeconds,
0,
depositPaymentWindowEnd,
false,
false
);
emit leaseCreated(
numberOfMonths,
0,
monthlyAmountUsd,
leaseDepositUsd,
leasePaymentWindowSeconds,
false,
false
);
}
function payLeaseDeposit() public payable {
Lease storage lease = tenantLease[msg.sender];
require(!lease.leaseDepositPaid, "Lease deposit is already paid.");
require(lease.depositPaymentWindowEnd >= now, "Lease deposit payment must fit into payment window");
tenantAddress = msg.sender;
tenantPayment = msg.value;
workingState = State.payingLeaseDeposit;
fetchUsdRate();
}
function _payLeaseDeposit() internal {
workingState = State.idle;
Lease storage lease = tenantLease[tenantAddress];
uint amountSentUsd = tenantPayment.mul(ETHUSD).div(1e18);
require(
amountSentUsd >= lease.leaseDepositUsd - 5 &&
amountSentUsd <= lease.leaseDepositUsd + 5,
"Deposit payment must equal to the deposit amount with a maximum offset of $5");
lease.leaseDepositPaid = true;
lease.depositPaymentWindowEnd = 0;
lease.leasePaymentWindowEnd = uint64(now + lease.leasePaymentWindowSeconds);
emit leaseDepositPaid(
tenantAddress,
amountSentUsd
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment