-
Notifications
You must be signed in to change notification settings - Fork 10
/
Marketplace.sol
231 lines (218 loc) · 9.33 KB
/
Marketplace.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
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
//pragma solidity 0.4.18;
pragma solidity ^0.4.21;
import "./IMarketplace.sol";
import "./BasicMarketplace.sol";
/*
*@title MarketPlace contract
*@dev This is the Marketplace contract. extending BasicMarketplace.
*/
contract Marketplace is IMarketplace,BasicMarketplace{
function Marketplace(address _tokenAddress) BasicMarketplace(_tokenAddress) public {}
/* public*/
function setPunishProvider(bytes32 _dataSourceName, bool _isPunished)
public
onlyOwner
returns (bool success){
require(mProviders[_dataSourceName].isProvider);
mProviders[_dataSourceName].isPunished = _isPunished;
if(_isPunished){
mProviders[_dataSourceName].punishTimeStamp = now;
mProviders[_dataSourceName].isActive = false;
}else{
mProviders[_dataSourceName].punishTimeStamp = 0;
mProviders[_dataSourceName].isActive = true;
}
emit ProviderPunishStatus(mProviders[_dataSourceName].owner,_dataSourceName,_isPunished);
success = true;
}
function refundSubscriber(bytes32 _dataSourceName)
public
returns
(bool success){
require(mProviders[_dataSourceName].isProvider);
uint256 refundAmount = 0;
uint size = mOrders[_dataSourceName].length;
for(uint i=0; i<size ;i++){
if(mOrders[_dataSourceName][i].subscriber == msg.sender){
uint256 refund = handleOrderRefundCalc(mOrders[_dataSourceName][i]);
if(refund > 0 && !mOrders[_dataSourceName][i].isRefundPaid){ // double check payment
mOrders[_dataSourceName][i].isRefundPaid = true;// mark refund as paid
refundAmount = refundAmount.add(refund);
}
}
}
require(safeToSubscriberTransfer(msg.sender,refundAmount));
emit SubscriberRefund(msg.sender,_dataSourceName,refundAmount);
success = true;
}
function getRefundAmount(address _subscriber , bytes32 _dataSourceName)
public
view
returns(uint256 refundAmount){
require(_subscriber != address(0));
require(mProviders[_dataSourceName].isProvider);
refundAmount = 0;
uint size = mOrders[_dataSourceName].length;
for(uint i=0; i< size ; i++){
if(mOrders[_dataSourceName][i].subscriber == _subscriber){
refundAmount = refundAmount.add(handleOrderRefundCalc(mOrders[_dataSourceName][i]));
}
}
return refundAmount;
}
function withdrawProvider(bytes32 _dataSourceName)
public
onlyDataProvider(_dataSourceName)
returns (bool success){
// calculate the withdraw amount
uint256 withdrawAmount = 0;
uint orderSize = mOrders[_dataSourceName].length;
for(uint i=0;i<orderSize;i++){
uint256 withdraw = handleOrderWithdrawCalc(mOrders[_dataSourceName][i]);
if(withdraw > 0 && !mOrders[_dataSourceName][i].isPaid){ // double check
mOrders[_dataSourceName][i].isPaid = true; // mark order as paid
withdrawAmount = withdrawAmount.add(withdraw);
}
}
// transfer ENG's to the provider -revert state if faild
require(safeToProviderTransfer(_dataSourceName,withdrawAmount));
emit ProviderWithdraw(mProviders[_dataSourceName].owner,_dataSourceName,withdrawAmount);
return true;
}
function getWithdrawAmount(bytes32 _dataSourceName)
public
view
returns(uint256 withdrawAmount){
require(mProviders[_dataSourceName].isProvider);
withdrawAmount = 0;
uint orderSize = mOrders[_dataSourceName].length;
for(uint i=0;i<orderSize;i++){
withdrawAmount = withdrawAmount.add(handleOrderWithdrawCalc(mOrders[_dataSourceName][i]));
}
return withdrawAmount;
}
function register(bytes32 _dataSourceName, uint256 _price, address _dataOwner)
public
uniqueDataName(_dataSourceName)
validPrice(_price)
returns (bool success){
require(_dataOwner != address(0));
mProviders[_dataSourceName].owner = _dataOwner;
mProviders[_dataSourceName].volume = 0;
mProviders[_dataSourceName].subscriptionsNum = 0;
mProviders[_dataSourceName].name = _dataSourceName;
mProviders[_dataSourceName].price = _price;
mProviders[_dataSourceName].isPunished = false;
mProviders[_dataSourceName].punishTimeStamp = 0;
mProviders[_dataSourceName].isProvider = true;
mProviders[_dataSourceName].isActive = true;
mProviders[_dataSourceName].nextProvider = "";
mProviders[mCurrent].nextProvider = _dataSourceName;
mCurrent = mProviders[_dataSourceName].name;
mProvidersSize = mProvidersSize.add(1);
emit Registered(_dataOwner,_dataSourceName,_price,true);
success = true;
}
function subscribe(bytes32 _dataSourceName)
public
validDataProvider(_dataSourceName)
returns (bool success){
require(safeToMarketPlaceTransfer(msg.sender,this,mProviders[_dataSourceName].price)); // revet state if failed
// update order
mOrders[_dataSourceName].push(Order({
dataSourceName : _dataSourceName,
subscriber : msg.sender,
provider : mProviders[_dataSourceName].owner,
price : mProviders[_dataSourceName].price,
startTime : now,
endTime : now + FIXED_SUBSCRIPTION_PERIOD,
isPaid : false,
isOrder : true,
isRefundPaid : false
}));
// update provider data
mProviders[_dataSourceName].volume = mProviders[_dataSourceName].volume.add(mProviders[_dataSourceName].price);
mProviders[_dataSourceName].subscriptionsNum = mProviders[_dataSourceName].subscriptionsNum.add(1);
emit Subscribed(msg.sender,
_dataSourceName,
mProviders[_dataSourceName].owner,
mProviders[_dataSourceName].price,
true);
success = true;
}
function getMarketplaceTotalBalance() public view returns (uint256 totalBalance){
return mToken.balanceOf(this);
}
/*internal */
function handleOrderWithdrawCalc(Order order) internal view returns(uint256 orderAmount){
orderAmount = 0;
if(!order.isPaid){ // if not paid yet
if(isOrderExpired(order)){ // expired
if(mProviders[order.dataSourceName].isPunished){ // if punished
if(mProviders[order.dataSourceName].punishTimeStamp >= order.endTime){ // punished after expiration date
return order.price;
}else{ // punished before expiration date
return calcRelativeWithdraw(order); //(punishtime / endtime) * amount
}
}else{ // not punished - return full amount
return order.price;
}
}else{ // not expired
return orderAmount;
}
}
return orderAmount;
}
function handleOrderRefundCalc(Order order) internal view returns(uint256 refundAmount){
refundAmount = 0;
if(!order.isRefundPaid){ //order not paid
if(mProviders[order.dataSourceName].isPunished){ // provider is punished
if(mProviders[order.dataSourceName].punishTimeStamp > order.startTime && mProviders[order.dataSourceName].punishTimeStamp < order.endTime){ // punished before the subscription is expired
refundAmount = order.price.sub(calcRelativeWithdraw(order)); // price - withdrawPrice
}
}
}
return refundAmount;
}
function calcRelativeWithdraw(Order order) internal view returns(uint256 relativeAmount){
require(mProviders[order.dataSourceName].isPunished);
// (punishTime- startTime) * PRICE / (endTime - startTime);
uint256 price = order.price;
uint256 a = (mProviders[order.dataSourceName].punishTimeStamp.sub(order.startTime)).mul(price);
uint256 b = order.endTime.sub(order.startTime);
return SafeMath.div(a,b);
}
function isOrderExpired(Order order) internal view returns (bool isExpired){
return order.endTime <= now;
}
function safeToMarketPlaceTransfer(address _from, address _to, uint256 _amount)
internal
validPrice(_amount)
returns (bool){
require(_from != address(0) && _to == address(this));
require(mToken.allowance(_from,_to) >= _amount);
require(mToken.transferFrom(_from,_to,_amount));
emit SubscriptionDeposited(_from, _to, _amount);
return true;
}
function safeToProviderTransfer(bytes32 _dataSourceName,uint256 _amount)
internal
validPrice(_amount)
onlyDataProvider(_dataSourceName)
returns (bool){
require(_amount > 0);
require(mProviders[_dataSourceName].owner != address(0));
require(mToken.transfer(mProviders[_dataSourceName].owner,_amount));
emit TransferToProvider(mProviders[_dataSourceName].owner,_dataSourceName,_amount);
return true;
}
function safeToSubscriberTransfer(address _subscriber,uint256 _amount)
internal
validPrice(_amount)
returns (bool){
require(_amount > 0);
require(_subscriber == msg.sender);
require(mToken.transfer(msg.sender, _amount));
return true;
}
}