Created
August 10, 2020 19:18
-
-
Save kshkong97/b4ee308aa094c09feaa63ec094b5de1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >0.4.21 <0.7.0; | |
pragma experimental ABIEncoderV2; | |
contract Project { | |
//todo: Customer request loan -done | |
//lender accept loan | |
//lender repayment | |
enum LoanStatus { NONE, PENDING_APPROVAL, APPROVED, REPAID } | |
struct Customer { | |
string name; | |
string dob; | |
string nric; | |
address caddress; | |
bool verified; | |
uint loanID; | |
} | |
struct Loan { | |
LoanStatus loanStatus; | |
address borrower; | |
uint loanamount; | |
} | |
uint256 loanIndex; | |
mapping(uint256 => Loan) public loans; | |
mapping(address => Customer) public customers; | |
/** | |
* @dev Return all loans in the storage array. | |
*/ | |
function getAllLoans() public view returns (Loan[] memory) { | |
uint256 len = loanIndex; | |
output = new Loan[](len); | |
for (uint256 i = 0; i < len; i++) output[i] = loans[i]; | |
return output; | |
} | |
//When call with web3, can just use 0,1,2,3 for status | |
function getLoansWithStatus(LoanStatus status) | |
external view returns (Loan[] memory outputs) { | |
uint256 count = 0; | |
// Copy loanIndex locally so we don't read storage every loop | |
uint256 stopAt = loanIndex; | |
// Count how many loans there are with the status to size the array | |
for (uint256 i = 0; i < stopAt; i++) { | |
if (loans[i].loanStatus == status) count++; | |
} | |
// Make the output array | |
outputs = new Loan[](count); | |
uint256 outIndex = 0; | |
// Loop through all the loans again to create the return data | |
for (uint256 i = 0; i < stopAt; i++) { | |
if (loans[i].loanStatus == status) outputs[outIndex++] = loans[i]; | |
} | |
} | |
/** | |
* @dev Create a new loan request, updates the user's account. | |
* Note: Throws if the borrower is not verified or already has a loan. | |
*/ | |
function _createLoanRequest(address borrower, uint loanAmount) internal { | |
Customer memory customer = customers[borrower]; | |
require( | |
customer.verified && | |
loans[customer.loanID].borrower != borrower, | |
"User not verified or already has a loan." | |
); | |
uint256 loanID = loanIndex; | |
loans[loanIndex++] = Loan(LoanStatus.PENDING_APPROVAL, borrower, loanAmount); | |
customers[borrower].loanID = loanID; | |
} | |
function acceptLoan(address caddress) public { | |
Customer memory customer = customers[caddress]; | |
Loan memory loan = loans[customer.loanID]; | |
require (loan.borrower == caddress && loan.loanStatus == LoanStatus.PENDING_APPROVAL); | |
loans[customer.loanID].loanStatus = LoanStatus.APPROVED; | |
} | |
bool verified = false; | |
address admin; | |
function Registration(string memory _name,string memory _dob,string memory _nric,address _caddress) public { | |
customers[_caddress] = Customer(_name, _dob, _nric, _caddress, verified, 0); | |
} | |
constructor() public { | |
admin = msg.sender; | |
} | |
modifier onlyAdmin() { | |
require(admin == msg.sender, "Caller is not the admin"); | |
_; | |
} | |
function VerifyCustomer(address caddress) public { | |
//require(!customers[caddress].verified, "This customer has already been verified"); | |
customers[caddress].verified = true; | |
} | |
function LoanRequest(uint256 _loanamount, address caddress) public { | |
_createLoanRequest(caddress, _loanamount); | |
} | |
function getCustomerName(address caddress) public view returns(string memory) { | |
return (customers[caddress].name); | |
} | |
function getCustomerDob(address caddress) public view returns(string memory) { | |
return (customers[caddress].dob); | |
} | |
function getCustomerNric(address caddress) public view returns(string memory) { | |
return (customers[caddress].nric); | |
} | |
function getCustomerVerified(address caddress) public view returns(bool) { | |
return (customers[caddress].verified); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment