美文网首页
2018-06-25 solidity版本导致的错误

2018-06-25 solidity版本导致的错误

作者: oracle3 | 来源:发表于2018-06-25 17:07 被阅读0次

    问题:

    今天调试一个以太坊的合约:


    pragma solidity ^0.4.18;

    contract MyToken {

        address creator;   

        uint256 public balanceOf = 0;

      // Initializes contract with initial supply tokens to the creator of the

      //contract

      constructor () public {

          // Give the creator all initial tokens

          creator = msg.sender;

      }

      // Send coins

      function transfer(address _to) public payable {

            require(msg.sender == creator);

            // Check if the sender has enough

            require(balanceOf >= msg.value);

            // Subtract from the sender

            balanceOf -= msg.value;   

            // Add the same to the recipient             

            _to.transfer(msg.value);

        }

        // fallback function

        function pay() public payable {

            balanceOf += msg.value; 

        }

    }


    解决过程

    在http://remix.ethereum.org上是正确的,但是在https://ethfiddle.com/出现两个错误,一个是

    :10:16: ParserError: Expected identifier, got 'LParen'

      constructor () public {

    我在constructor函数前面增加function ,看起来解决了

    调用transfer,又出现一个调用错误,

    VM Exception while processing transaction: invalid opcode

    后来各种百度,google发现问题是编译器版本的问题,因此在https://ethfiddle.com/里面选择版本0.4.24,问题全部解决,并且不需要在constructor函数前面增加function

    truffle的问题

    后来使用truffle的时候,truffle compile也出现上面两个错误,检查版本信息:

    truffle version

    Truffle v4.1.0 (core: 4.1.0)

    Solidity v0.4.19 (solc-js)

    原来也是Solidity版本问题,卸载truffle重新安装

    sudo npm uninstall -g truffle

    sudo npm install -g truffle

    这次查看版本

    truffle version

    Truffle v4.1.12 (core: 4.1.12)

    Solidity v0.4.24 (solc-js)

    重新truffle compile就没有问题了

    其他

    虽然我们可以安装指定版本的truffle,但是考虑到新版本功能更多,也就算了

    npm install -g truffle@X

    For example, to get solc 0.4.11 support, install truffle 3.2.2 or above.

    npm install -g truffle@3.2.2

    or

    npm update -g truffle@3.2.2

    相关文章

      网友评论

          本文标题:2018-06-25 solidity版本导致的错误

          本文链接:https://www.haomeiwen.com/subject/cewyyftx.html