Skip to content

Maybe wrapper for abi decoding #61

@cmditch

Description

@cmditch

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)
   

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions