前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2018-06-25 solidity版本导致的错误

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

作者头像
oracle3
发布2022-05-13 08:36:09
7810
发布2022-05-13 08:36:09
举报

问题:

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题:
  • 解决过程
  • truffle的问题
  • 其他
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档