Web3.js: call() method for smart contract only returns null

Created on 13 May 2019  ·  7Comments  ·  Source: ChainSafe/web3.js

Description

I wrote a simple smart contract to prep my system for development there I found this strange behaviour when i run the call() method for a getData method in the contract.

Expected behavior


Expected sucessful excuting return the data currently present in the variable

Actual behavior


returns null

Steps to reproduce the behavior


solidity code:

pragma solidity >=0.4.22 <0.6.0;
pragma experimental ABIEncoderV2;

contract test{
    string data;
    function getData() view external returns(string memory){
    return data;
    }
    function setData(string calldata _data) external{
    data = _data;
    }
}

JS code:

var Web3 = require('web3')

const web3 = new Web3('http://127.0.0.1:8545')

json = require('./build/contracts/test.json');

const interface = json['abi'];
const bytecode = json['bytecode'];


async function setup(){
    var contract =  new web3.eth.Contract(interface,'0xBcf2D22144C11C20B31A7Aa84875414B252a92DB');
    const accounts = await  web3.eth.getAccounts().then(res=>{return res;})
    const admin = accounts[9];
    var receiver = accounts[1];
    var sender = accounts[3];
    const xl = contract.deploy({ data: bytecode })
    const bb = xl.send({from: admin,gas: 4712388,gasPrice: 100000000000},(error,transactionHash)=>{console.log('w',error,transactionHash);}).then(inst=>{return inst}).catch(err=>{console.log});
    const bl =  contract.methods.setData('dob').send({from: sender},(error,transactionHash)=>{console.log('e',error,transactionHash);});
    const br = await contract.methods.getData.call();
    console.log(br);
}
setup();

Screenshot from 2019-05-13 13-01-40 see the ouptut obtained for the mentioned program.

Error Logs

Gists

Versions

  • web3.js: [email protected]
  • nodejs: v10.15.3
  • browser: terminal
  • ethereum node: ganache v 2.0.1

support

Most helpful comment

I finally resolved this problem.
It seemed that it was due to wrong Genesis Block content. After I used a tool called "puppeth" to generate genesis block according to my options(input by command line).

Here is my Genesis Block: (The bold text should be the important part.)

{
"config": {
"chainId": 1999,
"homesteadBlock": 1,
"eip150Block": 2,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 3,
"eip158Block": 3,
"byzantiumBlock": 4,
"constantinopleBlock": 5,

"ethash": {}
},
"nonce": "0x0",
"timestamp": "0x5d357c6b",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x10000000000",
"difficulty": "0x10000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x5b195b16dc4a8e8da06cf646cca12cc4ebaff12f",
"alloc": { },
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

All 7 comments

It's returning null because the data values isn't set.

Please use the example below for a deeper understanding:

async function setup(){
    const contract =  new web3.eth.Contract(abi);
    const accounts = await  web3.eth.getAccounts();
    const admin = accounts[9];
    const receiver = accounts[1];
    const sender = accounts[3];

    const deployedContract = await contract.deploy({ data: bytecode }).send({from: admin,gas: 4712388,gasPrice: 100000000000});
    const receipt =  await deployedContract.methods.setData('dob').send({from: sender});

    const data = await deployedContract.methods.getData.call();
    console.log(data);
}
setup();

It's returning null because the data values isn't set.

Please use the example below for a deeper understanding:

async function setup(){
    const contract =  new web3.eth.Contract(abi);
    const accounts = await  web3.eth.getAccounts();
    const admin = accounts[9];
    const receiver = accounts[1];
    const sender = accounts[3];

    const deployedContract = await contract.deploy({ data: bytecode }).send({from: admin,gas: 4712388,gasPrice: 100000000000});
    const receipt =  await deployedContract.methods.setData('dob').send({from: sender});

    const data = await deployedContract.methods.getData.call();
    console.log(data);
}
setup();

Hi @nivida , On following your example the code just runs without any response, waiting for the getData() to return something it doesn't work the program returns the following

(node:22090) UnhandledPromiseRejectionWarning: #<Object>
(node:22090) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:22090) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

after running for a very long time(15-30 mins).

I encountered this problem as well, any update?

I also encountered the same problem.

I think there is no problem with the connection to my geth,because I saw my geth node accept the tx when i send() my smart contract method.But when i used call(),the only thing returned was null

truffle config

networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: 1024 // Match any network id
}

run geth

geth --datadir data1 --rpc --rpcport 8545 --networkid 1024 --rpcapi eth,net,db,personal console

smart contract

pragma solidity >=0.4.22 <0.6.0;

myContract{
....
function tempfunc()public pure returns(string memory) {
return 'hello';
}
....
}

JS code

......
let node = {
web3 : new Web3('http://localhost:8545', null, {}),
account : null,
contract : null,

init : async function(){
    const {web3} = this;
    try {
        const networkId = await web3.eth.net.getId()
        let deployNetwork = artifact.networks[networkId]
        console.log("contract address at ",deployNetwork.address)
        this.contract = new web3.eth.Contract(
            artifact.abi,
            deployNetwork.address
        )
        const accounts = await web3.eth.getAccounts();
        this.account = accounts[0];
        console.log('account:',this.account)
        const {tempfunc} = this.contract.methods
        let temp = await tempfunc().call()     // expect to get string 'hello'
        console.log(temp)
    }catch{
        console.log('could not connect to ethereum')
    }
}

}

node.init()

output

contract address at 0x3f0b5C277B82144e017D6c73D65833C9C8F33Ea1
account: 0xC196015AdE210f6D37689F6A007a64A9bd274191
null

I finally resolved this problem.
It seemed that it was due to wrong Genesis Block content. After I used a tool called "puppeth" to generate genesis block according to my options(input by command line).

Here is my Genesis Block: (The bold text should be the important part.)

{
"config": {
"chainId": 1999,
"homesteadBlock": 1,
"eip150Block": 2,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 3,
"eip158Block": 3,
"byzantiumBlock": 4,
"constantinopleBlock": 5,

"ethash": {}
},
"nonce": "0x0",
"timestamp": "0x5d357c6b",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x10000000000",
"difficulty": "0x10000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x5b195b16dc4a8e8da06cf646cca12cc4ebaff12f",
"alloc": { },
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

Thanks very much, it works

At 2019-07-23 11:12:26, "李冠學" notifications@github.com wrote:

I finally resolved this problem.
It seemed that it was due to wrong Genesis Block content. After I used a tool called "puppeth" to generate genesis block according to my options(input by command line).

Here is my Genesis Block: (The bold text should be the important part.)

{
"config": {
"chainId": 1999,
"homesteadBlock": 1,
"eip150Block": 2,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 3,
"eip158Block": 3,
"byzantiumBlock": 4,
"constantinopleBlock": 5,
"ethash": {}
},
"nonce": "0x0",
"timestamp": "0x5d357c6b",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x10000000000",
"difficulty": "0x10000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x5b195b16dc4a8e8da06cf646cca12cc4ebaff12f",
"alloc": { },
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

I finally resolved this problem.
It seemed that it was due to wrong Genesis Block content. After I used a tool called "puppeth" to generate genesis block according to my options(input by command line).

Here is my Genesis Block: (The bold text should be the important part.)

{
"config": {
"chainId": 1999,
"homesteadBlock": 1, "eip150Block": 2, "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000", "eip155Block": 3, "eip158Block": 3, "byzantiumBlock": 4, "constantinopleBlock": 5,
"ethash": {}
},
"nonce": "0x0",
"timestamp": "0x5d357c6b",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x10000000000",
"difficulty": "0x10000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x5b195b16dc4a8e8da06cf646cca12cc4ebaff12f",
"alloc": { },
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

really thank you!

Was this page helpful?
0 / 5 - 0 ratings