{-# LANGUAGE DeriveDataTypeable         #-}
{-# LANGUAGE DeriveGeneric              #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE TemplateHaskell            #-}

-- |
-- Module      :  Network.Ethereum.Api.Types
-- Copyright   :  Aleksandr Krupenkin 2016-2021
-- License     :  Apache-2.0
--
-- Maintainer  :  mail@akru.me
-- Stability   :  experimental
-- Portability :  portable
--
-- Ethereum generic JSON-RPC types.
--

module Network.Ethereum.Api.Types where

import           Data.Aeson                 (FromJSON (..), Options (fieldLabelModifier, omitNothingFields),
                                             ToJSON (..), Value (Bool, String),
                                             defaultOptions, object, (.=))
import           Data.Aeson.TH              (deriveJSON)
import           Data.ByteArray.HexString   (HexString)
import           Data.Char                  (toLower)
import           Data.Default               (Default (..))
import           Data.Solidity.Prim.Address (Address)
import           Data.String                (IsString (..))
import qualified Data.Text                  as T (pack)
import qualified Data.Text.Lazy.Builder     as B (toLazyText)
import qualified Data.Text.Lazy.Builder.Int as B (hexadecimal)
import qualified Data.Text.Read             as R (decimal, hexadecimal)
import           GHC.Generics               (Generic)
import           Lens.Micro                 (over, _head)

-- | Should be viewed as type to representing QUANTITY in Web3 JSON RPC docs
--
--  When encoding QUANTITIES (integers, numbers): encode as hex, prefix with "0x",
--  the most compact representation (slight exception: zero should be represented as "0x0").
--  Examples:
--
--  0x41 (65 in decimal)
--  0x400 (1024 in decimal)
--  WRONG: 0x (should always have at least one digit - zero is "0x0")
--  WRONG: 0x0400 (no leading zeroes allowed)
--  WRONG: ff (must be prefixed 0x)
newtype Quantity = Quantity { Quantity -> Integer
unQuantity :: Integer }
    deriving (Integer -> Quantity
Quantity -> Quantity
Quantity -> Quantity -> Quantity
(Quantity -> Quantity -> Quantity)
-> (Quantity -> Quantity -> Quantity)
-> (Quantity -> Quantity -> Quantity)
-> (Quantity -> Quantity)
-> (Quantity -> Quantity)
-> (Quantity -> Quantity)
-> (Integer -> Quantity)
-> Num Quantity
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> Quantity
$cfromInteger :: Integer -> Quantity
signum :: Quantity -> Quantity
$csignum :: Quantity -> Quantity
abs :: Quantity -> Quantity
$cabs :: Quantity -> Quantity
negate :: Quantity -> Quantity
$cnegate :: Quantity -> Quantity
* :: Quantity -> Quantity -> Quantity
$c* :: Quantity -> Quantity -> Quantity
- :: Quantity -> Quantity -> Quantity
$c- :: Quantity -> Quantity -> Quantity
+ :: Quantity -> Quantity -> Quantity
$c+ :: Quantity -> Quantity -> Quantity
Num, Num Quantity
Ord Quantity
Num Quantity
-> Ord Quantity -> (Quantity -> Rational) -> Real Quantity
Quantity -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: Quantity -> Rational
$ctoRational :: Quantity -> Rational
$cp2Real :: Ord Quantity
$cp1Real :: Num Quantity
Real, Enum Quantity
Real Quantity
Real Quantity
-> Enum Quantity
-> (Quantity -> Quantity -> Quantity)
-> (Quantity -> Quantity -> Quantity)
-> (Quantity -> Quantity -> Quantity)
-> (Quantity -> Quantity -> Quantity)
-> (Quantity -> Quantity -> (Quantity, Quantity))
-> (Quantity -> Quantity -> (Quantity, Quantity))
-> (Quantity -> Integer)
-> Integral Quantity
Quantity -> Integer
Quantity -> Quantity -> (Quantity, Quantity)
Quantity -> Quantity -> Quantity
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: Quantity -> Integer
$ctoInteger :: Quantity -> Integer
divMod :: Quantity -> Quantity -> (Quantity, Quantity)
$cdivMod :: Quantity -> Quantity -> (Quantity, Quantity)
quotRem :: Quantity -> Quantity -> (Quantity, Quantity)
$cquotRem :: Quantity -> Quantity -> (Quantity, Quantity)
mod :: Quantity -> Quantity -> Quantity
$cmod :: Quantity -> Quantity -> Quantity
div :: Quantity -> Quantity -> Quantity
$cdiv :: Quantity -> Quantity -> Quantity
rem :: Quantity -> Quantity -> Quantity
$crem :: Quantity -> Quantity -> Quantity
quot :: Quantity -> Quantity -> Quantity
$cquot :: Quantity -> Quantity -> Quantity
$cp2Integral :: Enum Quantity
$cp1Integral :: Real Quantity
Integral, Int -> Quantity
Quantity -> Int
Quantity -> [Quantity]
Quantity -> Quantity
Quantity -> Quantity -> [Quantity]
Quantity -> Quantity -> Quantity -> [Quantity]
(Quantity -> Quantity)
-> (Quantity -> Quantity)
-> (Int -> Quantity)
-> (Quantity -> Int)
-> (Quantity -> [Quantity])
-> (Quantity -> Quantity -> [Quantity])
-> (Quantity -> Quantity -> [Quantity])
-> (Quantity -> Quantity -> Quantity -> [Quantity])
-> Enum Quantity
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Quantity -> Quantity -> Quantity -> [Quantity]
$cenumFromThenTo :: Quantity -> Quantity -> Quantity -> [Quantity]
enumFromTo :: Quantity -> Quantity -> [Quantity]
$cenumFromTo :: Quantity -> Quantity -> [Quantity]
enumFromThen :: Quantity -> Quantity -> [Quantity]
$cenumFromThen :: Quantity -> Quantity -> [Quantity]
enumFrom :: Quantity -> [Quantity]
$cenumFrom :: Quantity -> [Quantity]
fromEnum :: Quantity -> Int
$cfromEnum :: Quantity -> Int
toEnum :: Int -> Quantity
$ctoEnum :: Int -> Quantity
pred :: Quantity -> Quantity
$cpred :: Quantity -> Quantity
succ :: Quantity -> Quantity
$csucc :: Quantity -> Quantity
Enum, Quantity -> Quantity -> Bool
(Quantity -> Quantity -> Bool)
-> (Quantity -> Quantity -> Bool) -> Eq Quantity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Quantity -> Quantity -> Bool
$c/= :: Quantity -> Quantity -> Bool
== :: Quantity -> Quantity -> Bool
$c== :: Quantity -> Quantity -> Bool
Eq, Eq Quantity
Eq Quantity
-> (Quantity -> Quantity -> Ordering)
-> (Quantity -> Quantity -> Bool)
-> (Quantity -> Quantity -> Bool)
-> (Quantity -> Quantity -> Bool)
-> (Quantity -> Quantity -> Bool)
-> (Quantity -> Quantity -> Quantity)
-> (Quantity -> Quantity -> Quantity)
-> Ord Quantity
Quantity -> Quantity -> Bool
Quantity -> Quantity -> Ordering
Quantity -> Quantity -> Quantity
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Quantity -> Quantity -> Quantity
$cmin :: Quantity -> Quantity -> Quantity
max :: Quantity -> Quantity -> Quantity
$cmax :: Quantity -> Quantity -> Quantity
>= :: Quantity -> Quantity -> Bool
$c>= :: Quantity -> Quantity -> Bool
> :: Quantity -> Quantity -> Bool
$c> :: Quantity -> Quantity -> Bool
<= :: Quantity -> Quantity -> Bool
$c<= :: Quantity -> Quantity -> Bool
< :: Quantity -> Quantity -> Bool
$c< :: Quantity -> Quantity -> Bool
compare :: Quantity -> Quantity -> Ordering
$ccompare :: Quantity -> Quantity -> Ordering
$cp1Ord :: Eq Quantity
Ord, (forall x. Quantity -> Rep Quantity x)
-> (forall x. Rep Quantity x -> Quantity) -> Generic Quantity
forall x. Rep Quantity x -> Quantity
forall x. Quantity -> Rep Quantity x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Quantity x -> Quantity
$cfrom :: forall x. Quantity -> Rep Quantity x
Generic)

instance Show Quantity where
    show :: Quantity -> String
show = Integer -> String
forall a. Show a => a -> String
show (Integer -> String) -> (Quantity -> Integer) -> Quantity -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quantity -> Integer
unQuantity

instance IsString Quantity where
    fromString :: String -> Quantity
fromString (Char
'0' : Char
'x' : String
hex) =
        case Reader Integer
forall a. Integral a => Reader a
R.hexadecimal (String -> Text
T.pack String
hex) of
            Right (Integer
x, Text
"") -> Integer -> Quantity
Quantity Integer
x
            Either String (Integer, Text)
_             -> String -> Quantity
forall a. HasCallStack => String -> a
error (String
"Quantity " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> String
show String
hex String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not valid hex")
    fromString String
num =
        case Reader Integer
forall a. Integral a => Reader a
R.decimal (String -> Text
T.pack String
num) of
            Right (Integer
x, Text
"") -> Integer -> Quantity
Quantity Integer
x
            Either String (Integer, Text)
_             -> String -> Quantity
forall a. HasCallStack => String -> a
error (String
"Quantity " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> String
show String
num String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" is not valid decimal")

instance ToJSON Quantity where
    toJSON :: Quantity -> Value
toJSON (Quantity Integer
x) =
        let hexValue :: Text
hexValue = Builder -> Text
B.toLazyText (Integer -> Builder
forall a. Integral a => a -> Builder
B.hexadecimal Integer
x)
        in  Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Text
"0x" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
hexValue)

instance FromJSON Quantity where
    parseJSON :: Value -> Parser Quantity
parseJSON (String Text
v) =
        case Reader Integer
forall a. Integral a => Reader a
R.hexadecimal Text
v of
            Right (Integer
x, Text
"") -> Quantity -> Parser Quantity
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> Quantity
Quantity Integer
x)
            Either String (Integer, Text)
_             -> String -> Parser Quantity
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser Quantity) -> String -> Parser Quantity
forall a b. (a -> b) -> a -> b
$ String
"Quantity " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a. Show a => a -> String
show Text
v String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" is not valid hex"
    parseJSON Value
_ = String -> Parser Quantity
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Quantity should be a JSON String"

-- | An object with sync status data.
data SyncActive = SyncActive
    { SyncActive -> Quantity
syncStartingBlock :: !Quantity
    -- ^ QUANTITY - The block at which the import started (will only be reset, after the sync reached his head).
    , SyncActive -> Quantity
syncCurrentBlock  :: !Quantity
    -- ^ QUANTITY - The current block, same as eth_blockNumber.
    , SyncActive -> Quantity
syncHighestBlock  :: !Quantity
    -- ^ QUANTITY - The estimated highest block.
    }
    deriving (SyncActive -> SyncActive -> Bool
(SyncActive -> SyncActive -> Bool)
-> (SyncActive -> SyncActive -> Bool) -> Eq SyncActive
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SyncActive -> SyncActive -> Bool
$c/= :: SyncActive -> SyncActive -> Bool
== :: SyncActive -> SyncActive -> Bool
$c== :: SyncActive -> SyncActive -> Bool
Eq, (forall x. SyncActive -> Rep SyncActive x)
-> (forall x. Rep SyncActive x -> SyncActive) -> Generic SyncActive
forall x. Rep SyncActive x -> SyncActive
forall x. SyncActive -> Rep SyncActive x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep SyncActive x -> SyncActive
$cfrom :: forall x. SyncActive -> Rep SyncActive x
Generic, Int -> SyncActive -> ShowS
[SyncActive] -> ShowS
SyncActive -> String
(Int -> SyncActive -> ShowS)
-> (SyncActive -> String)
-> ([SyncActive] -> ShowS)
-> Show SyncActive
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SyncActive] -> ShowS
$cshowList :: [SyncActive] -> ShowS
show :: SyncActive -> String
$cshow :: SyncActive -> String
showsPrec :: Int -> SyncActive -> ShowS
$cshowsPrec :: Int -> SyncActive -> ShowS
Show)

$(deriveJSON (defaultOptions
    { fieldLabelModifier = over _head toLower . drop 4 }) ''SyncActive)

-- | Sync state pulled by low-level call 'eth_syncing'.
data SyncingState = Syncing SyncActive
    | NotSyncing
    deriving (SyncingState -> SyncingState -> Bool
(SyncingState -> SyncingState -> Bool)
-> (SyncingState -> SyncingState -> Bool) -> Eq SyncingState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SyncingState -> SyncingState -> Bool
$c/= :: SyncingState -> SyncingState -> Bool
== :: SyncingState -> SyncingState -> Bool
$c== :: SyncingState -> SyncingState -> Bool
Eq, (forall x. SyncingState -> Rep SyncingState x)
-> (forall x. Rep SyncingState x -> SyncingState)
-> Generic SyncingState
forall x. Rep SyncingState x -> SyncingState
forall x. SyncingState -> Rep SyncingState x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep SyncingState x -> SyncingState
$cfrom :: forall x. SyncingState -> Rep SyncingState x
Generic, Int -> SyncingState -> ShowS
[SyncingState] -> ShowS
SyncingState -> String
(Int -> SyncingState -> ShowS)
-> (SyncingState -> String)
-> ([SyncingState] -> ShowS)
-> Show SyncingState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SyncingState] -> ShowS
$cshowList :: [SyncingState] -> ShowS
show :: SyncingState -> String
$cshow :: SyncingState -> String
showsPrec :: Int -> SyncingState -> ShowS
$cshowsPrec :: Int -> SyncingState -> ShowS
Show)

instance FromJSON SyncingState where
    parseJSON :: Value -> Parser SyncingState
parseJSON (Bool Bool
_) = SyncingState -> Parser SyncingState
forall (f :: * -> *) a. Applicative f => a -> f a
pure SyncingState
NotSyncing
    parseJSON Value
v        = SyncActive -> SyncingState
Syncing (SyncActive -> SyncingState)
-> Parser SyncActive -> Parser SyncingState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser SyncActive
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v

-- | Changes pulled by low-level call 'eth_getFilterChanges', 'eth_getLogs',
-- and 'eth_getFilterLogs'
data Change = Change
    { Change -> Maybe Quantity
changeLogIndex         :: !(Maybe Quantity)
    -- ^ QUANTITY - integer of the log index position in the block. null when its pending log.
    , Change -> Maybe Quantity
changeTransactionIndex :: !(Maybe Quantity)
    -- ^ QUANTITY - integer of the transactions index position log was created from. null when its pending log.
    , Change -> Maybe HexString
changeTransactionHash  :: !(Maybe HexString)
    -- ^ DATA, 32 Bytes - hash of the transactions this log was created from. null when its pending log.
    , Change -> Maybe HexString
changeBlockHash        :: !(Maybe HexString)
    -- ^ DATA, 32 Bytes - hash of the block where this log was in. null when its pending. null when its pending log.
    , Change -> Maybe Quantity
changeBlockNumber      :: !(Maybe Quantity)
    -- ^ QUANTITY - the block number where this log was in. null when its pending. null when its pending log.
    , Change -> Address
changeAddress          :: !Address
    -- ^ DATA, 20 Bytes - address from which this log originated.
    , Change -> HexString
changeData             :: !HexString
    -- ^ DATA - contains one or more 32 Bytes non-indexed arguments of the log.
    , Change -> [HexString]
changeTopics           :: ![HexString]
    -- ^ Array of DATA - Array of 0 to 4 32 Bytes DATA of indexed log arguments.
    }
    deriving (Change -> Change -> Bool
(Change -> Change -> Bool)
-> (Change -> Change -> Bool) -> Eq Change
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Change -> Change -> Bool
$c/= :: Change -> Change -> Bool
== :: Change -> Change -> Bool
$c== :: Change -> Change -> Bool
Eq, Int -> Change -> ShowS
[Change] -> ShowS
Change -> String
(Int -> Change -> ShowS)
-> (Change -> String) -> ([Change] -> ShowS) -> Show Change
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Change] -> ShowS
$cshowList :: [Change] -> ShowS
show :: Change -> String
$cshow :: Change -> String
showsPrec :: Int -> Change -> ShowS
$cshowsPrec :: Int -> Change -> ShowS
Show, (forall x. Change -> Rep Change x)
-> (forall x. Rep Change x -> Change) -> Generic Change
forall x. Rep Change x -> Change
forall x. Change -> Rep Change x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Change x -> Change
$cfrom :: forall x. Change -> Rep Change x
Generic)

$(deriveJSON (defaultOptions
    { fieldLabelModifier = over _head toLower . drop 6 }) ''Change)

-- | The contract call params.
data Call = Call
    { Call -> Maybe Address
callFrom     :: !(Maybe Address)
    -- ^ DATA, 20 Bytes - The address the transaction is send from.
    , Call -> Maybe Address
callTo       :: !(Maybe Address)
    -- ^ DATA, 20 Bytes - (optional when creating new contract) The address the transaction is directed to.
    , Call -> Maybe Quantity
callGas      :: !(Maybe Quantity)
    -- ^ QUANTITY - (optional, default: 3000000) Integer of the gas provided for the transaction execution. It will return unused gas.
    , Call -> Maybe Quantity
callGasPrice :: !(Maybe Quantity)
    -- ^ QUANTITY - (optional, default: To-Be-Determined) Integer of the gasPrice used for each paid gas.
    , Call -> Maybe Quantity
callValue    :: !(Maybe Quantity)
    -- ^ QUANTITY - (optional) Integer of the value sent with this transaction.
    , Call -> Maybe HexString
callData     :: !(Maybe HexString)
    -- ^ DATA - The compiled code of a contract OR the hash of the invoked method signature and encoded parameters.
    , Call -> Maybe Quantity
callNonce    :: !(Maybe Quantity)
    -- ^ QUANTITY - (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.
    }
    deriving (Call -> Call -> Bool
(Call -> Call -> Bool) -> (Call -> Call -> Bool) -> Eq Call
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Call -> Call -> Bool
$c/= :: Call -> Call -> Bool
== :: Call -> Call -> Bool
$c== :: Call -> Call -> Bool
Eq, Int -> Call -> ShowS
[Call] -> ShowS
Call -> String
(Int -> Call -> ShowS)
-> (Call -> String) -> ([Call] -> ShowS) -> Show Call
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Call] -> ShowS
$cshowList :: [Call] -> ShowS
show :: Call -> String
$cshow :: Call -> String
showsPrec :: Int -> Call -> ShowS
$cshowsPrec :: Int -> Call -> ShowS
Show, (forall x. Call -> Rep Call x)
-> (forall x. Rep Call x -> Call) -> Generic Call
forall x. Rep Call x -> Call
forall x. Call -> Rep Call x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Call x -> Call
$cfrom :: forall x. Call -> Rep Call x
Generic)

$(deriveJSON (defaultOptions
    { fieldLabelModifier = over _head toLower . drop 4
    , omitNothingFields = True }) ''Call)

instance Default Call where
    def :: Call
def = Maybe Address
-> Maybe Address
-> Maybe Quantity
-> Maybe Quantity
-> Maybe Quantity
-> Maybe HexString
-> Maybe Quantity
-> Call
Call Maybe Address
forall a. Maybe a
Nothing Maybe Address
forall a. Maybe a
Nothing (Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
3000000) Maybe Quantity
forall a. Maybe a
Nothing (Quantity -> Maybe Quantity
forall a. a -> Maybe a
Just Quantity
0) Maybe HexString
forall a. Maybe a
Nothing Maybe Quantity
forall a. Maybe a
Nothing

-- | The state of blockchain for contract call.
data DefaultBlock = BlockWithNumber Quantity
    | Earliest
    | Latest
    | Pending
    deriving (DefaultBlock -> DefaultBlock -> Bool
(DefaultBlock -> DefaultBlock -> Bool)
-> (DefaultBlock -> DefaultBlock -> Bool) -> Eq DefaultBlock
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DefaultBlock -> DefaultBlock -> Bool
$c/= :: DefaultBlock -> DefaultBlock -> Bool
== :: DefaultBlock -> DefaultBlock -> Bool
$c== :: DefaultBlock -> DefaultBlock -> Bool
Eq, Int -> DefaultBlock -> ShowS
[DefaultBlock] -> ShowS
DefaultBlock -> String
(Int -> DefaultBlock -> ShowS)
-> (DefaultBlock -> String)
-> ([DefaultBlock] -> ShowS)
-> Show DefaultBlock
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DefaultBlock] -> ShowS
$cshowList :: [DefaultBlock] -> ShowS
show :: DefaultBlock -> String
$cshow :: DefaultBlock -> String
showsPrec :: Int -> DefaultBlock -> ShowS
$cshowsPrec :: Int -> DefaultBlock -> ShowS
Show, (forall x. DefaultBlock -> Rep DefaultBlock x)
-> (forall x. Rep DefaultBlock x -> DefaultBlock)
-> Generic DefaultBlock
forall x. Rep DefaultBlock x -> DefaultBlock
forall x. DefaultBlock -> Rep DefaultBlock x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep DefaultBlock x -> DefaultBlock
$cfrom :: forall x. DefaultBlock -> Rep DefaultBlock x
Generic)

instance ToJSON DefaultBlock where
    toJSON :: DefaultBlock -> Value
toJSON (BlockWithNumber Quantity
bn) = Quantity -> Value
forall a. ToJSON a => a -> Value
toJSON Quantity
bn
    toJSON DefaultBlock
parameter            = String -> Value
forall a. ToJSON a => a -> Value
toJSON (String -> Value)
-> (DefaultBlock -> String) -> DefaultBlock -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter String String Char Char -> (Char -> Char) -> ShowS
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter String String Char Char
forall s a. Cons s s a a => Traversal' s a
_head Char -> Char
toLower ShowS -> (DefaultBlock -> String) -> DefaultBlock -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DefaultBlock -> String
forall a. Show a => a -> String
show (DefaultBlock -> Value) -> DefaultBlock -> Value
forall a b. (a -> b) -> a -> b
$ DefaultBlock
parameter

-- | Low-level event filter data structure.
data Filter e = Filter
    { Filter e -> Maybe [Address]
filterAddress   :: !(Maybe [Address])
    -- ^ DATA|Array, 20 Bytes - (optional) Contract address or a list of addresses from which logs should originate.
    , Filter e -> DefaultBlock
filterFromBlock :: !DefaultBlock
    -- ^ QUANTITY|TAG - (optional, default: "latest") Integer block number, or "latest" for the last mined block or "pending", "earliest" for not yet mined transactions.
    , Filter e -> DefaultBlock
filterToBlock   :: !DefaultBlock
    -- ^ QUANTITY|TAG - (optional, default: "latest") Integer block number, or "latest" for the last mined block or "pending", "earliest" for not yet mined transactions.
    , Filter e -> Maybe [Maybe HexString]
filterTopics    :: !(Maybe [Maybe HexString])
    -- ^ Array of DATA, - (optional) Array of 32 Bytes DATA topics. Topics are order-dependent. Each topic can also be an array of DATA with "or" options.
    }
    deriving (Filter e -> Filter e -> Bool
(Filter e -> Filter e -> Bool)
-> (Filter e -> Filter e -> Bool) -> Eq (Filter e)
forall e. Filter e -> Filter e -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Filter e -> Filter e -> Bool
$c/= :: forall e. Filter e -> Filter e -> Bool
== :: Filter e -> Filter e -> Bool
$c== :: forall e. Filter e -> Filter e -> Bool
Eq, Int -> Filter e -> ShowS
[Filter e] -> ShowS
Filter e -> String
(Int -> Filter e -> ShowS)
-> (Filter e -> String) -> ([Filter e] -> ShowS) -> Show (Filter e)
forall e. Int -> Filter e -> ShowS
forall e. [Filter e] -> ShowS
forall e. Filter e -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Filter e] -> ShowS
$cshowList :: forall e. [Filter e] -> ShowS
show :: Filter e -> String
$cshow :: forall e. Filter e -> String
showsPrec :: Int -> Filter e -> ShowS
$cshowsPrec :: forall e. Int -> Filter e -> ShowS
Show, (forall x. Filter e -> Rep (Filter e) x)
-> (forall x. Rep (Filter e) x -> Filter e) -> Generic (Filter e)
forall x. Rep (Filter e) x -> Filter e
forall x. Filter e -> Rep (Filter e) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall e x. Rep (Filter e) x -> Filter e
forall e x. Filter e -> Rep (Filter e) x
$cto :: forall e x. Rep (Filter e) x -> Filter e
$cfrom :: forall e x. Filter e -> Rep (Filter e) x
Generic)

instance ToJSON (Filter e) where
    toJSON :: Filter e -> Value
toJSON Filter e
f = [Pair] -> Value
object [ Text
"address"   Text -> Maybe [Address] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Filter e -> Maybe [Address]
forall e. Filter e -> Maybe [Address]
filterAddress Filter e
f
                      , Text
"fromBlock" Text -> DefaultBlock -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Filter e -> DefaultBlock
forall e. Filter e -> DefaultBlock
filterFromBlock Filter e
f
                      , Text
"toBlock"   Text -> DefaultBlock -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Filter e -> DefaultBlock
forall e. Filter e -> DefaultBlock
filterToBlock Filter e
f
                      , Text
"topics"    Text -> Maybe [Maybe HexString] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Filter e -> Maybe [Maybe HexString]
forall e. Filter e -> Maybe [Maybe HexString]
filterTopics Filter e
f ]

instance Ord DefaultBlock where
    compare :: DefaultBlock -> DefaultBlock -> Ordering
compare DefaultBlock
Pending DefaultBlock
Pending                         = Ordering
EQ
    compare DefaultBlock
Latest DefaultBlock
Latest                           = Ordering
EQ
    compare DefaultBlock
Earliest DefaultBlock
Earliest                       = Ordering
EQ
    compare (BlockWithNumber Quantity
a) (BlockWithNumber Quantity
b) = Quantity -> Quantity -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Quantity
a Quantity
b
    compare DefaultBlock
_ DefaultBlock
Pending                               = Ordering
LT
    compare DefaultBlock
Pending DefaultBlock
Latest                          = Ordering
GT
    compare DefaultBlock
_ DefaultBlock
Latest                                = Ordering
LT
    compare DefaultBlock
Earliest DefaultBlock
_                              = Ordering
LT
    compare DefaultBlock
a DefaultBlock
b                                     = case DefaultBlock -> DefaultBlock -> Ordering
forall a. Ord a => a -> a -> Ordering
compare DefaultBlock
b DefaultBlock
a of
                                                        Ordering
LT -> Ordering
GT
                                                        Ordering
GT -> Ordering
LT
                                                        Ordering
EQ -> Ordering
EQ

-- | The Receipt of a Transaction
data TxReceipt = TxReceipt
    { TxReceipt -> HexString
receiptTransactionHash   :: !HexString
    -- ^ DATA, 32 Bytes - hash of the transaction.
    , TxReceipt -> Quantity
receiptTransactionIndex  :: !Quantity
    -- ^ QUANTITY - index of the transaction.
    , TxReceipt -> Maybe HexString
receiptBlockHash         :: !(Maybe HexString)
    -- ^ DATA, 32 Bytes - hash of the block where this transaction was in. null when its pending.
    , TxReceipt -> Maybe Quantity
receiptBlockNumber       :: !(Maybe Quantity)
    -- ^ QUANTITY - block number where this transaction was in. null when its pending.
    , TxReceipt -> Quantity
receiptCumulativeGasUsed :: !Quantity
    -- ^ QUANTITY - The total amount of gas used when this transaction was executed in the block.
    , TxReceipt -> Quantity
receiptGasUsed           :: !Quantity
    -- ^ QUANTITY - The amount of gas used by this specific transaction alone.
    , TxReceipt -> Maybe Address
receiptContractAddress   :: !(Maybe Address)
    -- ^ DATA, 20 Bytes - The contract address created, if the transaction was a contract creation, otherwise null.
    , TxReceipt -> [Change]
receiptLogs              :: ![Change]
    -- ^ Array - Array of log objects, which this transaction generated.
    , TxReceipt -> HexString
receiptLogsBloom         :: !HexString
    -- ^ DATA, 256 Bytes - Bloom filter for light clients to quickly retrieve related logs.
    , TxReceipt -> Maybe Quantity
receiptStatus            :: !(Maybe Quantity)
    -- ^ QUANTITY either 1 (success) or 0 (failure)
    }
    deriving (Int -> TxReceipt -> ShowS
[TxReceipt] -> ShowS
TxReceipt -> String
(Int -> TxReceipt -> ShowS)
-> (TxReceipt -> String)
-> ([TxReceipt] -> ShowS)
-> Show TxReceipt
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxReceipt] -> ShowS
$cshowList :: [TxReceipt] -> ShowS
show :: TxReceipt -> String
$cshow :: TxReceipt -> String
showsPrec :: Int -> TxReceipt -> ShowS
$cshowsPrec :: Int -> TxReceipt -> ShowS
Show, (forall x. TxReceipt -> Rep TxReceipt x)
-> (forall x. Rep TxReceipt x -> TxReceipt) -> Generic TxReceipt
forall x. Rep TxReceipt x -> TxReceipt
forall x. TxReceipt -> Rep TxReceipt x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxReceipt x -> TxReceipt
$cfrom :: forall x. TxReceipt -> Rep TxReceipt x
Generic)

$(deriveJSON (defaultOptions
    { fieldLabelModifier = over _head toLower . drop 7 }) ''TxReceipt)

-- | Transaction information.
data Transaction = Transaction
    { Transaction -> HexString
txHash             :: !HexString
    -- ^ DATA, 32 Bytes - hash of the transaction.
    , Transaction -> Quantity
txNonce            :: !Quantity
    -- ^ QUANTITY - the number of transactions made by the sender prior to this one.
    , Transaction -> Maybe HexString
txBlockHash        :: !(Maybe HexString)
    -- ^ DATA, 32 Bytes - hash of the block where this transaction was in. null when its pending.
    , Transaction -> Maybe Quantity
txBlockNumber      :: !(Maybe Quantity)
    -- ^ QUANTITY - block number where this transaction was in. null when its pending.
    , Transaction -> Maybe Quantity
txTransactionIndex :: !(Maybe Quantity)
    -- ^ QUANTITY - integer of the transactions index position in the block. null when its pending.
    , Transaction -> Address
txFrom             :: !Address
    -- ^ DATA, 20 Bytes - address of the sender.
    , Transaction -> Maybe Address
txTo               :: !(Maybe Address)
    -- ^ DATA, 20 Bytes - address of the receiver. null when its a contract creation transaction.
    , Transaction -> Quantity
txValue            :: !Quantity
    -- ^ QUANTITY - value transferred in Wei.
    , Transaction -> Quantity
txGasPrice         :: !Quantity
    -- ^ QUANTITY - gas price provided by the sender in Wei.
    , Transaction -> Quantity
txGas              :: !Quantity
    -- ^ QUANTITY - gas provided by the sender.
    , Transaction -> HexString
txInput            :: !HexString
    -- ^ DATA - the data send along with the transaction.
    }
    deriving (Transaction -> Transaction -> Bool
(Transaction -> Transaction -> Bool)
-> (Transaction -> Transaction -> Bool) -> Eq Transaction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Transaction -> Transaction -> Bool
$c/= :: Transaction -> Transaction -> Bool
== :: Transaction -> Transaction -> Bool
$c== :: Transaction -> Transaction -> Bool
Eq, Int -> Transaction -> ShowS
[Transaction] -> ShowS
Transaction -> String
(Int -> Transaction -> ShowS)
-> (Transaction -> String)
-> ([Transaction] -> ShowS)
-> Show Transaction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Transaction] -> ShowS
$cshowList :: [Transaction] -> ShowS
show :: Transaction -> String
$cshow :: Transaction -> String
showsPrec :: Int -> Transaction -> ShowS
$cshowsPrec :: Int -> Transaction -> ShowS
Show, (forall x. Transaction -> Rep Transaction x)
-> (forall x. Rep Transaction x -> Transaction)
-> Generic Transaction
forall x. Rep Transaction x -> Transaction
forall x. Transaction -> Rep Transaction x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Transaction x -> Transaction
$cfrom :: forall x. Transaction -> Rep Transaction x
Generic)

$(deriveJSON (defaultOptions
    { fieldLabelModifier = over _head toLower . drop 2 }) ''Transaction)

-- | Block information with full transactions.
type Block = BlockT Transaction

-- | Block information.
-- It is parameterized by the type of transactions stored in the block because
-- sometimes only TX hashes may be stored.
data BlockT tx = Block
    { BlockT tx -> Maybe Quantity
blockNumber           :: !(Maybe Quantity)
    -- ^ QUANTITY - the block number. null when its pending block.
    , BlockT tx -> Maybe HexString
blockHash             :: !(Maybe HexString)
    -- ^ DATA, 32 Bytes - hash of the block. null when its pending block.
    , BlockT tx -> HexString
blockParentHash       :: !HexString
    -- ^ DATA, 32 Bytes - hash of the parent block.
    , BlockT tx -> Maybe HexString
blockNonce            :: !(Maybe HexString)
    -- ^ DATA, 8 Bytes - hash of the generated proof-of-work. null when its pending block.
    , BlockT tx -> HexString
blockSha3Uncles       :: !HexString
    -- ^ DATA, 32 Bytes - SHA3 of the uncles data in the block.
    , BlockT tx -> Maybe HexString
blockLogsBloom        :: !(Maybe HexString)
    -- ^ DATA, 256 Bytes - the bloom filter for the logs of the block. null when its pending block.
    , BlockT tx -> HexString
blockTransactionsRoot :: !HexString
    -- ^ DATA, 32 Bytes - the root of the transaction trie of the block.
    , BlockT tx -> HexString
blockStateRoot        :: !HexString
    -- ^ DATA, 32 Bytes - the root of the final state trie of the block.
    , BlockT tx -> Maybe HexString
blockReceiptsRoot     :: !(Maybe HexString)
    -- ^ DATA, 32 Bytes - the root of the receipts trie of the block.
    , BlockT tx -> Address
blockMiner            :: !Address
    -- ^ DATA, 20 Bytes - the address of the beneficiary to whom the mining rewards were given.
    , BlockT tx -> Quantity
blockDifficulty       :: !Quantity
    -- ^ QUANTITY - integer of the difficulty for this block.
    , BlockT tx -> Quantity
blockTotalDifficulty  :: !Quantity
    -- ^ QUANTITY - integer of the total difficulty of the chain until this block.
    , BlockT tx -> HexString
blockExtraData        :: !HexString
    -- ^ DATA - the "extra data" field of this block.
    , BlockT tx -> Quantity
blockSize             :: !Quantity
    -- ^ QUANTITY - integer the size of this block in bytes.
    , BlockT tx -> Quantity
blockGasLimit         :: !Quantity
    -- ^ QUANTITY - the maximum gas allowed in this block.
    , BlockT tx -> Quantity
blockGasUsed          :: !Quantity
    -- ^ QUANTITY - the total used gas by all transactions in this block.
    , BlockT tx -> Quantity
blockTimestamp        :: !Quantity
    -- ^ QUANTITY - the unix timestamp for when the block was collated.
    , BlockT tx -> [tx]
blockTransactions     :: ![tx]
    -- ^ Array of transaction objects.
    , BlockT tx -> [HexString]
blockUncles           :: ![HexString]
    -- ^ Array - Array of uncle hashes.
    }
    deriving (Int -> BlockT tx -> ShowS
[BlockT tx] -> ShowS
BlockT tx -> String
(Int -> BlockT tx -> ShowS)
-> (BlockT tx -> String)
-> ([BlockT tx] -> ShowS)
-> Show (BlockT tx)
forall tx. Show tx => Int -> BlockT tx -> ShowS
forall tx. Show tx => [BlockT tx] -> ShowS
forall tx. Show tx => BlockT tx -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BlockT tx] -> ShowS
$cshowList :: forall tx. Show tx => [BlockT tx] -> ShowS
show :: BlockT tx -> String
$cshow :: forall tx. Show tx => BlockT tx -> String
showsPrec :: Int -> BlockT tx -> ShowS
$cshowsPrec :: forall tx. Show tx => Int -> BlockT tx -> ShowS
Show, (forall x. BlockT tx -> Rep (BlockT tx) x)
-> (forall x. Rep (BlockT tx) x -> BlockT tx)
-> Generic (BlockT tx)
forall x. Rep (BlockT tx) x -> BlockT tx
forall x. BlockT tx -> Rep (BlockT tx) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall tx x. Rep (BlockT tx) x -> BlockT tx
forall tx x. BlockT tx -> Rep (BlockT tx) x
$cto :: forall tx x. Rep (BlockT tx) x -> BlockT tx
$cfrom :: forall tx x. BlockT tx -> Rep (BlockT tx) x
Generic)

$(deriveJSON (defaultOptions
    { fieldLabelModifier = over _head toLower . drop 5 }) ''BlockT)