Skip to content

Commit

Permalink
Fix 'availableUtxo' calculation (use address instead of committed utx…
Browse files Browse the repository at this point in the history
…o...)
  • Loading branch information
KtorZ committed Sep 10, 2021
1 parent f999369 commit 2df25ed
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
33 changes: 18 additions & 15 deletions hydra-node/src/Hydra/Ledger/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -442,26 +442,29 @@ instance Crypto crypto => ToJSONKey (Cardano.TxIn crypto) where
-- Output
--

-- Serialise addresses in bech32 including the prefix as standardized:
-- https://github.com/cardano-foundation/CIPs/blob/master/CIP-0005/CIP-0005.md
encodeAddress ::
Cardano.Addr crypto ->
Text
encodeAddress addr =
Bech32.encodeLenient prefix . Bech32.dataPartFromBytes $ Cardano.serialiseAddr addr
where
-- REVIEW(SN): The ledger's 'Addr' type is bigger than we need here and we
-- are forced to come up with a prefix for Byron "bootstrap" addresses,
-- although they should actually be serialised differently..and would not be
-- relevant for Hydra in the first place.
prefix = case addr of
(Cardano.Addr Cardano.Mainnet _ _) -> [Bech32.humanReadablePart|addr|]
(Cardano.Addr Cardano.Testnet _ _) -> [Bech32.humanReadablePart|addr_test|]
(Cardano.AddrBootstrap _) -> [Bech32.humanReadablePart|addr_boot|]

instance Crypto crypto => ToJSON (Cardano.TxOut (MaryEra crypto)) where
toJSON (Cardano.TxOut addr value) =
object
[ "address" .= serialiseAddressBech32
[ "address" .= encodeAddress addr
, "value" .= value
]
where
-- Serialise addresses in bech32 including the prefix as standardized:
-- https://github.com/cardano-foundation/CIPs/blob/master/CIP-0005/CIP-0005.md
serialiseAddressBech32 =
Bech32.encodeLenient prefix . Bech32.dataPartFromBytes $ Cardano.serialiseAddr addr

-- REVIEW(SN): The ledger's 'Addr' type is bigger than we need here and we
-- are forced to come up with a prefix for Byron "bootstrap" addresses,
-- although they should actually be serialised differently..and would not be
-- relevant for Hydra in the first place.
prefix = case addr of
(Cardano.Addr Cardano.Mainnet _ _) -> [Bech32.humanReadablePart|addr|]
(Cardano.Addr Cardano.Testnet _ _) -> [Bech32.humanReadablePart|addr_test|]
(Cardano.AddrBootstrap _) -> [Bech32.humanReadablePart|addr_boot|]

instance Crypto crypto => FromJSON (Cardano.TxOut (MaryEra crypto)) where
parseJSON = withObject "TxOut" $ \o -> do
Expand Down
9 changes: 6 additions & 3 deletions hydra-tui/src/Hydra/TUI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Graphics.Vty.Attributes (defAttr)
import Hydra.Client (Client (Client, sendInput), HydraEvent (..), withClient)
import Hydra.ClientInput (ClientInput (..))
import Hydra.Ledger (Party, Tx (..))
import Hydra.Ledger.Cardano (CardanoAddress, CardanoKeyPair, CardanoTx, TxIn, TxOut, genKeyPair, genUtxoFor, mkSimpleCardanoTx, mkVkAddress, prettyBalance, prettyUtxo)
import Hydra.Ledger.Cardano (CardanoAddress, CardanoKeyPair, CardanoTx, TxIn, TxOut, encodeAddress, genKeyPair, genUtxoFor, mkSimpleCardanoTx, mkVkAddress, prettyBalance, prettyUtxo)
import Hydra.Network (Host (..))
import Hydra.ServerOutput (ServerOutput (..))
import Hydra.Snapshot (Snapshot (..))
Expand All @@ -31,8 +31,10 @@ import Lens.Micro (Lens', lens, (%~), (.~), (?~), (^.), (^?))
import Lens.Micro.TH (makeLensesFor)
import Paths_hydra_tui (version)
import Shelley.Spec.Ledger.API (UTxO (..))
import qualified Shelley.Spec.Ledger.API as Cardano
import Test.QuickCheck.Gen (Gen (..), scale)
import Test.QuickCheck.Random (mkQCGen)
import qualified Prelude

--
-- Model
Expand Down Expand Up @@ -283,7 +285,7 @@ handleNewTxEvent Client{sendInput} = \case
title = "Select a recipient"
-- FIXME: This crashes if peers are empty!
form =
let field = radioField (lens id const) [(p, show p, show p) | p <- peers]
let field = radioField (lens id seq) [(p, show p, show p) | p <- peers]
in newForm [field] (peers !! 0)
submit s (getAddress -> recipient) = do
liftIO (sendInput (NewTx tx))
Expand Down Expand Up @@ -505,7 +507,8 @@ myAvailableUtxo :: State -> Map TxIn TxOut
myAvailableUtxo s =
case s ^? headStateL of
Just Open{utxo = UTxO u'} ->
let u = myTotalUtxo s in u' `Map.intersection` u
let myAddress = getAddress (s ^. meL)
in Map.filter (\(Cardano.TxOut addr _) -> addr == myAddress) u'
_ ->
mempty

Expand Down

0 comments on commit 2df25ed

Please sign in to comment.