//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.17; contract P2PLending { //Global Counters for overall stats uint totalNumLoans; uint totalNumBorrowers; uint totalNumInvestors; uint totalNumFullyFundedLoans; //Arrays to hold all of our investors and borrowers and loans Investor[] public investors; Borrower[] public borrowers; Loan[] public loans; //Mappings mapping (address => uint) public borrowerNumLoans; //take a borrowerr address and get the number of loans they have in the marketplace mapping (address => uint) public investorNumInvestments; //take an investor address and get the number of loans they are funding mapping (uint => address) public borrowerToOwner; //take an id from our borrowers array and get the address of the borrower mapping (uint => address) public investorToOwner; // take an id from our investors arrray and get the address of the investor mapping (address => Borrower) public borrower; //take an address and get the Borrower struct mapping (address => Investor) public investor; //take an address and get the investor struct mapping (uint => address) public loanToBorrower; //take a loan id and get the Borrower address mapping (uint => Loan) public loan; //take an id from the loan and get the loan information back //Structus for our Objects -- Investor, Borrower, Loan struct Investor { address investorPublicKey; string firstName; string lastName; uint accountBalance; uint numInvestments; bool EXISTS; } struct Borrower { address borrowerPublicKey; string firstName; string lastName; uint accountBalance; uint numLoans; bool EXISTS; } struct Loan { string loanDescription; uint loanDuration; uint totalAmount; uint amountFunded; uint interestRate; bool EXISTS; } //Create Borrower, Lender, and Loan function createBorrower(string memory _firstName, string memory _lastName, uint _accountBalance) public { Borrower memory b = Borrower(msg.sender, _firstName, _lastName, _accountBalance, 0, true); uint id = borrowers.push(b) - 1; borrowerToOwner[id] = msg.sender; borrower[msg.sender] = b; totalNumBorrowers++; } function createInvestor(string memory _firstName, string memory _lastName, uint _accountBalance) public { Investor memory i = Investor(msg.sender, _firstName, _lastName, _accountBalance, 0, true); uint id = investors.push(i) - 1; investorToOwner[id] = msg.sender; investor[msg.sender] = i; totalNumInvestors++; } function createLoan(string memory _loanDetails, uint _loanDuration, uint _totalAmount, uint _amountFunded, uint _interestRate) public { //require (borrower[msg.sender].EXISTS == true); //Required to be a borrower to create a loan //Require a Borrower profile to create a loan then push it to our loans arrat require(isBorrower(msg.sender)); Loan memory l = Loan(_loanDetails, _loanDuration, _totalAmount, _amountFunded, _interestRate,true); uint id = loans.push(l) - 1; loanToBorrower[id] = msg.sender; //id of loan to address of the borrower who created the loan borrowerNumLoans[msg.sender]++; //increment the number of loans that the borrower has crreated totalNumLoans++; loan[id] = l; } function fundLoan(uint _loanId, uint _fundingAmount) public { require(isLoan(_loanId)); //check if loan exists require(isInvestor(msg.sender)); //check if msg.sender is an investor account require(investor[msg.sender].accountBalance >= _fundingAmount); //Make sure investor has enough money for funding the loan require (_fundingAmount <= (loan[_loanId].totalAmount - loan[_loanId].amountFunded )); //make sure amount <= (loan amount - amount funded) loan[_loanId].amountFunded += _fundingAmount; //Add investor to the loan so we can see all investors on a loan } //Functions to tell us if an address is an investor/borrower //Must have a borrower profile to create a loan //Must have a lender profile to invest in a loan function isInvestor(address account) public view returns (bool) {return investor[account].EXISTS;} function isBorrower(address account) public view returns (bool) {return borrower[account].EXISTS;} function isLoan(uint _id) public view returns (bool) {return loan[_id].EXISTS;} //Toy example of adding to an array and printing it string greeting = "What's up dog"; uint[] s; uint s2 = s.push(1); uint s3 = s.push(2); uint s4 = s.push(3); function sayHello() public view returns (uint[]) { return s; } function addToS(uint num) public { s.push(num); } //End of Toy Example }
0.4.18