1

I create my contract with the following code:

pragma solidity ^0.5.0;

contract Coin {
    function sayHello() public view returns(bytes32) {
        return "Hello";
    }
}

truffle-config:

module.exports = {
  rpc: {
      host: "localhost",
      port: 8543
  },

  networks: {
    development: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 8543,            // Standard Ethereum port (default: none)
      network_id: "*",       // Any network (default: none)
      from: "0x3242587c258109ed8722d667501d2116f08e91d1",
      gas: 2100000
    },
  },

  mocha: {
  },

  compilers: {
    solc: {
    }
  }
}

And I migrate it with truffle migrate on my private network and it outputs:

=====================

   Replacing 'Coin'
   ----------------
   > transaction hash:    0x0c9476750b030a7e8ee35678643573e3f941292d3909bbb33a5248185c307774
   > Blocks: 0            Seconds: 0
   > contract address:    0x71A258BE260db4397d5AA32aa7F1BcbB7f03C071
   > block number:        10230
   > block timestamp:     1561343003
   > account:             0x3242587C258109eD8722d667501d2116F08e91D1
   > balance:             9.87546732
   > gas used:            502220
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.0100444 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:           0.0100444 ETH


Summary
=======
> Total deployments:   2
> Final cost:          0.01527226 ETH

So I deployed it successfully, right?

Then I tried to interact with the contract with the following code

const Web3 = require('web3');
const Web3Accounts = require('web3-eth-accounts');

const web3 = new Web3(Web3.givenProvider || 'http://localhost:8543', null, {});

const coinbase = "0x7bd59507b55d46eb874e6a2f8ee4befa123e414e";
const owner = "0x3242587c258109ed8722d667501d2116f08e91d1";
const abi = /*abi*/
var coin = new web3.eth.Contract(abi, "0x71A258BE260db4397d5AA32aa7F1BcbB7f03C071", 
    {defaultAccount: owner, defaultGasPrice: '20000000'});                   // Contract object

const main = async () => {
    try {
        let reply = await coin.methods.sayHello().call({from: owner});
        console.log(reply);
    }
    catch(error) {
        console.error(`error: ${error}`);
    }
};

main()
    .then(() => { console.log("done"); })
    .catch(err => { console.log("Failed with error: " + err); });

It outputted:

null
done

my envs:

  "dependencies": {
    "web3": "^1.0.0-beta.55",
    "web3-eth-accounts": "^1.0.0-beta.55"
  }

I started my private network with geth --port 3000 --networkid 58343 --nodiscover --datadir=./blkchain --maxpeers=0 --rpc --rpcport 8543 --rpcaddr 127.0.0.1 --rpccorsdomain "*" --rpcapi "eth,net,web3,personal,miner"

I just start in the blockchain world. Please forgive me if this is a dumb question.

10
  • I may be missing something, but I could not find your declaration of coin variable.
    – Sanjay S B
    Jun 24, 2019 at 5:31
  • @SanjaySB sorry, updated it.
    – HFX
    Jun 24, 2019 at 6:04
  • What is your development network in truffle config. I think you should compress your question. It is too long and too much information. May be you can keep only the sayHello function in contract and keep the corresponding interaction code in js file.
    – Sanjay S B
    Jun 24, 2019 at 6:37
  • Sorry for the inconvenience, I updated the question to make it more clear.
    – HFX
    Jun 24, 2019 at 6:56
  • did you run your node as light node?
    – haxerl
    Jun 24, 2019 at 9:13

2 Answers 2

1

Take a look in this line of code. I think it should be coin.methods not coin.method correct me if I'm wrong.

let reply = await coin.method.sayHello().call({from: owner});
1
  • You're right, it's a typo in my question. I apologize. It's coin.methods in my code, otherwise it would have thrown an undefined error.
    – HFX
    Jun 27, 2019 at 6:48
0

I was developing with Truffle, web3, MetaMask and React and was having a similar issue: contract.methods.function().call() kept returning null for returning a private string in a simple contract. My contract object was deployed, I was being shown the methods, ABI looked correct. Very frustrating.

I think what ended up working for me was resetting the MetaMask nonce ( Metamask > Settings > Advanced > Reset Account ) . Meta Mask Docs

If you're running a test blockchain and restart it, you can accidentally confuse MetaMask because it calculates the next nonce based on both the network state and the known sent transactions.

Because I was compiling new contracts and migrating without disconnecting MetaMask, I think it became confused.

I say "maybe" because I basically started a new truffle react box and re implemented everything to make sure I wasn't missing something.

So, it technically could have been a couple things. But I would give this a shot if you are having this issue.

Your Answer

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

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