Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior if staticCall returns an empty array #4036

Closed
leduythuccs opened this issue May 10, 2023 · 3 comments
Closed

Unexpected behavior if staticCall returns an empty array #4036

leduythuccs opened this issue May 10, 2023 · 3 comments
Assignees
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published. v6 Issues regarding v6

Comments

@leduythuccs
Copy link

leduythuccs commented May 10, 2023

Ethers Version

6.3.0

Search Terms

abi, empty array, decoder

Describe the Problem

If a staticCall returns an empty array (let's call it arr). The arr.map(...).length will have the length of 1, which is not correct.

I think it has something to do with the Result class here.

Code Snippet

import { ethers } from 'ethers';
// =======

const provider = new ethers.JsonRpcProvider('https://rpc.ankr.com/eth');
const contract = new ethers.Contract(
  '0xcbC72d92b2dc8187414F6734718563898740C0BC',
  [
    'function rewardIndexesStored() external view returns (uint256[] memory indexes)',
  ],
  {
    provider,
  },
);

const indexes = await contract.rewardIndexesStored.staticCall(); // an empty array
const mappedIndexes = indexes.map((index) => index.toString()); // should be an empty array

console.log('pre map length:', indexes.length); // 0
console.log('post map length:', mappedIndexes.length); // 1

console.log('pre map:', indexes); // []
console.log('post map:', mappedIndexes); // [0]

Contract ABI

['function rewardIndexesStored() external view returns (uint256[] memory indexes)']

Errors

No response

Environment

Ethereum (mainnet/ropsten/rinkeby/goerli), node.js (v12 or newer)

Environment (Other)

No response

@leduythuccs leduythuccs added investigate Under investigation and may be a bug. v6 Issues regarding v6 labels May 10, 2023
@leduythuccs
Copy link
Author

Another way to reproduce this is just simply use the Result:

import { Result } from 'ethers';

const emptyArray = new Result();
const mappedEmptyArray = emptyArray.map((index) => index.toString());
console.log('pre map length:', emptyArray.length); // 0
console.log('post map length:', mappedEmptyArray.length); // 1

console.log('pre map:', emptyArray); // []
console.log('post map:', mappedEmptyArray); // [0]

Output:

pre map length: 0
post map length: 1
pre map: Result(0) []
post map: Result(1) [ 0 ]

@ricmoo ricmoo added bug Verified to be an issue. on-deck This Enhancement or Bug is currently being worked on. and removed investigate Under investigation and may be a bug. labels May 15, 2023
@ricmoo
Copy link
Member

ricmoo commented May 15, 2023

Confirmed as a bug. Fixed locally and I'll get a fix published shortly.

@ricmoo
Copy link
Member

ricmoo commented May 20, 2023

Fixed in v6.4.0.

Let me know if you have any more issues.

Thanks! :)

@ricmoo ricmoo closed this as completed May 20, 2023
@ricmoo ricmoo added fixed/complete This Bug is fixed or Enhancement is complete and published. and removed on-deck This Enhancement or Bug is currently being worked on. labels May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

2 participants