-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlending.sol
64 lines (51 loc) · 1.91 KB
/
lending.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract P2PLendingPlatform {
// Mapping from loan ID to loan info
mapping (uint => Loan) public loans;
// Struct to store loan info
struct Loan {
address lender;
address borrower;
uint amount;
bool repaid;
}
// Function to create a loan listing
function createLoanListing(uint amount) public payable {
require(msg.value >= amount, "Insufficient funds for loan listing.");
// Generate a unique ID for the loan
uint id = keccak256(abi.encodePacked(msg.sender, now, amount));
// Set the loan info
loans[id] = Loan({
lender: msg.sender,
borrower: address(0),
amount: amount,
repaid: false
});
}
// Function for a user to take out a loan
function takeOutLoan(uint id) public payable {
require(loans[id].borrower == address(0), "Loan has already been taken out.");
// Set the borrower for the loan
loans[id].borrower = msg.sender;
// Transfer the loan amount to the borrower
msg.sender.transfer(loans[id].amount);
}
// Function for a user to repay a loan
function repayLoan(uint id) public payable {
require(loans[id].borrower == msg.sender, "Sender is not the borrower of this loan.");
require(msg.value >= loans[id].amount, "Insufficient funds to repay loan.");
// Transfer the loan amount to the lender
loans[id].lender.transfer(loans[id].amount);
// Set the repaid flag to true
loans[id].repaid = true;
}
// Function to get the borrower for a loan
function getBorrower(uint id) public view returns (address) {
return loans[id].borrower;
}
// Function to get the repaid status for a loan
function getRepaid(uint id) public view returns (bool) {
return loans[id].repaid;
}
}