Extend Time in a TimedCrowdsale

Thank you for this information: How to simulate a date in the future with truffle tests?. I just went through the code and made some test
So I took the code from https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/test/crowdsale/TimedCrowdsale.test.js
and went trough it, but I got some questions about test that did not worked out during execution.

For example:
I got an error: TypeError: this.crowdsale.extendTime is not a function

Then i looked in the test.js which called this method here

context('before crowdsale start', function () {
        beforeEach(async function () {
          expect(await this.crowdsale.isOpen()).to.equal(false);
          await expectRevert(this.crowdsale.send(value), 'TimedCrowdsale: not open');
        });

        it('it extends end time', async function () {
          const newClosingTime = this.closingTime.add(time.duration.days(1));
          const { logs } = await this.crowdsale.extendTime(newClosingTime);

Ok fine. so This method does not seemed to be defined. So I moved to the TimedCrowdsale.sol

And indeed, I find an event and I find _extendTime. But I don’t find extendTime

So Is there a mistake in thinking or do I missed something out. Ok, I can define the method myself. But shouldn’t the method be there?

Thank you very much vor any advice

1 Like

Hi @blexx,

_extendTime is an internal function of TimedCrowdsale which can be called from your crowdsale contract.

The TimedCrowdsale.test.js uses a mock implementation, TimedCrowdsaleImpl.sol:

The mock implementation of a TimedCrowdsale, includes a public function extendTime. Please note this implementation does not have any access control, so any account could call it.

If you want the add the extend time functionality to your crowdsale contract you should create a function in your crowdsale that calls the internal function _extendTime. Though you also need to add some form of access control (e.g. Role-Based Access Control) so that only an account with the role can extend the time.

For example you could do something like this (you would need to create the access control and add a modifier such as onlyTimeExtender)

    function extendTime(uint256 closingTime) public onlyTimeExtender {
        _extendTime(closingTime);
    }

As always, recommend appropriate testing and auditing for your smart contracts.

Feel free to ask all the questions that you need.

1 Like

abcoathup,

thank you very much for that detailed answer. So this pointed me to the solution, which is so easy. So I just overlooked something:

The mock:

implements the method:

function extendTime(uint256 closingTime) public {
_extendTime(closingTime);
}

And here it is! Everythings fine. So as I was wondering. In case I like to extend the time, then I need to implement like in the mock.

So that’s it and the solution I was wondering. I just looked on the wrong contract (stupid). BTW. having mocks is really helpful

1 Like

Hi @blexx,

It’s an easy one to miss. I had to go looking for the contract.

Also don’t forget that you need to add access control as otherwise any account could extend the time for the crowd sale. The mock doesn’t have any access control.

It is worth testing that an account without the role can’t extend the time.

1 Like

abcoathup,

your absulutly right. Access control is the next to implement and test.

Thank you for your advice

1 Like