-
Notifications
You must be signed in to change notification settings - Fork 213
/
Wallet.hs
621 lines (544 loc) · 25.4 KB
/
Wallet.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Wallet.Emulator.Wallet where
import Cardano.Wallet.Primitive.Types qualified as Cardano.Wallet
import Control.Lens (makeLenses, makePrisms, over, view, (&), (.~), (^.))
import Control.Monad (foldM)
import Control.Monad.Freer (Eff, Member, Members, interpret, type (~>))
import Control.Monad.Freer.Error (Error, runError, throwError)
import Control.Monad.Freer.Extras.Log (LogMsg, logDebug, logInfo, logWarn)
import Control.Monad.Freer.State (State, get, gets, put)
import Control.Monad.Freer.TH (makeEffect)
import Data.Aeson (FromJSON (parseJSON), ToJSON (toJSON), ToJSONKey)
import Data.Aeson qualified as Aeson
import Data.Bifunctor (bimap, first, second)
import Data.Default (Default (def))
import Data.Foldable (Foldable (fold), find, foldl', for_)
import Data.Map qualified as Map
import Data.Maybe (catMaybes, fromMaybe, isNothing, listToMaybe)
import Data.OpenApi.Schema qualified as OpenApi
import Data.Semigroup (Sum (Sum, getSum))
import Data.Set qualified as Set
import Data.String (IsString (fromString))
import Data.Text qualified as T
import Data.Text.Class (fromText, toText)
import GHC.Generics (Generic)
import Ledger (Address (addressCredential), CardanoTx, ChainIndexTxOut,
PaymentPrivateKey (PaymentPrivateKey, unPaymentPrivateKey),
PaymentPubKey (PaymentPubKey, unPaymentPubKey),
PaymentPubKeyHash (PaymentPubKeyHash, unPaymentPubKeyHash), PubKeyHash,
ScriptValidationEvent (sveScript), StakePubKey, Tx (txFee, txMint), TxIn (TxIn, txInRef), TxOutRef,
UtxoIndex (UtxoIndex, getIndex), ValidationCtx (ValidationCtx), ValidatorHash, Value)
import Ledger qualified
import Ledger.Ada qualified as Ada
import Ledger.CardanoWallet (MockWallet, WalletNumber)
import Ledger.CardanoWallet qualified as CW
import Ledger.Constraints.OffChain (UnbalancedTx (UnbalancedTx, unBalancedTxTx, unBalancedTxUtxoIndex))
import Ledger.Constraints.OffChain qualified as U
import Ledger.Credential (Credential (PubKeyCredential, ScriptCredential))
import Ledger.Fee (FeeConfig, calcFees)
import Ledger.TimeSlot (SlotConfig, posixTimeRangeToContainedSlotRange)
import Ledger.Tx qualified as Tx
import Ledger.Value qualified as Value
import Plutus.ChainIndex (PageQuery)
import Plutus.ChainIndex qualified as ChainIndex
import Plutus.ChainIndex.Api (UtxosResponse (page))
import Plutus.ChainIndex.Emulator (ChainIndexEmulatorState, ChainIndexQueryEffect)
import Plutus.Contract (WalletAPIError)
import Plutus.Contract.Checkpoint (CheckpointLogMsg)
import PlutusTx.Prelude qualified as PlutusTx
import Prettyprinter (Pretty (pretty))
import Servant.API (FromHttpApiData (parseUrlPiece), ToHttpApiData (toUrlPiece))
import Wallet.API qualified as WAPI
import Wallet.Effects (NodeClientEffect,
WalletEffect (BalanceTx, OwnPaymentPubKeyHash, SubmitTxn, TotalFunds, WalletAddSignature, YieldUnbalancedTx),
publishTx)
import Wallet.Emulator.Chain (ChainState (_index))
import Wallet.Emulator.LogMessages (RequestHandlerLogMsg,
TxBalanceMsg (AddingCollateralInputsFor, AddingInputsFor, AddingPublicKeyOutputFor, BalancingUnbalancedTx, FinishedBalancing, NoCollateralInputsAdded, NoInputsAdded, NoOutputsAdded, SubmittingTx, ValidationFailed))
import Wallet.Emulator.NodeClient (NodeClientState, emptyNodeClientState)
newtype SigningProcess = SigningProcess {
unSigningProcess :: forall effs. (Member (Error WAPI.WalletAPIError) effs) => [PaymentPubKeyHash] -> Tx -> Eff effs Tx
}
instance Show SigningProcess where
show = const "SigningProcess <...>"
-- | A wallet identifier
newtype Wallet = Wallet { getWalletId :: WalletId }
deriving (Eq, Ord, Generic)
deriving newtype (ToHttpApiData, FromHttpApiData)
deriving anyclass (ToJSON, FromJSON, ToJSONKey)
toMockWallet :: MockWallet -> Wallet
toMockWallet = Wallet . WalletId . CW.mwWalletId
knownWallets :: [Wallet]
knownWallets = toMockWallet <$> CW.knownMockWallets
knownWallet :: Integer -> Wallet
knownWallet = fromWalletNumber . CW.WalletNumber
fromWalletNumber :: WalletNumber -> Wallet
fromWalletNumber = toMockWallet . CW.fromWalletNumber
instance Show Wallet where
showsPrec p (Wallet i) = showParen (p > 9) $ showString "Wallet " . shows i
instance Pretty Wallet where
pretty (Wallet i) = "W" <> pretty (T.take 7 $ toBase16 i)
deriving anyclass instance OpenApi.ToSchema Wallet
deriving anyclass instance OpenApi.ToSchema Cardano.Wallet.WalletId
newtype WalletId = WalletId { unWalletId :: Cardano.Wallet.WalletId }
deriving (Eq, Ord, Generic)
deriving anyclass (ToJSONKey)
instance Show WalletId where
show = T.unpack . toBase16
instance ToJSON WalletId where
toJSON = Aeson.String . toBase16
instance FromJSON WalletId where
parseJSON = Aeson.withText "WalletId" (either fail pure . fromBase16)
instance ToHttpApiData WalletId where
toUrlPiece = toBase16
instance FromHttpApiData WalletId where
parseUrlPiece = first T.pack . fromBase16
deriving anyclass instance OpenApi.ToSchema WalletId
toBase16 :: WalletId -> T.Text
toBase16 = toText . unWalletId
fromBase16 :: T.Text -> Either String WalletId
fromBase16 s = bimap show WalletId (fromText s)
-- | The 'MockWallet' whose ID is the given wallet ID (if it exists)
walletToMockWallet :: Wallet -> Maybe MockWallet
walletToMockWallet (Wallet wid) = find ((==) wid . WalletId . CW.mwWalletId) CW.knownMockWallets
-- | The public key of a mock wallet. (Fails if the wallet is not a mock wallet).
mockWalletPaymentPubKey :: Wallet -> PaymentPubKey
mockWalletPaymentPubKey w =
CW.paymentPubKey
$ fromMaybe (error $ "Wallet.Emulator.Wallet.walletPubKey: Wallet "
<> show w
<> " is not a mock wallet")
$ walletToMockWallet w
-- | The payment public key hash of a mock wallet. (Fails if the wallet is not a mock wallet).
mockWalletPaymentPubKeyHash :: Wallet -> PaymentPubKeyHash
mockWalletPaymentPubKeyHash =
PaymentPubKeyHash
. Ledger.pubKeyHash
. unPaymentPubKey
. mockWalletPaymentPubKey
-- | Get the address of a mock wallet. (Fails if the wallet is not a mock wallet).
mockWalletAddress :: Wallet -> Address
mockWalletAddress w = Ledger.pubKeyHashAddress (mockWalletPaymentPubKeyHash w) Nothing
data WalletEvent =
GenericLog T.Text
| CheckpointLog CheckpointLogMsg
| RequestHandlerLog RequestHandlerLogMsg
| TxBalanceLog TxBalanceMsg
deriving stock (Show, Eq, Generic)
deriving anyclass (ToJSON, FromJSON)
instance Pretty WalletEvent where
pretty = \case
GenericLog msg -> pretty msg
CheckpointLog msg -> pretty msg
RequestHandlerLog msg -> pretty msg
TxBalanceLog msg -> pretty msg
makePrisms ''WalletEvent
-- | The state used by the mock wallet environment.
data WalletState = WalletState {
_mockWallet :: MockWallet, -- ^ mock wallet with the user's private key
_nodeClient :: NodeClientState,
_chainIndexEmulatorState :: ChainIndexEmulatorState,
_signingProcess :: SigningProcess
} deriving Show
makeLenses ''WalletState
ownPaymentPrivateKey :: WalletState -> PaymentPrivateKey
ownPaymentPrivateKey = CW.paymentPrivateKey . _mockWallet
ownPaymentPublicKey :: WalletState -> PaymentPubKey
ownPaymentPublicKey = CW.paymentPubKey . _mockWallet
-- | Get the user's own payment public-key address.
ownAddress :: WalletState -> Address
ownAddress = flip Ledger.pubKeyAddress Nothing
. PaymentPubKey
. Ledger.toPublicKey
. unPaymentPrivateKey
. ownPaymentPrivateKey
-- | An empty wallet using the given private key.
-- for that wallet as the sole watched address.
fromMockWallet :: MockWallet -> WalletState
fromMockWallet mw = WalletState mw emptyNodeClientState mempty sp where
sp = signWithPrivateKey (CW.paymentPrivateKey mw)
-- | Empty wallet state for an emulator 'Wallet'. Returns 'Nothing' if the wallet
-- is not known in the emulator.
emptyWalletState :: Wallet -> Maybe WalletState
emptyWalletState = fmap fromMockWallet . walletToMockWallet
handleWallet ::
( Member (Error WalletAPIError) effs
, Member NodeClientEffect effs
, Member ChainIndexQueryEffect effs
, Member (State WalletState) effs
, Member (LogMsg TxBalanceMsg) effs
)
=> FeeConfig
-> WalletEffect ~> Eff effs
handleWallet feeCfg = \case
SubmitTxn tx -> submitTxnH tx
OwnPaymentPubKeyHash -> ownPaymentPubKeyHashH
BalanceTx utx -> balanceTxH utx
WalletAddSignature tx -> walletAddSignatureH tx
TotalFunds -> totalFundsH
YieldUnbalancedTx utx -> yieldUnbalancedTxH utx
where
submitTxnH :: (Member NodeClientEffect effs, Member (LogMsg TxBalanceMsg) effs) => CardanoTx -> Eff effs ()
submitTxnH (Left _) = error "Wallet.Emulator.Wallet.handleWallet: Expecting a mock tx, not an Alonzo tx when submitting it."
submitTxnH (Right tx) = do
logInfo $ SubmittingTx tx
publishTx tx
ownPaymentPubKeyHashH :: (Member (State WalletState) effs) => Eff effs PaymentPubKeyHash
ownPaymentPubKeyHashH = gets (CW.paymentPubKeyHash . _mockWallet)
balanceTxH ::
( Member NodeClientEffect effs
, Member ChainIndexQueryEffect effs
, Member (State WalletState) effs
, Member (LogMsg TxBalanceMsg) effs
)
=> UnbalancedTx
-> Eff effs (Either WalletAPIError CardanoTx)
balanceTxH utx' = runError $ do
logInfo $ BalancingUnbalancedTx utx'
utxo <- get >>= ownOutputs
slotConfig <- WAPI.getClientSlotConfig
let validitySlotRange = posixTimeRangeToContainedSlotRange slotConfig (utx' ^. U.validityTimeRange)
let utx = utx' & U.tx . Ledger.validRange .~ validitySlotRange
utxWithFees <- validateTxAndAddFees feeCfg slotConfig utxo utx
-- balance to add fees
tx' <- handleBalanceTx utxo (utx & U.tx . Ledger.fee .~ (utxWithFees ^. U.tx . Ledger.fee))
tx'' <- handleAddSignature tx'
logInfo $ FinishedBalancing tx''
pure $ Right tx''
walletAddSignatureH :: (Member (State WalletState) effs) => CardanoTx -> Eff effs CardanoTx
walletAddSignatureH (Left _) = error "Wallet.Emulator.Wallet.handleWallet: Expecting a mock tx, not an Alonzo tx when adding a signature."
walletAddSignatureH (Right tx) = Right <$> handleAddSignature tx
totalFundsH :: (Member (State WalletState) effs, Member ChainIndexQueryEffect effs) => Eff effs Value
totalFundsH = foldMap (view Ledger.ciTxOutValue) <$> (get >>= ownOutputs)
yieldUnbalancedTxH ::
( Member (Error WalletAPIError) effs
, Member NodeClientEffect effs
, Member ChainIndexQueryEffect effs
, Member (State WalletState) effs
, Member (LogMsg TxBalanceMsg) effs
)
=> UnbalancedTx
-> Eff effs ()
yieldUnbalancedTxH utx = do
balancedTxM <- balanceTxH utx
case balancedTxM of
Left err -> throwError err
Right balancedTx -> walletAddSignatureH balancedTx >>= submitTxnH
handleAddSignature ::
Member (State WalletState) effs
=> Tx
-> Eff effs Tx
handleAddSignature tx = do
(PaymentPrivateKey privKey) <- gets ownPaymentPrivateKey
pure (Ledger.addSignature' privKey tx)
ownOutputs :: forall effs.
( Member ChainIndexQueryEffect effs
)
=> WalletState
-> Eff effs (Map.Map TxOutRef ChainIndexTxOut)
ownOutputs WalletState{_mockWallet} = do
refs <- allUtxoSet (Just def)
Map.fromList . catMaybes <$> traverse txOutRefTxOutFromRef refs
where
cred :: Credential
cred = PubKeyCredential (unPaymentPubKeyHash $ CW.paymentPubKeyHash _mockWallet)
-- Accumulate all unspent 'TxOutRef's from the resulting pages.
allUtxoSet :: Maybe (PageQuery TxOutRef) -> Eff effs [TxOutRef]
allUtxoSet Nothing = pure []
allUtxoSet (Just pq) = do
refPage <- page <$> ChainIndex.utxoSetAtAddress pq cred
nextItems <- allUtxoSet (ChainIndex.nextPageQuery refPage)
pure $ ChainIndex.pageItems refPage ++ nextItems
txOutRefTxOutFromRef :: TxOutRef -> Eff effs (Maybe (TxOutRef, ChainIndexTxOut))
txOutRefTxOutFromRef ref = fmap (ref,) <$> ChainIndex.txOutFromRef ref
validateTxAndAddFees ::
( Member (Error WAPI.WalletAPIError) effs
, Member ChainIndexQueryEffect effs
, Member (LogMsg TxBalanceMsg) effs
, Member (State WalletState) effs
)
=> FeeConfig
-> SlotConfig
-> Map.Map TxOutRef ChainIndexTxOut -- ^ The current wallet's unspent transaction outputs.
-> UnbalancedTx
-> Eff effs UnbalancedTx
validateTxAndAddFees feeCfg slotCfg ownTxOuts utx = do
-- Balance and sign just for validation
tx <- handleBalanceTx ownTxOuts utx
signedTx <- handleAddSignature tx
let utxoIndex = Ledger.UtxoIndex $ fmap Ledger.toTxOut $ (U.fromScriptOutput <$> unBalancedTxUtxoIndex utx) <> ownTxOuts
((e, _), events) = Ledger.runValidation (Ledger.validateTransactionOffChain signedTx) (Ledger.ValidationCtx utxoIndex slotCfg)
for_ e $ \(phase, ve) -> do
logWarn $ ValidationFailed phase (Ledger.txId tx) tx ve events
throwError $ WAPI.ValidationError ve
let scriptsSize = getSum $ foldMap (Sum . Ledger.scriptSize . Ledger.sveScript) events
theFee = Ada.toValue $ calcFees feeCfg scriptsSize -- TODO: use protocol parameters
pure $ utx{ unBalancedTxTx = (unBalancedTxTx utx){ txFee = theFee }}
lookupValue ::
( Member (Error WAPI.WalletAPIError) effs
, Member ChainIndexQueryEffect effs
)
=> Tx.TxIn
-> Eff effs Value
lookupValue outputRef@TxIn {txInRef} = do
txoutMaybe <- ChainIndex.txOutFromRef txInRef
case txoutMaybe of
Just txout -> pure $ view Ledger.ciTxOutValue txout
Nothing ->
WAPI.throwOtherError $ "Unable to find TxOut for " <> fromString (show outputRef)
-- | Balance an unbalanced transaction by adding missing inputs and outputs
handleBalanceTx ::
forall effs.
( Member (State WalletState) effs
, Member ChainIndexQueryEffect effs
, Member (Error WAPI.WalletAPIError) effs
, Member (LogMsg TxBalanceMsg) effs
)
=> Map.Map TxOutRef ChainIndexTxOut -- ^ The current wallet's unspent transaction outputs.
-> UnbalancedTx
-> Eff effs Tx
handleBalanceTx utxo UnbalancedTx{unBalancedTxTx} = do
let filteredUnbalancedTxTx = removeEmptyOutputs unBalancedTxTx
let txInputs = Set.toList $ Tx.txInputs filteredUnbalancedTxTx
ownPaymentPubKey <- gets ownPaymentPublicKey
let ownStakePubKey = Nothing
inputValues <- traverse lookupValue (Set.toList $ Tx.txInputs filteredUnbalancedTxTx)
collateral <- traverse lookupValue (Set.toList $ Tx.txCollateral filteredUnbalancedTxTx)
let fees = txFee filteredUnbalancedTxTx
left = txMint filteredUnbalancedTxTx <> fold inputValues
right = fees <> foldMap (view Tx.outValue) (filteredUnbalancedTxTx ^. Tx.outputs)
remainingFees = fees PlutusTx.- fold collateral -- TODO: add collateralPercent
balance = left PlutusTx.- right
(neg, pos) = adjustBalanceWithMissingLovelace $ Value.split balance
tx' <- if Value.isZero pos
then do
logDebug NoOutputsAdded
pure filteredUnbalancedTxTx
else do
logDebug $ AddingPublicKeyOutputFor pos
pure $ addOutput ownPaymentPubKey ownStakePubKey pos filteredUnbalancedTxTx
tx'' <- if Value.isZero neg
then do
logDebug NoInputsAdded
pure tx'
else do
logDebug $ AddingInputsFor neg
-- filter out inputs from utxo that are already in unBalancedTx
let inputsOutRefs = map Tx.txInRef txInputs
filteredUtxo = flip Map.filterWithKey utxo $ \txOutRef _ ->
txOutRef `notElem` inputsOutRefs
addInputs filteredUtxo ownPaymentPubKey ownStakePubKey neg tx'
if remainingFees `Value.leq` PlutusTx.zero
then do
logDebug NoCollateralInputsAdded
pure tx''
else do
logDebug $ AddingCollateralInputsFor remainingFees
addCollateral utxo remainingFees tx''
-- | Adjust the left and right balance of an unbalanced 'Tx' with the missing
-- lovelace considering the minimum lovelace per transaction output constraint
-- from the Cardano blockchain.
adjustBalanceWithMissingLovelace
:: (Value, Value) -- ^ The unbalanced tx's left and right balance.
-> (Value, Value) -- ^ New left and right balance.
adjustBalanceWithMissingLovelace (neg, pos) = do
-- We find the missing lovelace from the new positive balance. If
-- the positive balance is > 0 and < 'Ledger.minAdaTxOut',
-- then we adjust it to the minimum Ada.
let missingLovelaceFromPosValue =
if valueIsZeroOrHasMinAda pos
then 0
else max 0 (Ledger.minAdaTxOut - Ada.fromValue pos)
-- We calculate the final negative and positive balances
newPos = pos <> Ada.toValue missingLovelaceFromPosValue
newNeg = neg <> Ada.toValue missingLovelaceFromPosValue
(newNeg, newPos)
addOutput :: PaymentPubKey -> Maybe StakePubKey -> Value -> Tx -> Tx
addOutput pk sk vl tx = tx & over Tx.outputs (pko :) where
pko = Tx.pubKeyTxOut vl pk sk
addCollateral
:: ( Member (Error WAPI.WalletAPIError) effs
)
=> Map.Map TxOutRef ChainIndexTxOut -- ^ The current wallet's unspent transaction outputs.
-> Value
-> Tx
-> Eff effs Tx
addCollateral mp vl tx = do
(spend, _) <- selectCoin (second (view Ledger.ciTxOutValue) <$> Map.toList mp) vl
let addTxCollateral =
let ins = Set.fromList (Tx.pubKeyTxIn . fst <$> spend)
in over Tx.collateralInputs (Set.union ins)
pure $ tx & addTxCollateral
-- | @addInputs mp pk vl tx@ selects transaction outputs worth at least
-- @vl@ from the UTXO map @mp@ and adds them as inputs to @tx@. A public
-- key output for @pk@ is added containing any leftover change.
addInputs
:: ( Member (Error WAPI.WalletAPIError) effs
)
=> Map.Map TxOutRef ChainIndexTxOut -- ^ The current wallet's unspent transaction outputs.
-> PaymentPubKey
-> Maybe StakePubKey
-> Value
-> Tx
-> Eff effs Tx
addInputs mp pk sk vl tx = do
(spend, change) <- selectCoin (second (view Ledger.ciTxOutValue) <$> Map.toList mp) vl
let
addTxIns =
let ins = Set.fromList (Tx.pubKeyTxIn . fst <$> spend)
in over Tx.inputs (Set.union ins)
addTxOut =
if Value.isZero change
then id
else addOutput pk sk change
pure $ tx & addTxOut & addTxIns
-- | Given a set of @a@s with coin values, and a target value, select a number
-- of @a@ such that their total value is greater than or equal to the target.
selectCoin ::
( Member (Error WAPI.WalletAPIError) effs
)
=> [(a, Value)]
-> Value
-> Eff effs ([(a, Value)], Value)
selectCoin fnds vl =
let
total = foldMap snd fnds
err = throwError
$ WAPI.InsufficientFunds
$ T.unwords
[ "Total:", T.pack $ show total
, "expected:", T.pack $ show vl ]
-- Values are in a partial order: what we want to check is that the
-- total available funds are bigger than (or equal to) the required value.
-- It is *not* correct to replace this condition with 'total `Value.lt` vl' -
-- consider what happens if the amounts are incomparable.
in if not (total `Value.geq` vl)
then err
else
let
-- Given the funds of a wallet, we take enough just enough such
-- that it's geq the target value, and if the resulting change
-- is not between 0 and the minimum Ada per tx output.
isTotalValueEnough totalVal =
vl `Value.leq` totalVal && valueIsZeroOrHasMinAda (totalVal PlutusTx.- vl)
fundsWithTotal = zip fnds (drop 1 $ scanl (<>) mempty $ fmap snd fnds)
fundsToSpend = takeUntil (isTotalValueEnough . snd) fundsWithTotal
totalSpent = maybe PlutusTx.zero snd $ listToMaybe $ reverse fundsToSpend
change = totalSpent PlutusTx.- vl
-- Make sure that the change is not less than the minimum amount
-- of lovelace per tx output.
in if valueIsZeroOrHasMinAda change
then pure (fst <$> fundsToSpend, change)
else throwError $ WAPI.ChangeHasLessThanNAda change Ledger.minAdaTxOut
-- | Check that a value is a proper TxOut value or is zero (i.e. the absence of a TxOut)
valueIsZeroOrHasMinAda :: Value -> Bool
valueIsZeroOrHasMinAda v = Value.isZero v || Ada.fromValue v >= Ledger.minAdaTxOut
-- | Removes transaction outputs with empty datum and empty value.
removeEmptyOutputs :: Tx -> Tx
removeEmptyOutputs tx = tx & over Tx.outputs (filter (not . isEmpty')) where
isEmpty' Tx.TxOut{Tx.txOutValue, Tx.txOutDatumHash} =
null (Value.flattenValue txOutValue) && isNothing txOutDatumHash
-- | Take elements from a list until the predicate is satisfied.
-- 'takeUntil' @p@ includes the first element for wich @p@ is true
-- (unlike @takeWhile (not . p)@).
takeUntil :: (a -> Bool) -> [a] -> [a]
takeUntil _ [] = []
takeUntil p (x:xs)
| p x = [x]
| otherwise = x : takeUntil p xs
-- | The default signing process is 'signWallet'
defaultSigningProcess :: MockWallet -> SigningProcess
defaultSigningProcess = signWallet
signWithPrivateKey :: PaymentPrivateKey -> SigningProcess
signWithPrivateKey pk = SigningProcess $
\pks tx -> foldM (signTxWithPrivateKey pk) tx pks
-- | Sign the transaction by calling 'WAPI.signTxnWithKey' (throwing a
-- 'PrivateKeyNotFound' error if called with a key other than the
-- wallet's private key)
signWallet :: MockWallet -> SigningProcess
signWallet wllt = SigningProcess $
\pks tx -> foldM (signTxnWithKey wllt) tx pks
-- | Sign the transaction with the private key of the mock wallet.
signTxnWithKey
:: (Member (Error WAPI.WalletAPIError) r)
=> MockWallet
-> Tx
-> PaymentPubKeyHash
-> Eff r Tx
signTxnWithKey mw = signTxWithPrivateKey (CW.paymentPrivateKey mw)
-- | Sign the transaction with the private key, if the hash is that of the
-- private key.
signTxWithPrivateKey
:: (Member (Error WAPI.WalletAPIError) r)
=> PaymentPrivateKey
-> Tx
-> PaymentPubKeyHash
-> Eff r Tx
signTxWithPrivateKey (PaymentPrivateKey pk) tx pkh@(PaymentPubKeyHash pubK) = do
let ownPaymentPubKey = Ledger.toPublicKey pk
if Ledger.pubKeyHash ownPaymentPubKey == pubK
then pure (Ledger.addSignature' pk tx)
else throwError (WAPI.PaymentPrivateKeyNotFound pkh)
-- | Sign the transaction with the given private keys,
-- ignoring the list of public keys that the 'SigningProcess' is passed.
signPrivateKeys :: [PaymentPrivateKey] -> SigningProcess
signPrivateKeys signingKeys = SigningProcess $ \_ tx ->
pure (foldr (Ledger.addSignature' . unPaymentPrivateKey) tx signingKeys)
data SigningProcessControlEffect r where
SetSigningProcess :: SigningProcess -> SigningProcessControlEffect ()
makeEffect ''SigningProcessControlEffect
type SigningProcessEffs = '[State SigningProcess, Error WAPI.WalletAPIError]
handleSigningProcessControl :: (Members SigningProcessEffs effs) => Eff (SigningProcessControlEffect ': effs) ~> Eff effs
handleSigningProcessControl = interpret $ \case
SetSigningProcess proc -> put proc
-- | An Entity is a thing that can hold 'Value'. Used in the 'balances'
-- function to compute who holds for a given chain state and set of wallets.
data Entity
= WalletEntity Wallet
| PubKeyHashEntity PubKeyHash
| ScriptEntity ValidatorHash
deriving (Eq, Ord)
instance Show Entity where
show (WalletEntity w) = show w
show (ScriptEntity h) = "Script " <> show h
show (PubKeyHashEntity h) = "PubKeyHash " <> show h
type WalletSet = Map.Map Wallet WalletState
-- | Pick out all the public keys from the set of wallets and map them back to
-- their corresponding wallets.
walletPaymentPubKeyHashes :: WalletSet -> Map.Map PaymentPubKeyHash Wallet
walletPaymentPubKeyHashes = foldl' f Map.empty . Map.toList
where
f m (w, ws) = Map.insert (CW.paymentPubKeyHash $ _mockWallet ws) w m
-- | For a set of wallets, convert them into a map of value: entity,
-- where entity is one of 'Entity'.
balances :: ChainState -> WalletSet -> Map.Map Entity Value
balances state wallets = foldl' f Map.empty . getIndex . _index $ state
where
toEntity :: Address -> Entity
toEntity a =
case addressCredential a of
PubKeyCredential h ->
case Map.lookup (PaymentPubKeyHash h) ws of
Nothing -> PubKeyHashEntity h
Just w -> WalletEntity w
ScriptCredential h -> ScriptEntity h
ws :: Map.Map PaymentPubKeyHash Wallet
ws = walletPaymentPubKeyHashes wallets
f m o = Map.insertWith (<>) (toEntity $ Ledger.txOutAddress o) (Ledger.txOutValue o) m