Solidity

無法更新繼承合約的狀態變數

  • October 24, 2018

我正在嘗試編寫 OpenZeppelin 的 CappedCrowdsale 的擴展,以允許以美元為單位的硬上限,因為我認為這比使用 wei 更易於閱讀。

但是,我在更新一些繼承的狀態變數(Crowdsale’srate和 CappedCrowdsale’s cap)時遇到問題。我嘗試在updateEthPrice函式中執行此操作,我知道該函式被呼叫是因為ethPriceInDollars變數被正確更新,但是cap並且rate總是返回 1 …(這是它們被初始化的內容)

起初我認為可能繼承的建構子在基本建構子之後觸發(並覆蓋我在基本建構子中設置的任何內容),但即使我updateEthPrice在部署契約後呼叫,我仍然沒有看到任何更改caprate

整天都在敲我的頭,感謝任何幫助!

pragma solidity ^0.4.19;

import "zeppelin-solidity/contracts/crowdsale/CappedCrowdsale.sol";


/**
* @title DollarCappedCrowdsale
* @author Gianni Settino
* @dev Extension of CappedCrowdsale with a cap in USD instead of wei.
* This means that Crowdsale's rate and CappedCrowdsale's cap now depend
* on ETH's USD price, which can be changed up until the start of the token sale.
*/
contract DollarCappedCrowdsale is CappedCrowdsale(1), Ownable {
 uint256 public ethPriceInDollars;
 uint256 public hardCapInDollars;
 uint256 public tokenPriceInCents;

 // @dev We temporarily init Crowdsale's rate and CappedCrowdsale's cap to 1.
 // Reason: their "real" values are subsequently calculated in updateEthPrice()
 function DollarCappedCrowdsale(
   uint256 _startTime,
   uint256 _endTime,
   address _wallet,
   uint256 _ethPriceInDollars,
   uint256 _hardCapInDollars,
   uint256 _tokenPriceInCents
   )
   public
   Crowdsale(_startTime, _endTime, 1, _wallet)
 {
   require(_hardCapInDollars > 0);
   require(_tokenPriceInCents > 0);
   hardCapInDollars = _hardCapInDollars;
   tokenPriceInCents = _tokenPriceInCents;
   updateEthPrice(_ethPriceInDollars);
 }

 function updateEthPrice(uint256 _ethPriceInDollars) public onlyOwner {
   require(now < startTime);
   require(_ethPriceInDollars > 0);
   ethPriceInDollars = _ethPriceInDollars;
   cap = (hardCapInDollars / ethPriceInDollars).mul(1 ether);
   rate = (ethPriceInDollars.mul(100)) / tokenPriceInCents;
 }
}

我試過你的contractin remix,它按預期工作。與最新版本的 OpenZeppelin 合約一起使用的修改版本。

pragma solidity ^0.4.19;

import "github.com/OpenZeppelin/zeppelin-solidity/contracts/crowdsale/CappedCrowdsale.sol";

contract DollarCappedCrowdsale is CappedCrowdsale(1), Ownable {
 uint256 public ethPriceInDollars;
 uint256 public hardCapInDollars;
 uint256 public tokenPriceInCents;

 function DollarCappedCrowdsale(
   uint256 _startTime,
   uint256 _endTime,
   address _wallet,
   uint256 _ethPriceInDollars,
   uint256 _hardCapInDollars,
   uint256 _tokenPriceInCents
   )
   public
   Crowdsale(_startTime, _endTime, 1, _wallet, MintableToken(1))
 {
   require(_hardCapInDollars > 0);
   require(_tokenPriceInCents > 0);
   hardCapInDollars = _hardCapInDollars;
   tokenPriceInCents = _tokenPriceInCents;
   updateEthPrice(_ethPriceInDollars);
 }

 function updateEthPrice(uint256 _ethPriceInDollars) public onlyOwner {
   require(now < startTime);
   require(_ethPriceInDollars > 0);
   ethPriceInDollars = _ethPriceInDollars;
   cap = (hardCapInDollars / ethPriceInDollars).mul(1 ether);
   rate = (ethPriceInDollars.mul(100)) / tokenPriceInCents;
 }
}

創建參數

1518412209,1518413209,"0x01","870","40000000","10"

結果參數:

  • 費率:8700
  • tokenPriceInCents: 10
  • hardCapInDollars: 40000000
  • ethPriceInDollars: 870
  • 上限:45977000000000000000000

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