forked from Plutonomicon/cardano-transaction-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTxChaining.purs
81 lines (67 loc) · 2.53 KB
/
TxChaining.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
-- | This module demonstrates how to enable transaction chaining when submitting
-- | multiple transactions in the correct order; and how subsequent transactions
-- | can use utxos created in the previous ones without waiting for them to
-- | leave the mempool and be included in the block.
module Ctl.Examples.TxChaining
( main
, example
, contract
) where
import Contract.Prelude
import Contract.BalanceTxConstraints
( BalanceTxConstraintsBuilder
, mustUseAdditionalUtxos
) as BalanceTxConstraints
import Contract.Config (ContractParams, testnetNamiConfig)
import Contract.Log (logInfo')
import Contract.Monad (Contract, launchAff_, liftedE, liftedM, runContract)
import Contract.PlutusData (PlutusData)
import Contract.ScriptLookups as Lookups
import Contract.Transaction
( awaitTxConfirmed
, balanceTxWithConstraints
, createAdditionalUtxos
, signTransaction
, submit
, withBalancedTx
)
import Contract.TxConstraints (TxConstraints)
import Contract.TxConstraints as Constraints
import Contract.Value as Value
import Contract.Wallet (ownPaymentPubKeyHashes)
import Data.Array (head)
import Data.BigInt as BigInt
main :: Effect Unit
main = example testnetNamiConfig
example :: ContractParams -> Effect Unit
example cfg = launchAff_ do
runContract cfg contract
contract :: Contract Unit
contract = do
pkh <- liftedM "Failed to get PKH" $ head <$> ownPaymentPubKeyHashes
let
constraints :: TxConstraints Unit Unit
constraints =
Constraints.mustPayToPubKey pkh
(Value.lovelaceValueOf $ BigInt.fromInt 1_000_000)
lookups0 :: Lookups.ScriptLookups PlutusData
lookups0 = mempty
unbalancedTx0 <- liftedE $ Lookups.mkUnbalancedTx lookups0 constraints
withBalancedTx unbalancedTx0 \balancedTx0 -> do
balancedSignedTx0 <- signTransaction balancedTx0
additionalUtxos <- createAdditionalUtxos balancedSignedTx0
logInfo' $ "Additional utxos: " <> show additionalUtxos
let
lookups1 :: Lookups.ScriptLookups PlutusData
lookups1 = Lookups.unspentOutputs additionalUtxos
balanceTxConstraints :: BalanceTxConstraints.BalanceTxConstraintsBuilder
balanceTxConstraints =
BalanceTxConstraints.mustUseAdditionalUtxos additionalUtxos
unbalancedTx1 <- liftedE $ Lookups.mkUnbalancedTx lookups1 constraints
balancedTx1 <-
liftedE $ balanceTxWithConstraints unbalancedTx1 balanceTxConstraints
balancedSignedTx1 <- signTransaction balancedTx1
txId0 <- submit balancedSignedTx0
txId1 <- submit balancedSignedTx1
awaitTxConfirmed txId0
awaitTxConfirmed txId1