pragma solidity ^0.4.18; contract CreditScoring { /*define a variable which is used to define the owner of an contract*/ address private owner; /*constructor function which designates an owner of the contract and sets the initial credit score to 50 */ function CreditScoring() public { owner = msg.sender; creditScore[msg.sender] = 50; } /*use the owner's address in two modifiers which help with security*/ modifier onlyOwner() { require(msg.sender == owner); _; } modifier ownerOf(uint _paymentId) { require(msg.sender == paymentToOwner[_paymentId]); _; } /*define a structure which is used to save the data about payments*/ struct Payment { uint32 paymentTime; uint32 amount; bool onTime; } /*define an array of payment structures used to store data about the payments on the blockchain*/ Payment[] private payments; /*define an event which can be fired if a payment is made. NOTE: only for demo purposes, since we need actual UI (eg javascript) to make use of the fired event */ event NewPayment(uint id, uint32 paymentTime, uint32 amount, bool onTime); //store payments on the blockchain as belonging to the person who made the payment mapping (uint => address) private paymentToOwner; //store the current credit score of an user on the blockchain mapping (address => uint) public creditScore; //function which allows the owner of a payment to view the payment's details function viewPayment (uint _paymentId) public view ownerOf(_paymentId) returns (uint32 paymentTime, uint32 amount, bool onTime) { return (payments[_paymentId].paymentTime, payments[_paymentId].amount, payments[_paymentId].onTime); } //function which is called if a payment on time is made (manually in this case) //which saves the info about the payment into the payments array, //assigns the payee's address as an owner of the payment in the defined mapping //increases the payee's credit score // and fires the NewPayment event function makePaymentOnTime (uint32 _amount) public { uint32 _timeNow = uint32(now); uint id = payments.push(Payment(_timeNow, _amount, true)) -1; paymentToOwner[id] = msg.sender; if (creditScore[msg.sender] <= 90) { creditScore[msg.sender] += 10; } else { creditScore[msg.sender] = 100; } NewPayment(id, _timeNow, _amount, true); } //almost identical function to makePaymentOnTime, but reduces the current credit score function makeLatePayment (uint32 _amount) public { uint32 _timeNow = uint32(now); uint id = payments.push(Payment(_timeNow, _amount, false)) -1; paymentToOwner[id] = msg.sender; if (creditScore[msg.sender] >= 10) { creditScore[msg.sender] -= 10; } else { creditScore[msg.sender] = 0; } NewPayment(id, _timeNow, _amount, true); } }
0.4.18