本次範例以短租為出發點,設計一個短租交易的合約,承租人可以決定一次付清或者每天固定時間支付當日租金,房東只能領取單日租金..(這邊我還要思考一下...)
pragma solidity^0.4.25;
contract DailyRentHouse{
address public Tenant; //承租人
address public Landlord; //房東
uint256 public Period; //算天數
uint256 public DaliyRent; //日租
uint256 public TotalRent; //總租金
uint256 public AmountPaid; //已付次數
uint256 public LoadingTime; //下一次付款時間
event Paid(); //record Paid log
event PaidAll(); //record PaidAll log
event Received(); //record Received log
enum State{
Creat, //初始
Paid, //已支付
Close //已結清
}
State public state;
modifier onlyTenant{
require(msg.sender == Tenant,"你不是承租人");
_;
}
modifier Stage(State _state){
require(_state == state,"不符合現階段");
_;
}
constructor(address _tenant, uint256 _daliyRent, uint256 _period) public{
Landlord = msg.sender;
Tenant = _tenant;
Period = _period;
DaliyRent = _daliyRent;
TotalRent = _daliyRent * _period;
}
//每天支付
function payDaily() public payable onlyTenant{
require(msg.value == DaliyRent,"支付費用不符合日租費用");
require(AmountPaid <= Period,"已付清約定租期");
if(AmountPaid == 0){
LoadingTime = now + 1 days;
}else{
require(now >= LoadingTime,"下一次付款時間還沒到");
LoadingTime = now + 1 days;
}
state = State.Paid; //已支付階段
AmountPaid++; //支付次數
emit Paid();
}
//一次付清
function payAll() public payable onlyTenant{
require(msg.value == TotalRent,"租金不完整");
require(AmountPaid <= Period,"已付清約定租期");
state = State.Paid;
AmountPaid = Period; //已付清全部租金
emit PaidAll();
}
//房東收取已支付的租金
function receive() public Stage(State.Paid){
Landlord.transfer(DaliyRent);
emit Received();
if(address(this).balance == 0){
state = State.Close;
}
}
}
(待補)