1

I am very new to Solidity and Javascript developments and I have faced some issues lately.

Use case:

  1. Store an array of data from Javascript to Solidity.
  2. Return the stored array from Solidity back to Javascript code.

Javascript code for getting the array:

    async loadBlockchain() {
    const web3 = window.web3
    const accounts = await web3.eth.getAccounts()
    console.log("Eth account: " + accounts)
    this.setState({account: accounts[0]})
    const networkID = await web3.eth.net.getId()
    console.log("Eth network ID: " + networkID)
    const networkData = storeHash.networks[networkID]
    
    if(networkData) {
        //Fetch the smart contract from the address
        const abi = storeHash.abi
        const address = networkData.address
        const contract = new web3.eth.Contract(abi, address)
        this.setState({ contract })
        console.log(contract)

        var fileHashList = await contract.methods.get().call()
        console.log(fileHashList)
    } 
    else 
    {
        window.alert('Smart contract not deployed to detected network')
    }
}

Solidity code for returning the array:

pragma solidity 0.5.16;
pragma experimental ABIEncoderV2;

contract storeHash {
//Upload stored file hash in blockchain

string[] public fileHash;
//uint256 public fileCount = 0;

//Store hash value
function set(string memory _fileHash) public {
    
    fileHash.push(_fileHash);
}


//Retrieve hash value
function get() public view returns (string[] memory) {

    return fileHash;

}

Issue:

  1. The array returned seems to be read as bigNumber and showing the following error:

    Unhandled Rejection (null): overflow (fault="overflow", operation="toNumber", value="36830543755683898667443985212771609229556685323236234700674529361303541601398", code=NUMERIC_FAULT, version=bignumber/5.3.0)

  2. The error message in console:

Uncaught (in promise) null: overflow (fault="overflow", operation="toNumber", value="36830543755683898667443985212771609229556685323236234700674529361303541601398", code=NUMERIC_FAULT, version=bignumber/5.3.0)
    at Logger.makeError (http://localhost:3000/static/js/vendors~main.chunk.js:6622:19)
    at Logger.throwError (http://localhost:3000/static/js/vendors~main.chunk.js:6633:18)
    at throwFault (http://localhost:3000/static/js/vendors~main.chunk.js:3782:17)
    at BigNumber.toNumber (http://localhost:3000/static/js/vendors~main.chunk.js:3584:9)
    at http://localhost:3000/static/js/vendors~main.chunk.js:666:54
    at Array.forEach (<anonymous>)
    at unpack (http://localhost:3000/static/js/vendors~main.chunk.js:661:10)
    at ArrayCoder.decode (http://localhost:3000/static/js/vendors~main.chunk.js:818:39)
    at http://localhost:3000/static/js/vendors~main.chunk.js:669:23
    at Array.forEach (<anonymous>)
    at unpack (http://localhost:3000/static/js/vendors~main.chunk.js:661:10)
    at TupleCoder.decode (http://localhost:3000/static/js/vendors~main.chunk.js:1275:92)
    at AbiCoder.decode (http://localhost:3000/static/js/vendors~main.chunk.js:185:20)
    at ABICoder.push../node_modules/web3-eth-abi/lib/index.js.ABICoder.decodeParametersWith (http://localhost:3000/static/js/vendors~main.chunk.js:308757:28)
    at ABICoder.push../node_modules/web3-eth-abi/lib/index.js.ABICoder.decodeParameters (http://localhost:3000/static/js/vendors~main.chunk.js:308739:15)
    at Contract.push../node_modules/web3-eth-contract/lib/index.js.Contract._decodeMethodReturn (http://localhost:3000/static/js/vendors~main.chunk.js:310221:20)
    at Method.outputFormatter (http://localhost:3000/static/js/vendors~main.chunk.js:310556:32)
    at Method.push../node_modules/web3-core-method/lib/index.js.Method.formatOutput (http://localhost:3000/static/js/vendors~main.chunk.js:306298:50)
    at sendTxCallback (http://localhost:3000/static/js/vendors~main.chunk.js:306848:25)
    at cb (http://localhost:3000/static/js/vendors~main.chunk.js:198169:22)
    at Item.push../node_modules/process/browser.js.Item.run (http://localhost:3000/static/js/vendors~main.chunk.js:242191:12)
    at drainQueue (http://localhost:3000/static/js/vendors~main.chunk.js:242155:34)

May I know how can I fix this issue and get the array I wanted in my NodeJs application? Thank you in advance!

supdev
  • 31
  • 1
  • 2
  • Have you sorted this out already? Got a similar error in a different case. AFAIK, solidity does not accept returning arrays for external (javascript) calls. – joaoavf Jul 09 '21 at 12:48
  • @joaoavf Not yet :( desperately looking for alternative as my project deadline is approaching. – supdev Jul 10 '21 at 15:16

1 Answers1

0
 // this will return array of BN objects. For example
 // [{"type":"BigNumber","hex":"0x0a"},{"type":"BigNumber","hex":"0x14"}] 
 var fileHashList = await contract.methods.get().call()

Now you need to iterate through array and convert each BN object to its value. I converted to string

 // ["10","20"] 
 var modifiedFileList=fileHashList.map((fileHash)=>web3.utils.BN(fileHash).toString())
Yilmaz
  • 35,338
  • 10
  • 157
  • 202