@@ -55,6 +55,9 @@ import Cardano.Ledger.Babbage.TxBody qualified as Babbage
55
55
import Cardano.Ledger.Babbage.UTxO (getReferenceScripts )
56
56
import Cardano.Ledger.BaseTypes qualified as Ledger
57
57
import Cardano.Ledger.Coin (Coin (.. ))
58
+ import Cardano.Ledger.Core (
59
+ TxUpgradeError ,
60
+ )
58
61
import Cardano.Ledger.Core qualified as Core
59
62
import Cardano.Ledger.Core qualified as Ledger
60
63
import Cardano.Ledger.Hashes (EraIndependentTxBody , HashAnnotated , hashAnnotated )
@@ -72,9 +75,13 @@ import Data.Ratio ((%))
72
75
import Data.Sequence.Strict ((|>) )
73
76
import Data.Set qualified as Set
74
77
import Hydra.Cardano.Api (
78
+ BlockHeader ,
79
+ ChainPoint ,
80
+ LedgerEra ,
75
81
NetworkId ,
76
82
PaymentCredential (PaymentCredentialByKey ),
77
83
PaymentKey ,
84
+ ShelleyAddr ,
78
85
SigningKey ,
79
86
StakeAddressReference (NoStakeAddress ),
80
87
VerificationKey ,
@@ -91,20 +98,49 @@ import Hydra.Cardano.Api (
91
98
)
92
99
import Hydra.Cardano.Api qualified as Api
93
100
import 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
- )
105
101
import Hydra.Ledger.Cardano ()
106
102
import Hydra.Logging (Tracer , traceWith )
107
103
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
+
108
144
-- | Create a new tiny wallet handle.
109
145
newTinyWallet ::
110
146
-- | A tracer for logging
@@ -193,6 +229,19 @@ getTxId ::
193
229
Ledger. TxId
194
230
getTxId tx = Ledger. TxId $ hashAnnotated (body tx)
195
231
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
+
196
245
-- | Cover fee for a transaction body using the given UTXO set. This calculate
197
246
-- necessary fees and augments inputs / outputs / collateral accordingly to
198
247
-- cover for the transaction cost and get the change back.
@@ -387,3 +436,21 @@ estimateScriptsCost pparams systemStart epochInfo utxo tx = do
387
436
{ redeemerPointer = show ptr
388
437
, scriptFailure = show failure
389
438
}
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