Truffle-Migration

松露測試失敗

  • February 13, 2019

我正在學習 Solidity 合約和乙太坊 dapp。我已經創建了一個智能合約,用於在使用 truffle 開發環境時創建 ERC20 代幣。

pragma solidity ^0.5.0;

contract LearnTokens {
   string  public name = "LearnToken";
   string  public symbol = "LT";
   string  public standard = "LearnToken Token v1.0";
   uint256 public totalSupply;

   event Transfer(
       address indexed _from,
       address indexed _to,
       uint256 _value
   );

   mapping(address => uint256) public balanceOf;

   function LearnToken (uint256 _initialSupply) public payable{
       balanceOf[msg.sender] = _initialSupply;
       totalSupply = _initialSupply;
   }

   function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
       require(_value <= balanceOf[_from]);


       balanceOf[_from] -= _value;
       balanceOf[_to] += _value;



       emit Transfer(_from, _to, _value);

       return true;
   } }

這是我的遷移文件

var LearnToken = artifacts.require("./LearnToken.sol");

module.exports = function(deployer) {
   deployer.deploy(LearnToken,100);
};

當我嘗試執行松露測試時,它會引發以下錯誤。

1) Contract: LearnToken
      sets the total supply upon deployment:

     sets the total supply to 100
     + expected - actual

     -0
     +100

我在這裡假設當我執行truffle migrate時,它會將初始供應量設置為 100,正如我在教程中看到的那樣。我做對了還是有其他方法?TIA

在執行truffle migrate您的契約時,不會將任何內容設置為初始供應。它沒有建構子。經過仔細檢查,我發現你有一個function

function LearnToken (uint256 _initialSupply) public payable{
   balanceOf[msg.sender] = _initialSupply;
   totalSupply = _initialSupply;
}

你想把它變成你的constructor嗎?函式名稱與您的合約名稱不匹配。即使它匹配,您在使用solidity 版本時也會收到另一個錯誤^5.0.0。你需要寫你constructor的如下

constructor (uint256 _initialSupply) public payable{
   balanceOf[msg.sender] = _initialSupply;
   totalSupply = _initialSupply;
}

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