11/* SPDX-License-Identifier: MIT */
2- pragma solidity ^ 0.6.10 ;
2+ pragma solidity 0.6.10 ;
33pragma experimental ABIEncoderV2;
44
55import { console } from "@nomiclabs/buidler/console.sol " ;
@@ -129,7 +129,7 @@ contract SinglePlayerCommit is ChainlinkClient, Ownable {
129129 "SPC::deposit - token transfer failed "
130130 );
131131
132- _changeCommitterBalance (amount, true );
132+ _changeCommitterBalance (msg . sender , amount, true );
133133
134134 emit Deposit (msg .sender , amount);
135135
@@ -150,7 +150,7 @@ contract SinglePlayerCommit is ChainlinkClient, Ownable {
150150
151151 require (amount <= available, "SPC::withdraw - not enough (unstaked) balance available " );
152152
153- _changeCommitterBalance (amount, false );
153+ _changeCommitterBalance (msg . sender , amount, false );
154154
155155 require (token.transfer (msg .sender , amount), "SPC::withdraw - token transfer failed " );
156156
@@ -244,7 +244,6 @@ contract SinglePlayerCommit is ChainlinkClient, Ownable {
244244 return true ;
245245 }
246246
247- //TODO DRY in procesCommitment methods
248247 /// @notice Enables processing of open commitments after endDate that have not been processed by creator
249248 /// @param committer address of the creator of the committer to process
250249 /// @dev Process commitment by lookup based on address, checking metrics, state and updating balances
@@ -256,13 +255,8 @@ contract SinglePlayerCommit is ChainlinkClient, Ownable {
256255 require (commitment.endTime < block .timestamp , "SPC::processCommitment - commitment is still active " );
257256 require (commitment.endTime < commitment.lastActivityUpdate, "SPC::processCommitment - update activity " );
258257
259- commitment.met = commitment.reportedValue >= commitment.goalValue ;
258+ require ( _commitmentSettlement ( commitment), " SPC::processCommitmentUser - reconciliation failed " ) ;
260259
261- if (! commitment.met) {
262- _slashFunds (commitment.stake, committer);
263- }
264-
265- commitment.exists = false ;
266260 emit CommitmentEnded (committer, commitment.met, commitment.stake);
267261 }
268262
@@ -273,17 +267,23 @@ contract SinglePlayerCommit is ChainlinkClient, Ownable {
273267 require (commitments[msg .sender ].exists, "SPC::processCommitmentUser - commitment does not exist " );
274268 Commitment storage commitment = commitments[msg .sender ];
275269
270+ require (_commitmentSettlement (commitment), "SPC::processCommitmentUser - reconciliation failed " );
271+ emit CommitmentEnded (msg .sender , commitment.met, commitment.stake);
272+ }
273+
274+ /// @notice Internal function for evaluating commitment and slashing funds if needed
275+ /// @dev Receive call with commitment object from storage
276+ function _commitmentSettlement (Commitment storage commitment ) internal returns (bool success ) {
276277 commitment.met = commitment.reportedValue > commitment.goalValue;
277278
278279 if (! commitment.met) {
279280 _slashFunds (commitment.stake, msg .sender );
280281 }
281282
282283 commitment.exists = false ;
283- emit CommitmentEnded ( msg . sender , commitment.met, commitment.stake) ;
284+ return true ;
284285 }
285286
286- //TODO state change after transfer is not recommended
287287 /// @notice Contract owner can withdraw funds not owned by committers. E.g. slashed from failed commitments
288288 /// @param amount Amount of <token> to withdraw
289289 /// @dev Check amount against slashedBalance, transfer amount and update slashedBalance
@@ -302,12 +302,12 @@ contract SinglePlayerCommit is ChainlinkClient, Ownable {
302302 /// @param amount Amount of <token> to deposit/withdraw
303303 /// @param add Boolean toggle to deposit or withdraw
304304 /// @dev Based on add param add or substract amount from msg.sender balance and total committerBalance
305- function _changeCommitterBalance (uint256 amount , bool add ) internal returns (bool success ) {
305+ function _changeCommitterBalance (address committer , uint256 amount , bool add ) internal returns (bool success ) {
306306 if (add) {
307- committerBalances[msg . sender ] = committerBalances[msg . sender ].add (amount);
307+ committerBalances[committer ] = committerBalances[committer ].add (amount);
308308 totalCommitterBalance = totalCommitterBalance.add (amount);
309309 } else {
310- committerBalances[msg . sender ] = committerBalances[msg . sender ].sub (amount);
310+ committerBalances[committer ] = committerBalances[committer ].sub (amount);
311311 totalCommitterBalance = totalCommitterBalance.sub (amount);
312312 }
313313
@@ -320,7 +320,7 @@ contract SinglePlayerCommit is ChainlinkClient, Ownable {
320320 /// @dev Substract amount from committer balance and add to slashedBalance
321321 function _slashFunds (uint256 amount , address committer ) internal returns (bool success ) {
322322 require (committerBalances[committer] >= amount, "SPC::_slashFunds - funds not available " );
323- _changeCommitterBalance (amount, false );
323+ _changeCommitterBalance (committer, amount, false );
324324 slashedBalance = slashedBalance.add (amount);
325325 return true ;
326326 }
0 commit comments