Examples
跳到导航
跳到搜索
EXAMPLE
(1)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;
}
} (2)Auction contract
Auction contract involves two parties: auctioneer who is an enterprise legal person engaged in auction activities, and bidders who are some citizens, legal persons or organizations to participate in bidding for auction targets. The highest bidding process is described as the following steps. The auctioneer starts the auction system after setting a reserve price and an auction end time, and waits for the auction to end; The bidders can bid anytime during the system. If the bid is greater than the current highest price, the system records it as new highest price, puts the bid into the fund pool, and returns the bid paid by the previous highest bidder; otherwise, the bidder fails and the bid is returned; After the auction time is over, the auctioneer can collect the highest bid from the fund pool.
// Simple Auction.scs
contract SimpleAuction{
party group bidders{
amount : Money
Bid()
withdrawOverbidMoney()
}
party auctioneer{
StartBidding(reservePrice : Money, biddingTime : Date)
StopBidding()
}
highestPrice : Money
highestBidder : biddersBiddingStop
Time : Date
term no1 : auctioneer can StartBidding,
when before auctioneer did StartBidding
where highestPrice = reservePrice and BiddingStopTime = biddingTime + now.
term no2 : bidders can Bid,
when after auctioneer did StartBidding and before BiddingStopTime
while deposit $ value > highestPrice
where highestPrice = value and highestBidder = this bidder and
this bidder : : amount = this bidder: :0ri amount + value .
term no3_1 : bidders can withdrawOverbidMoney,
when this bidder isn't highestBidder and this bidder: :amount > 0
while withdraw $this bidder : :amount
where this bidder : : amount = 0.
term no3_2 : bidders can withdrawOverbidMoney,
when this bidder is highestBidder and this bidder : : amount > highestPrice
while withdraw $this bidder : : amount - highestPrice
where this bidder : : amount = highestPrice.
term no4 : auctioneer can StopBidding,
when after BiddingStopTime and before auctioneer did StopBidding
while withdraw $highestPrice.
}/* auctioneerT.sol */
pragma solidity >=0.4.0 <0.6.0;
contract auctioneerT{
//attributes of actionStartBidding
bool _isStartBiddingDone;
uint _StartBiddingTime;
//attributes of actionCollectPayment
bool _isCollectPaymentDone;
uint _CollectPaymentTime;
address _auctioneerAddress;
uint _max;
constructor() public{
_max = now*1000;
}
function regist(address a) public {
_auctioneerAddress = a;
}
function getAddress() public returns (address a){
return _auctioneerAddress;
}
function StartBiddingDone() public{
_StartBiddingTime = now;
_isStartBiddingDone = true;
}
function StartBiddingTime() public returns (uint result){
if(_isStartBiddingDone){
return _StartBiddingTime;
}
return _max;
}
function CollectPaymentDone() public{
_CollectPaymentTime = now;
_isCollectPaymentDone = true;
}
function CollectPaymentTime() public returns (uint result){
if(_isCollectPaymentDone){
return _CollectPaymentTime;
}
return _max;
}
}/* biddersT.sol */
pragma solidity ^0.4.22;
contract biddersT{
struct bidderstype{
uint amount;
//attributes of actionBid
bool _isBidDone;
uint _BidTime;
//attributes of actionWithdrawOverbidMoney
bool _isWithdrawOverbidMoneyDone;
uint _WithdrawOverbidMoneyTime;
}
uint _max;//time max
uint _sum;//total member of this party
uint _BidDoneNum;
uint[] _BidTime;
uint _WithdrawOverbidMoneyDoneNum;
uint[] _WithdrawOverbidMoneyTime;
bidderstype _Empty;//used to initialize
bidderstype[] _biddersEntity;
address[] _biddersAddress;
mapping(address=>uint) _userlist;
function biddersT(){
_BidDoneNum = 0;
_WithdrawOverbidMoneyDoneNum = 0;
_biddersEntity.push(_Empty);
_max = now*1000;
}
function getSum() public returns(uint c){
return _sum;
}
function add(address a) public {
_biddersEntity.push(_Empty);
_biddersAddress.push(a);
// _userlist[address] = _sum;
_sum ++;
}
function remove(address a) public {
uint num = _userlist[a];
_biddersEntity[num] = _biddersEntity[_sum-1];
delete(_biddersEntity[num]);
delete(_userlist[a]);
_sum --;
}
function getList() public returns (address[] a){
return _biddersAddress;
}
function contains(address a) public returns (bool b){
return _userlist[a] != 0;
}
function getamount(address a) returns(uint _result){
uint num = _userlist[a];
return _biddersEntity[num].amount;
}
function setamount(address a, uint b){
uint num = _userlist[a];
_biddersEntity[num].amount = b;
}
function BidDone(address a){
uint num = _userlist[a];
_biddersEntity[num]._BidTime = now;
_biddersEntity[num]._isBidDone = true;
_BidTime.push(_biddersEntity[num]._BidTime);
_BidDoneNum ++;
}
function BidTime(address a) returns (uint result){
uint num = _userlist[a];
if(_biddersEntity[num]._isBidDone){
return _biddersEntity[num]._BidTime;
}
return _max;
}
function BidAllTime() returns (uint result){
if(_BidDoneNum == _max-1){
return _BidTime[_BidDoneNum-1];
}
return _max;
}
function BidSomeTime() returns (uint result){
if(_BidDoneNum >= 1){
return _BidTime[0];
}
return _max;
}
function WithdrawOverbidMoneyDone(address a){
uint num = _userlist[a];
_biddersEntity[num]._WithdrawOverbidMoneyTime = now;
_biddersEntity[num]._isWithdrawOverbidMoneyDone = true;
_WithdrawOverbidMoneyTime.push(_biddersEntity[num]._WithdrawOverbidMoneyTime);
_WithdrawOverbidMoneyDoneNum ++;
}
function WithdrawOverbidMoneyTime(address a) returns (uint result){
uint num = _userlist[a];
if(_biddersEntity[num]._isWithdrawOverbidMoneyDone){
return _biddersEntity[num]._WithdrawOverbidMoneyTime;
}
return _max;
}
function WithdrawOverbidMoneyAllTime() returns (uint result){
if(_WithdrawOverbidMoneyDoneNum == _max-1){
return _WithdrawOverbidMoneyTime[_WithdrawOverbidMoneyDoneNum-1];
}
return _max;
}
function WithdrawOverbidMoneySomeTime() returns (uint result){
if(_WithdrawOverbidMoneyDoneNum >= 1){
return _WithdrawOverbidMoneyTime[0];
}
return _max;
}
}/* goodsOwnerT.sol */
pragma solidity ^0.4.22;
contract goodsOwnerT{
//attributes of actionStartBidding
bool _isStartBiddingDone;
uint _StartBiddingTime;
bool _isgoodsOwnerDone;
uint _max;
//attributes of actionStopBidding
bool _isStopBiddingDone;
uint _StopBiddingTime;
address[] _goodsOwnerAddress;
constructor() public{
_max = now*1000;
}
function regist (address[] a){
_goodsOwnerAddress = a;
}
function getAddress() public returns (address[] a){
return _goodsOwnerAddress;
}
function StartBiddingDone(address[] a){
// StartBiddingTime = now;
_isgoodsOwnerDone = true;
//_isStartBiddingDone = true;
}
function StartBiddingTime() returns (uint result){
if(_isStartBiddingDone){
return _StartBiddingTime;
}
return _max;
}
function StopBiddingDone(address[] a){
// StopBiddingTime = now;
_isgoodsOwnerDone = true;
// _StopBiddingTime = true;
}
function StopBiddingTime() returns (uint result){
if(_isStopBiddingDone){
return _StopBiddingTime;
}
return _max;
}
}/* SimpleAuction.sol */
pragma solidity ^0.4.22;
contract SimpleAuction {
address public beneficiary;
uint public auctionEnd;
address public highestBidder;
uint public highestBid;
mapping(address => uint) pendingReturns;
bool ended;
event HighestBidIncreased(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);
constructor(uint _biddingTime, address _beneficiary) public {
beneficiary = _beneficiary;
auctionEnd = now + _biddingTime;
}
function bid() public payable {
require(now <= auctionEnd, "Auction already ended.");
require(msg.value > highestBid, "There already is a higher bid.");
if (highestBid != 0) {
pendingReturns[highestBidder] += highestBid;
}
highestBidder = msg.sender;
highestBid = msg.value;
HighestBidIncreased(msg.sender, msg.value);
}
function withdraw() public returns (bool) {
uint amount = pendingReturns[msg.sender];
if (amount > 0) {
pendingReturns[msg.sender] = 0;
if (!msg.sender.send(amount)) {
// No need to call throw here, just reset the amount owing
pendingReturns[msg.sender] = amount;
return false;
}
}
return true;
}
function auctionEnd() public {
require(now >= auctionEnd, "Auction not yet ended.");
require(!ended, "auctionEnd has already been called.");
ended = true;
AuctionEnded(highestBidder, highestBid);
beneficiary.transfer(highestBid);
}
} (3)House-leasing contract
House-leasing contract, takes assets as the transaction subject between lessor and lessee. There are 7 terms in the contract, including two aspects: 1) the lessor can register the house after depositing the rental deposit, and 2) the lessee can keep the right to use the house during the leasing period after depositing the rent. After confirming the leased house, the contract requires that the lessee shall pay the rent, and the lessor must transfer the right to the lessee within one week. Meanwhile, the contract stipulates the pre-condition for the lessor to charge the rent paid by the lessee, and the postcondition for both parties to get their deposits back after the house inspection.
// HouseLease.scs
contract Houselease{
party Renter{
registerHouse()
collectRent()
collectBail()
transferHouse()
checkHouse()
}
party Tenant{
confirmLease(endLeasingDuration:Date,
payDuration:Date)
payRent()
returnHouse()
collectBail()
}
house : House
infos : contractInfo
term term1 : Renter can registerHouse
while deposit $infos::renterBail.
term term2 : Tenant can confirmLease
when after Renter did registerHouse
while deposit $infos::tenantBail
where infos::startLeasingTime = now and
infos::endLeasingTime = endLeasingDuration + now
and infos::payDate = payDuration + now
and infos::payDuration = payDuration.
term term3 : Renter must transferHouse
when within 7 day after Tenant did confirmLease
while deposit $ house::useRight.
term term4 : Tenant must payRent
when before Tenant did confirmLease
while deposit $infos::rental
withdraw $house::useRight
where infos::payDate = infos::payDate + infos::payDuration
and infos::totalRental = infos::totalRental + infos::rental.
term term5 : Renter can collectRent
while withdraw $infos::totalRental
where infos::totalRental = 0.
term term6 : Tenant must returnHouse
when within 7 day after Renter did checkHouse
while deposit $house::useRight
transfer $house::useRight to Renter.
term term7_1 : Renter can collectBail
when within 15 day after Renter did checkHouse
while withdraw $infos::renterBail.
term term7_2 : Tenant can collectBail
when within 15 day after Renter did checkHouse
while withdraw $infos::tenantBail.
type contractInfo {
renterBail : Money
tenantBail : Money
rental : Money
totalRental : Money
penalty : Money
startLeasingTime : Date
endLeasingTime : Date
payDate : Date
payDuration : Date
}
type House {
ownershipNumber : integer
location : String
area : integer
usage : String
price : Money
useRight : String
usufruct : String
dispositionRight : String
possessionRight : String
}
}/* Houselease.sol */
pragma solidity ^0.4.0;
import "./RenterT.sol";
import "./TenantT.sol";
contract Houselease {
RenterT Renter;
TenantT Tenant;
House house;
contractInfo infos;
uint start;
struct contractInfo{
uint renterBail;
uint tenantBail;
uint rental;
uint totalRental;
uint penalty;
uint startLeasingTime;
uint endLeasingTime;
uint payDate;
uint payDuration;
}
struct House{
uint ownershipNumber;
bytes32 location;
uint area;
bytes32 usage;
uint price;
uint useRight;
uint usufruct;
uint dispositionRight;
uint possessionRight;
}
function Houselease(){
start = now;
Renter = new RenterT();
Tenant = new TenantT();
}
modifier onlyRenter{
require(Renter.contains(msg.sender));
_;
}
modifier onlyTenant{
require(Tenant.contains(msg.sender));
_;
}
modifier term2Modifier{
require(now > Renter.registerHouseTime());
require(infos.tenantBail > 0);
_;
}
modifier term3Modifier{
require((now > Tenant.confirmLeaseTime()) &&(now < Tenant.confirmLeaseTime()+604800));
require(house.useRight > 0);
_;
}
modifier term4Modifier{
require(now < Tenant.confirmLeaseTime());
require(infos.rental > 0);
_;
}
modifier term6Modifier{
require((now > Renter.checkHouseTime()) &&(now < Renter.checkHouseTime()+604800));
require(house.useRight > 0);
_;
}
modifier term7_1Modifier{
require((now > Renter.checkHouseTime()) &&(now < Renter.checkHouseTime()+1296000));
_;
}
modifier term7_2Modifier{
require((now > Renter.checkHouseTime()) &&(now < Renter.checkHouseTime()+1296000));
_;
}
function registerHouse() onlyRenter() public payable {
//USER CODE HERE
//CHECK
}
function confirmLease(uint endLeasingDuration, uint payDuration) onlyTenant() term2Modifier() public payable {
//USER CODE HERE
infos.startLeasingTime = now;
infos.endLeasingTime = endLeasingDuration + now;
infos.payDate = payDuration + now;
infos.payDuration = payDuration;
//CHECK
assert(infos.startLeasingTime == now && (infos.endLeasingTime == endLeasingDuration + now && (infos.payDate == payDuration + now && infos.payDuration == payDuration)));
}
function transferHouse() onlyRenter() term3Modifier() public payable {
//USER CODE HERE
//CHECK
}
function payRent() onlyTenant() term4Modifier() public payable {
//USER CODE HERE
infos.payDate = infos.payDate + infos.payDuration;
infos.totalRental = infos.totalRental + infos.rental;
msg.sender.transfer(house.useRight);
//CHECK
assert(infos.payDate == infos.payDate + infos.payDuration && infos.totalRental == infos.totalRental + infos.rental);
}
function collectRent() onlyRenter() term7_1Modifier() public payable {
//USER CODE HERE
infos.totalRental = 0;
msg.sender.transfer(infos.totalRental);
//CHECK
assert(infos.totalRental == 0);
}
function returnHouse() onlyTenant() term6Modifier() public payable {
//USER CODE HERE
address(Renter).transfer(house.useRight);
//CHECK
}
}/* RenterT.sol */
pragma solidity ^0.4.22;
contract RenterT{
//attributes of actionregisterHouse
bool _isregisterHouseDone;
uint _registerHouseTime;
//attributes of actioncollectRent
bool _iscollectRentDone;
uint _collectRentTime;
//attributes of actioncollectBail
bool _iscollectBailDone;
uint _collectBailTime;
//attributes of actiontransferHouse
bool _istransferHouseDone;
uint _transferHouseTime;
//attributes of actioncheckHouse
bool _ischeckHouseDone;
uint _checkHouseTime;
uint _max;
bool _isRenterDone;
uint _i;
address[] _RenterAddress;
function RenterT(){
_max = now*1000;
}
function regist(address a) public {
_RenterAddress[_i] = a;
_i++;
}
function getAddress(uint _i) public returns (address a){
return _RenterAddress[_i];
}
function registerHouseDone(address a){
_registerHouseTime = now;
_isRenterDone = true;
}
function registerHouseTime() returns (uint result){
if(_isregisterHouseDone){
return _registerHouseTime;
}
return _max;
}
function collectRentDone(address a){
_collectRentTime = now;
_isRenterDone = true;
}
function collectRentTime() returns (uint result){
if(_iscollectRentDone){
return _collectRentTime;
}
return _max;
}
function collectBailDone(address a){
_collectBailTime = now;
_isRenterDone = true;
}
function collectBailTime() returns (uint result){
if(_iscollectBailDone){
return _collectBailTime;
}
return _max;
}
function transferHouseDone(address a){
_transferHouseTime = now;
_isRenterDone = true;
}
function transferHouseTime() returns (uint result){
if(_istransferHouseDone){
return _transferHouseTime;
}
return _max;
}
function checkHouseDone(address a){
_checkHouseTime = now;
_isRenterDone = true;
}
function checkHouseTime() returns (uint result){
if(_ischeckHouseDone){
return _checkHouseTime;
}
return _max;
}
function contains(address a) returns (bool result) {
for (uint _j = 0; _j < _RenterAddress.length; _j++ ) { //for loop example
if (a == _RenterAddress[_j]){
return true;
}
}
return false;
}
}/* TenantT.sol */
pragma solidity ^0.4.22;
contract TenantT{
//attributes of actionconfirmLease
bool _isconfirmLeaseDone;
uint _confirmLeaseTime;
//attributes of actionpayRent
bool _ispayRentDone;
uint _payRentTime;
//attributes of actionreturnHouse
bool _isreturnHouseDone;
uint _returnHouseTime;
//attributes of actioncollectBail
bool _iscollectBailDone;
uint _collectBailTime;
uint _max;
bool _isTenantDone;
uint _i;
address[] _TenantAddress;
function TenantT(){
_max = now*1000;
}
function regist(address a) public {
_TenantAddress[_i] = a;
_i++;
}
function getAddress(uint _i) public returns (address a){
return _TenantAddress[_i];
}
function confirmLeaseDone(address a){
_confirmLeaseTime = now;
_isTenantDone = true;
}
function confirmLeaseTime() returns (uint result){
if(_isconfirmLeaseDone){
return _confirmLeaseTime;
}
return _max;
}
function payRentDone(address a){
_payRentTime = now;
_isTenantDone = true;
}
function payRentTime() returns (uint result){
if(_ispayRentDone){
return _payRentTime;
}
return _max;
}
function returnHouseDone(address a){
_returnHouseTime = now;
_isTenantDone = true;
}
function returnHouseTime() returns (uint result){
if(_isreturnHouseDone){
return _returnHouseTime;
}
return _max;
}
function collectBailDone(address a){
_collectBailTime = now;
_isTenantDone = true;
}
function collectBailTime() returns (uint result){
if(_iscollectBailDone){
return _collectBailTime;
}
return _max;
}
function contains(address a) returns (bool result) {
for (uint _j = 0; _j < _TenantAddress.length; _j++ ) { //for loop example
if (a == _TenantAddress[_j]){
return true;
}
}
return false;
}
} (4)Purchase contract
Purchase contract, transfers the ownership of goods from a seller to a buyer through logistics company. There are 10 terms in the contract, including three parts: 1) once the goods are confirmed, the seller will sign a contract and take twice the contracting price as the delivery price, 2) the goods are sent to the buyer through logistics when the buyer purchases the goods, and 3) only when the logistics successfully delivers the goods to the buyer can the seller get back the payment. According to relevant laws, the contract requires the buyer can apply for compensation if goods do not arrive within 10 days after payment, or the seller can automatically confirm the arrival if the buyer does not confirm or apply for refund within 7 days after arrival.
// purchase.scs
contract Purchase{
party seller{
//定义seller角色,有四个动作
create()
abort()
confirmClaim()
autoConfirm()
}
party buyer{
confirmPurchase()//
confirmReceived()
overtimeClaim()
claim()
revokeClaim()
}
party logistics{
//第三方角色,物流公司
arrive()
}
balance : integer
xxxDescription : goods//定义一个商品描述,good是下面定义的商品结构体
term no1 : seller can create
while deposit $ xxxDescription::price*2.
//卖家可以创建合约,同时要存入物品两倍价格作为保证金
term no2 : buyer can confirmPurchase
when after seller did create
while deposit $ xxxDescription::price*2.
//买家创建合约后买家可以确认购买,同时要存入物品两倍价格作为保证金和货款
term no3 : seller can abort
when before buyer did confirmPurchase
while withdraw $ xxxDescription::price*2.
//卖家在买家确认购买之前,可以撤回合约,并取走两倍的保证金
term no4 : logistics must arrive
when within 10 day after buyer did confirmPurchase.
//买家付款后10天内必须到货
term no5 : buyer must confirmReceived
when within 7 day after logistics did arrive
while
withdraw $ xxxDescription::price
transfer $ balance to seller.
//买家在到货后7天内必须确认到货,同时双方取走保证金,卖家取走货款
term no6 : buyer can claim
when within 7 day after logistics did arrive.
//买家可以在到货7天内申请赔款
term no7 : buyer can overtimeClaim
when after start + 10 day or after logistics did arrive.
//如果货物在付款后10天内没到,买家可以申请赔款
term no8 : seller can confirmClaim
when after buyer did claim or after buyer did overtimeClaim
while transfer $ balance to buyer.
//卖家可以同意退款,并把所有保证金及货款转给买家
term no9 : buyer can revokeClaim
when after buyer did claim or after buyer did overtimeClaim
while
withdraw $ xxxDescription::price
transfer $ balance to seller.
//买家可以撤回退款申请
term no10 : seller can autoConfirm
when before buyer did claim and 7 day after logistics did arrive
while
withdraw $ xxxDescription::price
transfer $ balance to seller.
//如果到货七天内买家没有确认或申请退款,卖家可以自动确认到货。
type goods {
name : Name//物品名称
quantity : integer//物品数量
price : Money//价格
package : String//运送包装
}
}/* buyerT.sol */
pragma solidity ^0.4.22;
contract buyerT{
//attributes of actionconfirmPurchase
bool _isconfirmPurchaseDone;
uint _confirmPurchaseTime;
//attributes of actionconfirmReceived
bool _isconfirmReceivedDone;
uint _confirmReceivedTime;
//attributes of actionovertimeClaim
bool _isovertimeClaimDone;
uint _overtimeClaimTime;
//attributes of actionclaim
bool _isclaimDone;
uint _claimTime;
//attributes of actionrevokeClaim
bool _isrevokeClaimDone;
uint _revokeClaimTime;
uint _max;
bool _isbuyerDone;
uint _i = 0;
address[] _buyerAddress;
function buyerT(){
_max = now*1000;
}
function regist(address a) public {
_buyerAddress[_i] = a;
_i++;
}
function getAddress() public returns (address a){
return _buyerAddress[_i];
}
function confirmPurchaseDone(address a){
_confirmPurchaseTime = now;
_isbuyerDone = true;
}
function confirmPurchaseTime() returns (uint result){
if(_isconfirmPurchaseDone){
return _confirmPurchaseTime;
}
return _max;
}
function confirmReceivedDone(address a){
_confirmReceivedTime = now;
_isbuyerDone = true;
}
function confirmReceivedTime() returns (uint result){
if(_isconfirmReceivedDone){
return _confirmReceivedTime;
}
return _max;
}
function overtimeClaimDone(address a){
_overtimeClaimTime = now;
_isbuyerDone = true;
}
function overtimeClaimTime() returns (uint result){
if(_isovertimeClaimDone){
return _overtimeClaimTime;
}
return _max;
}
function claimDone(address a){
_claimTime = now;
_isbuyerDone = true;
}
function claimTime() returns (uint result){
if(_isclaimDone){
return _claimTime;
}
return _max;
}
function revokeClaimDone(address a){
_revokeClaimTime = now;
_isbuyerDone = true;
}
function revokeClaimTime() returns (uint result){
if(_isrevokeClaimDone){
return _revokeClaimTime;
}
return _max;
}
function contains(address a) returns (bool result) {
for (uint _j = 0; _j < _buyerAddress.length; _j++ ) { //for loop example
if (a == _buyerAddress[_j]){
return true;
}
}
return false;
}
}/* logisticsT.sol */
pragma solidity ^0.4.22;
contract logisticsT{
//attributes of actionarrive
bool _isarriveDone;
uint _arriveTime;
uint _max;
bool _islogisticsDone;
uint _i = 0;
address[] _logisticsAddress;
function logisticsT(){
_max = now*1000;
}
function regist(address a) public {
_logisticsAddress[_i] = a;
}
function getAddress() public returns (address a){
return _logisticsAddress[_i];
}
function arriveDone(address a){
_arriveTime = now;
_islogisticsDone = true;
}
function arriveTime() returns (uint result){
if(_isarriveDone){
return _arriveTime;
}
return _max;
}
function contains(address a) returns (bool result) {
for (uint _j = 0; _j < _logisticsAddress.length; _j++ ) { //for loop example
if (a == _logisticsAddress[_j]){
return true;
}
}
return false;
}
}/* Purchase.sol */
pragma solidity ^0.4.0;
import "./sellerT.sol";
import "./buyerT.sol";
import "./logisticsT.sol";
contract Purchase {
sellerT seller;
buyerT buyer;
logisticsT logistics;
uint balance;
goods xxxDescription;
uint start;
struct goods{
bytes32 name;
uint quantity;
uint price;
bytes32 package;
}
function Purchase(){
start = now;
seller = new sellerT();
buyer = new buyerT();
logistics = new logisticsT();
}
modifier onlyseller{
require(seller.contains(msg.sender));
_;
}
modifier onlybuyer{
require(buyer.contains(msg.sender));
_;
}
modifier onlylogistics{
require(logistics.contains(msg.sender));
_;
}
modifier no2Modifier{
require(now > seller.createTime());
require(xxxDescription.price * 2 > 0);
_;
}
modifier no3Modifier{
require(now < buyer.confirmPurchaseTime());
_;
}
modifier no4Modifier{
require((now > buyer.confirmPurchaseTime()) &&(now < buyer.confirmPurchaseTime()+864000));
_;
}
modifier no5Modifier{
require((now > logistics.arriveTime()) &&(now < logistics.arriveTime()+604800));
_;
}
modifier no6Modifier{
require((now > logistics.arriveTime()) &&(now < logistics.arriveTime()+604800));
_;
}
modifier no7Modifier{
require(now > start + 864000 || now > logistics.arriveTime());
_;
}
modifier no8Modifier{
require(now > buyer.claimTime() || now > buyer.overtimeClaimTime());
_;
}
modifier no9Modifier{
require(now > buyer.claimTime() || now > buyer.overtimeClaimTime());
_;
}
modifier no10Modifier{
require(now < buyer.claimTime() && now > logistics.arriveTime()+604800);
_;
}
function create() onlyseller() public payable {
//USER CODE HERE
//CHECK
}
function confirmPurchase() onlybuyer() no2Modifier() public payable {
//USER CODE HERE
//CHECK
}
function abort() onlyseller() no3Modifier() public payable {
//USER CODE HERE
msg.sender.transfer(xxxDescription.price * 2);
//CHECK
}
function arrive() onlylogistics() no4Modifier() public {
//USER CODE HERE
//CHECK
}
function confirmReceived() onlybuyer() no5Modifier() public payable {
//USER CODE HERE
msg.sender.transfer(xxxDescription.price);
address(seller).transfer(balance);
//CHECK
}
function claim() onlybuyer() no6Modifier() public {
//USER CODE HERE
//CHECK
}
function overtimeClaim() onlybuyer() no7Modifier() public {
//USER CODE HERE
//CHECK
}
function confirmClaim() onlyseller() no8Modifier() public payable {
//USER CODE HERE
address(buyer).transfer(balance);
//CHECK
}
function revokeClaim() onlybuyer() no9Modifier() public payable {
//USER CODE HERE
msg.sender.transfer(xxxDescription.price);
address(seller).transfer(balance);
//CHECK
}
function autoConfirm() onlyseller() no10Modifier() public payable {
//USER CODE HERE
msg.sender.transfer(xxxDescription.price);
address(seller).transfer(balance);
//CHECK
}
}/* sellerT.sol */
pragma solidity ^0.4.22;
contract sellerT{
//attributes of actioncreate
bool _iscreateDone;
uint _createTime;
//attributes of actionabort
bool _isabortDone;
uint _abortTime;
//attributes of actionconfirmClaim
bool _isconfirmClaimDone;
uint _confirmClaimTime;
//attributes of actionautoConfirm
bool _isautoConfirmDone;
uint _autoConfirmTime;
uint _max;
bool _issellerDone;
uint _i = 0;
address[] _sellerAddress;
function sellerT(){
_max = now*1000;
}
function regist(address a) public {
_sellerAddress[_i] = a;
_i++;
}
function getAddress() public returns (address a){
return _sellerAddress[_i];
}
function createDone(address a){
_createTime = now;
_issellerDone = true;
}
function createTime() returns (uint result){
if(_iscreateDone){
return _createTime;
}
return _max;
}
function abortDone(address a){
_abortTime = now;
_issellerDone = true;
}
function abortTime() returns (uint result){
if(_isabortDone){
return _abortTime;
}
return _max;
}
function confirmClaimDone(address a){
_confirmClaimTime = now;
_issellerDone = true;
}
function confirmClaimTime() returns (uint result){
if(_isconfirmClaimDone){
return _confirmClaimTime;
}
return _max;
}
function autoConfirmDone(address a){
_autoConfirmTime = now;
_issellerDone = true;
}
function autoConfirmTime() returns (uint result){
if(_isautoConfirmDone){
return _autoConfirmTime;
}
return _max;
}
function contains(address a) returns (bool result) {
for (uint _j = 0; _j < _sellerAddress.length; _j++ ) { //for loop example
if (a == _sellerAddress[_j]){
return true;
}
}
return false;
}
}