ExampleofPurchaseContract
跳到导航
跳到搜索
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. 1 // purchase.scs
2 contract Purchase{
3 party seller{
4 //定义seller角色,有四个动作
5 create()
6 abort()
7 confirmClaim()
8 autoConfirm()
9 }
10
11 party buyer{
12 confirmPurchase()//
13 confirmReceived()
14 overtimeClaim()
15 claim()
16 revokeClaim()
17 }
18
19 party logistics{
20 //第三方角色,物流公司
21 arrive()
22 }
23
24 balance : integer
25
26 xxxDescription : goods//定义一个商品描述,good是下面定义的商品结构体
27
28 term no1 : seller can create
29 while deposit $ xxxDescription::price*2.
30 //卖家可以创建合约,同时要存入物品两倍价格作为保证金
31
32 term no2 : buyer can confirmPurchase
33 when after seller did create
34 while deposit $ xxxDescription::price*2.
35 //买家创建合约后买家可以确认购买,同时要存入物品两倍价格作为保证金和货款
36
37 term no3 : seller can abort
38 when before buyer did confirmPurchase
39 while withdraw $ xxxDescription::price*2.
40 //卖家在买家确认购买之前,可以撤回合约,并取走两倍的保证金
41
42 term no4 : logistics must arrive
43 when within 10 day after buyer did confirmPurchase.
44 //买家付款后10天内必须到货
45
46 term no5 : buyer must confirmReceived
47 when within 7 day after logistics did arrive
48 while
49 withdraw $ xxxDescription::price
50 transfer $ balance to seller.
51 //买家在到货后7天内必须确认到货,同时双方取走保证金,卖家取走货款
52
53 term no6 : buyer can claim
54 when within 7 day after logistics did arrive.
55 //买家可以在到货7天内申请赔款
56
57 term no7 : buyer can overtimeClaim
58 when after start + 10 day or after logistics did arrive.
59 //如果货物在付款后10天内没到,买家可以申请赔款
60
61 term no8 : seller can confirmClaim
62 when after buyer did claim or after buyer did overtimeClaim
63 while transfer $ balance to buyer.
64 //卖家可以同意退款,并把所有保证金及货款转给买家
65
66 term no9 : buyer can revokeClaim
67 when after buyer did claim or after buyer did overtimeClaim
68 while
69 withdraw $ xxxDescription::price
70 transfer $ balance to seller.
71 //买家可以撤回退款申请
72
73 term no10 : seller can autoConfirm
74 when before buyer did claim and 7 day after logistics did arrive
75 while
76 withdraw $ xxxDescription::price
77 transfer $ balance to seller.
78 //如果到货七天内买家没有确认或申请退款,卖家可以自动确认到货。
79
80 type goods {
81 name : Name//物品名称
82 quantity : integer//物品数量
83 price : Money//价格
84 package : String//运送包装
85 }
86 }
1 /* buyerT.sol */
2 pragma solidity ^0.4.22;
3
4 contract buyerT{
5
6
7 //attributes of actionconfirmPurchase
8 bool _isconfirmPurchaseDone;
9 uint _confirmPurchaseTime;
10
11 //attributes of actionconfirmReceived
12 bool _isconfirmReceivedDone;
13 uint _confirmReceivedTime;
14
15 //attributes of actionovertimeClaim
16 bool _isovertimeClaimDone;
17 uint _overtimeClaimTime;
18
19 //attributes of actionclaim
20 bool _isclaimDone;
21 uint _claimTime;
22
23 //attributes of actionrevokeClaim
24 bool _isrevokeClaimDone;
25 uint _revokeClaimTime;
26
27 uint _max;
28 bool _isbuyerDone;
29 uint _i = 0;
30
31 address[] _buyerAddress;
32 function buyerT(){
33 _max = now*1000;
34 }
35
36 function regist(address a) public {
37 _buyerAddress[_i] = a;
38 _i++;
39 }
40
41 function getAddress() public returns (address a){
42 return _buyerAddress[_i];
43 }
44
45 function confirmPurchaseDone(address a){
46 _confirmPurchaseTime = now;
47 _isbuyerDone = true;
48 }
49
50 function confirmPurchaseTime() returns (uint result){
51 if(_isconfirmPurchaseDone){
52 return _confirmPurchaseTime;
53 }
54 return _max;
55 }
56
57 function confirmReceivedDone(address a){
58 _confirmReceivedTime = now;
59 _isbuyerDone = true;
60 }
61
62 function confirmReceivedTime() returns (uint result){
63 if(_isconfirmReceivedDone){
64 return _confirmReceivedTime;
65 }
66 return _max;
67 }
68
69 function overtimeClaimDone(address a){
70 _overtimeClaimTime = now;
71 _isbuyerDone = true;
72 }
73
74 function overtimeClaimTime() returns (uint result){
75 if(_isovertimeClaimDone){
76 return _overtimeClaimTime;
77 }
78 return _max;
79 }
80
81 function claimDone(address a){
82 _claimTime = now;
83 _isbuyerDone = true;
84 }
85
86 function claimTime() returns (uint result){
87 if(_isclaimDone){
88 return _claimTime;
89 }
90 return _max;
91 }
92
93 function revokeClaimDone(address a){
94 _revokeClaimTime = now;
95 _isbuyerDone = true;
96 }
97
98 function revokeClaimTime() returns (uint result){
99 if(_isrevokeClaimDone){
100 return _revokeClaimTime;
101 }
102 return _max;
103 }
104
105 function contains(address a) returns (bool result) {
106 for (uint _j = 0; _j < _buyerAddress.length; _j++ ) { //for loop example
107 if (a == _buyerAddress[_j]){
108 return true;
109 }
110 }
111 return false;
112 }
113
114 }
1 /* logisticsT.sol */
2 pragma solidity ^0.4.22;
3
4 contract logisticsT{
5
6
7 //attributes of actionarrive
8 bool _isarriveDone;
9 uint _arriveTime;
10
11 uint _max;
12 bool _islogisticsDone;
13 uint _i = 0;
14
15 address[] _logisticsAddress;
16 function logisticsT(){
17 _max = now*1000;
18 }
19
20 function regist(address a) public {
21 _logisticsAddress[_i] = a;
22 }
23
24 function getAddress() public returns (address a){
25 return _logisticsAddress[_i];
26 }
27
28 function arriveDone(address a){
29 _arriveTime = now;
30 _islogisticsDone = true;
31 }
32
33 function arriveTime() returns (uint result){
34 if(_isarriveDone){
35 return _arriveTime;
36 }
37 return _max;
38 }
39
40 function contains(address a) returns (bool result) {
41 for (uint _j = 0; _j < _logisticsAddress.length; _j++ ) { //for loop example
42 if (a == _logisticsAddress[_j]){
43 return true;
44 }
45 }
46 return false;
47 }
48
49 }
1 /* Purchase.sol */
2 pragma solidity ^0.4.0;
3
4 import "./sellerT.sol";
5 import "./buyerT.sol";
6 import "./logisticsT.sol";
7
8 contract Purchase {
9
10 sellerT seller;
11 buyerT buyer;
12 logisticsT logistics;
13
14 uint balance;
15 goods xxxDescription;
16
17 uint start;
18 struct goods{
19 bytes32 name;
20 uint quantity;
21 uint price;
22 bytes32 package;
23 }
24
25 function Purchase(){
26 start = now;
27 seller = new sellerT();
28 buyer = new buyerT();
29 logistics = new logisticsT();
30 }
31
32 modifier onlyseller{
33 require(seller.contains(msg.sender));
34 _;
35 }
36
37 modifier onlybuyer{
38 require(buyer.contains(msg.sender));
39 _;
40 }
41
42 modifier onlylogistics{
43 require(logistics.contains(msg.sender));
44 _;
45 }
46
47 modifier no2Modifier{
48 require(now > seller.createTime());
49 require(xxxDescription.price * 2 > 0);
50 _;
51 }
52
53 modifier no3Modifier{
54 require(now < buyer.confirmPurchaseTime());
55 _;
56 }
57
58 modifier no4Modifier{
59 require((now > buyer.confirmPurchaseTime()) &&(now < buyer.confirmPurchaseTime()+864000));
60 _;
61 }
62
63 modifier no5Modifier{
64 require((now > logistics.arriveTime()) &&(now < logistics.arriveTime()+604800));
65 _;
66 }
67
68 modifier no6Modifier{
69 require((now > logistics.arriveTime()) &&(now < logistics.arriveTime()+604800));
70 _;
71 }
72
73 modifier no7Modifier{
74 require(now > start + 864000 || now > logistics.arriveTime());
75 _;
76 }
77
78 modifier no8Modifier{
79 require(now > buyer.claimTime() || now > buyer.overtimeClaimTime());
80 _;
81 }
82
83 modifier no9Modifier{
84 require(now > buyer.claimTime() || now > buyer.overtimeClaimTime());
85 _;
86 }
87
88 modifier no10Modifier{
89 require(now < buyer.claimTime() && now > logistics.arriveTime()+604800);
90 _;
91 }
92
93 function create() onlyseller() public payable {
94 //USER CODE HERE
95 //CHECK
96
97 }
98
99 function confirmPurchase() onlybuyer() no2Modifier() public payable {
100 //USER CODE HERE
101 //CHECK
102
103 }
104
105 function abort() onlyseller() no3Modifier() public payable {
106 //USER CODE HERE
107 msg.sender.transfer(xxxDescription.price * 2);
108 //CHECK
109
110 }
111
112 function arrive() onlylogistics() no4Modifier() public {
113 //USER CODE HERE
114 //CHECK
115
116 }
117
118 function confirmReceived() onlybuyer() no5Modifier() public payable {
119 //USER CODE HERE
120 msg.sender.transfer(xxxDescription.price);
121 address(seller).transfer(balance);
122 //CHECK
123
124 }
125
126 function claim() onlybuyer() no6Modifier() public {
127 //USER CODE HERE
128 //CHECK
129
130 }
131
132 function overtimeClaim() onlybuyer() no7Modifier() public {
133 //USER CODE HERE
134 //CHECK
135
136 }
137
138 function confirmClaim() onlyseller() no8Modifier() public payable {
139 //USER CODE HERE
140 address(buyer).transfer(balance);
141 //CHECK
142
143 }
144
145 function revokeClaim() onlybuyer() no9Modifier() public payable {
146 //USER CODE HERE
147 msg.sender.transfer(xxxDescription.price);
148 address(seller).transfer(balance);
149 //CHECK
150
151 }
152
153 function autoConfirm() onlyseller() no10Modifier() public payable {
154 //USER CODE HERE
155 msg.sender.transfer(xxxDescription.price);
156 address(seller).transfer(balance);
157 //CHECK
158
159 }
160
161 }
1 /* sellerT.sol */
2 pragma solidity ^0.4.22;
3
4 contract sellerT{
5
6
7 //attributes of actioncreate
8 bool _iscreateDone;
9 uint _createTime;
10
11 //attributes of actionabort
12 bool _isabortDone;
13 uint _abortTime;
14
15 //attributes of actionconfirmClaim
16 bool _isconfirmClaimDone;
17 uint _confirmClaimTime;
18
19 //attributes of actionautoConfirm
20 bool _isautoConfirmDone;
21 uint _autoConfirmTime;
22
23 uint _max;
24 bool _issellerDone;
25 uint _i = 0;
26
27 address[] _sellerAddress;
28 function sellerT(){
29 _max = now*1000;
30 }
31
32 function regist(address a) public {
33 _sellerAddress[_i] = a;
34 _i++;
35 }
36
37 function getAddress() public returns (address a){
38 return _sellerAddress[_i];
39 }
40
41 function createDone(address a){
42 _createTime = now;
43 _issellerDone = true;
44 }
45
46 function createTime() returns (uint result){
47 if(_iscreateDone){
48 return _createTime;
49 }
50 return _max;
51 }
52
53 function abortDone(address a){
54 _abortTime = now;
55 _issellerDone = true;
56 }
57
58 function abortTime() returns (uint result){
59 if(_isabortDone){
60 return _abortTime;
61 }
62 return _max;
63 }
64
65 function confirmClaimDone(address a){
66 _confirmClaimTime = now;
67 _issellerDone = true;
68 }
69
70 function confirmClaimTime() returns (uint result){
71 if(_isconfirmClaimDone){
72 return _confirmClaimTime;
73 }
74 return _max;
75 }
76
77 function autoConfirmDone(address a){
78 _autoConfirmTime = now;
79 _issellerDone = true;
80 }
81
82 function autoConfirmTime() returns (uint result){
83 if(_isautoConfirmDone){
84 return _autoConfirmTime;
85 }
86 return _max;
87 }
88
89 function contains(address a) returns (bool result) {
90 for (uint _j = 0; _j < _sellerAddress.length; _j++ ) { //for loop example
91 if (a == _sellerAddress[_j]){
92 return true;
93 }
94 }
95 return false;
96 }
97
98 }
|