Created
August 11, 2020 03:45
-
-
Save kshkong97/2207de5e361f25aae43e68d5b501ce7f 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 { | |
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; | |
function getAllLoans() public view returns (Loan[] memory output) { | |
uint256 len = loanIndex; | |
output = new Loan[](len); | |
for (uint256 i = 0; i < len; i++) output[i] = loans[i]; | |
return output; | |
} | |
function getLoansWithStatus(LoanStatus status) | |
external view returns (Loan[] memory outputs) { | |
uint256 count = 0; | |
uint256 stopAt = loanIndex; | |
for (uint256 i = 0; i < stopAt; i++) { | |
if (loans[i].loanStatus == status) count++; | |
} | |
outputs = new Loan[](count); | |
uint256 outIndex = 0; | |
for (uint256 i = 0; i < stopAt; i++) { | |
if (loans[i].loanStatus == status) outputs[outIndex++] = loans[i]; | |
} | |
return outputs; | |
} | |
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