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

Cannot get 2D array from view in web3.js

Open rstormsf opened this issue 8 years ago • 13 comments

    function return2d() public view returns(uint256[1][1]) {
        uint256 x = 354;
        return [[x]];
    }

in js

await contractX.return2d();

Error:

    Error: Error: [number-to-bn] while converting number ["354"] to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: "354"
      at Object.toBN (node_modules/truffle/build/webpack:/~/web3-utils/src/utils.js:64:1)
      at _convertNumber (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/reformat.js:18:1)
      at arr.map.item (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/reformat.js:31:1)
      at Array.map (<anonymous>)
      at _convertNumberArray (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/reformat.js:31:1)
      at abiSegment.forEach (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/reformat.js:57:1)
      at Array.forEach (<anonymous>)
      at Function.numbers (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/reformat.js:47:1)
      at Promise (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/execute.js:120:1)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:182:7)

rstormsf avatar Oct 26 '18 18:10 rstormsf

I suppose the first piece of code is Solidity. This should work

function return2d() public constant returns(uint256[1][1] res) {
        uint x = 10;
        res[0][0] = x; 
    }

Tested with truffle.

chmelva4 avatar Oct 27 '18 20:10 chmelva4

@chmelva4, thanks for the answer! I checked your code, but unfortunately I got the same result.

      at Object.toBN (node_modules/truffle/build/webpack:/~/web3-utils/src/utils.js:64:1)
      at _convertNumber (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/reformat.js:18:1)
      at arr.map.item (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/reformat.js:31:1)
      at Array.map (<anonymous>)
      at _convertNumberArray (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/reformat.js:31:1)
      at abiSegment.forEach (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/reformat.js:57:1)
      at Array.forEach (<anonymous>)
      at Function.numbers (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/reformat.js:47:1)
      at Promise (node_modules/truffle/build/webpack:/packages/truffle-contract/lib/execute.js:120:1)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

f0xx0f avatar Oct 31 '18 12:10 f0xx0f

Thats because you are most likely converting the number incorrectly after getting it from blockchain. I did it like this and it works.

it('should return 10', async () => { var test2d = await Test2d.deployed(); var x = await test2d.return2d() console.log(x); assert.isTrue(x[0][0].toNumber() == 10); })

You should take a look in debugger at what you are passing as arguments to toBN function. I think it cannot handle arrays.

chmelva4 avatar Oct 31 '18 12:10 chmelva4

@chmelva4 I get the error but I don't make the console.log. The error is output after await contractX.return2d(). What version of truffle do you have installed?

f0xx0f avatar Nov 01 '18 09:11 f0xx0f

v4.1.14

chmelva4 avatar Nov 01 '18 09:11 chmelva4

@chmelva4 exactly! it doesn't work on v5 truffle

rstormsf avatar Nov 01 '18 09:11 rstormsf

Apparently the issue happens when using async/await. If you switch to promisses (which is cumbersome), it works.

chmelva4 avatar Nov 01 '18 13:11 chmelva4

@chmelva4 so, it's valid issue. It should work with async/await

rstormsf avatar Nov 05 '18 17:11 rstormsf

Ye you're right, I posted that for info.

chmelva4 avatar Nov 05 '18 23:11 chmelva4

Using truffle v5 and also getting the same issue when using the truffle contract-abstraction to call a contract that returns a 2D array.

@chmelva4 Any way to not use async/await but promises with truffle contract abstractions?

Looks like it may be possible to wrap the Truffle Artifacts using own web3 in dependencies for testing and then call the contract functions via contract.methods.functions() and handle results case by case instead of relying on Truffle Contract Artifacts.

Uncaught Error: Error: [number-to-bn] while converting number ["5446375890709390460","0"] to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: "5446375890709390460,0" at Object.toBN (node_modules/web3-utils/src/utils.js:64:15) at _convertNumber (node_modules/truffle-contract/lib/reformat.js:18:40) at arr.map.item (node_modules/truffle-contract/lib/reformat.js:31:27) at Array.map (<anonymous>) at _convertNumberArray (node_modules/truffle-contract/lib/reformat.js:31:14) at abiSegment.forEach (node_modules/truffle-contract/lib/reformat.js:57:20) at Array.forEach (<anonymous>) at Function.numbers (node_modules/truffle-contract/lib/reformat.js:47:14) at Promise (node_modules/truffle-contract/lib/execute.js:120:37)

jacko125 avatar Apr 02 '19 04:04 jacko125

I'll check that with the truffle team asap :)

nivida avatar May 08 '19 19:05 nivida

Running into the same issue when trying to return a 2d array from Solidity (using pragma experimental ABIEncoderV2;). Any updates/way to fix it?

leonardoalt avatar Sep 26 '19 09:09 leonardoalt

I've tested this with web3.js v1.2.2 and it can return a 2D array without a problem.

Contract:

pragma solidity ^0.5.8;
pragma experimental ABIEncoderV2;

contract Test {
    function return2d() public view returns(uint256[1][1] memory) {
        uint256 x = 354;
        return [[x]];
    }
}

JS:

var web3 = new Web3('http://localhost:7545');

const contract = new web3.eth.Contract([
  {
    "constant": true,
    "inputs": [],
    "name": "return2d",
    "outputs": [
      {
        "name": "",
        "type": "uint256[1][1]"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  }
], '0x4F063F7C86561137d10a08C13Bb228BF93B90B9B');

contract.methods.return2d().call().then(console.log);
> [ [ '354' ] ]

nivida avatar Oct 31 '19 14:10 nivida