ExampleofLoanContract

来自智能法律合约
跳到导航 跳到搜索

Loan contract

    Loan contract, refers to a contract where a lender delivers a certain amount of funds to a borrower, and the latter returns the same amount of funds within a certain period of time and pays interests to the lender. There are 6 terms in the contract that consists of two aspects:
    1) the borrower shall submit a loan application stating the amount he/she wants to borrow, and
    2) the lender may deposit funds into the contract account until the target of loan is reached. Before borrowing, the contract requires that the borrower must confirm the loan information and check the historical loan records, and then calculate the corresponding interests. Meanwhile, the contract asserts that the borrower pays off the loan amount before the loan date comes.

// Lend Spesc.scs
contract Lend{

party User{
loan : Loan
SubmitLoanApplication(l : Loan)
confirm()
getHisLoan()
payback()
}


party Funder{
moneyFunded : Money
moneyWillGet : Money
Contribute(uid : User)
refund(uid : User)
}


term term1 : User can SubmitLoanApplication.


term term2 : Funder can Contribute
when before uid::loan::timelimit
while deposit $ value <= uid::loan::fundingGoal.


term term3 : Funder can refund
when after uid::loan::timelimit and uid::loan::amount < uid::loan::fundingGoal
while withdraw $ his::moneyFunded.


term term4 : User must confirm
when within 3 day after his::loan::timelimit and his::loan::amount = his::loan::fundingGoal.


term term5 : User can getHisLoan
when after User did confirm
while withdraw $ his::loan::amount.


term term6 : User must payback
when after User did confirm and before his::loan::tenorM
while
deposit $ his::loan::balance + his::loan::installment
transfer $ his::loan::funders::moneyWillGet to his::loan::funders.


type Loan
{
operationName : Name     // use of money
beneficiary : User     // Borrower
timelimit : Date     // time limit of collecting money
fundingGoal : integer     // the amount borrower want to borrow
amount : integer     // the money collected now
balance : integer     // the Capital have to pay back
numFunders : integer
interestRateM : integer     // monthly
gracePeriod : integer     // the period you don't have to pay the interest
tenorM : integer     // you have to pay back tenorM month later
installment : integer     // the interest have to pay back
funders : set Funder
}

}

 1 /* FunderT.sol */
 2 pragma solidity >=0.4.0 <0.6.0;
 3 
 4 contract FunderT{
 5 	
 6 	uint moneyFunded;
 7 	uint moneyWillGet;
 8 	
 9 	address _FunderAddress;
10 	uint _max;
11 	
12 	function funderT() public{
13 		_max = now*1000;
14 	}
15 	function regist(address a) public {
16 		_FunderAddress = a;
17 	}
18 	
19 	function getAddress() public returns (address a){
20 		return _FunderAddress;
21 	}
22 	
23 	 function getmoneyFunded() public returns(uint _result){
24 		return moneyFunded;
25 	}
26 	
27 	function setmoneyFunded ( uint a) public{
28 		moneyFunded = a;
29 	}
30 	
31 /*	function getmoneyWillGet() public returns(uint _result){
32 		return moneyWillGet;
33 	}
34 	
35 	function setmoneyWillGet(uint a) public {
36 		moneyWillGet = a;
37 	}*/
38 	
39 
40     
41     function setmoneyWillGet(uint256 newValue) public {
42         moneyWillGet = newValue;
43     }
44 
45    
46     function getmoneyWillGet() public view returns (uint256) {
47         return moneyWillGet;
48     }
49 }
  1 /* Lend.sol */
  2 pragma solidity >=0.4.0 <0.6.0;
  3 pragma experimental ABIEncoderV2;
  4 		
  5 import "./UserT.sol";
  6 import "./FunderT.sol";
  7 
  8 contract Lend {
  9 	
 10 	UserT User;
 11 	FunderT Funder;
 12 	uint start;
 13 	
 14 	
 15 	mapping (string => Loan) loan;
 16 	
 17 	struct Loan{
 18 		bytes32 operationName;
 19 		address beneficiary;
 20 		uint timelimit;
 21 		uint fundingGoal;
 22 		uint amount;
 23 		uint balance;
 24 		uint numFunders;
 25 		uint interestRateM;
 26 		uint gracePeriod;
 27 		uint tenorM;
 28 		uint installment;
 29 		address funders;
 30 	}
 31 	
 32 	
 33 	
 34 	constructor() public{
 35 		start = now;
 36 		User = new UserT();
 37 		Funder = new FunderT();
 38 	}
 39 	
 40 /*		constructor() public{
 41 	    start = now;
 42 		User = new UserT();
 43 		Funder = new FunderT();
 44 	}*/
 45 
 46 	modifier onlyUser{
 47 		require(User.getAddress()==msg.sender);
 48 		_;
 49 	}
 50 	
 51 	modifier onlyFunder{
 52 		require(Funder.getAddress()==msg.sender);
 53 		_;
 54 	}
 55 	
 56 	modifier term2Modifier(UserT uid){
 57 	//	require(now < uid.loan.timelimit);
 58 	//	require(msg.value <= uid.loan.fundingGoal);
 59 		_;
 60 	}
 61 	
 62 	modifier term3Modifier(UserT uid){
 63 //		require(now > (uid.loan.timelimit && uid.loan.amount < uid.loan.fundingGoal));
 64 		_;
 65 	}
 66 	
 67 	modifier term4Modifier{
 68 //		require((now > (User.getloan.timelimit(msg.sender) && User.getloan.amount(msg.sender) == User.getloan.fundingGoal(msg.sender))) &&(now < (User.getloan.timelimit(msg.sender) && User.getloan.amount(msg.sender) == User.getloan.fundingGoal(msg.sender))+259200));
 69 		_;
 70 	}
 71 	
 72 	modifier term5Modifier{
 73 		require(now > User.confirmTime());
 74 		_;
 75 	}
 76 	
 77 	modifier term6Modifier{
 78 //		require(now > (User.confirmTime() && now < User.getloan.tenorM(msg.sender)));
 79 //		require(User.getloan.balance(msg.sender) + User.getloan.installment(msg.sender));
 80 		_;
 81 	}
 82 	
 83 	function SubmitLoanApplication( Loan memory l) onlyUser() public {
 84 		//USER CODE HERE
 85 		//CHECK
 86 	
 87 	}
 88 	
 89 	function Contribute(UserT uid) onlyFunder() term2Modifier(uid) public payable {
 90 		//USER CODE HERE
 91 		//CHECK
 92 	
 93 	}
 94 	
 95 	function refund(UserT uid) onlyFunder() term3Modifier(uid) public payable {
 96 		//USER CODE HERE
 97 		msg.sender.transfer(Funder.getmoneyFunded());
 98 		//CHECK
 99 	
100 	}
101 	
102 	function confirm() onlyUser() term4Modifier() public {
103 		//USER CODE HERE
104 		//CHECK
105 	
106 	}
107 	
108 	function getHisLoan() onlyUser() term5Modifier() public payable {
109 		//USER CODE HERE
110 		msg.sender.transfer(User.getloan().amount);
111 		//CHECK
112 	
113 	}
114 	
115 	function payback() onlyUser() term6Modifier() public payable {
116 		//USER CODE HERE
117 		//User.getloan().funders.transfer(msg.sender);
118 		msg.sender.transfer(User.getloan().amount);
119 		//CHECK
120 	
121 	}
122 	
123 }
 1 /* UserT.sol */
 2 pragma solidity >=0.4.0 <0.6.0;
 3 pragma experimental ABIEncoderV2;
 4 
 5 
 6 import "./Lend.sol";
 7 
 8 contract UserT{
 9 	
10 	struct Loan{
11 		bytes32 operationName;
12 		address beneficiary;
13 		uint timelimit;
14 		uint fundingGoal;
15 		uint amount;
16 		uint balance;
17 		uint numFunders;
18 		uint interestRateM;
19 		uint gracePeriod;
20 		uint tenorM;
21 		uint installment;
22 		address funders;
23 	}
24 	
25 	
26 	
27 	
28 	Loan loan;
29 	//attributes of actionconfirm
30 	bool _isconfirmDone;
31 	uint _confirmTime;
32 	
33 	address _UserAddress;
34 	uint _max;
35 	
36 	constructor ()public{
37 		_max = now*1000;
38 	}
39 
40 	function regist(address a) public {
41 		_UserAddress = a;
42 	}
43 	
44 	function getAddress() public returns (address a){
45 		return _UserAddress;
46 	}
47 	
48 	function getloan() public returns(Loan memory _result) {
49 		return loan;
50 	}
51 	
52 	function setloan(Loan memory a) public{
53 		loan = a;
54 	}
55 	
56 	function confirmDone() public{
57 		_confirmTime = now;
58 		_isconfirmDone = true;
59 	}
60 	
61 	function confirmTime() public returns (uint result){
62 	    if(_isconfirmDone){
63 	        return _confirmTime;
64 	    }
65 	    return _max;
66 	}
67 	
68 }