0

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!

1 Answer 1

1

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.

1
  • 1
    I have implemented couple of getters to retrieve data from the mappings / arrays. Some mappings store array indexes - to avoid looping said arrays, however I have to use one loop to distribute rewards, since they don't come from inflation, rather from paid NFT minting. I might also just put all the properties in the top level of contract block instead of individual structs for easier querying. Thanks for the elaboration ^^ Jan 18, 2023 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.