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

Maybe wrapper for abi decoding #61

Open
cmditch opened this issue Aug 14, 2018 · 0 comments
Open

Maybe wrapper for abi decoding #61

cmditch opened this issue Aug 14, 2018 · 0 comments

Comments

@cmditch
Copy link
Owner

cmditch commented Aug 14, 2018

You might see mappings like this

isAdmin(address => bool)
adminAuthLevel(address => uint)

But they can be combined like so, and represented on the front end with a Maybe

isAdmin(address => uint) // returns the admins auth level, or 0 if is not an admin.

Maybe's tell us not only if something exists, but also what exists if so. It conveys multiple states in one data type. We can convey the information of two maps in one, by cleverly defining 0 as one distinct type of state, and 1..n as another type of state.

type alias AuthLevel = BigInt

isAdmin : AbiDecoder (Maybe AuthLevel)
isAdmin =
    Abi.maybe Abi.uint

Abi.maybe might look something like.

maybe : AbiDecoder a -> AbiDecoder (Maybe a)
maybe decoder =
    AbiDecoder <|
        \returnBlob -> 
            case returnBlob of
                0x000... -> Nothing
                n -> Just (decoder n)
   
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

No branches or pull requests

1 participant