Skip to content

Commit

Permalink
Create loan_contract.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Nov 22, 2024
1 parent a1c04c2 commit ca5d82c
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract LoanContract {
struct Loan {
uint256 amount;
uint256 interestRate; // in basis points (1/100th of a percent)
uint256 duration; // in seconds
uint256 startTime;
address borrower;
bool isActive;
}

mapping(address => Loan) public loans;
event LoanRequested(address indexed borrower, uint256 amount, uint256 interestRate, uint256 duration);
event LoanRepaid(address indexed borrower, uint256 amount);

// Request a loan
function requestLoan(uint256 _amount, uint256 _interestRate, uint256 _duration) public {
require(loans[msg.sender].isActive == false, "Existing loan must be repaid first.");
loans[msg.sender] = Loan(_amount, _interestRate, _duration, block.timestamp, msg.sender, true);
emit LoanRequested(msg.sender, _amount, _interestRate, _duration);
}

// Repay the loan
function repayLoan() public payable {
Loan storage loan = loans[msg.sender];
require(loan.isActive, "No active loan found.");
uint256 totalRepayment = loan.amount + (loan.amount * loan.interestRate / 10000);
require(msg.value >= totalRepayment, "Insufficient repayment amount.");

loan.isActive = false;
emit LoanRepaid(msg.sender, totalRepayment);
payable(address(this)).transfer(msg.value);
}

// Get loan details
function getLoanDetails(address _borrower) public view returns (uint256, uint256, uint256, uint256, bool) {
Loan storage loan = loans[_borrower];
return (loan.amount, loan.interestRate, loan.duration, loan.startTime, loan.isActive);
}
}

0 comments on commit ca5d82c

Please sign in to comment.