web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

getting value of public storage variable returns empty array

Open MananDesai54 opened this issue 5 years ago • 18 comments

Expected behavior

  • It must return valid address

Actual behavior

  • It is returning empty array. It works fine with remix but not work when i call the same function from react client, I tried making getter function but still same problem. getting data always returning empty array but making changes in contract is working fine. Also tried changing version of web3 but didn't help

This is my solidity code

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;

contract Test{
    address public creator;
    
    constructor() {
        creator= msg.sender;
    }
}

This is my react code

// test is instance of the solidity contract created with abi and deployed contract address
const creator= await test.methods
        .creator()
        .call({ from: accounts[0] });

Environment

npm: latest node: latest web3: latest OS: windows device: laptop

MananDesai54 avatar Mar 03 '21 09:03 MananDesai54

I tried reproducing this issue. With remix (injected web3) and local truffle suite, it was working as intended. Only in case the contract is deployed to remix javascript VM, I get the creator as undefined.

Kankan-0 avatar Apr 11 '21 13:04 Kankan-0

With remix it works fine, but when I integrated it with react and created connection to blockchain with web3js and then tried then it returns empty array, for every getters it returns empty array and for all transactions or calls that modifies blockchain it works fine.

MananDesai54 avatar Apr 11 '21 13:04 MananDesai54

@MananDesai54 Just for understanding, can you tell what is your local setup? I am using truffle with ganache, and I created a simple react app to check the code, and it is working fine for me. I am getting the creator as an array, with having only one value.

Kankan-0 avatar Apr 11 '21 13:04 Kankan-0

Mine is same only difference is mine is not ganache cli but mine is ethereum rinkeby network, for ganache is works fine but for real network it didn't work as expected

MananDesai54 avatar Apr 11 '21 13:04 MananDesai54

@MananDesai54 I checked by deploying the contract to rinkeby network. It's working for me there also. You can check the contract here 0xbAfe539eDE72D552D3E8A662C40813Db569B35C1

Kankan-0 avatar Apr 11 '21 15:04 Kankan-0

@Kankan-0 that's awesome can you share GitHub or code sand box link?

MananDesai54 avatar Apr 11 '21 15:04 MananDesai54

Btw I have used web3js for connecting to blockchain insgoof truffle @Kankan-0

MananDesai54 avatar Apr 11 '21 15:04 MananDesai54

@MananDesai54 I created this https://codesandbox.io/s/happy-dream-wfc9o

Kankan-0 avatar Apr 11 '21 16:04 Kankan-0

@Kankan-0 thanks I'll try this one

MananDesai54 avatar Apr 11 '21 16:04 MananDesai54

@Kankan-0 didn't work for me still getting empty array

MananDesai54 avatar Apr 13 '21 04:04 MananDesai54

@Kankan-0 this is my codepen link : https://codesandbox.io/s/silly-stallman-qfeec?file=/package.json

MananDesai54 avatar Apr 13 '21 04:04 MananDesai54

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

github-actions[bot] avatar Jun 13 '21 00:06 github-actions[bot]

@Kankan-0 this is my codepen link : https://codesandbox.io/s/silly-stallman-qfeec?file=/package.json

I have the same issue with an empty array for manager, did you figure out what is wrong?

ancutadaniel avatar Mar 17 '22 18:03 ancutadaniel

so I fix it was from abi - the correct ABI that needs to use is after I add JSON.stringnify(abi)
console.log(JSON.stringify(abi)); => copy/paste in new web3.eth.Contract(abi, address);

ancutadaniel avatar Mar 17 '22 19:03 ancutadaniel

Was you error fixed as I am having the same errors.

gokulkar879 avatar May 11 '22 14:05 gokulkar879

@MananDesai54

gokulkar879 avatar May 11 '22 18:05 gokulkar879

so I fix it was from abi - the correct ABI that needs to use is after I add JSON.stringnify(abi) console.log(JSON.stringify(abi)); => copy/paste in new web3.eth.Contract(abi, address);

Thanks @ancutadaniel . It worked for me.

keshavagarwal17 avatar Dec 05 '22 11:12 keshavagarwal17

The issue lies with the ABI you are feeding to web3.eth.Contract and you need a comprehensive one. Go to https://remix.ethereum.org/ and compile your contract there, then copy the ABI from there. Screenshot from 2023-04-07 12-44-41 You can find the copy button for the ABI at the bottom of this image.

snnbotchway avatar Apr 07 '23 12:04 snnbotchway