简体   繁体   English

使用简单的Solidity合同和脚本生成无效的操作码错误

[英]invalid opcode error with a simple Solidity contract and script

I am a newcomer to Solidity and web3.js . 我是Solidity和web3.js的新手。 I am following the tutorial here - 我在这里按照教程 -

https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2 https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2

to build a simple Voting Dapp. 建立一个简单的投票Dapp。 I've installed ganache-cli, solc and web3 version 0.20.2 in the local node_modules folder using npm. 我使用npm在本地node_modules文件夹中安装了ganache-cli,solc和web3版本0.20.2。 The Voting.sol contract in Solidity is : Solidity中的Voting.sol合约是:

pragma solidity ^0.4.18;

contract Voting {

  mapping (bytes32 => uint8) public votesReceived;
  bytes32[] public candidateList;

  function Voting(bytes32[] candidateNames) public {
    candidateList = candidateNames;
  }

  function totalVotesFor(bytes32 candidate) view public returns (uint8) {
    return votesReceived[candidate];
  }
}

with the following script called voting_main.js: 使用以下名为voting_main.js的脚本:

Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))

fs = require('fs')
code = fs.readFileSync('Voting.sol').toString()

solc = require('solc')
compiledCode = solc.compile(code)

abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
VotingContract = web3.eth.contract(abiDefinition)
byteCode = compiledCode.contracts[':Voting'].bytecode
deployedContract = VotingContract.new(['Rama','Nick','Jose'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
contractInstance = VotingContract.at(deployedContract.address)

contractInstance.totalVotesFor.call('Rama')

When I run ganache-cli on localhost:8545 and then run the script in another terminal, I get the following error. 当我在localhost:8545上运行ganache-cli然后在另一个终端中运行脚本时,我收到以下错误。

ameya@ameya-HP-ENVY-15-Notebook-PC:~/Fresh_install$ node voting_main.js 
/home/ameya/Fresh_install/node_modules/solc/soljson.js:1
(function (exports, require, module, __filename, __dirname) { var Module;if(!Module)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOverrides={};for(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}var ENVIRONMENT_IS_WEB=typeof window==="object";var ENVIRONMENT_IS_WORKER=typeof importScripts==="function";var ENVIRONMENT_IS_NODE=typeof process==="object"&&typeof require==="function"&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;var ENVIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WORKER;if(ENVIRONMENT_IS_NODE){if(!Module["print"])Module["print"]=function print(x){process["stdout"].write(x+"\n")};if(!Module["printErr"])Module["printErr"]=function printErr(x){process["stderr"].write(x+"\n")};var nodeFS=require("fs");var nodePath=require("path");Module["read"]=function read(filename,binary){filename=nodePath["normalize"](filename);var ret=nodeFS["readFileSync"](filename);if(!r

Error: VM Exception while processing transaction: invalid opcode
    at Object.InvalidResponse (/home/ameya/Fresh_install/node_modules/web3/lib/web3/errors.js:38:16)
    at RequestManager.send (/home/ameya/Fresh_install/node_modules/web3/lib/web3/requestmanager.js:61:22)
    at Eth.send [as call] (/home/ameya/Fresh_install/node_modules/web3/lib/web3/method.js:145:58)
    at SolidityFunction.call (/home/ameya/Fresh_install/node_modules/web3/lib/web3/function.js:135:32)
    at Object.<anonymous> (/home/ameya/Fresh_install/voting_main.js:16:32)
    at Module._compile (internal/modules/cjs/loader.js:654:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
    at Function.Module._load (internal/modules/cjs/loader.js:498:3)

This seems to be a very simple example which is still throwing the invalid opcode error. 这似乎是一个非常简单的例子,它仍然抛出无效的操作码错误。 Where am I going wrong ? 我哪里错了?

You don't need to call .at() if you're using .new() . 你并不需要调用.at()如果你使用.new() You use at() when you want to interact with a contract that has already been deployed to the blockchain. 当您想要与已经部署到区块链的合同进行交互时,可以使用at()

If you choose to deploy a new contract, you should use the async version of the method call (synchronous is no longer supported in web3j 1.0). 如果选择部署新合同,则应使用方法调用的异步版本(web3j 1.0不再支持同步)。 Your call should be something like this: 你的电话应该是这样的:

VotingContract.new(['Rama','Nick','Jose'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000}, (error, deployedContract) => {
    if (!error) {
        if (deployedContract.address) {
            console.log(deployedContract.totalVotesFor.call('Rama'));
        }
    }
});

Note that the callback is fired twice. 请注意,回调会被触发两次。 First time is for the submission of the transaction ( deployedContract.transactionHash will be set) and the second time after the transaction is mined. 第一次是提交事务(已设置deployedContract.transactionHash )和第二次挖掘事务后。

You can also look at the example in the web3js docs (pasted below for convenience). 您还可以查看web3js文档中的示例(为方便起见,粘贴在下面)。

const fs = require("fs");
const solc = require('solc')

let source = fs.readFileSync('nameContract.sol', 'utf8');
let compiledContract = solc.compile(source, 1);
let abi = compiledContract.contracts['nameContract'].interface;
let bytecode = compiledContract.contracts['nameContract'].bytecode;
let gasEstimate = web3.eth.estimateGas({data: bytecode});
let MyContract = web3.eth.contract(JSON.parse(abi));

var myContractReturned = MyContract.new(param1, param2, {
   from:mySenderAddress,
   data:bytecode,
   gas:gasEstimate}, function(err, myContract){
    if(!err) {
       // NOTE: The callback will fire twice!
       // Once the contract has the transactionHash property set and once its deployed on an address.

       // e.g. check tx hash on the first call (transaction send)
       if(!myContract.address) {
           console.log(myContract.transactionHash) // The hash of the transaction, which deploys the contract

       // check address on the second call (contract deployed)
       } else {
           console.log(myContract.address) // the contract address
       }

       // Note that the returned "myContractReturned" === "myContract",
       // so the returned "myContractReturned" object will also get the address set.
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试访问 Hardhat 中的 Solidity 结构数组时出现无效操作码错误 - Error invalid opcode trying to access Solidity array of structs in Hardhat 坚固性0.4.6中使用send()函数时,合同抛出无效地址 - Contract throws Invalid address when using send() function in solidity 0.4.6 如何运行一个成功的 Solidity 智能合约测试脚本? - How To Run A Successful Solidity Smart Contract Test Script? 来自solidity合约的app.js文件中的getJSON()出错 - Getting error in getJSON() in app.js file from solidity contract 无法将 Solidity 合约部署到 Rinkeby.network(无效的 asm.js:stdlib 的无效成员) - Unable to deploy Solidity contract to Rinkeby network (Invalid asm.js: Invalid member of stdlib) Solidity 错误““未定义”的参数数量无效” - Solidity error 'Invalid number of parameters for "undefined" ' Solidity:在智能合约中填充结构 - Solidity: populating a struct in a smart contract Solidity 错误:我们无法估算气体。 合约中可能有错误,此交易可能会失败 - Solidity Error: We were not able to estimate gas. There might be an error in the contract and this transaction may fail 松露测试因“无效的操作码”而失败 - Truffle test failed with "invalid opcode" Solidity 与 Rinkeby 测试网络 | 未捕获的错误:无效地址 - Solidity w/ Rinkeby Test Network | Uncaught Error: invalid address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM