2019-04-09 22:24:40 +00:00
|
|
|
pragma solidity ^0.5.2;
|
2019-04-08 14:46:14 +00:00
|
|
|
|
2019-04-10 09:00:08 +00:00
|
|
|
import "./token/MiniMeTokenInterface.sol";
|
|
|
|
import "./token/ApproveAndCallFallBack.sol";
|
2019-04-08 14:46:14 +00:00
|
|
|
import "./utils/SafeMath.sol";
|
|
|
|
import "./utils/BancorFormula.sol";
|
|
|
|
|
2019-04-09 20:54:21 +00:00
|
|
|
|
2019-04-19 09:03:03 +00:00
|
|
|
contract Discover is ApproveAndCallFallBack, BancorFormula {
|
2019-04-08 14:46:14 +00:00
|
|
|
using SafeMath for uint;
|
|
|
|
|
2019-04-10 00:26:00 +00:00
|
|
|
// Could be any MiniMe token
|
2019-04-08 14:46:14 +00:00
|
|
|
MiniMeTokenInterface SNT;
|
|
|
|
|
|
|
|
// Total SNT in circulation
|
2019-04-08 17:28:36 +00:00
|
|
|
uint public total;
|
2019-04-08 14:46:14 +00:00
|
|
|
|
|
|
|
// Parameter to calculate Max SNT any one DApp can stake
|
2019-04-08 17:28:36 +00:00
|
|
|
uint public ceiling;
|
2019-04-08 14:46:14 +00:00
|
|
|
|
|
|
|
// The max amount of tokens it is possible to stake, as a percentage of the total in circulation
|
2019-04-08 17:28:36 +00:00
|
|
|
uint public max;
|
2019-04-08 14:46:14 +00:00
|
|
|
|
|
|
|
// Decimal precision for this contract
|
2019-04-08 17:28:36 +00:00
|
|
|
uint public decimals;
|
2019-04-08 14:46:14 +00:00
|
|
|
|
2019-04-09 20:54:21 +00:00
|
|
|
// Prevents overflows in votesMinted
|
2019-04-08 17:28:36 +00:00
|
|
|
uint public safeMax;
|
2019-04-08 14:46:14 +00:00
|
|
|
|
|
|
|
// Whether we need more than an id param to identify arbitrary data must still be discussed.
|
|
|
|
struct Data {
|
|
|
|
address developer;
|
|
|
|
bytes32 id;
|
2019-04-12 09:40:24 +00:00
|
|
|
bytes32 metadata;
|
2019-04-08 14:46:14 +00:00
|
|
|
uint balance;
|
|
|
|
uint rate;
|
|
|
|
uint available;
|
2019-04-09 20:54:21 +00:00
|
|
|
uint votesMinted;
|
|
|
|
uint votesCast;
|
|
|
|
uint effectiveBalance;
|
2019-04-08 14:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Data[] public dapps;
|
|
|
|
mapping(bytes32 => uint) public id2index;
|
2019-04-10 13:18:08 +00:00
|
|
|
mapping(bytes32 => bool) existingIDs;
|
2019-04-08 14:46:14 +00:00
|
|
|
|
2019-04-11 15:15:40 +00:00
|
|
|
event DAppCreated(bytes32 indexed id, uint newEffectiveBalance);
|
2019-04-08 14:46:14 +00:00
|
|
|
event Upvote(bytes32 indexed id, uint newEffectiveBalance);
|
|
|
|
event Downvote(bytes32 indexed id, uint newEffectiveBalance);
|
|
|
|
event Withdraw(bytes32 indexed id, uint newEffectiveBalance);
|
2019-04-10 19:26:21 +00:00
|
|
|
event MetadataUpdated(bytes32 indexed id);
|
2019-04-08 14:46:14 +00:00
|
|
|
|
|
|
|
constructor(MiniMeTokenInterface _SNT) public {
|
|
|
|
SNT = _SNT;
|
|
|
|
|
|
|
|
total = 3470483788;
|
|
|
|
|
2019-04-09 07:16:48 +00:00
|
|
|
ceiling = 588; // See here for more: https://observablehq.com/@andytudhope/dapp-store-snt-curation-mechanism
|
2019-04-08 14:46:14 +00:00
|
|
|
|
2019-04-09 07:16:48 +00:00
|
|
|
decimals = 1000000; // 4 decimal points for %, 2 because we only use 1/100th of total in circulation
|
2019-04-08 14:46:14 +00:00
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
max = total.mul(ceiling).div(decimals);
|
2019-04-08 14:46:14 +00:00
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
safeMax = uint(77).mul(max).div(100); // Limited by accuracy of BancorFormula
|
2019-04-08 14:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
2019-04-12 09:40:24 +00:00
|
|
|
* @param _metadata metadata hex string
|
2019-04-08 14:46:14 +00:00
|
|
|
*/
|
2019-04-12 09:40:24 +00:00
|
|
|
function createDApp(bytes32 _id, uint _amount, bytes32 _metadata) external {
|
|
|
|
_createDApp(
|
|
|
|
msg.sender,
|
|
|
|
_id,
|
|
|
|
_amount,
|
|
|
|
_metadata);
|
2019-04-08 14:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2019-04-09 22:24:40 +00:00
|
|
|
function upvote(bytes32 _id, uint _amount) external {
|
2019-04-08 14:46:14 +00:00
|
|
|
_upvote(msg.sender, _id, _amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-09 14:35:30 +00:00
|
|
|
* @dev Sends SNT to the developer and lowers the DApp's effective balance by 1%
|
2019-04-08 14:46:14 +00:00
|
|
|
* @param _id bytes32 unique identifier.
|
2019-04-09 14:35:30 +00:00
|
|
|
* @param _amount uint, included for approveAndCallFallBack
|
2019-04-08 14:46:14 +00:00
|
|
|
*/
|
2019-05-16 11:28:34 +00:00
|
|
|
function downvote(bytes32 _id) external {
|
2019-04-08 19:22:45 +00:00
|
|
|
(,,uint c) = downvoteCost(_id);
|
|
|
|
_downvote(msg.sender, _id, c);
|
2019-04-08 14:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2019-04-09 22:24:40 +00:00
|
|
|
function withdraw(bytes32 _id, uint _amount) external {
|
2019-04-08 14:46:14 +00:00
|
|
|
uint dappIdx = id2index[_id];
|
|
|
|
Data storage d = dapps[dappIdx];
|
|
|
|
require(d.id == _id, "Error fetching correct data");
|
|
|
|
|
|
|
|
require(msg.sender == d.developer, "Only the developer can withdraw SNT staked on this data");
|
|
|
|
require(_amount <= d.available, "You can only withdraw a percentage of the SNT staked, less what you have already received");
|
|
|
|
|
|
|
|
uint precision;
|
|
|
|
uint result;
|
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
d.balance = d.balance.sub(_amount);
|
|
|
|
d.rate = decimals.sub(d.balance.mul(decimals).div(max));
|
|
|
|
d.available = d.balance.mul(d.rate);
|
2019-04-08 14:46:14 +00:00
|
|
|
|
2019-04-09 20:54:21 +00:00
|
|
|
(result, precision) = BancorFormula.power(
|
|
|
|
d.available,
|
|
|
|
decimals,
|
|
|
|
uint32(decimals),
|
|
|
|
uint32(d.rate));
|
2019-04-08 14:46:14 +00:00
|
|
|
|
2019-04-09 20:54:21 +00:00
|
|
|
d.votesMinted = result >> precision;
|
|
|
|
if (d.votesCast > d.votesMinted) {
|
|
|
|
d.votesCast = d.votesMinted;
|
2019-04-08 14:46:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
uint temp1 = d.votesCast.mul(d.rate).mul(d.available);
|
|
|
|
uint temp2 = d.votesMinted.mul(decimals).mul(decimals);
|
|
|
|
uint effect = temp1.div(temp2);
|
2019-04-09 10:21:23 +00:00
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
d.effectiveBalance = d.balance.sub(effect);
|
2019-04-09 10:21:23 +00:00
|
|
|
|
|
|
|
require(SNT.transfer(d.developer, _amount), "Transfer failed");
|
2019-04-08 14:46:14 +00:00
|
|
|
|
2019-04-09 20:54:21 +00:00
|
|
|
emit Withdraw(_id, d.effectiveBalance);
|
2019-04-08 14:46:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 19:26:21 +00:00
|
|
|
/**
|
|
|
|
* dev Set the content for the dapp
|
|
|
|
* @param _id bytes32 unique identifier.
|
2019-04-12 09:40:24 +00:00
|
|
|
* @param _metadata metadata info
|
2019-04-10 19:26:21 +00:00
|
|
|
*/
|
2019-04-12 09:40:24 +00:00
|
|
|
function setMetadata(bytes32 _id, bytes32 _metadata) external {
|
2019-04-10 19:26:21 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-04-08 14:46:14 +00:00
|
|
|
/**
|
|
|
|
* @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,
|
2019-04-09 22:24:40 +00:00
|
|
|
bytes calldata _data
|
2019-04-08 14:46:14 +00:00
|
|
|
)
|
2019-04-09 22:24:40 +00:00
|
|
|
external
|
2019-04-08 14:46:14 +00:00
|
|
|
{
|
|
|
|
require(_token == address(SNT), "Wrong token");
|
|
|
|
require(_token == address(msg.sender), "Wrong account");
|
2019-04-12 09:40:24 +00:00
|
|
|
require(_data.length <= 196, "Incorrect data");
|
2019-04-08 14:46:14 +00:00
|
|
|
|
|
|
|
bytes4 sig;
|
|
|
|
bytes32 id;
|
|
|
|
uint256 amount;
|
2019-04-12 09:40:24 +00:00
|
|
|
bytes32 metadata;
|
2019-04-08 14:46:14 +00:00
|
|
|
|
2019-04-12 09:40:24 +00:00
|
|
|
(sig, id, amount, metadata) = abiDecodeRegister(_data);
|
2019-04-08 14:46:14 +00:00
|
|
|
require(_amount == amount, "Wrong amount");
|
|
|
|
|
2019-04-12 09:40:24 +00:00
|
|
|
if (sig == bytes4(0x7e38d973)) {
|
|
|
|
_createDApp(
|
|
|
|
_from,
|
|
|
|
id,
|
|
|
|
amount,
|
|
|
|
metadata);
|
2019-04-09 20:54:21 +00:00
|
|
|
} else if (sig == bytes4(0xac769090)) {
|
2019-04-08 19:22:45 +00:00
|
|
|
_downvote(_from, id, amount);
|
2019-04-09 20:54:21 +00:00
|
|
|
} else if (sig == bytes4(0x2b3df690)) {
|
2019-04-08 14:46:14 +00:00
|
|
|
_upvote(_from, id, amount);
|
|
|
|
} else {
|
|
|
|
revert("Wrong method selector");
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 23:19:11 +00:00
|
|
|
|
2019-04-10 00:26:00 +00:00
|
|
|
/**
|
|
|
|
* @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) {
|
|
|
|
uint dappIdx = id2index[_id];
|
|
|
|
Data memory d = dapps[dappIdx];
|
|
|
|
require(d.id == _id, "Error fetching correct data");
|
2019-04-19 20:15:02 +00:00
|
|
|
require(d.balance.add(_amount) <= safeMax, "You cannot upvote by this much, try with a lower amount");
|
2019-04-10 00:26:00 +00:00
|
|
|
|
|
|
|
// Special case - no downvotes yet cast
|
|
|
|
if (d.votesCast == 0) {
|
|
|
|
return _amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint precision;
|
|
|
|
uint result;
|
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
uint mBalance = d.balance.add(_amount);
|
|
|
|
uint mRate = decimals.sub(mBalance.mul(decimals).div(max));
|
|
|
|
uint mAvailable = mBalance.mul(mRate);
|
2019-04-10 00:26:00 +00:00
|
|
|
|
|
|
|
(result, precision) = BancorFormula.power(
|
|
|
|
mAvailable,
|
|
|
|
decimals,
|
|
|
|
uint32(decimals),
|
|
|
|
uint32(mRate));
|
|
|
|
|
|
|
|
uint mVMinted = result >> precision;
|
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
uint temp1 = d.votesCast.mul(mRate).mul(mAvailable);
|
|
|
|
uint temp2 = mVMinted.mul(decimals).mul(decimals);
|
|
|
|
uint mEffect = temp1.div(temp2);
|
2019-04-10 00:26:00 +00:00
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
uint mEBalance = mBalance.sub(mEffect);
|
2019-04-10 00:26:00 +00:00
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
return (mEBalance.sub(d.effectiveBalance));
|
2019-04-10 00:26:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 23:19:11 +00:00
|
|
|
/**
|
|
|
|
* @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) public view returns(uint b, uint vR, uint c) {
|
|
|
|
uint dappIdx = id2index[_id];
|
|
|
|
Data memory d = dapps[dappIdx];
|
|
|
|
require(d.id == _id, "Error fetching correct data");
|
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
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);
|
2019-04-09 23:19:11 +00:00
|
|
|
return (balanceDownBy, votesRequired, cost);
|
|
|
|
}
|
|
|
|
|
2019-04-12 09:40:24 +00:00
|
|
|
function _createDApp(
|
|
|
|
address _from,
|
|
|
|
bytes32 _id,
|
|
|
|
uint _amount,
|
|
|
|
bytes32 _metadata
|
|
|
|
)
|
|
|
|
internal
|
|
|
|
{
|
2019-04-10 13:18:08 +00:00
|
|
|
require(!existingIDs[_id], "You must submit a unique ID");
|
|
|
|
|
2019-04-09 23:19:11 +00:00
|
|
|
require(_amount > 0, "You must spend some SNT to submit a ranking in order to avoid spam");
|
2019-04-11 15:15:40 +00:00
|
|
|
require (_amount <= safeMax, "You cannot stake more SNT than the ceiling dictates");
|
2019-04-09 23:19:11 +00:00
|
|
|
|
|
|
|
uint dappIdx = dapps.length;
|
|
|
|
|
|
|
|
dapps.length++;
|
|
|
|
|
|
|
|
Data storage d = dapps[dappIdx];
|
|
|
|
d.developer = _from;
|
|
|
|
d.id = _id;
|
2019-04-12 09:40:24 +00:00
|
|
|
d.metadata = _metadata;
|
2019-04-09 23:19:11 +00:00
|
|
|
|
|
|
|
uint precision;
|
|
|
|
uint result;
|
|
|
|
|
|
|
|
d.balance = _amount;
|
2019-04-19 20:15:02 +00:00
|
|
|
d.rate = decimals.sub((d.balance).mul(decimals).div(max));
|
|
|
|
d.available = d.balance.mul(d.rate);
|
2019-04-09 23:19:11 +00:00
|
|
|
|
|
|
|
(result, precision) = BancorFormula.power(
|
|
|
|
d.available,
|
|
|
|
decimals,
|
|
|
|
uint32(decimals),
|
|
|
|
uint32(d.rate));
|
|
|
|
|
|
|
|
d.votesMinted = result >> precision;
|
|
|
|
d.votesCast = 0;
|
|
|
|
d.effectiveBalance = _amount;
|
|
|
|
|
|
|
|
id2index[_id] = dappIdx;
|
2019-04-10 13:18:08 +00:00
|
|
|
existingIDs[_id] = true;
|
2019-04-09 23:19:11 +00:00
|
|
|
|
|
|
|
require(SNT.allowance(_from, address(this)) >= _amount, "Not enough SNT allowance");
|
|
|
|
require(SNT.transferFrom(_from, address(this), _amount), "Transfer failed");
|
|
|
|
|
2019-04-11 15:15:40 +00:00
|
|
|
emit DAppCreated(_id, d.effectiveBalance);
|
2019-04-09 23:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _upvote(address _from, bytes32 _id, uint _amount) internal {
|
|
|
|
require(_amount > 0, "You must send some SNT in order to upvote");
|
|
|
|
|
|
|
|
uint dappIdx = id2index[_id];
|
|
|
|
Data storage d = dapps[dappIdx];
|
|
|
|
require(d.id == _id, "Error fetching correct data");
|
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
require(d.balance.add(_amount) <= safeMax, "You cannot upvote by this much, try with a lower amount");
|
2019-04-09 23:19:11 +00:00
|
|
|
|
|
|
|
uint precision;
|
|
|
|
uint result;
|
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
d.balance = d.balance.add(_amount);
|
|
|
|
d.rate = decimals.sub((d.balance).mul(decimals).div(max));
|
|
|
|
d.available = d.balance.mul(d.rate);
|
2019-04-09 23:19:11 +00:00
|
|
|
|
|
|
|
(result, precision) = BancorFormula.power(
|
|
|
|
d.available,
|
|
|
|
decimals,
|
|
|
|
uint32(decimals),
|
|
|
|
uint32(d.rate));
|
|
|
|
|
|
|
|
d.votesMinted = result >> precision;
|
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
uint temp1 = d.votesCast.mul(d.rate).mul(d.available);
|
|
|
|
uint temp2 = d.votesMinted.mul(decimals).mul(decimals);
|
|
|
|
uint effect = temp1.div(temp2);
|
2019-04-09 23:19:11 +00:00
|
|
|
|
2019-04-19 20:15:02 +00:00
|
|
|
d.effectiveBalance = d.balance.sub(effect);
|
2019-04-09 23:19:11 +00:00
|
|
|
|
|
|
|
require(SNT.allowance(_from, address(this)) >= _amount, "Not enough SNT allowance");
|
|
|
|
require(SNT.transferFrom(_from, address(this), _amount), "Transfer failed");
|
|
|
|
|
|
|
|
emit Upvote(_id, d.effectiveBalance);
|
|
|
|
}
|
|
|
|
|
2019-05-16 11:28:34 +00:00
|
|
|
function _downvote(address _from, bytes32 _id) internal {
|
2019-04-09 23:19:11 +00:00
|
|
|
uint dappIdx = id2index[_id];
|
|
|
|
Data storage d = dapps[dappIdx];
|
|
|
|
require(d.id == _id, "Error fetching correct data");
|
|
|
|
|
|
|
|
(uint b, uint vR, uint c) = downvoteCost(_id);
|
|
|
|
|
2019-05-16 11:28:34 +00:00
|
|
|
d.available = d.available.sub(c);
|
2019-04-19 20:15:02 +00:00
|
|
|
d.votesCast = d.votesCast.add(vR);
|
|
|
|
d.effectiveBalance = d.effectiveBalance.sub(b);
|
2019-04-09 23:19:11 +00:00
|
|
|
|
2019-05-16 11:28:34 +00:00
|
|
|
require(SNT.allowance(_from, address(this)) >= c, "Not enough SNT allowance");
|
|
|
|
require(SNT.transferFrom(_from, address(this), c), "Transfer failed");
|
|
|
|
require(SNT.transfer(d.developer, c), "Transfer failed");
|
2019-04-09 23:19:11 +00:00
|
|
|
|
|
|
|
emit Downvote(_id, d.effectiveBalance);
|
|
|
|
}
|
2019-04-09 20:54:21 +00:00
|
|
|
|
2019-04-08 14:46:14 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
)
|
2019-04-12 09:42:41 +00:00
|
|
|
private
|
2019-04-08 14:46:14 +00:00
|
|
|
returns(
|
|
|
|
bytes4 sig,
|
|
|
|
bytes32 id,
|
2019-04-12 09:40:24 +00:00
|
|
|
uint256 amount,
|
|
|
|
bytes32 metadata
|
2019-04-08 14:46:14 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
assembly {
|
|
|
|
sig := mload(add(_data, add(0x20, 0)))
|
|
|
|
id := mload(add(_data, 36))
|
|
|
|
amount := mload(add(_data, 68))
|
2019-04-12 09:40:24 +00:00
|
|
|
metadata := mload(add(_data, 100))
|
2019-04-08 14:46:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|