-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
AccountState
to cardano-ledger-core
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
libs/cardano-ledger-core/src/Cardano/Ledger/State/AccountState.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |