-
Notifications
You must be signed in to change notification settings - Fork 45
/
CurationMarkets.sol
191 lines (157 loc) · 6.7 KB
/
CurationMarkets.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
pragma solidity ^0.4.10;
import "./ERC20Token.sol";
/*
DO NOT USE. This is a still a WIP.
Code is MIT licensed.
*/
/*
Continuous Token is about the minting/withdrawing of the curation market.
It allows tokens to be minted, the ETH kept in a pool, and then withdrawn from supply.
This can be re-used without the bonding functionality.
*/
contract ContinuousToken is ERC20Token {
uint public constant MAX_UINT = (2**256) - 1;
uint256 baseCost = 100000000000000; //100000000000000 wei 0.0001 ether
uint256 public costPerToken = 0;
uint256 public totalEverMinted;
uint256 public totalEverWithdrawn;
uint256 public poolBalance;
uint8 decimals;
string symbol;
string name;
function ContinuousToken(uint8 _decimals, string _symbol, string _name) {
decimals = _decimals;
symbol = _symbol;
name = _name;
updateCostOfToken(0); //first pass
}
// via: http://ethereum.stackexchange.com/questions/10425/is-there-any-efficient-way-to-compute-the-exponentiation-of-a-fraction-and-an-in/10432#10432
// Computes `k * (1+1/q) ^ N`, with precision `p`. The higher
// the precision, the higher the gas cost. It should be
// something around the log of `n`. When `p == n`, the
// precision is absolute (sans possible integer overflows).
// Much smaller values are sufficient to get a great approximation.
function fracExp(uint k, uint q, uint n, uint p) internal returns (uint) {
uint s = 0;
uint N = 1;
uint B = 1;
for (uint i = 0; i < p; ++i){
s += k * N / B / (q**i);
N = N * (n-i);
B = B * (i+1);
}
return s;
}
function updateCostOfToken(uint256 _supply) internal {
//from protocol design:
//costOfCoupon = (BaseCost + BaseCost*(1.000001618^AvailableSupply)+BaseCost*AvailableSupply/1000)
//totalSupply == AvailableSupply
costPerToken = baseCost+fracExp(baseCost, 618046, _supply, 2)+baseCost*_supply/1000;
LogCostOfTokenUpdate(costPerToken);
}
//mint
function mint(uint256 _amountToMint) payable returns (bool) {
//balance of msg.sender increases if paid right amount according to protocol
if(_amountToMint > 0 && (MAX_UINT - _amountToMint) >= totalSupply && msg.value > 0) {
uint256 totalMinted = 0;
uint256 totalCost = 0;
//for loop to determine cost at each point.
for(uint i = 0; i < _amountToMint; i+=1) {
if(totalCost + costPerToken <= msg.value) {
totalCost += costPerToken;
totalMinted += 1;
updateCostOfToken((totalSupply+i));
} else {
break;
}
}
if(totalCost < msg.value) { //some funds left, not enough for one token. Send back funds
msg.sender.transfer(msg.value - totalCost);
}
totalEverMinted += totalMinted;
totalSupply += totalMinted;
balances[msg.sender] += totalMinted;
poolBalance += totalCost;
LogMint(totalMinted, totalCost);
return true;
} else {
throw;
}
}
function withdraw(uint256 _amountToWithdraw) returns (bool) {
if(_amountToWithdraw > 0 && balances[msg.sender] >= _amountToWithdraw) {
//determine how much you can leave with.
uint256 reward = _amountToWithdraw * poolBalance/totalSupply; //rounding?
msg.sender.transfer(reward);
balances[msg.sender] -= _amountToWithdraw;
totalSupply -= _amountToWithdraw;
updateCostOfToken(totalSupply);
LogWithdraw(_amountToWithdraw, reward);
return true;
} else {
throw;
}
}
event LogMint(uint256 amountMinted, uint256 totalCost);
event LogWithdraw(uint256 amountWithdrawn, uint256 reward);
event LogCostOfTokenUpdate(uint256 newCost);
}
/*
Implements a continuous token that can be bonded to curators and subtopic for curation.
*/
contract CurationToken is ContinuousToken {
//token holder -> curator -> sub-topic -> amount
mapping (address => mapping (address => mapping(string => uint256))) public bonds;
mapping (address => mapping(string => uint256)) public totalBondsPerCuratorPerSubtopic;
uint256 public totalBonded = 0;
//main topic. eg #truffle. Hardcoded.
//sub topics examples = #truffle.features or #truffle.pullrequests
string topic;
function CurationToken(uint8 _decimals, string _symbol, string _name, string _topic) ContinuousToken(_decimals, _symbol, _name) {
topic = _topic;
}
function bond(address _curator, string _subtopic, uint256 _amount) returns (bool) {
if(balances[msg.sender] >= _amount) {
bonds[msg.sender][_curator][_subtopic] += _amount;
balances[msg.sender] -= _amount;
totalBonded += _amount;
totalBondsPerCuratorPerSubtopic[_curator][_subtopic] += _amount;
LogBond(msg.sender, _curator, _subtopic, _amount);
return true;
} else {
return false;
}
}
function withdrawBond(address _curator, string _subtopic, uint256 _amount) returns (bool) {
if(bonds[msg.sender][_curator][_subtopic] >= _amount) {
bonds[msg.sender][_curator][_subtopic] -= _amount;
balances[msg.sender] += _amount;
totalBonded -= _amount;
totalBondsPerCuratorPerSubtopic[_curator][_subtopic] -= _amount;
LogWithdrawBond(msg.sender, _curator, _subtopic, _amount);
return true;
} else {
return false;
}
}
event LogBond(address indexed holder, address curator, string subtopic, uint256 amount);
event LogWithdrawBond(address indexed holder, address curator, string subtopic, uint256 amount);
}
/*
Back information with full backing in that subtopic
Currently just uses event logs.
Have to build a local DB to filter these events.
No internal storage atm.
*/
contract Curator {
function back(address _token, string _subtopic, string _info) {
CurationToken token = CurationToken(_token);
LogBacking(msg.sender, _info, token.totalBondsPerCuratorPerSubtopic(msg.sender, _subtopic), token.totalBonded());
}
function revoke(address _token, string _subtopic, string _info) {
CurationToken token = CurationToken(_token);
LogRevoke(msg.sender, _info, token.totalBondsPerCuratorPerSubtopic(msg.sender, _subtopic), token.totalBonded());
}
event LogBacking(address curator, string info, uint256 bondedAmount, uint256 totalBonded);
event LogRevoke(address curator, string info, uint256 bondedAmount, uint256 totalBonded);
}