Skip to content

Commit

Permalink
Move AccountState to cardano-ledger-core
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed Feb 1, 2025
1 parent 9ca83e1 commit a96a158
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions libs/cardano-ledger-core/src/Cardano/Ledger/State/AccountState.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{-# 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
{ asTreasury :: !Coin
, asReserves :: !Coin
}
deriving (Show, Eq, Generic)

instance EncCBOR AccountState where
encCBOR (AccountState t r) =
encodeListLen 2 <> encCBOR t <> encCBOR r

instance DecCBOR AccountState where
decCBOR =
decodeRecordNamed "AccountState" (const 2) $ AccountState <$> decCBOR <*> decCBOR

instance ToJSON AccountState where
toJSON = object . toAccountStatePairs
toEncoding = pairs . mconcat . toAccountStatePairs

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

instance NoThunks AccountState

instance NFData AccountState

instance Default AccountState where
def = AccountState (Coin 0) (Coin 0)

0 comments on commit a96a158

Please sign in to comment.