-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAM-SC.sol
More file actions
350 lines (315 loc) · 11.8 KB
/
Copy pathDAM-SC.sol
File metadata and controls
350 lines (315 loc) · 11.8 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
pragma solidity 0.5.16;
contract DataAuction {
enum DataStatus {ForSale, Sold, Unsold}
enum DataCondition {New, Old}
uint public dataIndex;
mapping(address => mapping(uint => Data)) public stores;
mapping(uint => address) public dataIdInStore;
hB[] public highestBidders;
mapping(address => uint) public bidderId;
uint public startPrice;
uint public deposit;
address public DSaddress;
mapping (address => DB) public databuyers;
enum DBStatus {DBDeposited, SuccessfulAuction, Unsatisfied, AuctionCompleted, Refunded}
address public DBaddress;
enum SCStatus {WaitingforDB, Aborted}
SCStatus public status;
uint public numberOfDBs;
uint public highestBid;
uint public timeliness = 7;
address public ABRaddress;
address public DTCaddress;
struct DB {
DBStatus status;
int result;
bytes32 token;
}
struct Data {
uint id;
string name;
string category;
string imageLink;
string descLink;
uint auctionStartTime;
uint auctionEndTime;
address highestBidder;
uint highestBid;
uint totalBids;
DataStatus status;
DataCondition condition;
mapping(address => mapping(bytes32 => Bid)) bids;
mapping(address => mapping(bytes32 => Sell)) sells;
}
struct hB {
address highestBidder;
uint dataId;
uint highestBid;
}
struct Bid {
address bidder;
uint dataId;
uint value;
bool revealed;
}
struct Sell {
address seller;
uint dataId;
bytes32 value;
bool revealed;
}
modifier DSDeposit(){
require(msg.value == deposit);
_;
}
modifier OnlyDB(){
require(msg.sender == DBaddress);
_;
}
modifier DBCost(){
require(msg.value == deposit);
_;
}
modifier DBPay(){
require(msg.value == highestBid);
_;
}
modifier OnlyABR(){
require(msg.sender == ABRaddress);
_;
}
event sellCast(address seller, uint dataId, uint value);
event bidCast(address bidder, uint dataId, uint value);
event NewData(uint DATAId, string Name, string Category, string ImageLink, string DescLink,
uint AuctionStartTime, uint AuctionEndTime, uint DATACondition);
event DSDeposited(string info,address datapurchaser);
event DBDeposited(address databuyer, string info);
event tokenGeneratedforDB(address databuyer, bytes32 token, uint timestamp, uint duration);
event successfulAuction(address databuyer);
event dataCannotDownload(address databuyer, string info);
event ABRIsVerifyingforDB(address databuyer, string info, bytes32 token);
event dataUnavailable(address databuyer, string info);
event CouldNotDownload(address databuyer, string info);
event refundDone(address databuyer);
event Unavailable(address databuyer, string info);
event refundBasedonDBRequest(string info, address databuyer);
event DBPaid(string info, address databuyer);
event paymentSettled(address databuyer, string info);
constructor() public {
dataIndex = 0;
deposit = 3 ether;
status = SCStatus.WaitingforDB;
numberOfDBs = 0;
}
function numberOfItems() view external returns(uint) {
return dataIndex;
}
function payDeposit() DSDeposit external payable{
require(msg.sender == DSaddress);
DSDeposited("DS has paid a deposit.", DSaddress);
}
function requestBidData() OnlyDB DBCost external payable{
require(status == SCStatus.WaitingforDB);
databuyers[msg.sender].status = DBStatus.DBDeposited;
DBDeposited(msg.sender, "DB has paid a deposit.");
numberOfDBs++;
}
function DBRefund() OnlyDB external payable {
require(databuyers[msg.sender].status == DBStatus.DBDeposited);
msg.sender.transfer(deposit);
databuyers[msg.sender].status = DBStatus.Refunded;
refundBasedonDBRequest("The data buyer has been refunded.", msg.sender);
}
function sell(uint DATAId, bytes32 SELL) external payable returns(bool) {
Data storage data = stores[dataIdInStore[DATAId]][DATAId];
require(now >= data.auctionStartTime);
require(now <= data.auctionEndTime);
require(data.sells[msg.sender][SELL].seller == 0);
sellCast(msg.sender, DATAId, msg.value);
data.sells[msg.sender][SELL] = Sell(msg.sender, DATAId, SELL, false);
return true;
}
function bid(uint DATAId, bytes32 BID) external payable returns(bool) {
Data storage data = stores[dataIdInStore[DATAId]][DATAId];
require(now >= data.auctionStartTime);
require(now <= data.auctionEndTime);
require(msg.value > 0);
require(data.bids[msg.sender][BID].bidder == 0);
bidCast(msg.sender, DATAId, msg.value);
data.bids[msg.sender][BID] = Bid(msg.sender, DATAId, msg.value, false);
data.totalBids += 1;
return true;
}
function revealSell(uint DATAId, string SellerPrice, string Secret) external {
Data storage data = stores[dataIdInStore[DATAId]][DATAId];
require(now > data.auctionEndTime);
bytes32 sealedSell = keccak256(abi.encodePacked(SellerPrice, Secret));
Sell memory sellInfo = data.sells[msg.sender][sealedSell];
require(sellInfo.revealed == false);
uint amount = stringToUint(SellerPrice);
if (sellInfo.value == sealedSell) {
msg.sender.transfer(amount);
data.sells[msg.sender][sealedSell].revealed = true;
}
sellCast(msg.sender, DATAId, amount);
startPrice = amount;
}
function revealBid(uint DATAId, string Amount, string Secret, string SellerPrice) external {
Data storage data = stores[dataIdInStore[DATAId]][DATAId];
require(now > data.auctionEndTime);
bytes32 sealedBid = keccak256(abi.encodePacked(Amount, Secret));
Bid memory bidInfo = data.bids[msg.sender][sealedBid];
require(bidInfo.bidder > 0);
require(bidInfo.revealed == false);
uint refund;
uint amount = stringToUint(Amount);
uint sellerPrice = stringToUint(SellerPrice);
bidderId[msg.sender] = highestBidders.length;
uint id = bidderId[msg.sender];
if (sellerPrice == startPrice) {
if (bidInfo.value < amount || amount < sellerPrice) {
refund = bidInfo.value;
} else {
if (amount > data.highestBid) {
data.highestBidder = msg.sender;
data.highestBid = amount;
for (uint i = 0; i < 10; i++) {
highestBidders[i] = highestBidders[highestBidders.length - 1];
delete highestBidders[i];
}
highestBidders[id] = hB({
highestBidder: msg.sender,
dataId: DATAId,
highestBid: amount
});
refund = bidInfo.value - amount;
} else if (amount == data.highestBid) {
highestBidders[id] = hB({
highestBidder: msg.sender,
dataId: DATAId,
highestBid: amount
});
refund = bidInfo.value - amount;
} else {
refund = bidInfo.value;
}
if (refund > 0) {
msg.sender.transfer(refund);
data.bids[msg.sender][sealedBid].revealed = true;
}
}
}
}
function highestBidderInfo(uint DATAId) external returns(address[]) {
address[] memory adrs = new address[](10);
Data storage data = stores[dataIdInStore[DATAId]][DATAId];
uint counter = 0;
for (uint i = 0; i < 10; i++) {
data.highestBidder = highestBidders[i].highestBidder;
adrs[counter] = data.highestBidder;
counter++;
}
return adrs;
}
function totalBids(uint DATAId) view external returns(uint) {
Data memory data = stores[dataIdInStore[DATAId]][DATAId];
return data.totalBids;
}
function stringToUint(string s) pure external returns(uint) {
bytes memory b = bytes(s);
uint res = 0;
for (uint i = 0; i < 100; i++) {
if (b[i] >= 48 && b[i] <= 57) {
res = res * 10 + (uint(b[i]) - 48);
}
}
return res;
}
function addDataToStore(string Name, string Category, string ImageLink, string DescLink, uint AuctionStartTime, uint AuctionEndTime, uint DATACondition) external {
require(AuctionStartTime < AuctionEndTime);
dataIndex += 1;
Data memory data = Data(dataIndex, Name, Category, ImageLink, DescLink, AuctionStartTime, AuctionEndTime, 0, 0, 0, DataStatus.ForSale, DataCondition(DATACondition));
stores[msg.sender][dataIndex] = data;
dataIdInStore[dataIndex] = msg.sender;
NewData(dataIndex, Name, Category, ImageLink, DescLink, AuctionStartTime, AuctionEndTime, DATACondition);
}
function getData(uint DATAId) view external returns(uint, string, string, string, string, uint, uint, DataStatus, DataCondition) {
Data memory data = stores[dataIdInStore[DATAId]][DATAId];
return (data.id, data.name, data.category, data.imageLink, data.descLink, data.auctionStartTime,
data.auctionEndTime, data.status, data.condition);
}
function payBid(address databuyer) OnlyDB DBPay external payable{
for (uint i = 0; i < 10; i++) {
if (databuyer == highestBidders[i].highestBidder){
DBPaid("DB has paid the bid.", msg.sender);
generateToken(databuyer, timeliness);
}
}
}
function generateToken(address databuyer,uint Timeliness) internal{
bytes32 token = keccak256(abi.encodePacked(databuyer,DSaddress,block.timestamp,Timeliness));
databuyers[databuyer].token = token;
tokenGeneratedforDB(databuyer, token, block.timestamp, timeliness);
}
function DBComfirmedResult(int result) OnlyDB external{
if(result == 1){
successfulAuction(msg.sender);
databuyers[msg.sender].status = DBStatus.SuccessfulAuction;
settlement(msg.sender);
}
else if(result == 2){
dataCannotDownload(msg.sender,"The data resource can not be downloaded.");
databuyers[msg.sender].status = DBStatus.Unsatisfied;
ABRIsVerifyingforDB(msg.sender, "Token: ", databuyers[msg.sender].token);
}
else if(result == 3){
dataUnavailable(msg.sender, "The data resource is inconsistent with its description");
databuyers[msg.sender].status = DBStatus.Unsatisfied;
ABRIsVerifyingforDB(msg.sender, "Off-chain testing data and Token: ", databuyers[msg.sender].token);
}
}
function downloadResolutionAndPayment(address databuyer, bool arbitrationResult) OnlyABR external payable{
require(databuyers[databuyer].status == DBStatus.Unsatisfied);
if(arbitrationResult){
CouldNotDownload(databuyer, "The data auction is failed.");
uint x = deposit;
uint y = highestBid;
DTCaddress.transfer(x);
databuyer.transfer(y);
refundDone(databuyer);
databuyers[databuyer].status = DBStatus.AuctionCompleted;
}
else{
successfulAuction(databuyer);
databuyers[databuyer].status = DBStatus.SuccessfulAuction;
settlement(databuyer);
}
}
function qualityResolutionAndPayment(address databuyer, bool arbitrationResult) OnlyABR external payable{
require(databuyers[databuyer].status == DBStatus.Unsatisfied);
if(arbitrationResult){
Unavailable(databuyer, "The data auction is failed.");
uint x = deposit;
uint y = highestBid;
DTCaddress.transfer(x);
databuyer.transfer(y);
refundDone(databuyer);
databuyers[databuyer].status = DBStatus.AuctionCompleted;
}
else{
successfulAuction(databuyer);
databuyers[databuyer].status = DBStatus.SuccessfulAuction;
settlement(databuyer);
}
}
function settlement(address databuyer) internal{
require(databuyers[databuyer].status == DBStatus.SuccessfulAuction);
uint x = deposit;
uint y = highestBid/3;
ABRaddress.transfer(y);
DTCaddress.transfer(y);
DSaddress.transfer(x+y);
paymentSettled(databuyer, "Settlement has done successfully.");
databuyers[databuyer].status = DBStatus.AuctionCompleted;
}
}