-
Notifications
You must be signed in to change notification settings - Fork 9
/
Discover.sol
423 lines (338 loc) · 13.5 KB
/
Discover.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
pragma solidity ^0.5.2;
import "./token/MiniMeTokenInterface.sol";
import "./token/ApproveAndCallFallBack.sol";
import "./utils/SafeMath.sol";
import "./utils/BancorFormula.sol";
import "./common/Controlled.sol";
contract Discover is Controlled, ApproveAndCallFallBack, BancorFormula {
using SafeMath for uint;
// Could be any MiniMe token
MiniMeTokenInterface SNT;
// Total SNT in circulation
uint public total;
// Parameter to calculate Max SNT any one DApp can stake
uint public ceiling;
// The max amount of tokens it is possible to stake, as a percentage of the total in circulation
uint public max;
// Decimal precision for this contract
uint public decimals;
// Prevents overflows in votesMinted
uint public safeMax;
// Whether we need more than an id param to identify arbitrary data must still be discussed.
struct Data {
address developer;
bytes32 id;
bytes32 metadata;
uint balance;
uint rate;
uint available;
uint votesMinted;
uint votesCast;
uint effectiveBalance;
}
Data[] public dapps;
mapping(bytes32 => uint) public id2index;
mapping(bytes32 => bool) public existingIDs;
event DAppCreated(bytes32 indexed id, uint newEffectiveBalance);
event Upvote(bytes32 indexed id, uint newEffectiveBalance);
event Downvote(bytes32 indexed id, uint newEffectiveBalance);
event Withdraw(bytes32 indexed id, uint newEffectiveBalance);
event MetadataUpdated(bytes32 indexed id);
event CeilingUpdated(uint oldCeiling, uint newCeiling);
constructor(MiniMeTokenInterface _SNT) public {
SNT = _SNT;
total = 6804870174;
ceiling = 292; // See here for more: https://observablehq.com/@andytudhope/dapp-store-snt-curation-mechanism
decimals = 1000000; // 4 decimal points for %, 2 because we only use 1/100th of total in circulation
max = total.mul(ceiling).div(decimals);
safeMax = uint(77).mul(max).div(100); // Limited by accuracy of BancorFormula
}
/**
* @dev Update ceiling
* @param _newCeiling New ceiling value
*/
function setCeiling(uint _newCeiling) external onlyController {
emit CeilingUpdated(ceiling, _newCeiling);
ceiling = _newCeiling;
max = total.mul(ceiling).div(decimals);
safeMax = uint(77).mul(max).div(100);
}
/**
* @dev Anyone can create a DApp (i.e an arb piece of data this contract happens to care about).
* @param _id bytes32 unique identifier.
* @param _amount of tokens to stake on initial ranking.
* @param _metadata metadata hex string
*/
function createDApp(bytes32 _id, uint _amount, bytes32 _metadata) external {
_createDApp(
msg.sender,
_id,
_amount,
_metadata);
}
/**
* @dev Sends SNT directly to the contract, not the developer. This gets added to the DApp's balance, no curve required.
* @param _id bytes32 unique identifier.
* @param _amount of tokens to stake on DApp's ranking. Used for upvoting + staking more.
*/
function upvote(bytes32 _id, uint _amount) external {
_upvote(msg.sender, _id, _amount);
}
/**
* @dev Sends SNT to the developer and lowers the DApp's effective balance by 1%
* @param _id bytes32 unique identifier.
* @param _amount uint, included for approveAndCallFallBack
*/
function downvote(bytes32 _id, uint _amount) external {
_downvote(msg.sender, _id, _amount);
}
/**
* @dev Developers can withdraw an amount not more than what was available of the
SNT they originally staked minus what they have already received back in downvotes.
* @param _id bytes32 unique identifier.
* @return max SNT that can be withdrawn == available SNT for DApp.
*/
function withdrawMax(bytes32 _id) external view returns(uint) {
Data storage d = _getDAppById(_id);
return d.available;
}
/**
* @dev Developers can withdraw an amount not more than what was available of the
SNT they originally staked minus what they have already received back in downvotes.
* @param _id bytes32 unique identifier.
* @param _amount of tokens to withdraw from DApp's overall balance.
*/
function withdraw(bytes32 _id, uint _amount) external {
Data storage d = _getDAppById(_id);
uint256 tokensQuantity = _amount.div(1 ether);
require(msg.sender == d.developer, "Only the developer can withdraw SNT staked on this data");
require(tokensQuantity <= d.available, "You can only withdraw a percentage of the SNT staked, less what you have already received");
uint precision;
uint result;
d.balance = d.balance.sub(tokensQuantity);
d.rate = decimals.sub(d.balance.mul(decimals).div(max));
d.available = d.balance.mul(d.rate);
(result, precision) = BancorFormula.power(
d.available,
decimals,
uint32(decimals),
uint32(d.rate));
d.votesMinted = result >> precision;
if (d.votesCast > d.votesMinted) {
d.votesCast = d.votesMinted;
}
uint temp1 = d.votesCast.mul(d.rate).mul(d.available);
uint temp2 = d.votesMinted.mul(decimals).mul(decimals);
uint effect = temp1.div(temp2);
d.effectiveBalance = d.balance.sub(effect);
require(SNT.transfer(d.developer, _amount), "Transfer failed");
emit Withdraw(_id, d.effectiveBalance);
}
/**
* dev Set the content for the dapp
* @param _id bytes32 unique identifier.
* @param _metadata metadata info
*/
function setMetadata(bytes32 _id, bytes32 _metadata) external {
uint dappIdx = id2index[_id];
Data storage d = dapps[dappIdx];
require(d.developer == msg.sender, "Only the developer can update the metadata");
d.metadata = _metadata;
emit MetadataUpdated(_id);
}
/**
* @dev Used in UI in order to fetch all dapps
* @return dapps count
*/
function getDAppsCount() external view returns(uint) {
return dapps.length;
}
/**
* @notice Support for "approveAndCall".
* @param _from Who approved.
* @param _amount Amount being approved, needs to be equal `_amount` or `cost`.
* @param _token Token being approved, needs to be `SNT`.
* @param _data Abi encoded data with selector of `register(bytes32,address,bytes32,bytes32)`.
*/
function receiveApproval(
address _from,
uint256 _amount,
address _token,
bytes calldata _data
)
external
{
require(_token == address(SNT), "Wrong token");
require(_token == address(msg.sender), "Wrong account");
require(_data.length <= 196, "Incorrect data");
bytes4 sig;
bytes32 id;
uint256 amount;
bytes32 metadata;
(sig, id, amount, metadata) = abiDecodeRegister(_data);
require(_amount == amount, "Wrong amount");
if (sig == bytes4(0x7e38d973)) {
_createDApp(
_from,
id,
amount,
metadata);
} else if (sig == bytes4(0xac769090)) {
_downvote(_from, id, amount);
} else if (sig == bytes4(0x2b3df690)) {
_upvote(_from, id, amount);
} else {
revert("Wrong method selector");
}
}
/**
* @dev Used in UI to display effect on ranking of user's donation
* @param _id bytes32 unique identifier.
* @param _amount of tokens to stake/"donate" to this DApp's ranking.
* @return effect of donation on DApp's effectiveBalance
*/
function upvoteEffect(bytes32 _id, uint _amount) external view returns(uint effect) {
Data memory d = _getDAppById(_id);
require(d.balance.add(_amount) <= safeMax, "You cannot upvote by this much, try with a lower amount");
// Special case - no downvotes yet cast
if (d.votesCast == 0) {
return _amount;
}
uint precision;
uint result;
uint mBalance = d.balance.add(_amount);
uint mRate = decimals.sub(mBalance.mul(decimals).div(max));
uint mAvailable = mBalance.mul(mRate);
(result, precision) = BancorFormula.power(
mAvailable,
decimals,
uint32(decimals),
uint32(mRate));
uint mVMinted = result >> precision;
uint temp1 = d.votesCast.mul(mRate).mul(mAvailable);
uint temp2 = mVMinted.mul(decimals).mul(decimals);
uint mEffect = temp1.div(temp2);
uint mEBalance = mBalance.sub(mEffect);
return (mEBalance.sub(d.effectiveBalance));
}
/**
* @dev Downvotes always remove 1% of the current ranking.
* @param _id bytes32 unique identifier.
* @return balance_down_by, votes_required, cost
*/
function downvoteCost(bytes32 _id) external view returns(uint b, uint vR, uint c) {
Data memory d = _getDAppById(_id);
return _downvoteCost(d);
}
function _createDApp(
address _from,
bytes32 _id,
uint _amount,
bytes32 _metadata
)
internal
{
require(!existingIDs[_id], "You must submit a unique ID");
uint256 tokensQuantity = _amount.div(1 ether);
require(tokensQuantity > 0, "You must spend some SNT to submit a ranking in order to avoid spam");
require (tokensQuantity <= safeMax, "You cannot stake more SNT than the ceiling dictates");
uint dappIdx = dapps.length;
dapps.length++;
Data storage d = dapps[dappIdx];
d.developer = _from;
d.id = _id;
d.metadata = _metadata;
uint precision;
uint result;
d.balance = tokensQuantity;
d.rate = decimals.sub((d.balance).mul(decimals).div(max));
d.available = d.balance.mul(d.rate);
(result, precision) = BancorFormula.power(
d.available,
decimals,
uint32(decimals),
uint32(d.rate));
d.votesMinted = result >> precision;
d.votesCast = 0;
d.effectiveBalance = tokensQuantity;
id2index[_id] = dappIdx;
existingIDs[_id] = true;
require(SNT.transferFrom(_from, address(this), _amount), "Transfer failed");
emit DAppCreated(_id, d.effectiveBalance);
}
function _upvote(address _from, bytes32 _id, uint _amount) internal {
uint256 tokensQuantity = _amount.div(1 ether);
require(tokensQuantity > 0, "You must send some SNT in order to upvote");
Data storage d = _getDAppById(_id);
require(d.balance.add(tokensQuantity) <= safeMax, "You cannot upvote by this much, try with a lower amount");
uint precision;
uint result;
d.balance = d.balance.add(tokensQuantity);
d.rate = decimals.sub((d.balance).mul(decimals).div(max));
d.available = d.balance.mul(d.rate);
(result, precision) = BancorFormula.power(
d.available,
decimals,
uint32(decimals),
uint32(d.rate));
d.votesMinted = result >> precision;
uint temp1 = d.votesCast.mul(d.rate).mul(d.available);
uint temp2 = d.votesMinted.mul(decimals).mul(decimals);
uint effect = temp1.div(temp2);
d.effectiveBalance = d.balance.sub(effect);
require(SNT.transferFrom(_from, address(this), _amount), "Transfer failed");
emit Upvote(_id, d.effectiveBalance);
}
function _downvote(address _from, bytes32 _id, uint _amount) internal {
uint256 tokensQuantity = _amount.div(1 ether);
Data storage d = _getDAppById(_id);
(uint b, uint vR, uint c) = _downvoteCost(d);
require(tokensQuantity == c, "Incorrect amount: valid iff effect on ranking is 1%");
d.available = d.available.sub(tokensQuantity);
d.votesCast = d.votesCast.add(vR);
d.effectiveBalance = d.effectiveBalance.sub(b);
require(SNT.transferFrom(_from, d.developer, _amount), "Transfer failed");
emit Downvote(_id, d.effectiveBalance);
}
function _downvoteCost(Data memory d) internal view returns(uint b, uint vR, uint c) {
uint balanceDownBy = (d.effectiveBalance.div(100));
uint votesRequired = (balanceDownBy.mul(d.votesMinted).mul(d.rate)).div(d.available);
uint votesAvailable = d.votesMinted.sub(d.votesCast).sub(votesRequired);
uint temp = (d.available.div(votesAvailable)).mul(votesRequired);
uint cost = temp.div(decimals);
return (balanceDownBy, votesRequired, cost);
}
/**
* @dev Used internally in order to get a dapp while checking if it exists
* @return existing dapp
*/
function _getDAppById(bytes32 _id) internal view returns(Data storage d) {
uint dappIdx = id2index[_id];
d = dapps[dappIdx];
require(d.id == _id, "Error fetching correct data");
}
/**
* @dev Decodes abi encoded data with selector for "functionName(bytes32,uint256)".
* @param _data Abi encoded data.
* @return Decoded registry call.
*/
function abiDecodeRegister(
bytes memory _data
)
private
pure
returns(
bytes4 sig,
bytes32 id,
uint256 amount,
bytes32 metadata
)
{
assembly {
sig := mload(add(_data, add(0x20, 0)))
id := mload(add(_data, 36))
amount := mload(add(_data, 68))
metadata := mload(add(_data, 100))
}
}
}