Ethers.js: Contract call returns weird data

Created on 13 Jun 2018  路  7Comments  路  Source: ethers-io/ethers.js

Hello, i'm having a weird problem with ethers.js. My goal is to ditch web3 completely, but right now i can't because of this. here is an example code:

var Web3 = require("web3");

var web3Provider = new Web3.providers.HttpProvider(configParams.INFURA_URL_MAIN);

var web3 = new Web3(web3Provider);


var provider =  new ethers.providers.InfuraProvider("homestead");


var getDataWeb3 = async() =>{

  let contract = await (new web3.eth.Contract(configParams.LEDGER_ABI,  configParams.LEDGER_ADDRESS_MAIN));

 let list= await contract.methods.filterByState(3).call();

 console.log(list);

  list= await contract.methods.filterByState(4).call();

 console.log(list);

  list = await contract.methods.filterByState(5).call();

  console.log(list);

};

var getDataEthers = async() => {



let contract = await (new ethers.Contract(  configParams.LEDGER_ADDRESS_MAIN,configParams.LEDGER_ABI,provider));

 let list= await contract.filterByState(3);

console.log(list);

 list= await contract.filterByState(5);

console.log(list);
 list= await contract.filterByState(7);

console.log(list);

};

var run = async() => {

  console.log("Using web3: ");

 await getDataWeb3();

  console.log("Using Ethers: ");

  await getDataEthers();



};

run();

if i run this code, the Web3 method prints the data correctly (the returned data of filterByState is a list of addresses). The ethers method tho prints correctly only the first call output, while the result of the other two is the value 0x0000000000000000000000000000000000000020. This is an example output:

Using ethers
[ '0x7E1c94a5eDbfB7A8b5956f9d441fc049057222c2',
  '0xF307C7888Cf8C4b9ec2FBAeD7b10e0c336Ac9A3F',
  _addresses: '0x7E1c94a5eDbfB7A8b5956f9d441fc049057222c2' ]
0x0000000000000000000000000000000000000020
0x0000000000000000000000000000000000000020

don't honestly know what to do, code looks so simple i can't find any mistake. Tried other providers and got the same result, even using the Web3 provider. Any thoughts?

bug fixed

All 7 comments

I have no idea what鈥檚 the root cause for this but I observed similar issues sporadically when calling contract methods that return arrays. Haven鈥檛 found any repro scenario for this though.

Can you provide the ABI and possibly a deployed contract on one of the testnets?

Thanks! :)

i'm sorry i cannot provide the ABI, it's not my IP but if i can help in any way i'd be glad to give you a hand.

Same for me, sorry :/

Here is the relevant part of the contract:

    address[] private players;

    function getPlayerAddresses()
    public
    view
    returns (address[] memory result) {
        result = new address[](players.length);
        for (uint playerIndex = 0; playerIndex < players.length; playerIndex++) {
            result[playerIndex] = players[playerIndex];
        }
    }

Called from an Angular application like this:

const addresses: string[] = await contract.getPlayerAddresses();

And sometimes addresses is not an array, but some other object with garbage values.

We also found out that this does not happen when the Angular application (and therefore ethers) is served from localhost?!

A ha! Found it.

This was actually fixed yesterday in https://github.com/ethers-io/ethers.js/commit/059b03e09070024acab0ad3fdded9f529d47c2ab.

I did not realize how important this fix was, so I had not published it to npm yet. It has now been published to 3.0.22. If you upgrade, everything should be peachy.

You also pointed out another issue which will be up soon too, although not critical, which is the stray _addresses key. I will also add test cases for these.

Thanks! :)

Thanks for the super fast reply (based on the limited amount of information we provided). Just had quick try with the latest version and the issue seems to be gone for our scenario. Will observe more on Monday and post an update here. Thanks again!

Update: Verified with a couple of people: The issue is gone with 3.0.22!

Awesome! Glad hear it.

I've just pushed 3.0.24, which also gets rid of that stray weird value that was showing up.

Was this page helpful?
0 / 5 - 0 ratings