Ethers.js Mapping Struct – Retrieve a Mapping Nested Inside a Struct from Ethers

ethers.jsmappingstruct

Hello Ethereum community,

I have couple of structs in my solidity code to organize things. Here is one struct from the code:

struct Staking {
  uint total;
  uint currentIndex;
  mapping(address => uint) registry;
}

Staking     public staking;

When compiling, I also get a staking() getter in the abi. Now on my frontend I have configured ethers npm library and am trying to retrive all of my structs. Calling

const data = await ethers.gameContract.staking();

Gives me this, JSON.stringified

[{"type":"BigNumber","hex":"0x00"},{"type":"BigNumber","hex":"0x00"}]

I can confirm that those two are total and currentIndex uints. But I can't see registry mapping.

Is there a way to query data modeled like this, or should I just declare all these properties in the top level of contract block (which should solve the issue by creating a getter for each prop in the abi), or maybe create custom getter functions that will retrieve these nested mappings?

EDIT: Calling staking() from the Remix in VSCode, I get this data:

{"0":"0","1":"0","total":"0","lastIndex":"0"}

Now I'm definitely confused. Are the first 2 items in this object, "0" and "1" keys actually items from the mapping? Maybe I am asking a wrong question here. But I still can't figure out how to query this mapping.

Thanks!

Best Answer

The ABI doesn't contain them due to their dynamic size. The accessory of maps and arrays does not return the whole data; hence, we query according to our needs.

Well for mapping, we don't have any way to iterate it. This works quite well for declaring state variables, but it is not possible to supply the argument for a struct with an array or map (dynamic) as a member.

Also, the struct is turned into a tuple of all its elementary members and the Non-elementary members are stripped from the tuple.

Related Topic