Giter Site home page Giter Site logo

Comments (8)

tmm avatar tmm commented on May 22, 2024 1

[email protected]

from abitype.

Raiden1411 avatar Raiden1411 commented on May 22, 2024

This is intended behavior. This comes down to the Abi type using the union of literals "pure" | "view" | "nonpayable" | "payable" and while zod does the same at runtime, at the typelevel it infers that as a string type and not their literal values and string extends "anyliteralvalue" is false so you get that type error when trying to assign that type to the type return from the zod schema parse

from abitype.

mathieu-bour avatar mathieu-bour commented on May 22, 2024

The biggest problem is that it makes the Zod extension very difficult to use with abitype itself and viem by extension. I have to make horrible conversions like:

import { Abi } from 'abitype';
import { Abi as AbiSchema } from 'abitype/zod';

const result: Abi = AbiSchema.parse([]) as unknown as Abi;

from abitype.

Raiden1411 avatar Raiden1411 commented on May 22, 2024

But why are you assign the ABI to the zod schema parsed? The zod type should be assignable to function were the args expect the Abi type. Here is an example https://tsplay.dev/w2vD9w

from abitype.

mathieu-bour avatar mathieu-bour commented on May 22, 2024

My goal is to build a platform where users can submit ABIs via a form. To verify that the submitted data is a valid ABI, I am using import { Abi as AbiSchema } from 'abitype/zod'; to check if the ABI is valid. If the ABI is valid, I will then use viem to interact with the contracts.

import { Abi } from 'abitype';
import { Abi as AbiSchema } from 'abitype/zod';

const validAbi: Abi = AbiSchema.parse(maybeNotAbi); // not assignable to Abi

To sum up, since the AbiSchema is provided by abitype/zod, I would expect a parsed zod Abi to be assignable to abitype's Abi.

from abitype.

Raiden1411 avatar Raiden1411 commented on May 22, 2024

You can still do this without having to cast the parsed zod abi to Abi.

Example

Take the example above. Its using viem's getContract function with the zod parsed abi. There is no need here to perform manual casting as this will also works.
Viem docs

However i believed i have found the problem and it should be fixed with #192

from abitype.

mathieu-bour avatar mathieu-bour commented on May 22, 2024

Thanks a lot for pointing this to me. While I fully agree that the provided code works, this issue was more about the fact that the Zod schema was not working with the abitype Abi type. However, since there full compatibility with viem, I would consider this issue as "nice-to-have".

I just checked your PR #192 and it looks like that it fixes the issue!

from abitype.

github-actions avatar github-actions commented on May 22, 2024

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest ABIType version. If you have any other comments you can create a new discussion.

from abitype.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.