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

Decoding array of struct (MyStruct[]) to an array of objects? #3591

Closed
kevholder opened this issue Jun 18, 2020 · 11 comments
Closed

Decoding array of struct (MyStruct[]) to an array of objects? #3591

kevholder opened this issue Jun 18, 2020 · 11 comments
Labels
1.x 1.0 related issues Enhancement Includes improvements or optimizations Stale Has not received enough activity

Comments

@kevholder
Copy link

kevholder commented Jun 18, 2020

Expected behavior

When a method returns an array of structs (e.g. MyStruct[]), web3js should decode it to an array of objects that have the same shape as MyStruct.

Actual behavior

When a method returns an array of structs (e.g. MyStruct[]), web3js decodes it as an array of arrays.

Steps to reproduce the behavior

  1. Deploy test contract
pragma solidity >= 0.5.0 < 0.7.0;
pragma experimental ABIEncoderV2;

contract MyContract {
  struct Entry {
    uint inputAmount;
    uint outputAmount;
  }

  Entry[] public entries;

  constructor() public {
    Entry memory entry = Entry({
      inputAmount: 1,
      outputAmount: 2
    });
    entries.push(entry);
  }

  // workaround: otherwise web3js can only index into the array
  function entriesArray() external view returns (Entry[] memory) {
    return entries;
  }
}
  1. Call entriesArray() getter
const data = await myContract.methods.entriesArray().call();
// returns [["1", "2"]] but it should return:
// [{ inputAmount: "1", outputAmount: "2" }]

Environment

web3 1.2.9
@kevholder
Copy link
Author

Using this as a workaround:

const entryProps = exchangeAbi
  .find((abi) => abi.name === "entriesArray")
  .outputs[0].components.map((c) => c.name);

const decodeEntriesArray = (entries) => {
  // workaround: https://github.com/ethereum/web3.js/issues/3591
  return entries.map((entry) => {
    return Object.fromEntries(
      entry.map((val, idx) => {
        return [entryProps[idx], val];
      })
    );
  });
};

@cgewecke cgewecke added 1.x 1.0 related issues Enhancement Includes improvements or optimizations labels Jun 19, 2020
@GregTheGreek
Copy link
Contributor

GregTheGreek commented Jun 29, 2020

Sorry for the delay, this is a pretty great idea! Drastically improves the UX.

This would definitely be a breaking change in the way existing applications use web3.js, but an alternative option would be to include the above helpers, until a breaking change is ready. We could create a method (eg: mapABItoResults ) where you could wrap your results into.

Thoughts: @cgewecke @kevholder

@cgewecke
Copy link
Collaborator

Agree, a really good improvement.

There's also precedent at Web3 for gating some enhancements like this behind a settings flag. For example revert reason fetching is currently enabled by setting web3.eth.handleRevert = true.

@GregTheGreek
Copy link
Contributor

Great idea. We can probably push this up the list

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

@github-actions github-actions bot added the Stale Has not received enough activity label Jul 30, 2020
@GregTheGreek GregTheGreek removed the Stale Has not received enough activity label Jul 30, 2020
@kevholder
Copy link
Author

Would you like me to send a pull request?

@GregTheGreek
Copy link
Contributor

That would be great!

@github-actions
Copy link

github-actions bot commented Oct 3, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

@github-actions github-actions bot added the Stale Has not received enough activity label Oct 3, 2020
@debnath-d
Copy link

debnath-d commented Nov 17, 2020

@GregTheGreek Any update on this issue? Were the helper functions mentioned in the discussion above implemented?

Also, I think the issue should be reopened and not marked stale.

@Ajit-91
Copy link

Ajit-91 commented Aug 10, 2022

is there any inbuilt method in web3 for achieving this ?

@ilya-korotya
Copy link

Hi @GregTheGreek. Any updates on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Enhancement Includes improvements or optimizations Stale Has not received enough activity
Projects
None yet
Development

No branches or pull requests

6 participants