/
BalanceTx.purs
557 lines (495 loc) · 18.1 KB
/
BalanceTx.purs
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
module Ctl.Internal.BalanceTx
( module BalanceTxErrorExport
, module FinalizedTransaction
, balanceTxWithConstraints
) where
import Prelude
import Control.Monad.Error.Class (liftMaybe)
import Control.Monad.Except.Trans (ExceptT(ExceptT), except, runExceptT)
import Control.Monad.Logger.Class (class MonadLogger)
import Control.Monad.Logger.Class as Logger
import Control.Monad.Reader.Class (asks)
import Control.Parallel (parTraverse)
import Ctl.Internal.BalanceTx.Collateral
( addTxCollateral
, addTxCollateralReturn
)
import Ctl.Internal.BalanceTx.Constraints (BalanceTxConstraintsBuilder)
import Ctl.Internal.BalanceTx.Constraints
( _changeAddress
, _maxChangeOutputTokenQuantity
, _nonSpendableInputs
, _srcAddresses
) as Constraints
import Ctl.Internal.BalanceTx.Error
( Actual(Actual)
, BalanceTxError
( CouldNotConvertScriptOutputToTxInput
, CouldNotGetChangeAddress
, CouldNotGetCollateral
, CouldNotGetUtxos
, ExUnitsEvaluationFailed
, InsufficientTxInputs
, ReindexRedeemersError
, UtxoLookupFailedFor
, UtxoMinAdaValueCalculationFailed
)
, Expected(Expected)
, printTxEvaluationFailure
) as BalanceTxErrorExport
import Ctl.Internal.BalanceTx.Error
( Actual(Actual)
, BalanceTxError
( CouldNotConvertScriptOutputToTxInput
, CouldNotGetChangeAddress
, CouldNotGetCollateral
, CouldNotGetUtxos
, InsufficientTxInputs
, UtxoLookupFailedFor
, UtxoMinAdaValueCalculationFailed
)
, Expected(Expected)
)
import Ctl.Internal.BalanceTx.ExUnitsAndMinFee
( evalExUnitsAndMinFee
, finalizeTransaction
)
import Ctl.Internal.BalanceTx.Helpers
( _body'
, _redeemersTxIns
, _transaction'
, _unbalancedTx
)
import Ctl.Internal.BalanceTx.Types
( BalanceTxM
, FinalizedTransaction
, PrebalancedTransaction(PrebalancedTransaction)
, askCip30Wallet
, askCoinsPerUtxoUnit
, askNetworkId
, asksConstraints
, liftContract
, liftEitherContract
, withBalanceTxConstraints
)
import Ctl.Internal.BalanceTx.Types (FinalizedTransaction(FinalizedTransaction)) as FinalizedTransaction
import Ctl.Internal.BalanceTx.UtxoMinAda (utxoMinAdaValue)
import Ctl.Internal.Cardano.Types.Transaction
( Certificate(StakeRegistration, StakeDeregistration)
, Transaction(Transaction)
, TransactionOutput(TransactionOutput)
, TxBody(TxBody)
, UtxoMap
, _body
, _certs
, _fee
, _inputs
, _mint
, _networkId
, _outputs
, _referenceInputs
, _withdrawals
)
import Ctl.Internal.Cardano.Types.Value
( Coin(Coin)
, Value
, coinToValue
, equipartitionValueWithTokenQuantityUpperBound
, geq
, getNonAdaAsset
, minus
, mkValue
, posNonAdaAsset
, valueToCoin'
)
import Ctl.Internal.Contract.Monad (Contract)
import Ctl.Internal.Contract.QueryHandle (getQueryHandle)
import Ctl.Internal.Contract.Wallet
( filterLockedUtxos
, getWalletCollateral
)
import Ctl.Internal.Contract.Wallet (getChangeAddress, getWalletAddresses) as Contract
import Ctl.Internal.Serialization.Address
( Address
, addressPaymentCred
, withStakeCredential
)
import Ctl.Internal.Types.OutputDatum (OutputDatum(NoOutputDatum))
import Ctl.Internal.Types.ScriptLookups (UnattachedUnbalancedTx)
import Ctl.Internal.Types.Transaction (TransactionInput)
import Ctl.Internal.Types.UnbalancedTransaction (_utxoIndex)
import Data.Array as Array
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Array.NonEmpty (toArray) as NEArray
import Data.BigInt (BigInt)
import Data.Either (Either(Left, Right), hush, note)
import Data.Foldable (fold, foldMap, foldl, foldr, sum)
import Data.Lens.Getter ((^.))
import Data.Lens.Setter ((%~), (.~), (?~))
import Data.Log.Tag (tag)
import Data.Map (empty, filterKeys, lookup, toUnfoldable, union) as Map
import Data.Maybe (Maybe(Nothing, Just), fromMaybe, isJust, maybe)
import Data.Newtype (unwrap, wrap)
import Data.Set (Set)
import Data.Set as Set
import Data.Traversable (traverse, traverse_)
import Data.Tuple.Nested (type (/\), (/\))
import Effect.Aff.Class (liftAff)
import Effect.Class (class MonadEffect, liftEffect)
-- | Balances an unbalanced transaction using the specified balancer
-- | constraints.
balanceTxWithConstraints
:: UnattachedUnbalancedTx
-> BalanceTxConstraintsBuilder
-> Contract (Either BalanceTxError FinalizedTransaction)
balanceTxWithConstraints unbalancedTx constraintsBuilder = do
pparams <- asks $ _.ledgerConstants >>> _.pparams
queryHandle <- getQueryHandle
withBalanceTxConstraints constraintsBuilder $ runExceptT do
let
depositValuePerCert = (unwrap pparams).stakeAddressDeposit
certsFee = getStakingBalance (unbalancedTx ^. _transaction')
depositValuePerCert
srcAddrs <-
asksConstraints Constraints._srcAddresses
>>= maybe (liftContract Contract.getWalletAddresses) pure
changeAddr <- getChangeAddress
utxos <- liftEitherContract $
parTraverse (queryHandle.utxosAt >>> liftAff >>> map hush) srcAddrs <#>
traverse (note CouldNotGetUtxos)
>>> map (foldr Map.union Map.empty) -- merge all utxos into one map
unbalancedCollTx <-
case Array.null (unbalancedTx ^. _redeemersTxIns) of
true ->
-- Don't set collateral if tx doesn't contain phase-2 scripts:
unbalancedTxWithNetworkId
false ->
setTransactionCollateral changeAddr =<< unbalancedTxWithNetworkId
let
allUtxos :: UtxoMap
allUtxos =
-- Combine utxos at the user address and those from any scripts
-- involved with the contract in the unbalanced transaction:
utxos `Map.union` (unbalancedTx ^. _unbalancedTx <<< _utxoIndex)
availableUtxos <- liftContract $ filterLockedUtxos allUtxos
logTx "unbalancedCollTx" availableUtxos unbalancedCollTx
-- Balance and finalize the transaction:
runBalancer allUtxos availableUtxos changeAddr certsFee
(unbalancedTx # _transaction' .~ unbalancedCollTx)
where
getChangeAddress :: BalanceTxM Address
getChangeAddress =
liftMaybe CouldNotGetChangeAddress
=<< maybe (liftContract Contract.getChangeAddress) (pure <<< Just)
=<< asksConstraints Constraints._changeAddress
unbalancedTxWithNetworkId :: BalanceTxM Transaction
unbalancedTxWithNetworkId = do
let transaction = unbalancedTx ^. _transaction'
networkId <- maybe askNetworkId pure (transaction ^. _body <<< _networkId)
pure (transaction # _body <<< _networkId ?~ networkId)
setTransactionCollateral :: Address -> Transaction -> BalanceTxM Transaction
setTransactionCollateral changeAddr transaction = do
collateral <-
liftEitherContract $ note CouldNotGetCollateral <$> getWalletCollateral
let collaterisedTx = addTxCollateral collateral transaction
-- Don't mess with Cip30 collateral
isCip30 <- isJust <$> askCip30Wallet
if isCip30 then pure collaterisedTx
else addTxCollateralReturn collateral collaterisedTx changeAddr
--------------------------------------------------------------------------------
-- Balancing Algorithm
--------------------------------------------------------------------------------
type ChangeAddress = Address
type TransactionChangeOutput = TransactionOutput
type MinFee = BigInt
type Iteration = Int
runBalancer
:: UtxoMap
-> UtxoMap
-> ChangeAddress
-> Coin
-> UnattachedUnbalancedTx
-> BalanceTxM FinalizedTransaction
runBalancer allUtxos utxos changeAddress certsFee =
mainLoop one zero <=< addLovelacesToTransactionOutputs
where
mainLoop
:: Iteration
-> MinFee
-> UnattachedUnbalancedTx
-> BalanceTxM FinalizedTransaction
mainLoop iteration minFee unbalancedTx = do
let
unbalancedTxWithMinFee =
setTransactionMinFee minFee unbalancedTx
unbalancedTxWithInputs <-
addTransactionInputs changeAddress utxos certsFee unbalancedTxWithMinFee
traceMainLoop "added transaction inputs" "unbalancedTxWithInputs"
unbalancedTxWithInputs
prebalancedTx <-
addTransactionChangeOutputs changeAddress utxos certsFee
unbalancedTxWithInputs
traceMainLoop "added transaction change output" "prebalancedTx"
prebalancedTx
balancedTx /\ newMinFee <- evalExUnitsAndMinFee prebalancedTx allUtxos
traceMainLoop "calculated ex units and min fee" "balancedTx" balancedTx
case newMinFee == minFee of
true -> do
finalizedTransaction <- finalizeTransaction balancedTx allUtxos
traceMainLoop "finalized transaction" "finalizedTransaction"
finalizedTransaction
pure finalizedTransaction
false ->
mainLoop (iteration + one) newMinFee unbalancedTxWithInputs
where
traceMainLoop
:: forall (a :: Type). Show a => String -> String -> a -> BalanceTxM Unit
traceMainLoop meta message object =
let
tagMessage :: String
tagMessage =
"mainLoop (iteration " <> show iteration <> "): " <> meta
in
Logger.trace (tag tagMessage "^") $ message <> ": " <> show object
setTransactionMinFee
:: MinFee -> UnattachedUnbalancedTx -> UnattachedUnbalancedTx
setTransactionMinFee minFee = _body' <<< _fee .~ Coin minFee
-- | For each transaction output, if necessary, adds some number of lovelaces
-- | to cover the utxo min-ada-value requirement.
addLovelacesToTransactionOutputs
:: UnattachedUnbalancedTx -> BalanceTxM UnattachedUnbalancedTx
addLovelacesToTransactionOutputs unbalancedTx =
map (\txOutputs -> unbalancedTx # _body' <<< _outputs .~ txOutputs) $
traverse addLovelacesToTransactionOutput
(unbalancedTx ^. _body' <<< _outputs)
addLovelacesToTransactionOutput
:: TransactionOutput -> BalanceTxM TransactionOutput
addLovelacesToTransactionOutput txOutput = do
coinsPerUtxoUnit <- askCoinsPerUtxoUnit
txOutputMinAda <- ExceptT $ liftEffect $
utxoMinAdaValue coinsPerUtxoUnit txOutput
<#> note UtxoMinAdaValueCalculationFailed
let
txOutputRec = unwrap txOutput
txOutputValue :: Value
txOutputValue = txOutputRec.amount
newCoin :: Coin
newCoin = Coin $ max (valueToCoin' txOutputValue) txOutputMinAda
pure $ wrap txOutputRec
{ amount = mkValue newCoin (getNonAdaAsset txOutputValue) }
-- | Accounts for:
-- |
-- | - stake registration deposit
-- | - stake deregistration deposit returns
-- | - stake withdrawals fees
getStakingBalance :: Transaction -> Coin -> Coin
getStakingBalance tx depositLovelacesPerCert =
let
stakeDeposits :: BigInt
stakeDeposits =
(tx ^. _body <<< _certs) # fold
>>> map
case _ of
StakeRegistration _ -> unwrap depositLovelacesPerCert
StakeDeregistration _ -> negate $ unwrap depositLovelacesPerCert
_ -> zero
>>> sum
stakeWithdrawals =
unwrap $ fold $ fromMaybe Map.empty $ tx ^. _body <<<
_withdrawals
fee = stakeDeposits - stakeWithdrawals
in
Coin fee
-- | Generates change outputs to return all excess `Value` back to the owner's
-- | address. If necessary, adds lovelaces to the generated change outputs to
-- | cover the utxo min-ada-value requirement.
-- |
-- | If the `maxChangeOutputTokenQuantity` constraint is set, partitions the
-- | change `Value` into smaller `Value`s (where the Ada amount and the quantity
-- | of each token is equipartitioned across the resultant `Value`s; and no
-- | token quantity in any of the resultant `Value`s exceeds the given upper
-- | bound). Then builds `TransactionChangeOutput`s using those `Value`s.
genTransactionChangeOutputs
:: ChangeAddress
-> UtxoMap
-> Coin
-> UnattachedUnbalancedTx
-> BalanceTxM (Array TransactionChangeOutput)
genTransactionChangeOutputs changeAddress utxos certsFee tx = do
let
txBody :: TxBody
txBody = tx ^. _body'
totalInputValue :: Value
totalInputValue = getInputValue utxos txBody
changeValue :: Value
changeValue = posValue $
(totalInputValue <> mintValue txBody)
`minus`
( totalOutputValue txBody <> minFeeValue txBody <> coinToValue
certsFee
)
mkChangeOutput :: Value -> TransactionChangeOutput
mkChangeOutput amount =
TransactionOutput
{ address: changeAddress
, amount
, datum: NoOutputDatum
, scriptRef: Nothing
}
asksConstraints Constraints._maxChangeOutputTokenQuantity >>= case _ of
Nothing ->
Array.singleton <$>
addLovelacesToTransactionOutput (mkChangeOutput changeValue)
Just maxChangeOutputTokenQuantity ->
let
values :: NonEmptyArray Value
values =
equipartitionValueWithTokenQuantityUpperBound changeValue
maxChangeOutputTokenQuantity
in
traverse (addLovelacesToTransactionOutput <<< mkChangeOutput)
(NEArray.toArray values)
addTransactionChangeOutputs
:: ChangeAddress
-> UtxoMap
-> Coin
-> UnattachedUnbalancedTx
-> BalanceTxM PrebalancedTransaction
addTransactionChangeOutputs changeAddress utxos certsFee unbalancedTx = do
changeOutputs <- genTransactionChangeOutputs changeAddress utxos certsFee
unbalancedTx
pure $ PrebalancedTransaction $
unbalancedTx # _body' <<< _outputs %~ flip append changeOutputs
-- | Selects a combination of unspent transaction outputs from the wallet's
-- | utxo set so that the total input value is sufficient to cover all
-- | transaction outputs, including the change that will be generated
-- | when using that particular combination of inputs.
-- |
-- | Prerequisites:
-- | 1. Must be called with a transaction with no change output.
-- | 2. The `fee` field of a transaction body must be set.
addTransactionInputs
:: ChangeAddress
-> UtxoMap
-> Coin
-> UnattachedUnbalancedTx
-> BalanceTxM UnattachedUnbalancedTx
addTransactionInputs changeAddress utxos certsFee unbalancedTx = do
let
txBody :: TxBody
txBody = unbalancedTx ^. _body'
txInputs :: Set TransactionInput
txInputs = txBody ^. _inputs
nonMintedValue :: Value
nonMintedValue = totalOutputValue txBody `minus` mintValue txBody
txChangeOutputs <-
genTransactionChangeOutputs changeAddress utxos certsFee unbalancedTx
nonSpendableInputs <- asksConstraints Constraints._nonSpendableInputs
let
changeValue :: Value
changeValue = foldMap getAmount txChangeOutputs
requiredInputValue :: Value
requiredInputValue = nonMintedValue <> minFeeValue txBody <> changeValue <>
coinToValue certsFee
spendableUtxos :: UtxoMap
spendableUtxos =
flip Map.filterKeys utxos \oref -> not $
Set.member oref nonSpendableInputs
|| Set.member oref (txBody ^. _referenceInputs)
newTxInputs <-
except $ collectTransactionInputs txInputs spendableUtxos requiredInputValue
case newTxInputs == txInputs of
true ->
pure unbalancedTx
false ->
addTransactionInputs changeAddress utxos certsFee
(unbalancedTx # _body' <<< _inputs %~ Set.union newTxInputs)
collectTransactionInputs
:: Set TransactionInput
-> UtxoMap
-> Value
-> Either BalanceTxError (Set TransactionInput)
collectTransactionInputs originalTxIns utxos value = do
txInsValue <- updatedInputs >>= getTxInsValue utxos
updatedInputs' <- updatedInputs
case isSufficient updatedInputs' txInsValue of
true ->
pure $ Set.fromFoldable updatedInputs'
false ->
Left $ InsufficientTxInputs (Expected value) (Actual txInsValue)
where
updatedInputs :: Either BalanceTxError (Array TransactionInput)
updatedInputs =
foldl
( \newTxIns txIn -> do
txIns <- newTxIns
txInsValue <- getTxInsValue utxos txIns
case Array.elem txIn txIns || isSufficient txIns txInsValue of
true -> newTxIns
false ->
Right $ Array.insert txIn txIns -- treat as a set.
)
(Right $ Array.fromFoldable originalTxIns)
$ utxosToTransactionInput utxos
isSufficient :: Array TransactionInput -> Value -> Boolean
isSufficient txIns' txInsValue =
not (Array.null txIns') && txInsValue `geq` value
getTxInsValue
:: UtxoMap -> Array TransactionInput -> Either BalanceTxError Value
getTxInsValue utxos' =
map (Array.foldMap getAmount) <<<
traverse (\x -> note (UtxoLookupFailedFor x) $ Map.lookup x utxos')
utxosToTransactionInput :: UtxoMap -> Array TransactionInput
utxosToTransactionInput =
Array.mapMaybe (hush <<< getPublicKeyTransactionInput) <<< Map.toUnfoldable
getAmount :: TransactionOutput -> Value
getAmount = _.amount <<< unwrap
totalOutputValue :: TxBody -> Value
totalOutputValue txBody = foldMap getAmount (txBody ^. _outputs)
mintValue :: TxBody -> Value
mintValue txBody = maybe mempty (mkValue mempty <<< unwrap) (txBody ^. _mint)
minFeeValue :: TxBody -> Value
minFeeValue txBody = mkValue (txBody ^. _fee) mempty
posValue :: Value -> Value
posValue value = mkValue
(Coin $ max (valueToCoin' value) zero)
(posNonAdaAsset $ getNonAdaAsset value)
-- | Get `TransactionInput` such that it is associated to `PaymentCredentialKey`
-- | and not `PaymentCredentialScript`, i.e. we want wallets only
getPublicKeyTransactionInput
:: TransactionInput /\ TransactionOutput
-> Either BalanceTxError TransactionInput
getPublicKeyTransactionInput (txOutRef /\ txOut) =
note CouldNotConvertScriptOutputToTxInput $ do
paymentCred <- unwrap txOut # (_.address >>> addressPaymentCred)
-- TEST ME: using StakeCredential to determine whether wallet or script
paymentCred # withStakeCredential
{ onKeyHash: const $ pure txOutRef
, onScriptHash: const Nothing
}
getInputValue :: UtxoMap -> TxBody -> Value
getInputValue utxos (TxBody txBody) =
Array.foldMap
getAmount
( Array.mapMaybe (flip Map.lookup utxos)
<<< Array.fromFoldable
<<< _.inputs $ txBody
)
--------------------------------------------------------------------------------
-- Logging Helpers
--------------------------------------------------------------------------------
-- Logging for Transaction type without returning Transaction
logTx
:: forall (m :: Type -> Type)
. MonadEffect m
=> MonadLogger m
=> String
-> UtxoMap
-> Transaction
-> m Unit
logTx msg utxos (Transaction { body: body'@(TxBody body) }) =
traverse_ (Logger.trace (tag msg mempty))
[ "Input Value: " <> show (getInputValue utxos body')
, "Output Value: " <> show (Array.foldMap getAmount body.outputs)
, "Fees: " <> show body.fee
]