Looks like there are a couple of unanswered threads about this already, though mine is a more simple setup so maybe it will be easier to debug.

Running through testrpc I have a deployed contract, it deploys ok. I can access a contract function displayMessage in the callback of deployed contract ok.

However, I wish run the function on another page:

const contract = web3.eth.contract(abi);
const existingContract = contract.at(contractAddress);

The callback function in the response var returns: Error: VM Exception while processing transaction: invalid opcode.

Running console.log(existingContract.displayMessage()) returns the same error.

I have tried manually writing out my abi variable as an array:

const abi = [{
  type: 'function',
  name: 'displayMessage',
  inputs: [],
  outputs: [],
  constant: true,
  payable: false

as well as using the dynamic approach (getting from the file) just incase something was wrong there: const abi = JSON.parse(compiledContract.contracts[':contractName'].interface)

Can anyone advise on why I would be receiving this error & how to proceed?


As requested, here is my contract:

pragma solidity ^0.4.0;

contract contractName {
  function displayMessage() constant returns (string){
    return ("{var}");

To give a bit more insight, when creating the initial contract, I am replacing {var} with dynamic data:

fs.readFile(__dirname+'/../solc/contract.sol', 'utf8', function(err, data){
  const source = data.replace('\{var\}', data);
  const compiled = solc.compile(source);
  • From your error message it is failing while trying to execute displayMessage. What does displayMessage do?
    – Ismael
    Commented Nov 12, 2017 at 21:22
  • It just returns a string, I'll edit my question & add it in
    – joe
    Commented Nov 12, 2017 at 22:01
  • I've tried your code in remix and it work as expected. It seems you are missing the output parameter, the abi produced with solc v0.4.15 is [{"constant":true,"inputs":[],"name":"displayMessage","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function","stateMutability":"view"}].
    – Ismael
    Commented Nov 12, 2017 at 22:36
  • I have just tried copying that abi you kindly provided (it had some hidden special chars in it which I removed), I also changed to solc 0.4.15 (was previously 0.4.18) in my package.json and re-ran yarn install. I still get the same error unfortunately. Do you have any other suggestions?
    – joe
    Commented Nov 12, 2017 at 23:26
  • Why would you remove chars in the abi? Commented Nov 13, 2017 at 1:07

1 Answer 1


Sorry, I've tried your contract and it work as expected here

$ testrpc              
EthereumJS TestRPC v4.1.3 (ganache-core: 1.1.3)

Contract deployed using remix.ethereum.org connected to testrpc.

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

const contractAddress = "0xeea88acc603d832800e60e11eaae00d3a3197709";
const contractAbi = [{"constant":true,"inputs":[],"name":"displayMessage","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}];

const contract = web3.eth.contract(contractAbi);
const existingContract = contract.at(contractAddress);
existingContract.displayMessage(function(error, response){
  if (error) {
    console.log(`displayerr: ${error}`);
  } else {
    console.log(`displaymsg: ${response}`);
pragma solidity ^0.4.0;

contract contractName {
  function displayMessage() constant returns (string){
    return ("{var}");
  "name": "strings",
  "version": "1.0.0",
  "description": "",
  "main": "query.js",
  "license": "ISC",
  "dependencies": {
    "web3": "^0.20.2"
  • Thank you for spending time testing this for me. I'll try strip it back and mimic exactly what you have here. I'll report back later today.
    – joe
    Commented Nov 13, 2017 at 9:25
  • 1
    Hi @Ismael just letting you know that I've realised my mistake. It was that my contract address was undefined! I was testing on an older page which did not have one linked. I've added further checks thanks for your help.
    – joe
    Commented Nov 13, 2017 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.