Skip to content

Commit

Permalink
Clean-up the TUI code
Browse files Browse the repository at this point in the history
  - Remove the top-level sum-type which is more annoying than useful: we have some shared state to keep track on, so it's useful to have that in one place.
  - Moved some functions into Hydra.Ledger.Cardano (although we need to split this module, those functions really make more sense there).
  - Rewrite the Dialog sequences in a more declarative way to avoid a callback pyramid of doom.
  - Split a bit the update function into smaller functions for clarity.

fixup restructuing
  • Loading branch information
KtorZ committed Sep 10, 2021
1 parent 47c284a commit 41e6b3f
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 232 deletions.
25 changes: 25 additions & 0 deletions hydra-node/src/Hydra/Ledger/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import qualified Data.Map.Strict as Map
import Data.Maybe (fromJust)
import qualified Data.Sequence.Strict as StrictSeq
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Text as Text
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Hydra.Ledger (Balance (..), Ledger (..), Tx (..), ValidationError (ValidationError))
Expand Down Expand Up @@ -292,6 +293,25 @@ signWith (_unTxId -> safeHash) credentials =
(asWitness $ Cardano.vKey credentials)
(signedDSIGN @StandardCrypto (Cardano.sKey credentials) (extractHash safeHash))

--
-- Balance
--

prettyBalance :: Balance tx -> Text
prettyBalance Balance{lovelace, assets} =
let (ada, decimal) = lovelace `quotRem` 1000000
in unwords $
[ show ada <> "." <> show decimal
, ""
]
++ if null assets
then mempty
else
[ "and"
, show (Map.size assets)
, "asset(s)"
]

--
-- Transaction Id
--
Expand Down Expand Up @@ -537,6 +557,11 @@ instance Crypto crypto => ToJSON (Cardano.UTxO (MaryEra crypto)) where
instance Crypto crypto => FromJSON (Cardano.UTxO (MaryEra crypto)) where
parseJSON v = Cardano.UTxO <$> parseJSON v

prettyUtxo :: (TxIn, TxOut) -> Text
prettyUtxo (k, v) =
let value = prettyBalance $ balance @CardanoTx $ Cardano.UTxO (Map.singleton k v)
in T.drop 48 (txInToText k) <> "" <> value

--
-- Witnesses
--
Expand Down

0 comments on commit 41e6b3f

Please sign in to comment.