Solidity

更改/覆蓋主契約中的方法

  • October 27, 2018

我正在使用 openzeppelin 創建一個作為 CappedCrowdsale 和 TimedCrowdsale 的 ICO,如以下連結中提到的範例中所述。

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

FinalizableCrowdsale 中的 finalized() 方法是合約中的可訪問方法,如 MEW 所示。該方法目前僅通過僅檢查關閉時間來檢查合約是否已關閉。即使達到(或)時間結束,我也想更改此方法以完成它。

問題:

  1. 如何從 SampleCrowdsale 更改 FinalizableCrowdsale 中的 finalize() 方法。這包括 capReached() 檢查?
  2. 如何更改 FinalizableCrowdsale 的 finalize() 方法中的變數 _finalized。請注意,我需要從 SampleCrowdsale 契約中執行此操作而不更改 open-zeppelin 庫嗎?

請協助

在此處輸入圖像描述

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"); 
} 



}

以下契約來自我不想更改的 open-zeppelin 庫

可退款眾籌

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

}

可定案的眾售

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 {
}
}

為了覆蓋父方法,您需要在具有相同簽名的子契約中定義方法。

函式可以被另一個具有相同名稱和相同數量/類型的輸入的函式覆蓋。如果覆蓋函式具有不同類型的輸出參數,則會導致錯誤。本地和基於消息的函式呼叫都考慮了這些覆蓋。

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

在你的情況下finalize()看起來不錯。

_finalized是私有的,無法編寫它,但可以finalize()像這樣覆蓋:

function finalize() public {
   require(capReached());
   super.finalize();       // call parent's finalize function
}

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