Skip to content

Unexpected behavior if staticCall returns an empty array #4036

Closed
@leduythuccs

Description

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

bugVerified to be an issue.fixed/completeThis Bug is fixed or Enhancement is complete and published.v6Issues regarding v6

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions