web3-solidity-1.0.1.0: Solidity language for Haskell Web3 library.
CopyrightAleksandr Krupenkin 2016-2024
LicenseApache-2.0
Maintainermail@akru.me
Stabilityexperimental
Portabilitynoportable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Solidity.Abi.Codec

Description

Solidity contract ABI encoding functions.

Synopsis

AbiPut/AbiGet type class encoding

encode :: (AbiPut a, ByteArray ba) => a -> ba Source #

Encode datatype to Ethereum Abi-encoding

decode :: (ByteArrayAccess ba, AbiGet a) => ba -> Either String a Source #

Decode datatype from Ethereum Abi-encoding

Generic encoding

encode' :: (Generic a, Rep a ~ rep, GenericAbiPut rep, ByteArray ba) => a -> ba Source #

Generic driven version of encode

decode' :: (Generic a, Rep a ~ rep, GenericAbiGet rep, ByteArrayAccess ba) => ba -> Either String a Source #

Generic driven version of decode

Generic type re-export

class All (SListI :: [Type] -> Constraint) (Code a) => Generic a #

The class of representable datatypes.

The SOP approach to generic programming is based on viewing datatypes as a representation (Rep) built from the sum of products of its components. The components of a datatype are specified using the Code type family.

The isomorphism between the original Haskell datatype and its representation is witnessed by the methods of this class, from and to. So for instances of this class, the following laws should (in general) hold:

to . from === id :: a -> a
from . to === id :: Rep a -> Rep a

You typically don't define instances of this class by hand, but rather derive the class instance automatically.

Option 1: Derive via the built-in GHC-generics. For this, you need to use the DeriveGeneric extension to first derive an instance of the Generic class from module GHC.Generics. With this, you can then give an empty instance for Generic, and the default definitions will just work. The pattern looks as follows:

import qualified GHC.Generics as GHC
import Generics.SOP

...

data T = ... deriving (GHC.Generic, ...)

instance Generic T -- empty
instance HasDatatypeInfo T -- empty, if you want/need metadata

Option 2: Derive via Template Haskell. For this, you need to enable the TemplateHaskell extension. You can then use deriveGeneric from module Generics.SOP.TH to have the instance generated for you. The pattern looks as follows:

import Generics.SOP
import Generics.SOP.TH

...

data T = ...

deriveGeneric ''T -- derives HasDatatypeInfo as well

Tradeoffs: Whether to use Option 1 or 2 is mainly a matter of personal taste. The version based on Template Haskell probably has less run-time overhead.

Non-standard instances: It is possible to give Generic instances manually that deviate from the standard scheme, as long as at least

to . from === id :: a -> a

still holds.

Instances

Instances details
Generic Address Source # 
Instance details

Defined in Data.Solidity.Prim.Address

Associated Types

type Code Address :: [[Type]] #

Generic (a) Source # 
Instance details

Defined in Data.Solidity.Prim.Tuple

Associated Types

type Code (a) :: [[Type]] #

Methods

from :: (a) -> Rep (a) #

to :: Rep (a) -> (a) #

Generic a => Generic (Tagged t a) Source # 
Instance details

Defined in Data.Solidity.Prim.Tagged

Associated Types

type Code (Tagged t a) :: [[Type]] #

Methods

from :: Tagged t a -> Rep (Tagged t a) #

to :: Rep (Tagged t a) -> Tagged t a #