@@ -55,6 +55,9 @@ import Cardano.Ledger.Babbage.TxBody qualified as Babbage
5555import Cardano.Ledger.Babbage.UTxO (getReferenceScripts )
5656import Cardano.Ledger.BaseTypes qualified as Ledger
5757import Cardano.Ledger.Coin (Coin (.. ))
58+ import Cardano.Ledger.Core (
59+ TxUpgradeError ,
60+ )
5861import Cardano.Ledger.Core qualified as Core
5962import Cardano.Ledger.Core qualified as Ledger
6063import Cardano.Ledger.Hashes (EraIndependentTxBody , HashAnnotated , hashAnnotated )
@@ -72,9 +75,13 @@ import Data.Ratio ((%))
7275import Data.Sequence.Strict ((|>) )
7376import Data.Set qualified as Set
7477import Hydra.Cardano.Api (
78+ BlockHeader ,
79+ ChainPoint ,
80+ LedgerEra ,
7581 NetworkId ,
7682 PaymentCredential (PaymentCredentialByKey ),
7783 PaymentKey ,
84+ ShelleyAddr ,
7885 SigningKey ,
7986 StakeAddressReference (NoStakeAddress ),
8087 VerificationKey ,
@@ -91,20 +98,49 @@ import Hydra.Cardano.Api (
9198 )
9299import Hydra.Cardano.Api qualified as Api
93100import Hydra.Chain.CardanoClient (QueryPoint (.. ))
94- import Hydra.Chain.Wallet (
95- Address ,
96- ChainQuery ,
97- ChangeError (.. ),
98- ErrCoverFee (.. ),
99- TinyWallet (.. ),
100- TinyWalletLog (.. ),
101- TxIn ,
102- TxOut ,
103- WalletInfoOnChain (.. ),
104- )
105101import Hydra.Ledger.Cardano ()
106102import Hydra.Logging (Tracer , traceWith )
107103
104+ type Address = Ledger. Addr StandardCrypto
105+ type TxIn = Ledger. TxIn StandardCrypto
106+ type TxOut = Ledger. TxOut LedgerEra
107+
108+ -- | A 'TinyWallet' is a small abstraction of a wallet with basic UTXO
109+ -- management. The wallet is assumed to have only one address, and only one UTXO
110+ -- at that address. It can sign transactions and keeps track of its UTXO behind
111+ -- the scene.
112+ --
113+ -- The wallet is connecting to the node initially and when asked to 'reset'.
114+ -- Otherwise it can be fed blocks via 'update' as the chain rolls forward.
115+ data TinyWallet m = TinyWallet
116+ { getUTxO :: STM m (Map TxIn TxOut )
117+ -- ^ Return all known UTxO addressed to this wallet.
118+ , getSeedInput :: STM m (Maybe Api. TxIn )
119+ -- ^ Returns the /seed input/
120+ -- This is the special input needed by `Direct` chain component to initialise
121+ -- a head
122+ , sign :: Api. Tx -> Api. Tx
123+ , coverFee ::
124+ UTxO ->
125+ Api. Tx ->
126+ m (Either ErrCoverFee Api. Tx )
127+ , reset :: m ()
128+ -- ^ Re-initializ wallet against the latest tip of the node and start to
129+ -- ignore 'update' calls until reaching that tip.
130+ , update :: BlockHeader -> [Api. Tx ] -> m ()
131+ -- ^ Update the wallet state given a block and list of txs. May be ignored if
132+ -- wallet is still initializing.
133+ }
134+
135+ data WalletInfoOnChain = WalletInfoOnChain
136+ { walletUTxO :: Map TxIn TxOut
137+ , systemStart :: SystemStart
138+ , tip :: ChainPoint
139+ -- ^ Latest point on chain the wallet knows of.
140+ }
141+
142+ type ChainQuery m = QueryPoint -> Api. Address ShelleyAddr -> m WalletInfoOnChain
143+
108144-- | Create a new tiny wallet handle.
109145newTinyWallet ::
110146 -- | A tracer for logging
@@ -193,6 +229,19 @@ getTxId ::
193229 Ledger. TxId
194230getTxId tx = Ledger. TxId $ hashAnnotated (body tx)
195231
232+ -- | This are all the error that can happen during coverFee.
233+ data ErrCoverFee
234+ = ErrNotEnoughFunds ChangeError
235+ | ErrNoFuelUTxOFound
236+ | ErrUnknownInput { input :: TxIn }
237+ | ErrScriptExecutionFailed { redeemerPointer :: Text , scriptFailure :: Text }
238+ | ErrTranslationError (ContextError LedgerEra )
239+ | ErrConwayUpgradeError (TxUpgradeError Conway )
240+ deriving stock (Show )
241+
242+ data ChangeError = ChangeError { inputBalance :: Coin , outputBalance :: Coin }
243+ deriving stock (Show )
244+
196245-- | Cover fee for a transaction body using the given UTXO set. This calculate
197246-- necessary fees and augments inputs / outputs / collateral accordingly to
198247-- cover for the transaction cost and get the change back.
@@ -387,3 +436,21 @@ estimateScriptsCost pparams systemStart epochInfo utxo tx = do
387436 { redeemerPointer = show ptr
388437 , scriptFailure = show failure
389438 }
439+
440+ --
441+ -- Logs
442+ --
443+
444+ data TinyWalletLog
445+ = BeginInitialize
446+ | EndInitialize { initialUTxO :: Api. UTxO , tip :: ChainPoint }
447+ | BeginUpdate { point :: ChainPoint }
448+ | EndUpdate { newUTxO :: Api. UTxO}
449+ | SkipUpdate { point :: ChainPoint }
450+ deriving stock (Eq , Generic , Show )
451+
452+ deriving anyclass instance ToJSON TinyWalletLog
453+
454+ instance Arbitrary TinyWalletLog where
455+ arbitrary = genericArbitrary
456+ shrink = genericShrink
0 commit comments