I would like to call token.transferOwnership(msg.sender) after finishing timed crowdsale. So, I inherited FinalizableCrowdsale contract, then override finalization() function to call transferOwnership on the crowdsale contract.

Actually, I can test to call crowdsale.finalize() on Truffle console and it is working well, but I don't know how to call the function automatically after finishing crowdsale. When crowdsale started, the token ownership has been transferred to crowdsale contract. Um, Do I have to modify hasClosed() function in TimedCrowdsale? or is there any way to call the fucntion...?

I tried to call it on Etherscan after verifying the code, but I couldn't do cos the crowdsale contract has the ownership...

Crowdsale Migration.js

const DappToken = artifacts.require("./DappToken.sol");
const DappTokenCrowdsale = artifacts.require('DappTokenCrowdsale');

const ether = (n) => new web3.BigNumber(web3.toWei(n, 'ether'));

const duration = {
    seconds: function (val) { return val; },
    minutes: function (val) { return val * this.seconds(60); },
    hours: function (val) { return val * this.minutes(60); },
    days: function (val) { return val * this.hours(24); },
    weeks: function (val) { return val * this.days(7); },
    years: function (val) { return val * this.days(365); },

// module.exports = function(deployer, network, accounts) {
module.exports = function(deployer, network, accounts) {
    const latestTime = Math.floor(Date.now() / 1000);
    const _token = DappToken.address;
    const _rate = 1000;

    const _wallet = accounts[3]; // Collecting Wallet
    const _openingTime = latestTime + duration.minutes(1);
    const _closingTime = _openingTime + duration.minutes(2);
    const _cap = ether(100);

    console.log("Open: " + new Date(_openingTime*1000) + " Close: " + new Date(_closingTime*1000));

    return deployer.deploy(DappTokenCrowdsale, _rate, _wallet, _token, _cap, _openingTime, _closingTime)
        .then(() => {
            return DappToken.deployed().then((token) => {
                return token.transferOwnership(DappTokenCrowdsale.address)

Crowdsale Contract.sol

pragma solidity 0.4.24;

import "./DappToken.sol";

import "../node_modules/openzeppelin-solidity/contracts/crowdsale/Crowdsale.sol";
import "../node_modules/openzeppelin-solidity/contracts/crowdsale/emission/MintedCrowdsale.sol";
import "../node_modules/openzeppelin-solidity/contracts/crowdsale/validation/CappedCrowdsale.sol";
import "../node_modules/openzeppelin-solidity/contracts/crowdsale/validation/TimedCrowdsale.sol";
import "../node_modules/openzeppelin-solidity/contracts/crowdsale/distribution/FinalizableCrowdsale.sol";
// import "../node_modules/openzeppelin-solidity/contracts/crowdsale/validation/WhitelistedCrowdsale.sol";

contract DappTokenCrowdsale is Crowdsale, MintedCrowdsale, CappedCrowdsale, TimedCrowdsale, FinalizableCrowdsale {
    MintableToken public token;
    address public tokenAddress;
    address public crowdsaleOwner;
    address public founderWallet;

    bool public isFinalized = false;

      uint256 _rate,
      address _wallet,
      MintableToken _token,
      uint256 _cap,
      uint256 _openingTime,
      uint256 _closingTime
    Crowdsale(_rate, _wallet, _token)
    TimedCrowdsale(_openingTime, _closingTime)
      founderWallet = _wallet;
      token = _token;          

    function finalization() internal {

automatically after finishing crowdsale

it is not possible to execute smart contract function automatically literally

in order to call finalize function after some period of time you can:

| improve this answer | |

Add the following function:

function finalizeIfNeeded () internal {
    if (!finalized && block.timestamp >= crowdsaleEndTime) {
        finalization ();
        finalized = true;

and call it at the beginning of all your non-constant public functions whose behavior depend on whether crowd sale is finalized or not.

If you have some constant (e.g. view) functions whose behavior also depends on finalize status, add the following function:

function isFinalized () view returns (bool) {
    return finalized || block.timestamp >= crowdsaleEndTime;

and use it in your constant function to check whether crowd sale is finalized.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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