2

I am using openzeppelin to create a ICO as a CappedCrowdsale and TimedCrowdsale as mentioned in the example mentioned in the below link.

Reference : https://github.com/buddies2705/Solidity-Tutorial/blob/master/contracts/ExampleTokenCrowdsale.sol

The finalized() method in FinalizableCrowdsale is the accessible method in the contract as shown in MEW. This method is as of now checking only whether the contract is closed or not by checking only the closing time. I want to alter this method to finalize it even if the cap is reached (or) time is over.

Question:

  1. How to alter the finalize() method in FinalizableCrowdsale from SampleCrowdsale. This is to include the capReached() check ?

  2. How to change the variable _finalized which is in finalize() method of FinalizableCrowdsale. Please note that I need to do this from SampleCrowdsale contract without altering the open-zeppelin library ?

Please assist

enter image description here

contract SampleCrowdsale is CappedCrowdsale, RefundableCrowdsale, MintedCrowdsale {


constructor(
uint256 openingTime,
uint256 closingTime,
uint256 rate,
address wallet,
uint256 cap,
ERC20Mintable token,
uint256 goal
)
public
Crowdsale(rate, wallet, token)
CappedCrowdsale(cap)
TimedCrowdsale(openingTime, closingTime)
RefundableCrowdsale(goal)
{
//As goal needs to be met for a successful crowdsale
//the value needs to less or equal than a cap which is limit for accepted funds
require(goal <= cap,"Goal <= Cap is an requirement"); 
} 



}

Below contracts are from open-zeppelin library which I do not want to alter

RefundableCrowdsale

contract RefundableCrowdsale is FinalizableCrowdsale {
using SafeMath for uint256;

// minimum amount of funds to be raised in weis
uint256 private _goal;

// refund escrow used to hold funds while crowdsale is running
RefundEscrow private _escrow;

/**
* @dev Constructor, creates RefundEscrow.
* @param goal Funding goal
*/
constructor(uint256 goal) internal {
require(goal > 0);
_escrow = new RefundEscrow(wallet());
_goal = goal;
}

/**
* @return minimum amount of funds to be raised in wei.
*/
function goal() public view returns(uint256) {
return _goal;
}

/**
* @dev Investors can claim refunds here if crowdsale is unsuccessful
* @param beneficiary Whose refund will be claimed.
*/
function claimRefund(address beneficiary) public {
require(finalized());
require(!goalReached());

_escrow.withdraw(beneficiary);
}

/**
* @dev Checks whether funding goal was reached.
* @return Whether funding goal was reached
*/
function goalReached() public view returns (bool) {
return weiRaised() >= _goal;
}

/**
* @dev escrow finalization task, called when finalize() is called
*/
function _finalization() internal {
if (goalReached()) {
_escrow.close();
_escrow.beneficiaryWithdraw();
} else {
_escrow.enableRefunds();
}

super._finalization();
}

/**
* @dev Overrides Crowdsale fund forwarding, sending funds to escrow.
*/
function _forwardFunds() internal {
_escrow.deposit.value(msg.value)(msg.sender);
}

}

FinalizableCrowdsale

contract FinalizableCrowdsale is TimedCrowdsale {
using SafeMath for uint256;

bool private _finalized;

event CrowdsaleFinalized();

constructor() internal {
_finalized = false;
}

/**
* @return true if the crowdsale is finalized, false otherwise.
*/
function finalized() public view returns (bool) {
return _finalized;
}

/**
* @dev Must be called after crowdsale ends, to do some extra finalization
* work. Calls the contract's finalization function.
*/
function finalize() public {
require(!_finalized);
require(hasClosed());

_finalized = true;

_finalization();
emit CrowdsaleFinalized();
}

/**
* @dev Can be overridden to add finalization logic. The overriding function
* should call super._finalization() to ensure the chain of finalization is
* executed entirely.
*/
function _finalization() internal {
}
}

1 Answer 1

3

In order to override parent method you need to define method in child contract with same signature.

Functions can be overridden by another function with the same name and the same number/types of inputs. If the overriding function has different types of output parameters, that causes an error. Both local and message-based function calls take these overrides into account.

https://solidity.readthedocs.io/en/v0.4.21/contracts.html#inheritance

In your case finalize() looks ok.

_finalized is private, it is not possible to write it, but it is possible to override finalize() like this:

function finalize() public {
    require(capReached());
    super.finalize();       // call parent's finalize function
}
7
  • This way It will check capReached() && hasClosed(). But in my case I need to finalize the contract if Time is over (or) when cap is reached. i.e.,, capReached() || hasClosed(). Please advice how this can be achieved
    – iappmaker
    Oct 27, 2018 at 14:49
  • yeah, got it, in this case you need write alternative finalization with own variables and checks.
    – Aquila
    Oct 27, 2018 at 14:54
  • Snce the RefundableCrowdsale contract from open-zeppelin uses FinalizableCrowdsale the finalize() is shown in MEW while Accessing contract like I have shown in the screenshot. By some means I need to have a function which will update the variable _finalized to true when apReached() || hasClosed() happens. Could you please assist ?
    – iappmaker
    Oct 27, 2018 at 15:03
  • There couple of options, mark _finalized as internal in FinalizableCrowdsale and you can override finalize() with own logic in child, or rewrite entire FinalizableCrowdsale
    – Aquila
    Oct 27, 2018 at 15:09
  • By any means I need to alter the open-zeppelin library right ?. I thought I can do something without touching it
    – iappmaker
    Oct 27, 2018 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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