{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}

module Cardano.Ledger.State.AccountState (
  AccountState (..),
) 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 NoThunks.Class (NoThunks)

data AccountState = AccountState
  { AccountState -> Coin
asTreasury :: !Coin
  , AccountState -> Coin
asReserves :: !Coin
  }
  deriving (Int -> AccountState -> ShowS
[AccountState] -> ShowS
AccountState -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AccountState] -> ShowS
$cshowList :: [AccountState] -> ShowS
show :: AccountState -> String
$cshow :: AccountState -> String
showsPrec :: Int -> AccountState -> ShowS
$cshowsPrec :: Int -> AccountState -> ShowS
Show, AccountState -> AccountState -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AccountState -> AccountState -> Bool
$c/= :: AccountState -> AccountState -> Bool
== :: AccountState -> AccountState -> Bool
$c== :: AccountState -> AccountState -> Bool
Eq, forall x. Rep AccountState x -> AccountState
forall x. AccountState -> Rep AccountState x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AccountState x -> AccountState
$cfrom :: forall x. AccountState -> Rep AccountState x
Generic)

instance EncCBOR AccountState where
  encCBOR :: AccountState -> Encoding
encCBOR (AccountState Coin
t Coin
r) =
    Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR Coin
t forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR Coin
r

instance DecCBOR AccountState where
  decCBOR :: forall s. Decoder s AccountState
decCBOR =
    forall a s. Text -> (a -> Int) -> Decoder s a -> Decoder s a
decodeRecordNamed Text
"AccountState" (forall a b. a -> b -> a
const Int
2) forall a b. (a -> b) -> a -> b
$ Coin -> Coin -> AccountState
AccountState forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. DecCBOR a => Decoder s a
decCBOR

instance ToJSON AccountState where
  toJSON :: AccountState -> Value
toJSON = [Pair] -> Value
object forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. KeyValue e a => AccountState -> [a]
toAccountStatePairs
  toEncoding :: AccountState -> Encoding
toEncoding = Series -> Encoding
pairs forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. KeyValue e a => AccountState -> [a]
toAccountStatePairs

toAccountStatePairs :: KeyValue e a => AccountState -> [a]
toAccountStatePairs :: forall e a. KeyValue e a => AccountState -> [a]
toAccountStatePairs as :: AccountState
as@(AccountState Coin
_ Coin
_) =
  let AccountState {Coin
asTreasury :: Coin
asTreasury :: AccountState -> Coin
asTreasury, Coin
asReserves :: Coin
asReserves :: AccountState -> Coin
asReserves} = AccountState
as
   in [ Key
"treasury" forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Coin
asTreasury
      , Key
"reserves" forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Coin
asReserves
      ]

instance NoThunks AccountState

instance NFData AccountState

instance Default AccountState where
  def :: AccountState
def = Coin -> Coin -> AccountState
AccountState (Integer -> Coin
Coin Integer
0) (Integer -> Coin
Coin Integer
0)