Solidity
令牌傳輸失敗並恢復
我在 Github https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/examples/SampleCrowdsale.sol中有您最新版本的契約和幾乎相同的範例- 我使用 Ganache 在 MAC 作業系統上進行測試. 只有當我評論該行時,契約才有效
function _deliverTokens(address _beneficiary, uint256 _tokenAmount) internal { //token.transfer(_beneficiary, _tokenAmount); }
如果未對此進行評論,我會遇到執行時錯誤並恢復。
我的簡單程式碼如下:
代幣
pragma solidity ^0.4.18; import 'zeppelin-solidity/contracts/token/ERC20/MintableToken.sol'; /** * The LeonardianToken contract does this and that... */ contract LeonardianToken is MintableToken { uint256 public constant INITIAL_SUPPLY = 10000; string public constant name = "Leonardian"; // solium-disable-line uppercase string public constant symbol = "LEON"; // solium-disable-line uppercase uint8 public constant decimals = 18; // solium-disable-line uppercase }
契約:
pragma solidity ^0.4.18; import 'zeppelin-solidity/contracts/crowdsale/validation/TimedCrowdsale.sol'; import "zeppelin-solidity/contracts/crowdsale/Crowdsale.sol"; import './LeonardianToken.sol'; contract LeonardianCrowdsale is Crowdsale { function LeonardianCrowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, address _wallet, MintableToken _token) public Crowdsale(_rate, _wallet, _token) // TimedCrowdsale(_startTime, _endTime) { } }
遷移(沒有任何錯誤):
var LeonardianCrowdsale = artifacts.require("./LeonardianCrowdsale.sol"); var LeonardianToken = artifacts.require("./LeonardianToken.sol"); module.exports = function(deployer) { deployer.deploy(LeonardianToken).then(function () { const startTime = Math.round((new Date(Date.now() - 86400000).getTime())/1000); // Yesterday const endTime = Math.round((new Date().getTime() + (86400000 * 20))/1000); // Today + 20 days var exchangeRate = 1; // 1 LEON = 0.0025 ETH or 1 ETH = 400 LEON deployer.deploy(LeonardianCrowdsale, startTime, endTime, exchangeRate, "0x627306090abaB3A6e1400e9345bC60c78a8BEf57", // Replace this wallet address with the last one (10th account) from Ganache UI. This will be treated as the beneficiary address. LeonardianToken.address ); }); };
你能幫我做錯什麼嗎?
解決方案
如前所述 ERC20 _token = new LeonardianToken(); 必須在 Crowdsale 契約中實例化,並且在此之後進行所有工作的重要事情是稍微更改您的遷移
return deployer .then(() => { return deployer.deploy(Token); }) .then(() => { return deployer.deploy( Crowdsale, startTime, endTime, exchangeRate, "wallet", Token.address ); }) .then(() => { var token = Token.at(Token.address); token.transferOwnership(Crowdsale.address); });
令牌必須與 Crowdsale 分開部署並在 Crowdsale 中實例化
pragma solidity ^0.4.18; import 'zeppelin-solidity/contracts/crowdsale/validation/TimedCrowdsale.sol'; import "zeppelin-solidity/contracts/crowdsale/Crowdsale.sol"; import './LeonardianToken.sol'; contract LeonardianCrowdsale is Crowdsale { ERC20 _token = new LeonardianToken(); function LeonardianCrowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, address _wallet, MintableToken _token) public Crowdsale(_rate, _wallet, _token) // TimedCrowdsale(_startTime, _endTime) { } }