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]
[CS 298 (PDF)]
|
Deliverable 6
Tracking and updating the credit score in the Credit Bureau Smart Contract
Mayuri Shimpi (mayuripraveen.shimpi@sjsu.edu)
The Credit Bureau Smart contract
In the ALOE system [1], client anonymity is preserved, while their credit score is openly linked to their Ethereum address. The process initiates with a registration phase, where an initial credit score is allocated to an Ethereum address according to the client's real-world credit score. Upon the creation of a new loan, borrowers and lenders engage to either request or invest funds. Once the loan amount garners adequate demand from borrowers and sufficient investment from lenders, the loan terms come into effect.
The Credit Bureau Smart Contract (CBSC) maintains a mapping linking Ethereum addresses with corresponding credit scoring data. This supplementary data includes the credit score linked with the hashed identity, a timestamp marking when this linkage was established, and a position in our credit scoring spectrum. The CBSC facilitates the connection between lenders and borrowers, monitors loan transactions and repayments, and manages updates to the borrower's credit score. Exclusive authority to invoke specific methods of this smart contract rests with the notary.
In this deliverable, I have implemented the updateScoreBorrow, updateScoreRepayment, and update the getScore methods as described below:
- The smart contract:
The Credit Bureau Smart Contract (CBSC) includes fields to store the notary's address, available loans, number of loans, credit score initialization status, credit scores mapped to addresses, and timestamps of score associations. The constructor initializes the notary address upon deployment.
address private _notary;
Loan[] private _availableLoans;
uint8 private _numLoans;
mapping(address => bool) private _scoreInitialized;
mapping(address => uint) public _creditScores;
mapping(address => uint) private _scoreTimestamps;
constructor() {
_notary = msg.sender;
}
- The getScore(address client, uint amountRequested): function: This function retrieves the credit score of a client and adjusts it based on the amount of loan requested. It decreases the score by 1 for every 100 units requested and returns the adjusted score.
function getScore(address client, uint amountRequested) public view returns (uint) {
// Version 1, no encryption of scores
// return _creditScores[client].score;
// Adjust the score based on the amountRequested
uint baseScore = _creditScores[client];
uint adjustedScore = baseScore - (amountRequested / 100); // Decrease score by 1 for every 100 units requested
return adjustedScore; }
- Testing the getScore() function:
To write test cases for the invest function, we'll cover various scenarios to ensure its correctness. Here are some scenarios:
- TestGetScore: This test case verifies that the getScore function correctly adjusts the client's credit score based on the amount requested. It sets up initial conditions with an initial credit score for the client and a specific amount requested. Then, it calls the getScore function and checks if the adjusted score matches the expected value.
- TestGetScoreWithZeroAmountRequested: This test case ensures that when zero amount is requested, the getScore function does not change the client's credit score.
function testGetScore() public {
address client = address(0x123);
uint initialScore = 750;
uint amountRequested = 500;
cbsc._creditScores[client] = initialScore;
uint adjustedScore = cbsc.getScore(client, amountRequested);
uint expectedScore = initialScore - (amountRequested / 100); // Expected adjusted score
Assert.equal(adjustedScore, expectedScore, "Adjusted score should be calculated correctly based on the amount requested");
}
function testGetScoreWithZeroAmountRequested() public {
address client = address(0x123);
uint initialScore = 750;
uint amountRequested = 0;
cbsc._creditScores[client] = initialScore;
uint adjustedScore = cbsc.getScore(client, amountRequested);
Assert.equal(adjustedScore, initialScore, "Adjusted score should remain unchanged when zero amount is requested");
}
- The updateScoreBorrow(uint amount):function: This function updates the credit score of the borrower when a loan is taken. It decreases the credit score based on the borrowed amount, ensuring it doesn't fall below a minimum threshold (300 in this case). Finally, it subtracts the borrowed amount from the credit score of the borrower.
function updateScoreBorrow(uint amount) public {
uint baseScore = _creditScores[tx.origin];
uint scaledAmount = amount / 100;
uint scaledScore = baseScore - scaledAmount;
if (scaledScore < 300) {
scaledScore = 300;
}
_creditScores[tx.origin] = scaledScore;
_creditScores[tx.origin] -= amount;
}
- Testing the borrow(uint amount) function:
To write test cases for this function, we'll cover various scenarios to ensure its correctness. Here are some scenarios:
- Test to ensure borrower can't borrow twice.
- Test case to ensure borrower can't borrow more than total amount available.
function testUpdateScoreBorrow() public {
= address borrower = msg.sender;
uint initialScore = 750;
uint borrowedAmount = 500;
// Set the initial credit score of the borrower
yourContract._creditScores[borrower] = initialScore;
// Call the function to be tested
cbsc.updateScoreBorrow(borrowedAmount);
// Retrieve the updated credit score of the borrower
uint updatedScore = cbsc._creditScores[borrower];
// Calculate the expected updated score
uint expectedScore = initialScore - (borrowedAmount / 100); // Expected updated score
// Assert that the updated score is calculated correctly
Assert.equal(updatedScore, expectedScore, "Updated score should be calculated correctly based on the borrowed amount");
}
- The updateScoreRepayment(uint amount) function: This function updates the credit score of the borrower when a repayment is made. It increases the credit score based on the repayment amount, ensuring it doesn't exceed the maximum threshold (850 in this case). Finally, it updates the credit score of the borrower with the scaled score.
function updateScoreRepayment(uint amount) public {
uint baseScore = _creditScores[tx.origin];
uint scaledAmount = amount / 100;
uint scaledScore = baseScore + scaledAmount;
if (scaledScore > 850) {
scaledScore = 850;
}
_creditScores[tx.origin] = scaledScore;
}
- Testing the updateScoreRepayment() function:
Verifies that the function updates the credit score of the borrower correctly according to the provided repayment amount and constraints (such as not exceeding the maximum threshold).
- Test case to ensure interest calculation is correct when numPayments is greater than 0.
function testUpdateScoreRepayment() public {
// Set up initial conditions
address borrower = address(0x123); // Address of the borrower
uint initialScore = 750; // Initial credit score of the borrower
uint repaymentAmount = 500; // Amount to be repaid
cbsc._creditScores[borrower] = initialScore;
// Call the function to be tested
cbsc.updateScoreRepayment(repaymentAmount);
// Retrieve the updated credit score of the borrower
uint updatedScore = cbsc._creditScores[borrower];
// Assert that the credit score is updated correctly
uint expectedScore = initialScore + (repaymentAmount / 100); // Expected credit score after repayment
uint maxScore = 850; // Maximum credit score allowed
Assert.isAtMost(updatedScore, maxScore, "Credit score should not exceed maximum threshold");
Assert.equal(updatedScore, expectedScore, "Credit score should be updated correctly after repayment");
}
Deploying the Smart Contract:
References:
[1] T. H. Austin, K. Potika and C. Pollett, 'Autonomous Lending Organization on Ethereum with Credit Scoring,' 2023 Silicon Valley Cybersecurity Conference (SVCC), San Jose, CA, USA, 2023.
[2] G. Wood, "Ethereum: A Secure Decentralised Generalised Transaction Ledger, EIP-150 Revision," Ethereum & Ethcore, gavin@ethcore.io
|