{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
module Cardano.Ledger.State.ChainAccount (
CanGetChainAccountState (..),
CanSetChainAccountState (..),
ChainAccountState (AccountState, asTreasury, asReserves, ..),
AccountState,
casTreasuryL,
casReservesL,
treasuryL,
reservesL,
) where
import Cardano.Ledger.Binary
import Cardano.Ledger.Coin
import Control.DeepSeq (NFData)
import Data.Aeson (KeyValue, ToJSON (..), object, pairs, (.=))
import Data.Default (Default (def))
import GHC.Generics (Generic)
import Lens.Micro
import NoThunks.Class (NoThunks)
class CanGetChainAccountState t where
chainAccountStateG :: SimpleGetter (t era) ChainAccountState
default chainAccountStateG :: CanSetChainAccountState t => SimpleGetter (t era) ChainAccountState
chainAccountStateG = (ChainAccountState -> Const r ChainAccountState)
-> t era -> Const r (t era)
forall era. Lens' (t era) ChainAccountState
forall (t :: * -> *) era.
CanSetChainAccountState t =>
Lens' (t era) ChainAccountState
chainAccountStateL
{-# INLINE chainAccountStateG #-}
class CanGetChainAccountState t => CanSetChainAccountState t where
chainAccountStateL :: Lens' (t era) ChainAccountState
type AccountState = ChainAccountState
pattern AccountState :: Coin -> Coin -> AccountState
pattern $mAccountState :: forall {r}.
ChainAccountState -> (Coin -> Coin -> r) -> ((# #) -> r) -> r
$bAccountState :: Coin -> Coin -> ChainAccountState
AccountState {ChainAccountState -> Coin
asTreasury, ChainAccountState -> Coin
asReserves} = ChainAccountState asTreasury asReserves
{-# DEPRECATED AccountState "In favor of `ChainAccountState`" #-}
{-# DEPRECATED asTreasury "In favor of `casTreasury`" #-}
{-# DEPRECATED asReserves "In favor of `casReserves`" #-}
data ChainAccountState = ChainAccountState
{ ChainAccountState -> Coin
casTreasury :: !Coin
, ChainAccountState -> Coin
casReserves :: !Coin
}
deriving (Int -> ChainAccountState -> ShowS
[ChainAccountState] -> ShowS
ChainAccountState -> String
(Int -> ChainAccountState -> ShowS)
-> (ChainAccountState -> String)
-> ([ChainAccountState] -> ShowS)
-> Show ChainAccountState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChainAccountState -> ShowS
showsPrec :: Int -> ChainAccountState -> ShowS
$cshow :: ChainAccountState -> String
show :: ChainAccountState -> String
$cshowList :: [ChainAccountState] -> ShowS
showList :: [ChainAccountState] -> ShowS
Show, ChainAccountState -> ChainAccountState -> Bool
(ChainAccountState -> ChainAccountState -> Bool)
-> (ChainAccountState -> ChainAccountState -> Bool)
-> Eq ChainAccountState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChainAccountState -> ChainAccountState -> Bool
== :: ChainAccountState -> ChainAccountState -> Bool
$c/= :: ChainAccountState -> ChainAccountState -> Bool
/= :: ChainAccountState -> ChainAccountState -> Bool
Eq, (forall x. ChainAccountState -> Rep ChainAccountState x)
-> (forall x. Rep ChainAccountState x -> ChainAccountState)
-> Generic ChainAccountState
forall x. Rep ChainAccountState x -> ChainAccountState
forall x. ChainAccountState -> Rep ChainAccountState x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ChainAccountState -> Rep ChainAccountState x
from :: forall x. ChainAccountState -> Rep ChainAccountState x
$cto :: forall x. Rep ChainAccountState x -> ChainAccountState
to :: forall x. Rep ChainAccountState x -> ChainAccountState
Generic)
instance EncCBOR ChainAccountState where
encCBOR :: ChainAccountState -> Encoding
encCBOR (ChainAccountState Coin
t Coin
r) =
Word -> Encoding
encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Coin -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR Coin
t Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Coin -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR Coin
r
instance DecCBOR ChainAccountState where
decCBOR :: forall s. Decoder s ChainAccountState
decCBOR =
Text
-> (ChainAccountState -> Int)
-> Decoder s ChainAccountState
-> Decoder s ChainAccountState
forall a s. Text -> (a -> Int) -> Decoder s a -> Decoder s a
decodeRecordNamed Text
"ChainAccountState" (Int -> ChainAccountState -> Int
forall a b. a -> b -> a
const Int
2) (Decoder s ChainAccountState -> Decoder s ChainAccountState)
-> Decoder s ChainAccountState -> Decoder s ChainAccountState
forall a b. (a -> b) -> a -> b
$ Coin -> Coin -> ChainAccountState
ChainAccountState (Coin -> Coin -> ChainAccountState)
-> Decoder s Coin -> Decoder s (Coin -> ChainAccountState)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Coin
forall s. Decoder s Coin
forall a s. DecCBOR a => Decoder s a
decCBOR Decoder s (Coin -> ChainAccountState)
-> Decoder s Coin -> Decoder s ChainAccountState
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Coin
forall s. Decoder s Coin
forall a s. DecCBOR a => Decoder s a
decCBOR
instance ToJSON ChainAccountState where
toJSON :: ChainAccountState -> Value
toJSON = [Pair] -> Value
object ([Pair] -> Value)
-> (ChainAccountState -> [Pair]) -> ChainAccountState -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChainAccountState -> [Pair]
forall e a. KeyValue e a => ChainAccountState -> [a]
toChainAccountStatePairs
toEncoding :: ChainAccountState -> Encoding
toEncoding = Series -> Encoding
pairs (Series -> Encoding)
-> (ChainAccountState -> Series) -> ChainAccountState -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Series] -> Series
forall a. Monoid a => [a] -> a
mconcat ([Series] -> Series)
-> (ChainAccountState -> [Series]) -> ChainAccountState -> Series
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChainAccountState -> [Series]
forall e a. KeyValue e a => ChainAccountState -> [a]
toChainAccountStatePairs
toChainAccountStatePairs :: KeyValue e a => ChainAccountState -> [a]
toChainAccountStatePairs :: forall e a. KeyValue e a => ChainAccountState -> [a]
toChainAccountStatePairs as :: ChainAccountState
as@(ChainAccountState Coin
_ Coin
_) =
let ChainAccountState {Coin
casTreasury :: ChainAccountState -> Coin
casTreasury :: Coin
casTreasury, Coin
casReserves :: ChainAccountState -> Coin
casReserves :: Coin
casReserves} = ChainAccountState
as
in [ Key
"treasury" Key -> Coin -> a
forall v. ToJSON v => Key -> v -> a
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Coin
casTreasury
, Key
"reserves" Key -> Coin -> a
forall v. ToJSON v => Key -> v -> a
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Coin
casReserves
]
instance NoThunks ChainAccountState
instance NFData ChainAccountState
instance Default ChainAccountState where
def :: ChainAccountState
def = Coin -> Coin -> ChainAccountState
ChainAccountState (Integer -> Coin
Coin Integer
0) (Integer -> Coin
Coin Integer
0)
casTreasuryL :: Lens' ChainAccountState Coin
casTreasuryL :: Lens' ChainAccountState Coin
casTreasuryL = (ChainAccountState -> Coin)
-> (ChainAccountState -> Coin -> ChainAccountState)
-> Lens' ChainAccountState Coin
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens ChainAccountState -> Coin
casTreasury (\ChainAccountState
ds Coin
u -> ChainAccountState
ds {casTreasury = u})
{-# INLINE casTreasuryL #-}
casReservesL :: Lens' ChainAccountState Coin
casReservesL :: Lens' ChainAccountState Coin
casReservesL = (ChainAccountState -> Coin)
-> (ChainAccountState -> Coin -> ChainAccountState)
-> Lens' ChainAccountState Coin
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens ChainAccountState -> Coin
casReserves (\ChainAccountState
ds Coin
u -> ChainAccountState
ds {casReserves = u})
{-# INLINE casReservesL #-}
treasuryL :: CanSetChainAccountState t => Lens' (t era) Coin
treasuryL :: forall (t :: * -> *) era.
CanSetChainAccountState t =>
Lens' (t era) Coin
treasuryL = (ChainAccountState -> f ChainAccountState) -> t era -> f (t era)
forall era. Lens' (t era) ChainAccountState
forall (t :: * -> *) era.
CanSetChainAccountState t =>
Lens' (t era) ChainAccountState
chainAccountStateL ((ChainAccountState -> f ChainAccountState) -> t era -> f (t era))
-> ((Coin -> f Coin) -> ChainAccountState -> f ChainAccountState)
-> (Coin -> f Coin)
-> t era
-> f (t era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ChainAccountState -> Coin)
-> (ChainAccountState -> Coin -> ChainAccountState)
-> Lens' ChainAccountState Coin
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens ChainAccountState -> Coin
casTreasury (\ChainAccountState
ds Coin
u -> ChainAccountState
ds {casTreasury = u})
{-# INLINE treasuryL #-}
reservesL :: CanSetChainAccountState t => Lens' (t era) Coin
reservesL :: forall (t :: * -> *) era.
CanSetChainAccountState t =>
Lens' (t era) Coin
reservesL = (ChainAccountState -> f ChainAccountState) -> t era -> f (t era)
forall era. Lens' (t era) ChainAccountState
forall (t :: * -> *) era.
CanSetChainAccountState t =>
Lens' (t era) ChainAccountState
chainAccountStateL ((ChainAccountState -> f ChainAccountState) -> t era -> f (t era))
-> ((Coin -> f Coin) -> ChainAccountState -> f ChainAccountState)
-> (Coin -> f Coin)
-> t era
-> f (t era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ChainAccountState -> Coin)
-> (ChainAccountState -> Coin -> ChainAccountState)
-> Lens' ChainAccountState Coin
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens ChainAccountState -> Coin
casReserves (\ChainAccountState
ds Coin
u -> ChainAccountState
ds {casReserves = u})
{-# INLINE reservesL #-}