all files / contracts/Entity/ Funding.sol

99.4% Statements 165/166
95.56% Branches 86/90
100% Functions 23/23
99.39% Lines 164/165
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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716                                                                                                                                                                                                  116× 116×     116× 116×   116×       98× 98×           116× 116× 116× 116× 116× 116× 116× 116× 116× 116×     116× 116× 116× 116×                                 99×     98× 98× 98×                                         206×                                       205×     204×           203×     202× 100×         100×       99×         200× 200× 200× 200× 200× 200×     200× 200×     200× 200× 200×     200× 200×   200×   200×         200×   200× 298×     298×     298× 200×     298× 200×       200×       200× 200×       244×       32×       230×           98× 98×       468×       466×                         232×   231× 231×   231× 145×     231×   231× 231×                   233× 232×       235×   234×   232×                       232× 232× 231×                                                     190× 190× 190×                                 190× 190×                               191×   191× 191×   191× 190×                                 1799× 1799×   1799× 844×             955× 918×                           955×   302×                                 653×   10×               643× 336×     643×       244× 244×   244× 244×   244×   190× 190× 190×     244×     243× 243× 243×                           1347×   1347× 1347×   1347× 202×   1347× 295×   1347×             190× 190× 190× 103×   54×           243×       243×   243×               49×   37×   12×                                     1799×   1799× 1799× 1799×         1799×     586×   274×         312×   153×       159×                 1213×         22×                               172× 99×                                       53× 27×                                           1799×      
/*
 
 * @name        Funding Contract
 * @package     BlockBitsIO
 * @author      Micky Socaci <micky@nowlive.ro>
 
 Contains the Funding Contract code deployed and linked to the Application Entity
 
 
    !!! Links directly to Milestones
 
*/
 
pragma solidity ^0.4.17;
 
import "./../ApplicationAsset.sol";
import "./../Entity/FundingManager.sol";
import "./../Entity/TokenManager.sol";
 
import "./../Inputs/FundingInputDirect.sol";
import "./../Inputs/FundingInputMilestone.sol";
 
contract Funding is ApplicationAsset {
 
    address public multiSigOutputAddress;
    FundingInputDirect public DirectInput;
    FundingInputMilestone public MilestoneInput;
 
 
    // mapping (bytes32 => uint8) public FundingMethods;
    enum FundingMethodIds {
        __IGNORED__,
        DIRECT_ONLY, 				//
        MILESTONE_ONLY, 		    //
        DIRECT_AND_MILESTONE		//
    }
 
    TokenManager public TokenManagerEntity;
    FundingManager public FundingManagerEntity;
 
    event FundingStageCreated( uint8 indexed index, bytes32 indexed name );
 
    struct FundingStage {
        bytes32 name;
        uint8   state;
        uint256 time_start;
        uint256 time_end;
        uint256 amount_cap_soft;            // 0 = not enforced
        uint256 amount_cap_hard;            // 0 = not enforced
        uint256 amount_raised;              // 0 = not enforced
        // funding method settings
        uint256 minimum_entry;
        uint8   methods;                    // FundingMethodIds
        // token settings
        uint256 start_parity;
        uint8   price_addition_percentage;  //
        uint8   token_share_percentage;
        uint8   index;
    }
 
    mapping (uint8 => FundingStage) public Collection;
    uint8 public FundingStageNum = 0;
    uint8 public currentFundingStage = 1;
 
    // funding settings
    uint256 public AmountRaised = 0;
    uint256 public MilestoneAmountRaised = 0;
 
    uint256 public GlobalAmountCapSoft = 0;
    uint256 public GlobalAmountCapHard = 0;
 
    uint8 public TokenSellPercentage = 0;
 
    uint256 public Funding_Setting_funding_time_start = 0;
    uint256 public Funding_Setting_funding_time_end = 0;
 
    uint256 public Funding_Setting_cashback_time_start = 0;
    // end time is ignored at this stage, anyone can cashback forever if funding fails.
    uint256 public Funding_Setting_cashback_time_end = 0;
 
    // to be taken from application bylaws
    uint256 public Funding_Setting_cashback_before_start_wait_duration = 7 days;
    uint256 public Funding_Setting_cashback_duration = 90 days;
 
    event LifeCycle();
    event DebugRecordRequiredChanges( bytes32 indexed _assetName, uint8 indexed _current, uint8 indexed _required );
    event DebugCallAgain(uint8 indexed _who);
 
    event EventEntityProcessor(bytes32 indexed _assetName, uint8 indexed _current, uint8 indexed _required);
    event EventRecordProcessor(bytes32 indexed _assetName, uint8 indexed _current, uint8 indexed _required);
 
    event DebugAction(bytes32 indexed _name, bool indexed _allowed);
 
 
    event EventFundingReceivedPayment(address indexed _sender, uint8 indexed _payment_method, uint256 indexed _amount );
 
    function runBeforeInitialization() internal requireNotInitialised {
        DirectInput = new FundingInputDirect();
        MilestoneInput = new FundingInputMilestone();
 
        // instantiate token manager, moved from runBeforeApplyingSettings
        TokenManagerEntity = TokenManager( getApplicationAssetAddressByName('TokenManager') );
        FundingManagerEntity = FundingManager( getApplicationAssetAddressByName('FundingManager') );
 
        EventRunBeforeInit(assetName);
    }
 
    function runBeforeApplyingSettings() internal requireInitialised requireSettingsNotApplied {
        AllocateTokens();
        EventRunBeforeApplyingSettings(assetName);
    }
 
 
    function setAssetStates() internal {
        // Asset States
        EntityStates["__IGNORED__"]     = 0;
        EntityStates["NEW"]             = 1;
        EntityStates["WAITING"]         = 2;
        EntityStates["IN_PROGRESS"]     = 3;
        EntityStates["COOLDOWN"]        = 4;
        EntityStates["FUNDING_ENDED"]   = 5;
        EntityStates["FAILED"]          = 6;
        EntityStates["FAILED_FINAL"]    = 7;
        EntityStates["SUCCESSFUL"]      = 8;
        EntityStates["SUCCESSFUL_FINAL"]= 9;
 
        // Funding Stage States
        RecordStates["__IGNORED__"]     = 0;
        RecordStates["NEW"]             = 1;
        RecordStates["IN_PROGRESS"]     = 2;
        RecordStates["FINAL"]           = 3;
    }
 
 
    /*
        When using a funding model that can sell tokens at the market decided value, then a global hard cap is required.
        If global hard cap is defined:
            - funding stage caps are ignored.
            - token distribution is done based on fractions in each funding stage
            - tokens left unsold in funding stages get redistributed to all participants
    */
 
    function addSettings(address _outputAddress, uint256 soft_cap, uint256 hard_cap )
        public
        requireInitialised
        requireSettingsNotApplied
    {
        if(soft_cap > hard_cap) {
            revert();
        }
 
        multiSigOutputAddress = _outputAddress;
        GlobalAmountCapSoft = soft_cap;
        GlobalAmountCapHard = hard_cap;
    }
 
    function addFundingStage(
        bytes32 _name,
        uint256 _time_start,
        uint256 _time_end,
        uint256 _amount_cap_soft,
        uint256 _amount_cap_hard,   // required > 0
        uint8   _methods,
        uint256 _minimum_entry,
        uint256 _start_parity,
        uint8   _price_addition_percentage,
        uint8   _token_share_percentage
    )
        public
        // requireInitialised
        // requireSettingsNotApplied
    {
 
        // make sure end time is later than start time
        if(_time_end <= _time_start) {
            revert();
        }
 
        // if TokenSCADA requires hard cap, then we require it, otherwise we reject it if provided
        /*
        if(TokenManagerEntity.getTokenSCADARequiresHardCap() == true)
        {
            // make sure hard cap exists!
            if(_amount_cap_hard == 0) {
                revert();
            }
 
            // make sure soft cap is not higher than hard cap
            if(_amount_cap_soft > _amount_cap_hard) {
                revert();
            }
 
        } else {
        */
            // make sure record hard cap and soft cap is zero!
            if(_amount_cap_hard != 0) {
                revert();
            }
 
            if(_amount_cap_soft != 0) {
                revert();
            }
        //}
 
 
        // make sure we're not selling more than 100% of token share... as that's not possible
        if(_token_share_percentage > 100) {
            revert();
        }
 
        if(FundingStageNum > 0) {
            FundingStage storage prevRecord = Collection[FundingStageNum];
 
            // if other funding stages exist we need to make sure that the following things don't happen:
 
            // 1 - new stage does not start before the previous one ends
            if( _time_start <= prevRecord.time_end ) {
                revert();
            }
 
            // 2 - make sure previous stage + new stage token percentage does not amount to over 100%
            if( _token_share_percentage + prevRecord.token_share_percentage > 100 ) {
                 revert();
            }
 
        }
 
        FundingStage storage record = Collection[++FundingStageNum];
        record.name             = _name;
        record.time_start       = _time_start;
        record.time_end         = _time_end;
        record.amount_cap_soft  = _amount_cap_soft;
        record.amount_cap_hard  = _amount_cap_hard;
 
        // funding method settings
        record.methods          = _methods;
        record.minimum_entry    = _minimum_entry;
 
        // token settings
        record.start_parity              = _start_parity;
        record.price_addition_percentage = _price_addition_percentage;
        record.token_share_percentage    = _token_share_percentage;
 
        // state new
        record.state = getRecordState("NEW");
        record.index = FundingStageNum;
 
        FundingStageCreated( FundingStageNum, _name );
 
        adjustFundingSettingsBasedOnNewFundingStage();
    }
 
    function adjustFundingSettingsBasedOnNewFundingStage() internal {
 
        uint8 local_TokenSellPercentage;
 
        for(uint8 i = 1; i <= FundingStageNum; i++) {
            FundingStage storage rec = Collection[i];
 
            // cumulate sell percentages
            local_TokenSellPercentage+= rec.token_share_percentage;
 
            // first stage determines when we start receiving funds
            if( i == 1 ) {
                Funding_Setting_funding_time_start = rec.time_start;
            }
            // last stage determines when we stop receiving funds
            if( i == FundingStageNum ) {
                Funding_Setting_funding_time_end = rec.time_end;
            }
        }
 
        TokenSellPercentage = local_TokenSellPercentage;
 
        // set cashback just in case
        // cashback starts 1 day after funding status is failed
        Funding_Setting_cashback_time_start = Funding_Setting_funding_time_end + Funding_Setting_cashback_before_start_wait_duration;
        Funding_Setting_cashback_time_end = Funding_Setting_cashback_time_start + Funding_Setting_cashback_duration;
    }
 
    function getStageTokenSharePercentage(uint8 StageId) public view returns ( uint8 ) {
        return Collection[StageId].token_share_percentage;
    }
 
    function getStagePriceAdd(uint8 StageId) public view returns ( uint8 ) {
        return Collection[StageId].price_addition_percentage;
    }
 
    function getStageAmountRaised(uint8 StageId) public view returns ( uint256 ) {
        return Collection[StageId].amount_raised;
    }
 
    event EventAllocateTokens(address _addr, uint8 _value);
 
    function AllocateTokens() internal {
        EventAllocateTokens(address(FundingManagerEntity), TokenSellPercentage);
        TokenManagerEntity.AllocateInitialTokenBalances(TokenSellPercentage, address(FundingManagerEntity), getApplicationAssetAddressByName('BountyManager'));
    }
 
    function allowedPaymentMethod(uint8 _payment_method) public pure returns (bool) {
        if(
        _payment_method == uint8(FundingMethodIds.DIRECT_ONLY) ||
        _payment_method == uint8(FundingMethodIds.MILESTONE_ONLY)
        ){
            return true;
        } else {
            return false;
        }
    }
 
    function receivePayment(address _sender, uint8 _payment_method)
        payable
        public
        requireInitialised
        onlyInputPaymentMethod
        returns(bool)
    {
        // check that msg.value is higher than 0, don't really want to have to deal with minus in case the network breaks this somehow
        if(allowedPaymentMethod(_payment_method) && canAcceptPayment(msg.value) ) {
 
            Collection[currentFundingStage].amount_raised+= msg.value;
            AmountRaised+= msg.value;
 
            if(_payment_method == uint8(FundingMethodIds.MILESTONE_ONLY)) {
                MilestoneAmountRaised+=msg.value;
            }
 
            EventFundingReceivedPayment(_sender, _payment_method, msg.value);
 
            Eif( FundingManagerEntity.receivePayment.value(msg.value)( _sender, _payment_method, currentFundingStage ) ) {
                return true;
            } else {
                revert();
            }
 
        } else {
            revert();
        }
    }
 
    modifier onlyInputPaymentMethod() {
        require(msg.sender != 0x0 && ( msg.sender == address(DirectInput) || msg.sender == address(MilestoneInput) ));
        _;
    }
 
    function canAcceptPayment(uint256 _amount) public view returns (bool) {
        if( _amount > 0 ) {
            // funding state should be IN_PROGRESS, no state changes should be required
            if( CurrentEntityState == getEntityState("IN_PROGRESS") && hasRequiredStateChanges() == false) {
 
                FundingStage memory record = Collection[currentFundingStage];
                /*
                if(TokenManagerEntity.getTokenSCADARequiresHardCap() == true)
                {
                    // case 1
                    // soft cap is used to determine if funding is successful
                    // hard cap is used to determine if payment can be accepted,
                    //     based on SCADA each funding stage has it's own hard cap!
                    //     so.. globals do nothing here
                } else {
                */
                    // case 2 - SCADA1Market
                    uint256 remaining = GlobalAmountCapHard - AmountRaised;
                    if( _amount >= record.minimum_entry && _amount <= remaining ) {
                        return true;
                    }
                // }
            }
        }
        return false;
    }
 
    /*
    * Update Existing FundingStage
    *
    * @param        uint8 _record_id
    * @param        uint8 _new_state
    * @param        uint8 _duration
    *
    * @access       public
    * @type         method
    * @modifiers    onlyOwner, requireInitialised, updateAllowed
    *
    * @return       void
    */
 
    function updateFundingStage( uint8 _new_state )
        internal
        requireInitialised
        FundingStageUpdateAllowed(_new_state)
        returns (bool)
    {
        FundingStage storage rec = Collection[currentFundingStage];
        rec.state       = _new_state;
        return true;
    }
 
 
    /*
    * Modifier: Validate if record updates are allowed
    *
    * @type         modifier
    *
    * @param        uint8 _record_id
    * @param        uint8 _new_state
    * @param        uint256 _duration
    *
    * @return       bool
    */
 
    modifier FundingStageUpdateAllowed(uint8 _new_state) {
        Erequire( isFundingStageUpdateAllowed( _new_state )  );
        _;
    }
 
    /*
     * Method: Validate if record can be updated to requested state
     *
     * @access       public
     * @type         method
     *
     * @param        uint8 _record_id
     * @param        uint8 _new_state
     *
     * @return       bool
     */
    function isFundingStageUpdateAllowed(uint8 _new_state ) public view returns (bool) {
 
        var (CurrentRecordState, RecordStateRequired, EntityStateRequired) = getRequiredStateChanges();
 
        CurrentRecordState = 0;
        EntityStateRequired = 0;
 
        if(_new_state == uint8(RecordStateRequired)) {
            return true;
        }
        return false;
    }
 
    /*
     * Funding Phase changes
     *
     * Method: Get FundingStage Required State Changes
     *
     * @access       public
     * @type         method
     * @modifiers    onlyOwner
     *
     * @return       uint8 RecordStateRequired
     */
    function getRecordStateRequiredChanges() public view returns (uint8) {
 
        FundingStage memory record = Collection[currentFundingStage];
        uint8 RecordStateRequired = getRecordState("__IGNORED__");
 
        if(record.state == getRecordState("FINAL")) {
            return getRecordState("__IGNORED__");
        }
 
        /*
            If funding stage is not started and timestamp is after start time:
            - we need to change state to IN_PROGRESS so we can start receiving funds
        */
        if( getTimestamp() >= record.time_start ) {
            RecordStateRequired = getRecordState("IN_PROGRESS");
        }
 
        /*
            This is where we're accepting payments unless we can change state to FINAL
 
            1. Check if timestamp is after record time_end
            2. if SCADA requires hard cap
                2.a Check if AmountRaised equals GlobalAmountCapHard
                2.b Check if AmountRaised equals Funding Phase HardCap
            All lead to state change => FINAL
        */
 
        // Time check
        if(getTimestamp() >= record.time_end) {
            // Funding Phase ended passed
            return getRecordState("FINAL");
        }
 
        // if TokenSCADA requires hard cap, then we require it, otherwise we reject it if provided
        /*
        if(TokenManagerEntity.getTokenSCADARequiresHardCap() == true)
        {
            // Has Funding Phase HardCap
            if(record.amount_cap_hard > 0) {
                // amount raised is Funding Phase HardCap
                if(AmountRaised == record.amount_cap_hard) {
                    return getRecordState("FINAL");
                }
            }
        } else {
        */
            // Global Hard Cap Check
            if(AmountRaised == GlobalAmountCapHard) {
                // hard cap reached
                return getRecordState("FINAL");
            }
        // }
 
        /*
            - else we need to wait for funding stage to start..
        */
 
        if( record.state == RecordStateRequired ) {
            RecordStateRequired = getRecordState("__IGNORED__");
        }
 
        return RecordStateRequired;
    }
 
    function doStateChanges() public {
        var (CurrentRecordState, RecordStateRequired, EntityStateRequired) = getRequiredStateChanges();
        bool callAgain = false;
 
        DebugRecordRequiredChanges( assetName, CurrentRecordState, RecordStateRequired );
        DebugEntityRequiredChanges( assetName, CurrentEntityState, EntityStateRequired );
 
        if( RecordStateRequired != getRecordState("__IGNORED__") ) {
            // process record changes.
            RecordProcessor(CurrentRecordState, RecordStateRequired);
            DebugCallAgain(2);
            callAgain = true;
        }
 
        if(EntityStateRequired != getEntityState("__IGNORED__") ) {
            // process entity changes.
            // if(CurrentEntityState != EntityStateRequired) {
            EntityProcessor(EntityStateRequired);
            DebugCallAgain(1);
            callAgain = true;
            //}
        }
 
        /*
        if(recursive && callAgain) {
            if(hasRequiredStateChanges()) {
                doStateChanges(recursive);
            }
        }
        */
    }
 
    function hasRequiredStateChanges() public view returns (bool) {
        bool hasChanges = false;
 
        var (CurrentRecordState, RecordStateRequired, EntityStateRequired) = getRequiredStateChanges();
        CurrentRecordState = 0;
 
        if( RecordStateRequired != getRecordState("__IGNORED__") ) {
            hasChanges = true;
        }
        if(EntityStateRequired != getEntityState("__IGNORED__") ) {
            hasChanges = true;
        }
        return hasChanges;
    }
 
    // view methods decide if changes are to be made
    // in case of tasks, we do them in the Processors.
 
    function RecordProcessor(uint8 CurrentRecordState, uint8 RecordStateRequired) internal {
        EventRecordProcessor( assetName, CurrentRecordState, RecordStateRequired );
        updateFundingStage( RecordStateRequired );
        if( RecordStateRequired == getRecordState("FINAL") ) {
            if(currentFundingStage < FundingStageNum) {
                // jump to next stage
                currentFundingStage++;
            }
        }
    }
 
    function EntityProcessor(uint8 EntityStateRequired) internal {
        EventEntityProcessor( assetName, CurrentEntityState, EntityStateRequired );
 
        // Do State Specific Updates
        // Update our Entity State
        CurrentEntityState = EntityStateRequired;
 
        if ( EntityStateRequired == getEntityState("FUNDING_ENDED") ) {
            /*
                STATE: FUNDING_ENDED
                @Processor hook
                Action: Check if funding is successful or not, and move state to "FAILED" or "SUCCESSFUL"
            */
 
            // Global Hard Cap Check
            if(AmountRaised >= GlobalAmountCapSoft) {
                // hard cap reached
                CurrentEntityState = getEntityState("SUCCESSFUL");
            } else {
                CurrentEntityState = getEntityState("FAILED");
            }
        }
 
 
    }
 
    /*
     * Method: Get Record and Entity State Changes
     *
     * @access       public
     * @type         method
     * @modifiers    onlyOwner
     *
     * @return       ( uint8 CurrentRecordState, uint8 RecordStateRequired, uint8 EntityStateRequired)
     */
    function getRequiredStateChanges() public view returns (uint8, uint8, uint8) {
 
        // get FundingStage current state
        FundingStage memory record = Collection[currentFundingStage];
 
        uint8 CurrentRecordState = record.state;
        uint8 RecordStateRequired = getRecordStateRequiredChanges();
        uint8 EntityStateRequired = getEntityState("__IGNORED__");
 
 
        // Funding Record State Overrides
        // if(CurrentRecordState != RecordStateRequired) {
        if(RecordStateRequired != getRecordState("__IGNORED__"))
        {
            // direct state overrides by funding stage
            if(RecordStateRequired == getRecordState("IN_PROGRESS") ) {
                // both funding stage and entity states need to move to IN_PROGRESS
                EntityStateRequired = getEntityState("IN_PROGRESS");
 
            } else Eif (RecordStateRequired == getRecordState("FINAL")) {
                // funding stage moves to FINAL
 
                if (currentFundingStage == FundingStageNum) {
                    // if current funding is last
                    EntityStateRequired = getEntityState("FUNDING_ENDED");
                }
                else {
                    // start cooldown between funding stages
                    EntityStateRequired = getEntityState("COOLDOWN");
                }
            }
 
        } else {
 
            // Records do not require any updates.
            // Do Entity Checks
 
            if( CurrentEntityState == getEntityState("NEW") ) {
                /*
                    STATE: NEW
                    Processor Action: Allocate Tokens to Funding / Owners then Update to WAITING
                */
                EntityStateRequired = getEntityState("WAITING");
            } else  Iif ( CurrentEntityState == getEntityState("FUNDING_ENDED") ) {
                /*
                    STATE: FUNDING_ENDED
                    Processor Action: Check if funding is successful or not, and move state to "SUCCESSFUL" or "FAILED"
                */
            } else if ( CurrentEntityState == getEntityState("SUCCESSFUL") ) {
                /*
                    STATE: SUCCESSFUL
                    Processor Action: none
 
                    External Action:
                    FundingManager - Run Internal Processor ( deliver tokens, deliver direct funding eth )
                */
 
                // check funding manager has processed the FUNDING_SUCCESSFUL Task, if true => FUNDING_SUCCESSFUL_DONE
                if(FundingManagerEntity.taskByHash( FundingManagerEntity.getHash("FUNDING_SUCCESSFUL_START", "") ) == true) {
                    EntityStateRequired = getEntityState("SUCCESSFUL_FINAL");
                }
                /*
                if( FundingManagerEntity.CurrentEntityState() == FundingManagerEntity.getEntityState("FUNDING_SUCCESSFUL_DONE") ) {
                    EntityStateRequired = getEntityState("SUCCESSFUL_FINAL");
                }
                */
 
            } else if ( CurrentEntityState == getEntityState("FAILED") ) {
                /*
                    STATE: FAILED
                    Processor Action: none
 
                    External Action:
                    FundingManager - Run Internal Processor (release tokens to owner) ( Cashback is available )
                */
 
                // check funding manager state, if FUNDING_NOT_PROCESSED -> getEntityState("__IGNORED__")
                // if FUNDING_FAILED_DONE
 
                if(FundingManagerEntity.taskByHash( FundingManagerEntity.getHash("FUNDING_FAILED_START", "") ) == true) {
                    EntityStateRequired = getEntityState("FAILED_FINAL");
                }
            } else if ( CurrentEntityState == getEntityState("SUCCESSFUL_FINAL") ) {
                /*
                    STATE: SUCCESSFUL_FINAL
                    Processor Action: none
 
                    External Action:
                    Application: Run Internal Processor ( Change State to IN_DEVELOPMENT )
                */
            } else if ( CurrentEntityState == getEntityState("FAILED_FINAL") ) {
                /*
                    STATE: FINAL_FAILED
                    Processor Action: none
 
                    External Action:
                    Application: Run Internal Processor ( Change State to FUNDING_FAILED )
                */
            }
        }
 
 
        return (CurrentRecordState, RecordStateRequired, EntityStateRequired);
    }
 
}