Truffle

(Truffle) 眾籌結束後如何呼叫 finalize() 函式

  • January 3, 2021

我想token.transferOwnership(msg.sender)在完成定時眾籌後打電話。所以,我繼承了 FinalizableCrowdsale 合約,然後重寫finalization()函式來呼叫transferOwnership眾包合約。

實際上,我可以測試crowdsale.finalize()在 Truffle 控制台上呼叫它,它執行良好,但我不知道如何在完成 crowdsale 後自動呼叫該函式。當眾籌開始時,代幣所有權已轉移到眾籌合約。嗯,我必須修改hasClosed()函式TimedCrowdsale嗎?或者有什麼方法可以呼叫函式…?

驗證程式碼後,我嘗試在 Etherscan 上呼叫它,但我做不到,因為眾籌合約擁有所有權……

眾籌 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)
           });
       });
};

眾售合約.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;

   constructor(
     uint256 _rate,
     address _wallet,
     MintableToken _token,
     uint256 _cap,
     uint256 _openingTime,
     uint256 _closingTime
   )
   Crowdsale(_rate, _wallet, _token)
   CappedCrowdsale(_cap)
   TimedCrowdsale(_openingTime, _closingTime)
   FinalizableCrowdsale()
   MintedCrowdsale()
   public
   {
     founderWallet = _wallet;
     token = _token;          
   }

   function finalization() internal {
     token.transferOwnership(msg.sender);
     super.finalization();
   }
}

完成眾籌後自動

無法從字面上自動執行智能合約功能

為了finalize在一段時間後呼叫函式,您可以:

添加以下功能:

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

並在所有非常量公共函式的開頭呼叫它,這些函式的行為取決於眾籌是否完成。

如果您有一些view行為也取決於最終狀態的常量(例如 )函式,請添加以下函式:

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

並在你的常量函式中使用它來檢查人群銷售是否完成。

引用自:https://ethereum.stackexchange.com/questions/63375