all files / contracts/ ROSCA.sol

8.23% Statements 13/158
2.08% Branches 2/96
6.25% Functions 2/32
7.78% Lines 13/167
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
pragma solidity ^0.4.15;
 
import "./deps/ERC20TokenInterface.sol";
import "./deps/SafeMath.sol";
 
/**
 * @title ROSCA on a blockchain.
 *
 * A ROSCA (Rotating and Savings Credit Association) is an agreement between
 * trusted friends to contribute funds on a periodic basis to a "pot", and in
 * each round one of the participants receives the pot (termed "winner").
 * The winner is selected as the person who makes the lowest bid in that round
 * among those who have not won a bid before.
 * The discount (gap between bid and total round contributions) is dispersed
 * evenly between the participants.
 *
 * Supports ETH and ERC20-compliant tokens.
 */
contract ROSCA {
 
  uint16 public constant CONTRACT_VERSION  = 3;
  ////////////
  // CONSTANTS
  ////////////
  // Maximum fee (in 1/1000s) from dispersements that is shared between foreperson and other stakeholders..
  uint16 constant internal MAX_FEE_IN_THOUSANDTHS = 20;
 
  // Start time of the ROSCA must be not more than this much time ago when the ROSCA is created
  // This is to prevent accidental or malicious deployment of ROSCAs that are supposedly
  // already in round number X > 1 and participants are supposedly delinquent.
  uint32 constant internal MAXIMUM_TIME_PAST_SINCE_ROSCA_START_SECS = 900;  // 15 minutes
 
  // the winning bid must be at least this much of the maximum (aka default) pot value
  uint8 constant internal MIN_DISTRIBUTION_PERCENT = 65;
 
  // Every new bid has to be at most this much of the previous lowest bid
  uint8 constant internal MAX_NEXT_BID_RATIO = 98;
 
  // WeTrust's account from which Escape Hatch can be enabled.
  address constant internal ESCAPE_HATCH_ENABLER = 0x5bb8D0DfdAf3a03cF51F9c8623C160a4c021E522;
 
  /////////
  // EVENTS
  /////////
  event LogContributionMade(address user, uint256 amount, uint256 currentRound);
  event LogStartOfRound(uint256 currentRound);
  event LogBidSurpassed(uint256 prevBid, address prevWinnerAddress, uint256 currentRound);
  event LogNewLowestBid(uint256 bid, address winnerAddress, uint256 currentRound);
  event LogRoundFundsReleased(address winnerAddress, uint256 amount, uint256 roundDiscount, uint256 currentRound);
  event LogFundsWithdrawal(address user, uint256 amount, uint256 currentRound);
  // Fired when withdrawer is entitled for a larger amount than the contract
  // actually holds (excluding fees). A LogFundsWithdrawal will follow
  // this event with the actual amount released, if send() is successful.
  event LogCannotWithdrawFully(uint256 creditAmount, uint256 currentRound);
  event LogUnsuccessfulBid(address bidder, uint256 bid, uint256 lowestBid, uint256 currentRound);
  event LogEndOfROSCA();
  event LogForepersonSurplusWithdrawal(uint256 amount);
  event LogFeesWithdrawal(uint256 amount);
 
  // Escape hatch related events.
  event LogEscapeHatchEnabled();
  event LogEscapeHatchActivated();
  event LogEmergencyWithdrawalPerformed(uint256 fundsDispersed, uint256 currentRound);
 
  ////////////////////
  // STORAGE VARIABLES
  ////////////////////
 
  // ROSCA parameters
  uint256 internal roundPeriodInSecs;
  uint16 internal serviceFeeInThousandths;
  uint16 public currentRound = 1;  // rosca is started the moment it is created
  address internal foreperson;
  uint128 internal contributionSize;
  uint256 internal startTime;
  ERC20TokenInterface public tokenContract;  // public - allow easy verification of token contract.
 
  // ROSCA state
  // Currently, we support three different types of ROSCA
  // 0 : ROSCA where lowest bidder wins (Bidding ROSCA)
  // 1 : ROSCA where winners are chosen at random (random Selection ROSCA)
  // 2 : ROSCA where winners are selected through a ordered list (pre-determined ROSCA)
  enum typesOfROSCA { BIDDING_ROSCA, RANDOM_SELECTION_ROSCA, PRE_DETERMINED_ROSCA }
  typesOfROSCA roscaType;
  bool public endOfROSCA = false;
  bool public forepersonSurplusCollected = false;
  // A discount is the difference between a winning bid and the pot value. totalDiscounts is the amount
  // of discounts accumulated so far, divided by the number of ROSCA participants.
  uint256 public totalDiscounts = 0;
 
  // Amount of fees reserved in the contract for fees.
  uint256 public totalFees = 0;
 
  // Round state variables
  uint256 public lowestBid = 0;
  address public winnerAddress = 0;  // bidder who bid the lowest so far
 
  mapping(address => User) internal members;
  address[] public membersAddresses;  // for iterating through members' addresses
 
  // Other state
  // An escape hatch is used in case a major vulnerability is discovered in the contract code.
  // The following procedure is then put into action:
  // 1. WeTrust sends a transaction to make escapeHatchEnabled true.
  // 2. foreperson is notified and can decide to activate the escapeHatch.
  // 3. If escape hatch is activated, no contributions and/or withdrawals are allowed. The foreperson
  //    may call withdraw() to withdraw all of the contract's funds and then disperse them offline
  //    among the participants.
  bool public escapeHatchEnabled = false;
  bool public escapeHatchActive = false;
 
  struct User {
    uint256 credit;  // amount of funds user has contributed - winnings (not including discounts) so far
    bool debt; // true if user won the pot while not in good standing and is still not in good standing
    bool paid; // yes if the member had won a Round
    bool alive; // needed to check if a member is indeed a member
  }
 
  ////////////
  // MODIFIERS
  ////////////
  modifier onlyFromMember {
    require(members[msg.sender].alive);
    _;
  }
 
  modifier onlyFromForeperson {
    require(msg.sender == foreperson);
    _;
  }
 
  modifier onlyIfRoscaNotEnded {
    require(!endOfROSCA);
    _;
  }
 
  modifier onlyIfRoscaEnded {
    require(endOfROSCA);
    _;
  }
 
  modifier onlyIfEscapeHatchActive {
    require(escapeHatchActive);
    _;
  }
 
  modifier onlyIfEscapeHatchInactive {
    require(!escapeHatchActive);
    _;
  }
 
  modifier onlyBIDDING_ROSCA {
    require(roscaType == typesOfROSCA.BIDDING_ROSCA);
    _;
  }
 
  modifier onlyFromEscapeHatchEnabler {
    require(msg.sender == ESCAPE_HATCH_ENABLER);
    _;
  }
 
  ////////////
  // FUNCTIONS
  ////////////
 
  /**
    * @dev Creates a new ROSCA and initializes the necessary variables. ROSCA starts after startTime.
    * Creator of the contract becomes foreperson but not a participant (unless creator's address
    *   is included in members_ parameter).
    *
    *
    * If erc20TokenContract is 0, ETH is taken to be the currency of this ROSCA. Otherwise, this
    * contract assumes `erc20tokenContract` specifies an ERC20-compliant token contract.
    * Note it's the creator's responsibility to check that the provided contract is ERC20 compliant and that
    * it's safe to use.
    */
  function ROSCA(
      ERC20TokenInterface erc20tokenContract,  // pass 0 to use ETH
      typesOfROSCA roscaType_,
      uint256 roundPeriodInSecs_,
      uint128 contributionSize_,
      uint256 startTime_,
      address[] members_,
      uint16 serviceFeeInThousandths_) {
    Erequire(roundPeriodInSecs_ != 0 &&
      startTime_ >= (now - MAXIMUM_TIME_PAST_SINCE_ROSCA_START_SECS) &&
      serviceFeeInThousandths_ <= MAX_FEE_IN_THOUSANDTHS &&
      members_.length > 0);
 
    roundPeriodInSecs = roundPeriodInSecs_;
    contributionSize = contributionSize_;
    startTime = startTime_;
    roscaType = roscaType_;
    tokenContract = erc20tokenContract;
    serviceFeeInThousandths = serviceFeeInThousandths_;
 
    foreperson = msg.sender;
 
    for (uint16 i = 0; i < members_.length; i++) {
      addMember(members_[i]);
    }
 
    LogStartOfRound(currentRound);
  }
 
  function addMember(address newMember) internal {
    Erequire(!members[newMember].alive);  // already registered
 
    members[newMember] = User({paid: false , credit: 0, alive: true, debt: false});
    membersAddresses.push(newMember);
  }
 
  /**
    * @dev Calculates the winner of the current round's pot, and credits her.
    * If there were no bids during the round, winner is selected semi-randomly.
    * Priority is given to non-delinquent participants.
    */
  function startRound() onlyIfRoscaNotEnded external {
    uint256 roundStartTime = SafeMath.add(startTime, (SafeMath.mul(uint(currentRound), roundPeriodInSecs)));
    require(now >= roundStartTime ); // too early to start a new round.
 
    if (currentRound != 0) {
      cleanUpPreviousRound();
    }
    if (currentRound < membersAddresses.length) {
      lowestBid = 0;
      winnerAddress = 0;
      currentRound++;
      LogStartOfRound(currentRound);
    } else {
        endOfROSCA = true;
        LogEndOfROSCA();
    }
  }
 
  function cleanUpPreviousRound() internal {
    uint256 winnerIndex;
    bool winnerSelectedThroughBid = (winnerAddress != 0);
    uint16 numUnpaidParticipants = uint16(membersAddresses.length) - (currentRound - 1);
    // for pre-ordered ROSCA, pick the next person in the list (delinquent or not)
    if (roscaType == typesOfROSCA.PRE_DETERMINED_ROSCA) {
      winnerAddress = membersAddresses[currentRound - 1];
    }
    if (winnerAddress == 0) {
      winnerIndex = findSemiRandomWinner(numUnpaidParticipants);
    }
    // We keep the unpaid participants at positions [0..num_participants - current_round) so that we can uniformly select
    // among them (if we didn't do that and there were a few consecutive paid participants, we'll be more likely to select the
    // next unpaid member).
    if (roscaType != typesOfROSCA.PRE_DETERMINED_ROSCA) {
      swapWinner(winnerIndex, winnerSelectedThroughBid, numUnpaidParticipants - 1);
    }
 
    creditWinner();
    recalculateTotalFees();
  }
 
  function creditWinner() internal {
    if (lowestBid == 0) {
      lowestBid = potSize();
    }
    uint256 currentRoundTotalDiscounts = removeFees(potSize() - lowestBid);
    uint256 roundDiscount = currentRoundTotalDiscounts / membersAddresses.length;
    totalDiscounts += roundDiscount;
    members[winnerAddress].credit += removeFees(lowestBid);
    members[winnerAddress].paid = true;
    LogRoundFundsReleased(winnerAddress, lowestBid, roundDiscount, currentRound);
  }
 
  function findSemiRandomWinner(uint16 numUnpaidParticipants) internal returns (uint256) {
    address delinquentWinner = 0x0;
    uint256 winnerIndex;
    // There was no bid in this round. Find an unpaid address for this epoch.
    // Give priority to members in good standing (not delinquent).
    // Note this randomness does not require high security, that's why we feel ok with using the block's timestamp.
    // Everyone will be paid out eventually.
    uint256 semi_random = now % numUnpaidParticipants;
    for (uint16 i = 0; i < numUnpaidParticipants; i++) {
      uint256 index = (semi_random + i) % numUnpaidParticipants;
      address candidate = membersAddresses[index];
      if (!members[candidate].paid) {
        winnerIndex = index;
        if (members[candidate].credit + totalDiscounts >= requiredContribution()) {
          // We found a non-delinquent winner.
          winnerAddress = candidate;
          break;
        }
        delinquentWinner = candidate;
      }
    }
    if (winnerAddress == 0) {  // we did not find any non-delinquent winner.
      // Perform some basic sanity checks.
      assert(delinquentWinner != 0 && !members[delinquentWinner].paid);
      winnerAddress = delinquentWinner;
      // Set the flag to true so we know this user cannot withdraw until debt has been paid.
      members[winnerAddress].debt = true;
    }
    // Set lowestBid to the right value since there was no winning bid.
    lowestBid = potSize();
    return winnerIndex;
  }
 
  // Recalculates that total fees that should be allocated in the contract.
  function recalculateTotalFees() internal {
    // Start with the max theoretical fees if no one was delinquent, and
    // reduce funds not actually contributed because of delinquencies.
    uint256 grossTotalFees = SafeMath.mul(requiredContribution(), membersAddresses.length);
 
    for (uint16 j = 0; j < membersAddresses.length; j++) {
      User memory member = members[membersAddresses[j]];
      uint256 credit = member.credit;
      uint256 debit = requiredContribution();
      if (member.debt) {
        // As a delinquent member won, we'll reduce the funds subject to fees by the default pot they must have won (since
        // they could not bid), to correctly calculate their delinquency.
        debit = SafeMath.add(debit, removeFees(potSize()));
      }
      if (credit + totalDiscounts < debit) {
        grossTotalFees = SafeMath.sub(grossTotalFees, (debit - credit - totalDiscounts));
      }
    }
 
    totalFees = SafeMath.mul(grossTotalFees, serviceFeeInThousandths) / 1000;
  }
 
  // Swaps membersAddresses[winnerIndex] with membersAddresses[indexToSwap]. However,
  // if winner was selected through a bid, winnerIndex was not set, and we find it first.
  function swapWinner(
    uint256 winnerIndex, bool winnerSelectedThroughBid, uint256 indexToSwap) internal {
    if (winnerSelectedThroughBid) {
      // Since winner was selected through a bid, we were not able to set winnerIndex, so search
      // for the winner among the unpaid participants.
      for (uint16 i = 0; i <= indexToSwap; i++) {
        if (membersAddresses[i] == winnerAddress) {
          winnerIndex = i;
          break;
        }
      }
    }
    // We now want to swap winnerIndex with indexToSwap, but we already know membersAddresses[winnerIndex] == winnerAddress.
    membersAddresses[winnerIndex] = membersAddresses[indexToSwap];
    membersAddresses[indexToSwap] = winnerAddress;
  }
 
  // Calculates the specified amount net amount after fees.
  function removeFees(uint256 amount) internal constant returns (uint256) {
    // First multiply to reduce roundoff errors.
    return SafeMath.mul(amount, (1000 - serviceFeeInThousandths)) / 1000;
  }
 
  // Validates a non-zero contribution from msg.sender and returns
  // the amount.
  function validateAndReturnContribution() internal returns (uint256) {  // dontMakePublic
    bool isEthRosca = (tokenContract == address(0));
    require(isEthRosca || msg.value <= 0);  // token ROSCAs should not accept ETH
 
    uint256 value = (isEthRosca ? msg.value : tokenContract.allowance(msg.sender, address(this)));
    require(value != 0);
 
    if (isEthRosca) {
      return value;
    }
    require(tokenContract.transferFrom(msg.sender, address(this), value));
    return value;
  }
 
  /**
   * Processes a periodic contribution. msg.sender must be one of the participants and will thus
   * identify the contributor.
   *
   * Any excess funds are withdrawable through withdraw() without fee.
   */
  function contribute() payable onlyFromMember onlyIfRoscaNotEnded onlyIfEscapeHatchInactive external {
    User storage member = members[msg.sender];
    uint256 value = validateAndReturnContribution();
    member.credit = SafeMath.add(member.credit, value);
    if (member.debt) {
      // Check if user comes out of debt. We know they won an entire pot as they could not bid,
      // so we check whether their credit w/o that winning is non-delinquent.
      // check that credit must defaultPot (when debt is set to true, defaultPot was added to credit as winnings) +
      // currentRound in order to be out of debt
      if (SafeMath.sub(member.credit + totalDiscounts, removeFees(potSize())) >= requiredContribution()) {
          member.debt = false;
      }
    }
 
    LogContributionMade(msg.sender, value, currentRound);
  }
 
  /**
   * Registers a bid from msg.sender. Participant should call this method
   * only if all of the following holds for her:
   * + Never won a round.
   * + Is in good standing (i.e. actual contributions, including this round's,
   *   plus any past earned discounts are together greater than required contributions).
   * + New bid is lower than the lowest bid so far.
   */
  function bid(uint256 bid) onlyFromMember onlyIfRoscaNotEnded onlyIfEscapeHatchInactive onlyBIDDING_ROSCA external {
    require(!members[msg.sender].paid  &&
        currentRound != 0 &&  // ROSCA hasn't started yet
        // participant not in good standing
        members[msg.sender].credit + totalDiscounts >= requiredContribution() &&
        // bid is less than minimum allowed
        bid >= SafeMath.mul(potSize(), MIN_DISTRIBUTION_PERCENT) / 100);
 
    // If winnerAddress is 0, this is the first bid, hence allow full pot.
    // Otherwise, make sure bid is low enough compared to previous bid.
    uint256 maxAllowedBid = winnerAddress == 0
        ? potSize()
        : SafeMath.mul(lowestBid, MAX_NEXT_BID_RATIO) / 100;
    if (bid > maxAllowedBid) {
      // We don't throw as this may be hard for the frontend to predict on the
      // one hand because someone else might have bid at the same time,
      // and we'd like to avoid wasting the caller's gas.
      LogUnsuccessfulBid(msg.sender, bid, lowestBid, currentRound);
      return;
    }
    if (winnerAddress != 0) {
      LogBidSurpassed(lowestBid, winnerAddress, currentRound);
    }
 
    lowestBid = bid;
    winnerAddress = msg.sender;
    LogNewLowestBid(lowestBid, winnerAddress, currentRound);
  }
 
  // Sends funds (either ETH or tokens) to msg.sender. Returns whether successful.
  function sendFundsToMsgSender(uint256 value) internal returns (bool) {
    bool isEthRosca = (tokenContract == address(0));
    if (isEthRosca) {
      return msg.sender.send(value);
    }
    return tokenContract.transfer(msg.sender, value);
  }
 
  /**
   * Withdraws available funds for msg.sender.
   */
  function withdraw() onlyFromMember onlyIfEscapeHatchInactive external returns(bool success) {
    require (!members[msg.sender].debt || endOfROSCA); // delinquent winners need to first pay their debt
 
    uint256 totalCredit = members[msg.sender].credit + totalDiscounts;
 
    uint256 totalDebit = members[msg.sender].debt
        ? removeFees(potSize())  // this must be end of rosca
        : requiredContribution();
    require(totalDebit < totalCredit);  // nothing to withdraw
 
    uint256 amountToWithdraw = SafeMath.sub(totalCredit, totalDebit);
    uint256 amountAvailable = SafeMath.sub(getBalance(), totalFees);
 
    if (amountAvailable < amountToWithdraw) {
      // This may happen if some participants are delinquent.
      LogCannotWithdrawFully(amountToWithdraw, currentRound);
      amountToWithdraw = amountAvailable;
    }
    members[msg.sender].credit -= amountToWithdraw;
    if (!sendFundsToMsgSender(amountToWithdraw)) {   // if the send() fails, restore the allowance
      // No need to call throw here, just reset the amount owing. This may happen
      // for nonmalicious reasons, e.g. the receiving contract running out of gas.
      members[msg.sender].credit += amountToWithdraw;
      return false;
    }
    LogFundsWithdrawal(msg.sender, amountToWithdraw, currentRound);
    return true;
  }
 
  /**
   * Returns how much a user can withdraw (positive return value),
   * or how much they need to contribute to be in good standing (negative return value)
   */
  function getParticipantBalance(address user) onlyFromMember external constant returns(int256) {
    int256 totalCredit = int256(members[user].credit + totalDiscounts);
 
    // if rosca have ended, we don't need to subtract as totalDebit should equal to default winnings
    if (members[user].debt && !endOfROSCA) {
        totalCredit -= int256(removeFees(potSize()));
    }
    int256 totalDebit = int256(requiredContribution());
 
    return totalCredit - totalDebit;
  }
 
  /**
   * Returns the amount of funds this contract holds excluding fees. This is
   * the amount withdrawable by participants.
   */
  function getContractNetBalance() external constant returns(uint256) {
    return SafeMath.sub(getBalance(), totalFees);
  }
 
  /**
   * Returns the balance of this contract, in ETH or the ERC20 token involved.
   */
  function getBalance() internal constant returns (uint256) {
    bool isEthRosca = (tokenContract == address(0));
 
    return isEthRosca ? this.balance : tokenContract.balanceOf(address(this));
  }
 
  /**
   * @dev Allows the foreperson to retrieve any surplus funds, one roundPeriodInSecs after
   * the end of the ROSCA. Note this does not retrieve the foreperson's fees, which should
   * be retireved by calling endOfROSCARetrieveFees.
   *
   * Note that startRound() must be called first after the last round, as it
   * does the bookeeping of that round.
   */
  function endOfROSCARetrieveSurplus() onlyFromForeperson onlyIfRoscaEnded external {
    uint256 roscaCollectionTime = SafeMath.add(startTime, SafeMath.mul((membersAddresses.length + 1), roundPeriodInSecs));
    require(now >= roscaCollectionTime && !forepersonSurplusCollected);
 
    forepersonSurplusCollected = true;
    uint256 amountToCollect = SafeMath.sub(getBalance(), totalFees);
    if (!sendFundsToMsgSender(amountToCollect)) {   // if the send() fails, restore the flag
      // No need to call throw here, just reset the amount owing. This may happen
      // for nonmalicious reasons, e.g. the receiving contract running out of gas.
      forepersonSurplusCollected = false;
    } else {
      LogForepersonSurplusWithdrawal(amountToCollect);
    }
  }
 
  /**
   * @dev Allows the foreperson to extract the fees in the contract. Can be called
   * after the end of the ROSCA.
   *
   * Note that startRound() must be called first after the last round, as it
   * does the bookeeping of that round.
   */
  function endOfROSCARetrieveFees() onlyFromForeperson onlyIfRoscaEnded external {
    uint256 tempTotalFees = totalFees;  // prevent re-entry.
    totalFees = 0;
    if (!sendFundsToMsgSender(tempTotalFees)) {   // if the send() fails, restore totalFees
      // No need to call throw here, just reset the amount owing. This may happen
      // for nonmalicious reasons, e.g. the receiving contract running out of gas.
      totalFees = tempTotalFees;
    } else {
      LogFeesWithdrawal(tempTotalFees);
    }
  }
 
  /**
   * Allows the Escape Hatch Enabler (controlled by WeTrust) to enable the Escape Hatch in case of
   * emergency (e.g. a major vulnerability found in the contract).
   */
  function enableEscapeHatch() onlyFromEscapeHatchEnabler external {
    escapeHatchEnabled = true;
    LogEscapeHatchEnabled();
  }
 
  /**
   * Allows the foreperson to active the Escape Hatch after the Enabled enabled it. This will freeze all
   * contributions and withdrawals, and allow the foreperson to retrieve all funds into their own account,
   * to be dispersed offline to the other participants.
   */
  function activateEscapeHatch() onlyFromForeperson external {
    require(escapeHatchEnabled);
 
    escapeHatchActive = true;
    LogEscapeHatchActivated();
  }
 
  /**
   * Can only be called by the foreperson after an escape hatch is activated,
   * this sends all the funds to the foreperson by selfdestructing this contract.
   */
  function emergencyWithdrawal() onlyFromForeperson onlyIfEscapeHatchActive external {
    LogEmergencyWithdrawalPerformed(getBalance(), currentRound);
    // Send everything, including potential fees, to foreperson to disperse offline to participants.
    bool isEthRosca = (tokenContract == address(0));
    if (!isEthRosca) {
      uint256 balance = tokenContract.balanceOf(address(this));
      // we don't care much about the success of transfer` here as there's not much we can do.
      tokenContract.transfer(foreperson, balance);
    }
    selfdestruct(foreperson);
  }
 
  /**
   * Helper Functions
   */
  function potSize() internal constant returns (uint256) {
    return SafeMath.mul(contributionSize, membersAddresses.length);
  }
 
  function requiredContribution() internal constant returns (uint256) {
    return SafeMath.mul(contributionSize, currentRound);
  }
}