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

feat: add custom struct type matching to Register via structTypeMatches #251

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

architect-dev
Copy link

@architect-dev architect-dev commented Aug 27, 2024

This PR allows users and library authors to inject custom types that match their custom solidity structs. The use case is narrow, but allows library authors to create more expressive tools that interact with viem and wagmi. I have created a discussion that this PR references: #250

I've added a new test in register.test-d.ts which unfortunately can only be run locally since it relies on declaration merging to test. Hopefully the test shows the kind of problem I'm trying to solve with this PR, which is that without the internalType it is impossible to differentiate between structs with different names but matching types after the ABI has been broken down into primitives.

This PR is somewhat outside the scope of what abitype is, so I can understand if this isn't something you would like to add to the project, however it is useful for me and may be useful for others, so I've opened the PR in case that is true.

Thanks for taking a look!


PR-Codex overview

This PR introduces structTypeMatches to Register in abitype, allowing custom struct types to be matched against structs before processing.

Detailed summary

  • Added structTypeMatches to Register in abitype
  • Defined ResolvedStructMatchesMap and ResolvedStructMatchesUnion
  • Updated AbiParameterToPrimitiveType in utils.ts to handle user-defined struct matches

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Copy link

changeset-bot bot commented Aug 27, 2024

🦋 Changeset detected

Latest commit: 5c026b4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
abitype Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Aug 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
abitype ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 28, 2024 7:33pm

@tmm
Copy link
Member

tmm commented Aug 28, 2024

Thanks for the discussion and PR! Haven't had a chance to review or consider this yet, but figured I would provide some quick comments.

The use case is narrow, but allows library authors to create more expressive tools that interact with viem and wagmi.

What's an example of how this would look in Viem/Wagmi?

I've added a new test in register.test-d.ts which unfortunately can only be run locally since it relies on declaration merging to test.

You should be able to add the register tests in packages/register-tests/default/src.

@architect-dev
Copy link
Author

It would allow typings like this when integrated with wagmi hooks:

CleanShot 2024-08-28 at 20 11 07@2x

In this particular case, all of the EncryptedXXXX share the same type, but need to be encrypted differently in order to be handled appropriately by the contract. By hooking into the struct name I can assign a custom type for each that I can discriminate with and improve other engineers typesafety.

As an example, I can imagine a contract where a user is dealing with shapes, and creates something like struct Box {uint8 x, uint8 y, uint8 w, uint8 h} in their contract. A function like addBox(ShapeBox calldata box) would end up in the viem/wagmi args as [{ x: number, y: number, w: number, h: number} | undefined], but they could use this feature and set the ShapeBox struct in the Register to a typescript Box type. As a result, in viem/wagmi the args of addBox would be [Box | undefined]. This use case only improves readability, but it might help explain where I see it having value.

Thanks for the tip on the register-tests I missed that one, I'll add a test in there tomorrow!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants