ExampleofLoanContract

来自智能法律合约
20201027讨论 | 贡献2021年3月9日 (二) 15:46的版本 (建立内容为“=== <div style=" background:#EBEBEB; padding-top: 10px; height:45px; padding-left:15px "> Loan contract </div> === {| class="wikitable" ||    &nbs…”的新页面)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

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//monthly//you have to pay back tenorM month later
        installment : integer//monthly//the interest have to pay back
        funders : set Funder
    }
}
/* FunderT.sol */
pragma solidity >=0.4.0 <0.6.0;

contract FunderT{
	
	uint moneyFunded;
	uint moneyWillGet;
	
	address _FunderAddress;
	uint _max;
	
	function funderT() public{
		_max = now*1000;
	}
	function regist(address a) public {
		_FunderAddress = a;
	}
	
	function getAddress() public returns (address a){
		return _FunderAddress;
	}
	
	 function getmoneyFunded() public returns(uint _result){
		return moneyFunded;
	}
	
	function setmoneyFunded ( uint a) public{
		moneyFunded = a;
	}
	
/*	function getmoneyWillGet() public returns(uint _result){
		return moneyWillGet;
	}
	
	function setmoneyWillGet(uint a) public {
		moneyWillGet = a;
	}*/
	

    
    function setmoneyWillGet(uint256 newValue) public {
        moneyWillGet = newValue;
    }

   
    function getmoneyWillGet() public view returns (uint256) {
        return moneyWillGet;
    }
}
/* Lend.sol */
pragma solidity >=0.4.0 <0.6.0;
pragma experimental ABIEncoderV2;
		
import "./UserT.sol";
import "./FunderT.sol";

contract Lend {
	
	UserT User;
	FunderT Funder;
	uint start;
	
	
	mapping (string => Loan) loan;
	
	struct Loan{
		bytes32 operationName;
		address beneficiary;
		uint timelimit;
		uint fundingGoal;
		uint amount;
		uint balance;
		uint numFunders;
		uint interestRateM;
		uint gracePeriod;
		uint tenorM;
		uint installment;
		address funders;
	}
	
	
	
	constructor() public{
		start = now;
		User = new UserT();
		Funder = new FunderT();
	}
	
/*		constructor() public{
	    start = now;
		User = new UserT();
		Funder = new FunderT();
	}*/

	modifier onlyUser{
		require(User.getAddress()==msg.sender);
		_;
	}
	
	modifier onlyFunder{
		require(Funder.getAddress()==msg.sender);
		_;
	}
	
	modifier term2Modifier(UserT uid){
	//	require(now < uid.loan.timelimit);
	//	require(msg.value <= uid.loan.fundingGoal);
		_;
	}
	
	modifier term3Modifier(UserT uid){
//		require(now > (uid.loan.timelimit && uid.loan.amount < uid.loan.fundingGoal));
		_;
	}
	
	modifier term4Modifier{
//		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));
		_;
	}
	
	modifier term5Modifier{
		require(now > User.confirmTime());
		_;
	}
	
	modifier term6Modifier{
//		require(now > (User.confirmTime() && now < User.getloan.tenorM(msg.sender)));
//		require(User.getloan.balance(msg.sender) + User.getloan.installment(msg.sender));
		_;
	}
	
	function SubmitLoanApplication( Loan memory l) onlyUser() public {
		//USER CODE HERE
		//CHECK
	
	}
	
	function Contribute(UserT uid) onlyFunder() term2Modifier(uid) public payable {
		//USER CODE HERE
		//CHECK
	
	}
	
	function refund(UserT uid) onlyFunder() term3Modifier(uid) public payable {
		//USER CODE HERE
		msg.sender.transfer(Funder.getmoneyFunded());
		//CHECK
	
	}
	
	function confirm() onlyUser() term4Modifier() public {
		//USER CODE HERE
		//CHECK
	
	}
	
	function getHisLoan() onlyUser() term5Modifier() public payable {
		//USER CODE HERE
		msg.sender.transfer(User.getloan().amount);
		//CHECK
	
	}
	
	function payback() onlyUser() term6Modifier() public payable {
		//USER CODE HERE
		//User.getloan().funders.transfer(msg.sender);
		msg.sender.transfer(User.getloan().amount);
		//CHECK
	
	}
	
}
/* UserT.sol */
pragma solidity >=0.4.0 <0.6.0;
pragma experimental ABIEncoderV2;


import "./Lend.sol";

contract UserT{
	
	struct Loan{
		bytes32 operationName;
		address beneficiary;
		uint timelimit;
		uint fundingGoal;
		uint amount;
		uint balance;
		uint numFunders;
		uint interestRateM;
		uint gracePeriod;
		uint tenorM;
		uint installment;
		address funders;
	}
	
	
	
	
	Loan loan;
	//attributes of actionconfirm
	bool _isconfirmDone;
	uint _confirmTime;
	
	address _UserAddress;
	uint _max;
	
	constructor ()public{
		_max = now*1000;
	}

	function regist(address a) public {
		_UserAddress = a;
	}
	
	function getAddress() public returns (address a){
		return _UserAddress;
	}
	
	function getloan() public returns(Loan memory _result) {
		return loan;
	}
	
	function setloan(Loan memory a) public{
		loan = a;
	}
	
	function confirmDone() public{
		_confirmTime = now;
		_isconfirmDone = true;
	}
	
	function confirmTime() public returns (uint result){
	    if(_isconfirmDone){
	        return _confirmTime;
	    }
	    return _max;
	}
	
}