Which lines do I remove and what do I replace them with to change this from a timed crowdsale contract into one I can end manually?

pragma solidity ^0.4.18;

interface token {
    function transfer(address receiver, uint amount) external;

contract Crowdsale {
    address public beneficiary;
    uint public fundingGoal;
    uint public amountRaised;
    uint public deadline;
    uint public price;
    token public tokenReward;
    mapping(address => uint256) public balanceOf;
    bool fundingGoalReached = false;
    bool crowdsaleClosed = false;

    event GoalReached(address recipient, uint totalAmountRaised);
    event FundTransfer(address backer, uint amount, bool isContribution);

     * Constructor function
     * Setup the owner
    function Crowdsale(
        address ifSuccessfulSendTo,
        uint fundingGoalInEthers,
        uint durationInMinutes,
        uint etherCostOfEachToken,
        address addressOfTokenUsedAsReward
    ) public {
        beneficiary = ifSuccessfulSendTo;
        fundingGoal = fundingGoalInEthers * 1 ether;
        deadline = now + durationInMinutes * 1 minutes;
        price = etherCostOfEachToken * 1 ether;
        tokenReward = token(addressOfTokenUsedAsReward);

     * Fallback function
     * The function without name is the default function that is called whenever anyone sends funds to a contract
    function () payable public {
        uint amount = msg.value;
        balanceOf[msg.sender] += amount;
        amountRaised += amount;
        tokenReward.transfer(msg.sender, amount / price);
       emit FundTransfer(msg.sender, amount, true);

    modifier afterDeadline() { if (now >= deadline) _; }

     * Check if goal was reached
     * Checks if the goal or time limit has been reached and ends the campaign
    function checkGoalReached() public afterDeadline {
        if (amountRaised >= fundingGoal){
            fundingGoalReached = true;
            emit GoalReached(beneficiary, amountRaised);
        crowdsaleClosed = true;

     * Withdraw the funds
     * Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached,
     * sends the entire amount to the beneficiary. If goal was not reached, each contributor can withdraw
     * the amount they contributed.
    function safeWithdrawal() public afterDeadline {
        if (!fundingGoalReached) {
            uint amount = balanceOf[msg.sender];
            balanceOf[msg.sender] = 0;
            if (amount > 0) {
                if (msg.sender.send(amount)) {
                   emit FundTransfer(msg.sender, amount, false);
                } else {
                    balanceOf[msg.sender] = amount;

        if (fundingGoalReached && beneficiary == msg.sender) {
            if (beneficiary.send(amountRaised)) {
               emit FundTransfer(beneficiary, amountRaised, false);
            } else {
                //If we fail to send the funds to beneficiary, unlock funders balance
                fundingGoalReached = false;

closed as too broad by Ismael, Aquila, Richard Horrocks, Achala Dissanayake, Nicolas Massart Oct 19 '18 at 22:03

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • What have you tried? What about the contract do you understand / confuses you? – Shawn Tabrizi Oct 17 '18 at 20:21
  • I tried removing everything that was related to funding goals, time starts etc. but then it didn't work so I am obviously removing too much or not enough and that is where I get lost. – C North Oct 17 '18 at 20:42
  • Then what did you add to enable "manual finalization"? Have you looked at open source crowd-sale contracts like openzeppelin? – Shawn Tabrizi Oct 17 '18 at 20:46
  • I didn't because after removing the stuff mentioned above it wouldn't work so I didn't even attempt it. I have looked at openzeppelin contracts but again haven't found one that has a manual finalization feature, at least not that I could see. Thanks for your help and if you have any other suggestions I would appreciate them. – C North Oct 17 '18 at 20:52
  • I think your question is not very specific. Your smart contract is not trivial and you request to remove a feature is complicated. A good answer will imply an almost total rewrite of your contract. – Ismael Oct 19 '18 at 4:17

You can simply remove every instance of the afterDeadline modifier and the contract will no longer rely on any timing function.

I advise you to learn more about the contract and perhaps rewrite it to follow best practices. Take a look at the OpenZeppelin crowdsale contracts.

Not the answer you're looking for? Browse other questions tagged or ask your own question.