forked from hyperchaincn/Contract-SupplyChain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupplyChain.sol
executable file
·155 lines (140 loc) · 5.69 KB
/
SupplyChain.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
contract SupplyChain {
enum Actor{ Others, Producer, Retailer, Customer}
enum Commoditytype{ Wine, Jewelry, Shrimp, Others}
enum Phase{ Supplier, Producer, Dealer, Retailer, Customer}
struct User{
bytes32 ID;
bytes32 name;
Actor actor;
}
struct Commodity{
bytes32 commodityID;
bytes32 commodityName;
uint produceTime;
bytes32 producerName;
uint[] timestamps;
bytes32[] retailerNames;
uint sellTime;
bytes32 customerName;
bool isBinding;
address owner;
}
mapping(address => User) producerMap;
mapping(address => User) retailerMap;
mapping(address => User) customerMap;
mapping(bytes32 => Commodity) commodityMap;
mapping(address => bool) whiteList;
address owner;
function SupplyChain(){
owner = msg.sender;
whiteList[owner] = true;
}
function addWhiteList(address addr){
whiteList[addr] = true;
}
function newUser(bytes32 ID, bytes32 name,Actor actor) returns(bool, string){
User user;
if(actor == Actor.Producer){
user = producerMap[msg.sender];
}else if(actor == Actor.Retailer){
user = retailerMap[msg.sender];
}else if(actor == Actor.Customer){
user = customerMap[msg.sender];
}else{
return (false,"the actor is not belong");
}
if(user.ID != 0x0){
return (false, "this ID has been occupied!");
}
user.ID = ID;
user.name = name;
user.actor = actor;
return (true, "Success");
}
// this interface just for producer
function newProduct(bytes32 commodityID, bytes32 commodityName,
uint timestamp) returns(bool,bytes32){
Commodity commodity = commodityMap[commodityID];
if(commodity.commodityID != 0x0) {
return (false,"The commodityID already exist!");
}
User user = producerMap[msg.sender];
if(user.ID == 0x0) {
return (false,"The producer don't exist!");
}
commodity.commodityID = commodityID;
commodity.commodityName = commodityName;
commodity.produceTime = timestamp;
commodity.producerName = user.name;
return (true,"Success,produce a new product");
}
// this interface just for retailer
function retailerInnerTransfer(bytes32 commodityID,uint timestamp) returns(bool, string){
Commodity commodity = commodityMap[commodityID];
if(commodity.commodityID == 0x0) {
return (false,"The commodityID don't exist!");
}
User user = retailerMap[msg.sender];
if(user.ID == 0x0) {
return (false,"The retailer don't exist!");
}
commodity.timestamps.push(timestamp);
commodity.retailerNames.push( user.name );
return (true,"Success");
}
function fromRetailerToCustomer(bytes32 commodityID,uint timestamp) returns(bool, string){
Commodity commodity = commodityMap[commodityID];
if(commodity.commodityID == 0x0) {
return (false,"The commodityID don't exist!");
}
commodity.sellTime = timestamp;
return (true,"Success,Has been sold");
}
// just for Supervision organization
function getCommodityRecordsByWhiteList(bytes32 commodityID) returns(bool,string,
bytes32 producerName,uint produceTime, bytes32[] retailerNames,uint[] retailerTimes
, bytes32 customerName,uint sellTime){
if(!whiteList[msg.sender]){
return (false,"you has no access",producerName, produceTime, retailerNames, retailerTimes, customerName,commodity.sellTime);
}
Commodity commodity = commodityMap[commodityID];
if(commodity.commodityID == 0x0){
return (false,"The commodityID is not exist",producerName, produceTime, retailerNames, retailerTimes,customerName,commodity.sellTime);
}
return (true,"Success",commodity.producerName, commodity.produceTime, commodity.retailerNames, commodity.timestamps, commodity.customerName,commodity.sellTime);
}
function getCommodity(bytes32 commodityID,Actor actor) returns(bool,string,
bytes32 producerName,uint produceTime,bytes32[] retailerNames,uint[] retailerTimes
, bytes32 customerName,uint sellTime){
Commodity commodity = commodityMap[commodityID];
if(commodity.commodityID == 0x0){
return (false,"The commodityID is not exist",producerName,produceTime,
retailerNames,retailerTimes,customerName,sellTime);
}
User user;
if(actor == Actor.Producer){
user = producerMap[msg.sender];
}else if(actor == Actor.Retailer){
user = retailerMap[msg.sender];
}else if(actor == Actor.Customer){
user = customerMap[msg.sender];
}else{
return (false,"the actor is not belong",producerName,produceTime,
retailerNames,retailerTimes,customerName,sellTime);
}
if(commodity.isBinding){
if(commodity.owner != msg.sender){
return (false,"warning,this commodity has been bound",producerName,produceTime,
retailerNames,retailerTimes,customerName,sellTime);
}else{
(false,"has already bind",commodity.producerName,commodity.retailerNames,commodity.customerName);
}
}
if(commodity.sellTime > 0 ) {
commodity.isBinding = true;
commodity.owner = msg.sender;
commodity.customerName = user.name;
}
return (true,"Success",commodity.producerName,commodity.produceTime,commodity.retailerNames,commodity.timestamps,commodity.customerName,commodity.sellTime);
}
}