Chris Pollett > Students >
Mayuri

    ( Print View)

    [Bio]

    [Blog]

    [C297 Proposal]

    [Bitcoin Slides-pdf]

    [Deliverable 1]

    [Deliverable 2]

    [Deliverable 3]

    [Deliverable 4]

    [CS 297 (PDF)]

    [CS 298 Proposal]

    [Deliverable 5]

    [Deliverable 6]

Deliverable 4

Developing the Loan Smart Contract

Mayuri Shimpi (mayuripraveen.shimpi@sjsu.edu)

The Loan Smart contract

This deliverable centers around the Loan Smart Contract, serving as an intermediary to connect lenders and borrowers. The Credit Bureau Smart Contract creates a new loan that borrowers and lenders can associate with. In our system, lenders can invest funds, and borrowers can specify the funds they require. After that, the interest rate is calculated based on the terms set by the EOA that initializes the Loan Smart Contract. The system allows borrowers to make payments and to track their credit scores, we update the CreditBureau smart contract with the capability of updating the credit score of the borrower. The system also allows lenders to make withdrawals

In this deliverable, I have implemented the invest, borrow, and calculate interest methods as described below:

  • Constructor: This constructor is defining and initializing variables for the Loan smart contract. The contract tracks the total loan amount, interest rate, number of payments, time intervals between payments, and a minimum credit score requirement. The variables _amountInvested and _amountBorrowed are initialized to 0 and are used for tracking investment and borrowing activity within the contract.
  •   constructor(
          uint totalAmount,
          uint interestRatePerMil,
          uint numPayments,
          uint secondsBetweenPayments,
          uint minCreditScore) {
    
        _amountInvested = 0;
        _amountBorrowed = 0;
    
        _totalAmount = totalAmount;
    
        _interestRatePerMil = interestRatePerMil;
        _numPayments = numPayments;
        _secondsBetweenPayments = secondsBetweenPayments;
        _minCreditScore = minCreditScore;
        }
    
    
  • The invest() function: This function withdraws the amount specified by msg.value from the lender (msg.sender) and adds the balance to the loan smart contract. If sufficient funds are received, the timestamp of this transaction is utilized as the starting time for the loan. This timestamp is then employed to calculate the accrued interest.
  •   function invest() public payable {
        require(msg.value + _amountInvested <= _totalAmount, "Exceeds total amount of investment");
        _investors[msg.sender] += msg.value;
        _amountInvested += msg.value;
        if (isReady()) {
          _timeLoanStart = block.timestamp;
        }
        }
    
  • The borrow(uint amount) function: This function is utilized by the borrower (msg.sender) to request a loan. The borrower's credit score and the requested amount must meet the loan requirements. This method involves a call to "getScore()", which will be elaborated on below and may incur gas costs. Similarly, the findLoan operation mentioned earlier incurs gas costs for computation and may have already computed the same getScore. To prevent costly recomputations, these calls may be cached. I haven't called the getScore() function yet; however, I plan to do so in the future. The isReady() function returns whether or not the loan has been funded.
  •   function borrow(uint amount) public {
        uint creditScore = 750;
        require(creditScore > _minCreditScore, "Insufficient credit score");
        require(_borrower[msg.sender] == 0, "Can't borrow twice");
        require(_amountBorrowed + amount <= _totalAmount, "Not enough ether left to borrow");
        _borrower[msg.sender] = amount;
        uint costOfLoan = calculateInterest(amount, _numPayments) + amount;
        _borrowerExpectedPayment[msg.sender] = costOfLoan / _numPayments;
        _amountBorrowed += amount;
        if (isReady()) {
          _timeLoanStart = block.timestamp;
        }
        
      }
        
        function isReady() view public returns (bool) {
        return _totalAmount == _amountInvested && _totalAmount == _amountBorrowed;
      }
    
    
  • The calculateInterest(uint owed, uint numPayments) function: This function, named calculateInterest, is designed to compute the accrued interest on a loan based on the provided parameters.
  • 
      function calculateInterest(uint owed, uint numPayments) public view returns (uint) {
        if (numPayments <= 0) {
          return 0;
        }
        uint secondsPerYear = 365*86400 + 86400/4; //365.25 * seconds/day
        uint ratePerMilPayment = (_interestRatePerMil * _secondsBetweenPayments)/secondsPerYear;
        uint milAugmentInterest = owed * ((1000000 + ratePerMilPayment) ** numPayments);
        uint newAmountOwed = milAugmentInterest / (1000000 ** numPayments);
        return newAmountOwed - owed;
      }
    
    

    Deploying the Loan Smart Contract:


    Conclusion:To complete this smart contract, I will be continuing to implement the rest of the functions like get$$$(), the function transfers the amount of ether that was previously requested by the borrower once the loan has been successfully funded. Another function I will need to implement is the makePayment(uint amount):Borrowers use this function to make payments towards the loan. The method succeeds if the sender (msg.sender) has sufficient funds. Additionally, this method is designed to change the borrower's credit score as part of its implementation. And lastly, the withdraw(unit amount) function allows lenders to withdraw money from the account up to the invested amount plus interest.