1

This is my smart contract written in solidity (v.0.5.16) -

pragma solidity ^0.5.0;

contract MyContract {
    function myFunction() public pure returns(uint256 myNumber, string memory myString) {
        return (23456, "Hello!%");
    }
}

This is my web3 (v.1.2.6) code written in NodeJS (v.10.16.1) -

const Web3 = require('web3');

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

const abi = [{"constant":true,"inputs":[],"name":"myFunction","outputs":[{"internalType":"uint256","name":"myNumber","type":"uint256"},{"internalType":"string","name":"myString","type":"string"}],"payable":false,"stateMutability":"pure","type":"function"}];

const address = '0x692a70d2e424a56d2c6c27aa97d1a86395877b3a';
const default_address = '0x597c0d6383aAEad3895Ee8191F4eF2Dd8940B6E7';

const myContract = new web3.eth.Contract(abi, address);
myContract.deploy({
      data: '0x608060405234801561001057600080fd5b5061012f806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063c3780a3a14602d575b600080fd5b603360b2565b6040518083815260200180602001828103825283818151815260200191508051906020019080838360005b838110156077578082015181840152602081019050605e565b50505050905090810190601f16801560a35780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b60006060615ba08090506040518060400160405280600781526020017f48656c6c6f21250000000000000000000000000000000000000000000000000081525091509150909156fea265627a7a723158207c74308dd7b2369a27728a1c18b137dbf8de6f83a3015e84d898f4312374719764736f6c63430005100032'
    })
    .send({
      from: default_address,    
      gas: 1500000,
      gasPrice: '30000000000000'
    }).then(function(newContractInstance){
        console.log("contract address -");
        console.log(newContractInstance.options.address); // instance with the new contract address
});

myContract.methods.myFunction().call()
.then(function(result){
    console.log(result);
})
.catch(function(error){
    console.log(error);
});

It prints the value of newContractInstance.options.address but gives this ERROR on call() -

Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.
    at ABICoder.decodeParameters (...\node_modules\web3-eth-abi\src\index.js:239:15)
    at Contract._decodeMethodReturn (...\node_modules\web3-eth-contract\src\index.js:557:22)
    at Method.outputFormatter (...\node_modules\web3-eth-contract\src\index.js:910:46)
    at Method.formatOutput (...\node_modules\web3-core-method\src\index.js:167:54)
    at sendTxCallback (...\node_modules\web3-core-method\src\index.js:596:33)
    at ...\node_modules\web3-core-requestmanager\src\index.js:147:9
    at XMLHttpRequest.request.onreadystatechange (...\node_modules\web3-providers-http\src\index.js:110:13)
    at XMLHttpRequestEventTarget.dispatchEvent (...\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (...\node_modules\xhr2-cookies\dist\xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (...\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (...\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61)
    at IncomingMessage.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
2
  • 1
    You should do the call to myContract.methods.myFunction inside function(newContractInstance). Otherwise, you are doing it without waiting for the contract deployment to complete. Feb 26 '20 at 8:13
  • Use 'await' dude...
    – Majd TL
    Feb 26 '20 at 8:24
0

First of all, change this:

const myContract = new web3.eth.Contract(abi, address);

To this:

const myContract = new web3.eth.Contract(abi);

You should not provide an address when deploying a contract, since the address of that contract is obviously not viable until you have deployed it.

Second, change this:

myContract.methods.myFunction().call()

To this:

newContractInstance.methods.myFunction().call()

Because the myContract object does not hold a deployed contract, but the newContractInstance object does.

Of course, you'll need to make that call from inside function(newContractInstance), otherwise, you are doing it without waiting for the contract deployment to complete.

10
  • Thanks a lot for the answer! I did those two changes mentioned above. Now I'm getting a new ERROR - Error: Returned error: VM Exception while processing transaction: revert at Object.ErrorResponse.... I haven't changed the smart contract. There are no modifiers or require() in the contract.
    – surhud004
    Feb 26 '20 at 20:44
  • @surhud004: Before posting this answer, I have actually tried your code with the changes that I later suggested here, and it worked fine. Please make sure that you are applying the exact same changes. Feb 26 '20 at 21:13
  • Yes, I did the exact changes. Here's the code snippet - .then(function(newContractInstance){ console.log("contract address -"); console.log(newContractInstance.options.address); // instance with the new contract address newContractInstance.methods.myFunction().call() .then(function(result){ console.log(result); }) .catch(console.log); });
    – surhud004
    Feb 26 '20 at 21:34
  • Here's the complete code snippet with the ERROR snippet - gist.github.com/surhud004/59f876f6f991941e2145e9cbca2f6134
    – surhud004
    Feb 26 '20 at 21:41
  • @surhud004: The bytecode in that link is different from the bytecode in your questio!!! A simple comparison of the two code snippets (as I just did) would have revealed that to you. You should be more meticulous when writing your code! Feb 26 '20 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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