Skip to content

Commit bba7d0f

Browse files
authored
curation: rename events, language and add tests (#181)
* curation: update language used for unstaking and refactor events * curation: remove totalTokens state as it can be obtained from the graph token balance * curation: - when unstake check if all shares sold instead of tokens - remove under minimum required stake when redeeming, that should never happen as you can't sell a fraction of share - fix reward collection test for event * curation: add test for when staking below minimum * curation: update event name * curation: add natspec to events * curation: rename distributor to staking to avoid introducing a new concept
1 parent 1ab2db1 commit bba7d0f

File tree

5 files changed

+444
-426
lines changed

5 files changed

+444
-426
lines changed

contracts/Curation.sol

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ contract Curation is Governed, BancorFormula {
2323
uint256 reserveRatio; // Ratio for the bonding curve
2424
uint256 tokens; // Tokens that constitute the subgraph reserve
2525
uint256 shares; // Shares issued for this subgraph
26-
mapping(address => uint256) curatorShares;
26+
mapping(address => uint256) curatorShares; // Mapping of curator => shares
2727
}
2828

2929
// 100% in parts per million
@@ -41,33 +41,55 @@ contract Curation is Governed, BancorFormula {
4141
// Parts per million. (Allows for 4 decimal points, 999,999 = 99.9999%)
4242
uint256 public defaultReserveRatio;
4343

44-
// Minimum amount allowed to be staked by Market Curators
44+
// Minimum amount allowed to be staked by curators
4545
// This is the `startPoolBalance` for the bonding curve
4646
uint256 public minimumCurationStake;
4747

48-
// Total staked tokens across all subgraphs
49-
uint256 public totalTokens;
50-
51-
// Subgraphs and curators mapping : subgraphID => Subgraph
48+
// Mapping of subgraphID => Subgraph
5249
mapping(bytes32 => Subgraph) public subgraphs;
5350

54-
// Address of a party that will distribute fees to subgraph reserves
55-
address public distributor;
51+
// Address of a staking contract that will distribute fees to subgraph reserves
52+
address public staking;
5653

5754
// Token used for staking
5855
GraphToken public token;
5956

6057
// -- Events --
6158

62-
event CuratorStakeUpdated(address indexed curator, bytes32 indexed subgraphID, uint256 shares);
63-
event SubgraphStakeUpdated(bytes32 indexed subgraphID, uint256 shares, uint256 tokens);
59+
/**
60+
* @dev Emitted when `curator` staked `tokens` on `subgraphID` as curation signal.
61+
* The `curator` receives `shares` amount according to the subgraph bonding curve.
62+
*/
63+
event Staked(
64+
address indexed curator,
65+
bytes32 indexed subgraphID,
66+
uint256 tokens,
67+
uint256 shares
68+
);
69+
70+
/**
71+
* @dev Emitted when `curator` redeemed `shares` for a `subgraphID`.
72+
* The curator will receive `tokens` according to the value of the bonding curve.
73+
*/
74+
event Redeemed(
75+
address indexed curator,
76+
bytes32 indexed subgraphID,
77+
uint256 tokens,
78+
uint256 shares
79+
);
80+
81+
/**
82+
* @dev Emitted when `tokens` amount were collected for `subgraphID` as part of fees
83+
* distributed by index node from the settlement of query fees on the subgraph.
84+
*/
85+
event Collected(bytes32 indexed subgraphID, uint256 tokens);
6486

6587
/**
6688
* @dev Contract Constructor
6789
* @param _governor Owner address of this contract
6890
* @param _token Address of the Graph Protocol token
69-
* @param _defaultReserveRatio Address of the staking contract used for slashing
70-
* @param _minimumCurationStake Percent of stake the fisherman gets on slashing (in PPM)
91+
* @param _defaultReserveRatio Reserve ratio used for the bonding curves of subgraphs
92+
* @param _minimumCurationStake Minimum amount of tokens that curators can stake on subgraphs
7193
*/
7294
constructor(
7395
address _governor,
@@ -105,12 +127,12 @@ contract Curation is Governed, BancorFormula {
105127
}
106128

107129
/**
108-
* @dev Set the address of party in charge of fee distributions into reserves
109-
* @notice Update the distributor address to `_distributor`
110-
* @param _distributor Address of the party doing fee distributions
130+
* @dev Set the staking contract used for fees distribution
131+
* @notice Update the staking contract to `_staking`
132+
* @param _staking Address of the staking contract
111133
*/
112-
function setDistributor(address _distributor) external onlyGovernor {
113-
distributor = _distributor;
134+
function setStaking(address _staking) external onlyGovernor {
135+
staking = _staking;
114136
}
115137

116138
/**
@@ -149,52 +171,45 @@ contract Curation is Governed, BancorFormula {
149171
// Decode subgraphID
150172
bytes32 subgraphID = _data.slice(0, 32).toBytes32(0);
151173

152-
// Transfers from distributor means we are assigning fees to reserves
153-
if (_from == distributor) {
174+
// Transfers from staking means we are assigning fees to reserves
175+
if (_from == staking) {
154176
_collect(subgraphID, _value);
155177
return true;
156178
}
157179

158-
// Any other source address means they are staking
180+
// Any other source address means they are staking tokens for shares
159181
_stake(_from, subgraphID, _value);
160182
return true;
161183
}
162184

163185
/**
164-
* @dev Return any amount of shares to get tokens back (above the minimum)
165-
* @notice Unstake _shares from the subgraph with _subgraphID
186+
* @dev Return an amount of shares to get tokens back
187+
* @notice Redeem _shares from the subgraph with _subgraphID
166188
* @param _subgraphID Subgraph ID the Curator is returning shares for
167189
* @param _shares Amount of shares to return
168190
*/
169-
function unstake(bytes32 _subgraphID, uint256 _shares) external {
191+
function redeem(bytes32 _subgraphID, uint256 _shares) external {
170192
address curator = msg.sender;
171193
Subgraph storage subgraph = subgraphs[_subgraphID];
172194

173-
require(_shares > 0, "Cannot unstake zero shares");
195+
require(_shares > 0, "Cannot redeem zero shares");
174196
require(
175197
subgraph.curatorShares[curator] >= _shares,
176-
"Cannot unstake more shares than you own"
198+
"Cannot redeem more shares than you own"
177199
);
178200

179201
// Update balance and get the amount of tokens to refund based on returned shares
180-
uint256 tokensToRefund = _sellShares(curator, _subgraphID, _shares);
181-
182-
// Ensure we are not under minimum required stake
183-
require(
184-
subgraph.tokens >= minimumCurationStake || subgraph.tokens == 0,
185-
"Cannot unstake below minimum required stake for subgraph"
186-
);
202+
uint256 tokens = _sellShares(curator, _subgraphID, _shares);
187203

188-
// Delete if left without stakes
189-
if (subgraph.tokens == 0) {
204+
// If all shares redeemed delete subgraph
205+
if (subgraph.shares == 0) {
190206
delete subgraphs[_subgraphID];
191207
}
192208

193209
// Return the tokens to the curator
194-
require(token.transfer(curator, tokensToRefund), "Error sending curator tokens");
210+
require(token.transfer(curator, tokens), "Error sending curator tokens");
195211

196-
emit CuratorStakeUpdated(curator, _subgraphID, subgraph.curatorShares[curator]);
197-
emit SubgraphStakeUpdated(_subgraphID, subgraph.shares, subgraph.tokens);
212+
emit Redeemed(curator, _subgraphID, tokens, _shares);
198213
}
199214

200215
/**
@@ -289,9 +304,6 @@ contract Curation is Governed, BancorFormula {
289304
subgraph.shares = subgraph.shares.add(shares);
290305
subgraph.curatorShares[_curator] = subgraph.curatorShares[_curator].add(shares);
291306

292-
// Update global balance
293-
totalTokens = totalTokens.add(_tokens);
294-
295307
return shares;
296308
}
297309

@@ -316,35 +328,29 @@ contract Curation is Governed, BancorFormula {
316328
subgraph.shares = subgraph.shares.sub(_shares);
317329
subgraph.curatorShares[_curator] = subgraph.curatorShares[_curator].sub(_shares);
318330

319-
// Update global balance
320-
totalTokens = totalTokens.sub(tokens);
321-
322331
return tokens;
323332
}
324333

325334
/**
326-
* @dev Assign Graph Tokens received from distributor to the subgraph reserve
335+
* @dev Assign Graph Tokens received from staking to the subgraph reserve
327336
* @param _subgraphID Subgraph where funds should be allocated as reserves
328337
* @param _tokens Amount of Graph Tokens to add to reserves
329338
*/
330339
function _collect(bytes32 _subgraphID, uint256 _tokens) private {
331340
require(isSubgraphCurated(_subgraphID), "Subgraph must be curated to collect fees");
332341

333-
// Collect new funds to reserve
342+
// Collect new funds into a subgraph reserve
334343
Subgraph storage subgraph = subgraphs[_subgraphID];
335344
subgraph.tokens = subgraph.tokens.add(_tokens);
336345

337-
// Update global tokens balance
338-
totalTokens = totalTokens.add(_tokens);
339-
340-
emit SubgraphStakeUpdated(_subgraphID, subgraph.shares, subgraph.tokens);
346+
emit Collected(_subgraphID, _tokens);
341347
}
342348

343349
/**
344-
* @dev Stake Graph Tokens for Market Curation by subgraphID
345-
* @param _subgraphID Subgraph ID the Curator is staking Graph Tokens for
346-
* @param _curator Address of Staking party
347-
* @param _tokens Amount of Graph Tokens to be staked
350+
* @dev Deposit Graph Tokens in exchange for shares of a subgraph
351+
* @param _subgraphID Subgraph ID where the curator is staking Graph Tokens
352+
* @param _curator Address of staking party
353+
* @param _tokens Amount of Graph Tokens to stake
348354
*/
349355
function _stake(address _curator, bytes32 _subgraphID, uint256 _tokens) private {
350356
Subgraph storage subgraph = subgraphs[_subgraphID];
@@ -358,9 +364,8 @@ contract Curation is Governed, BancorFormula {
358364
}
359365

360366
// Update subgraph balances
361-
_buyShares(_curator, _subgraphID, _tokens);
367+
uint256 shares = _buyShares(_curator, _subgraphID, _tokens);
362368

363-
emit CuratorStakeUpdated(_curator, _subgraphID, subgraph.curatorShares[_curator]);
364-
emit SubgraphStakeUpdated(_subgraphID, subgraph.shares, subgraph.tokens);
369+
emit Staked(_curator, _subgraphID, _tokens, shares);
365370
}
366371
}

migrations/2_deploy_contracts.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ module.exports = async (deployer, network, accounts) => {
7171
staking.setChannelDisputeEpochs(config.staking.channelDisputeEpochs),
7272
' > Staking -> Set channelDisputeEpochs: ',
7373
)
74-
await executeAndLog(
75-
curation.setDistributor(staking.address),
76-
' > Curation -> Set distributor: ',
77-
)
74+
await executeAndLog(curation.setStaking(staking.address), ' > Curation -> Set staking: ')
7875

7976
// Summary
8077
log('\n')

0 commit comments

Comments
 (0)