@@ -20,13 +20,10 @@ contract Curation is Governed, BancorFormula {
20
20
// -- Curation --
21
21
22
22
struct Subgraph {
23
- uint256 reserveRatio;
24
- uint256 totalTokens;
25
- uint256 totalShares;
26
- }
27
-
28
- struct SubgraphCurator {
29
- uint256 totalShares;
23
+ uint256 reserveRatio; // Ratio for the bonding curve
24
+ uint256 tokens; // Tokens that constitute the subgraph reserve
25
+ uint256 shares; // Shares issued for this subgraph
26
+ mapping (address => uint256 ) curatorShares;
30
27
}
31
28
32
29
// 100% in parts per million
@@ -53,7 +50,6 @@ contract Curation is Governed, BancorFormula {
53
50
54
51
// Subgraphs and curators mapping
55
52
mapping (bytes32 => Subgraph) public subgraphs;
56
- mapping (bytes32 => mapping (address => SubgraphCurator)) public subgraphCurators;
57
53
58
54
// Address of a party that will distribute fees to subgraph reserves
59
55
address public distributor;
@@ -66,13 +62,13 @@ contract Curation is Governed, BancorFormula {
66
62
event CuratorStakeUpdated (
67
63
address indexed curator ,
68
64
bytes32 indexed subgraphID ,
69
- uint256 totalShares
65
+ uint256 shares
70
66
);
71
67
72
68
event SubgraphStakeUpdated (
73
69
bytes32 indexed subgraphID ,
74
- uint256 totalShares ,
75
- uint256 totalTokens
70
+ uint256 shares ,
71
+ uint256 tokens
76
72
);
77
73
78
74
/**
@@ -193,7 +189,7 @@ contract Curation is Governed, BancorFormula {
193
189
}
194
190
195
191
// Any other source address means they are staking
196
- stake (subgraphID, _from , _value);
192
+ stake (_from, subgraphID , _value);
197
193
return true ;
198
194
}
199
195
@@ -207,43 +203,28 @@ contract Curation is Governed, BancorFormula {
207
203
address curator = msg .sender ;
208
204
Subgraph storage subgraph = subgraphs[_subgraphID];
209
205
210
-
211
- SubgraphCurator storage subgraphCurator
212
- = subgraphCurators[_subgraphID][curator];
213
-
214
206
require (_shares > 0 , "Cannot unstake zero shares " );
215
207
require (
216
- subgraphCurator.totalShares >= _shares,
208
+ subgraph.curatorShares[curator] >= _shares,
217
209
"Cannot unstake more shares than you own "
218
210
);
219
211
220
212
// Obtain the amount of tokens to refund based on returned shares
221
213
uint256 tokensToRefund = subgraphSharesToTokens (_subgraphID, _shares);
222
- uint256 tokensLeft = subgraph.totalTokens.sub (tokensToRefund);
214
+
215
+ // Update balance
216
+ decreaseBalance (curator, _subgraphID, tokensToRefund, _shares);
223
217
224
218
// Ensure we are not under minimum required stake
225
219
require (
226
- tokensLeft >= minimumCurationStake || tokensLeft == 0 ,
220
+ subgraph.tokens >= minimumCurationStake || subgraph.tokens == 0 ,
227
221
"Cannot unstake below minimum required stake for subgraph "
228
222
);
229
223
230
- // Update subgraph balances
231
- subgraph.totalTokens = tokensLeft;
232
- subgraph.totalShares = subgraph.totalShares.sub (_shares);
233
-
234
- // Update subgraph/curator balances
235
- subgraphCurator.totalShares = subgraphCurator.totalShares.sub (_shares);
236
-
237
- // Update global tokens balance
238
- totalTokens = totalTokens.sub (tokensToRefund);
239
-
240
224
// Delete if left without stakes
241
- if (subgraph.totalTokens == 0 ) {
225
+ if (subgraph.tokens == 0 ) {
242
226
delete subgraphs[_subgraphID];
243
227
}
244
- if (subgraphCurator.totalShares == 0 ) {
245
- delete subgraphCurators[_subgraphID][curator];
246
- }
247
228
248
229
// Return the tokens to the curator
249
230
require (
@@ -254,12 +235,12 @@ contract Curation is Governed, BancorFormula {
254
235
emit CuratorStakeUpdated (
255
236
curator,
256
237
_subgraphID,
257
- subgraphCurator.totalShares
238
+ subgraph.curatorShares[curator]
258
239
);
259
240
emit SubgraphStakeUpdated (
260
241
_subgraphID,
261
- subgraph.totalShares ,
262
- subgraph.totalTokens
242
+ subgraph.shares ,
243
+ subgraph.tokens
263
244
);
264
245
}
265
246
@@ -269,7 +250,21 @@ contract Curation is Governed, BancorFormula {
269
250
* @return <bool> True if the subgraph is curated
270
251
*/
271
252
function isSubgraphCurated (bytes32 _subgraphID ) public view returns (bool ) {
272
- return subgraphs[_subgraphID].totalTokens > 0 ;
253
+ return subgraphs[_subgraphID].tokens > 0 ;
254
+ }
255
+
256
+ /**
257
+ * @dev Get the number of shares a curator has on a particular subgraph
258
+ * @param _curator <address> Curator owning the shares
259
+ * @param _subgraphID <uint256> Subgraph of issued shares
260
+ * @return <uint256> Number of subgraph shares issued for a curator
261
+ */
262
+ function getCuratorShares (address _curator , bytes32 _subgraphID )
263
+ public
264
+ view
265
+ returns (uint256 )
266
+ {
267
+ return subgraphs[_subgraphID].curatorShares[_curator];
273
268
}
274
269
275
270
/**
@@ -283,20 +278,22 @@ contract Curation is Governed, BancorFormula {
283
278
view
284
279
returns (uint256 )
285
280
{
286
- // handle initialization of bonding curve
281
+ // Handle initialization of bonding curve
282
+ uint256 tokens = _tokens;
287
283
uint256 shares = 0 ;
288
284
Subgraph memory subgraph = subgraphs[_subgraphID];
289
- if (subgraph.totalTokens == 0 ) {
285
+ if (subgraph.tokens == 0 ) {
290
286
subgraph = getDefaultSubgraph ();
291
- shares = 1 ;
287
+ tokens = tokens.sub (subgraph.tokens);
288
+ shares = subgraph.shares;
292
289
}
293
290
294
291
return
295
292
calculatePurchaseReturn (
296
- subgraph.totalShares ,
297
- subgraph.totalTokens ,
293
+ subgraph.shares ,
294
+ subgraph.tokens ,
298
295
uint32 (subgraph.reserveRatio),
299
- _tokens
296
+ tokens
300
297
) + shares;
301
298
}
302
299
@@ -313,17 +310,17 @@ contract Curation is Governed, BancorFormula {
313
310
{
314
311
Subgraph memory subgraph = subgraphs[_subgraphID];
315
312
require (
316
- subgraph.totalTokens > 0 ,
313
+ subgraph.tokens > 0 ,
317
314
"Subgraph must be curated to perform calculations "
318
315
);
319
316
require (
320
- subgraph.totalShares >= _shares,
317
+ subgraph.shares >= _shares,
321
318
"Shares must be above or equal to total shares issued for the subgraph "
322
319
);
323
320
return
324
321
calculateSaleReturn (
325
- subgraph.totalShares ,
326
- subgraph.totalTokens ,
322
+ subgraph.shares ,
323
+ subgraph.tokens ,
327
324
uint32 (subgraph.reserveRatio),
328
325
_shares
329
326
);
@@ -334,6 +331,7 @@ contract Curation is Governed, BancorFormula {
334
331
* @return <Subgraph> An initialized subgraph
335
332
*/
336
333
function getDefaultSubgraph () private view returns (Subgraph memory ) {
334
+ // Note: The first share costs minimumCurationStake amount of tokens
337
335
Subgraph memory subgraph = Subgraph (
338
336
defaultReserveRatio,
339
337
minimumCurationStake,
@@ -342,47 +340,93 @@ contract Curation is Governed, BancorFormula {
342
340
return subgraph;
343
341
}
344
342
343
+ /**
344
+ * @dev Update balances after buy of shares and deposit of tokens
345
+ * @param _curator <address> - Curator
346
+ * @param _subgraphID <bytes32> - Subgraph
347
+ * @param _tokens <uint256> - Amount of tokens
348
+ * @param _shares <uint256> - Amount of shares
349
+ */
350
+ function increaseBalance (
351
+ address _curator ,
352
+ bytes32 _subgraphID ,
353
+ uint256 _tokens ,
354
+ uint256 _shares
355
+ ) internal {
356
+ // Update subgraph balance
357
+ Subgraph storage subgraph = subgraphs[_subgraphID];
358
+ subgraph.tokens = subgraph.tokens.add (_tokens);
359
+ subgraph.shares = subgraph.shares.add (_shares);
360
+ subgraph.curatorShares[_curator] = subgraph.curatorShares[_curator].add (
361
+ _shares
362
+ );
363
+
364
+ // Update global balance
365
+ totalTokens = totalTokens.add (_tokens);
366
+ }
367
+
368
+ /**
369
+ * @dev Update balances after sell of shares and return of tokens
370
+ * @param _curator <address> - Curator
371
+ * @param _subgraphID <bytes32> - Subgraph
372
+ * @param _tokens <uint256> - Amount of tokens
373
+ * @param _shares <uint256> - Amount of shares
374
+ */
375
+ function decreaseBalance (
376
+ address _curator ,
377
+ bytes32 _subgraphID ,
378
+ uint256 _tokens ,
379
+ uint256 _shares
380
+ ) internal {
381
+ // Update subgraph balance
382
+ Subgraph storage subgraph = subgraphs[_subgraphID];
383
+ subgraph.tokens = subgraph.tokens.sub (_tokens);
384
+ subgraph.shares = subgraph.shares.sub (_shares);
385
+ subgraph.curatorShares[_curator] = subgraph.curatorShares[_curator].sub (
386
+ _shares
387
+ );
388
+
389
+ // Update global balance
390
+ totalTokens = totalTokens.sub (_tokens);
391
+ }
392
+
345
393
/**
346
394
* @dev Assign Graph Tokens received from distributor to the subgraph reserve
347
395
* @param _subgraphID <bytes32> - Subgraph where funds should be allocated as reserves
348
- * @param _amount <uint256> - Amount of Graph Tokens to add to reserves
396
+ * @param _tokens <uint256> - Amount of Graph Tokens to add to reserves
349
397
*/
350
- function collect (bytes32 _subgraphID , uint256 _amount ) private {
398
+ function collect (bytes32 _subgraphID , uint256 _tokens ) private {
351
399
require (
352
400
isSubgraphCurated (_subgraphID),
353
401
"Subgraph must be curated to collect fees "
354
402
);
355
403
356
404
// Collect new funds to reserve
357
405
Subgraph storage subgraph = subgraphs[_subgraphID];
358
- subgraph.totalTokens = subgraph.totalTokens .add (_amount );
406
+ subgraph.tokens = subgraph.tokens .add (_tokens );
359
407
360
408
// Update global tokens balance
361
- totalTokens = totalTokens.add (_amount );
409
+ totalTokens = totalTokens.add (_tokens );
362
410
363
411
emit SubgraphStakeUpdated (
364
412
_subgraphID,
365
- subgraph.totalShares ,
366
- subgraph.totalTokens
413
+ subgraph.shares ,
414
+ subgraph.tokens
367
415
);
368
416
}
369
417
370
418
/**
371
419
* @dev Stake Graph Tokens for Market Curation by subgraphID
372
420
* @param _subgraphID <bytes32> - Subgraph ID the Curator is staking Graph Tokens for
373
421
* @param _curator <address> - Address of Staking party
374
- * @param _amount <uint256> - Amount of Graph Tokens to be staked
422
+ * @param _tokens <uint256> - Amount of Graph Tokens to be staked
375
423
*/
376
- function stake (bytes32 _subgraphID , address _curator , uint256 _amount )
424
+ function stake (address _curator , bytes32 _subgraphID , uint256 _tokens )
377
425
private
378
426
{
379
- uint256 tokens = _amount ;
427
+ uint256 tokens = _tokens ;
380
428
Subgraph storage subgraph = subgraphs[_subgraphID];
381
429
382
-
383
- SubgraphCurator storage subgraphCurator
384
- = subgraphCurators[_subgraphID][_curator];
385
-
386
430
// If this subgraph hasn't been curated before then initialize the curve
387
431
// Sets the initial slope for the curve, controlled by minimumCurationStake
388
432
if (! isSubgraphCurated (_subgraphID)) {
@@ -393,14 +437,14 @@ contract Curation is Governed, BancorFormula {
393
437
);
394
438
395
439
// Initialize subgraph
396
- // Note: The first share costs minimumCurationStake amount of tokens
397
440
subgraphs[_subgraphID] = getDefaultSubgraph ();
398
-
399
- // Update subgraph/curator balance
400
- subgraphCurator.totalShares = SHARES_PER_MINIMUM_STAKE;
441
+ subgraph.curatorShares[_curator] = SHARES_PER_MINIMUM_STAKE;
401
442
402
443
// Update tokens used for stake
403
444
tokens = tokens.sub (minimumCurationStake);
445
+
446
+ // Update global tokens balance
447
+ totalTokens = totalTokens.add (minimumCurationStake);
404
448
}
405
449
406
450
// Process unallocated tokens
@@ -409,27 +453,18 @@ contract Curation is Governed, BancorFormula {
409
453
uint256 newShares = subgraphTokensToShares (_subgraphID, tokens);
410
454
411
455
// Update subgraph balances
412
- subgraph.totalTokens = subgraph.totalTokens.add (tokens);
413
- subgraph.totalShares = subgraph.totalShares.add (newShares);
414
-
415
- // Update subgraph/curator balances
416
- subgraphCurator.totalShares = subgraphCurator.totalShares.add (
417
- newShares
418
- );
456
+ increaseBalance (_curator, _subgraphID, tokens, newShares);
419
457
}
420
458
421
- // Update global tokens balance
422
- totalTokens = totalTokens.add (_amount);
423
-
424
459
emit CuratorStakeUpdated (
425
460
_curator,
426
461
_subgraphID,
427
- subgraphCurator.totalShares
462
+ subgraph.curatorShares[_curator]
428
463
);
429
464
emit SubgraphStakeUpdated (
430
465
_subgraphID,
431
- subgraph.totalShares ,
432
- subgraph.totalTokens
466
+ subgraph.shares ,
467
+ subgraph.tokens
433
468
);
434
469
}
435
470
}
0 commit comments