Skip to content

Commit c1918de

Browse files
authored
Merge pull request #83 from mlabs-haskell/nazrhom/await-tx-status-change
Add support for AwaitTxStatusChangeReq contract request
2 parents e52d857 + ff9db92 commit c1918de

File tree

17 files changed

+454
-286
lines changed

17 files changed

+454
-286
lines changed

bot-plutus-interface.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ library
8585
BotPlutusInterface.Types
8686
BotPlutusInterface.UtxoParser
8787
BotPlutusInterface.Server
88+
BotPlutusInterface.Helpers
8889
build-depends:
8990
, aeson ^>=1.5.0.0
9091
, attoparsec >=0.13.2.2

examples/plutus-game/app/Main.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ main = do
6161
, pcTipPollingInterval = 10_000_000
6262
, pcSlotConfig = def
6363
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
64+
, pcOwnStakePubKeyHash = Nothing
6465
, pcScriptFileDir = "./scripts"
6566
, pcSigningKeyFileDir = "./signing-keys"
6667
, pcTxFileDir = "./txs"
6768
, pcDryRun = True
6869
, pcLogLevel = Debug
6970
, pcProtocolParamsFile = "./protocol.json"
70-
, pcForceBudget = Just (1000, 1000)
71+
, pcForceBudget = Just (9_000_000_000, 15_000_000)
7172
, pcEnableTxEndpoint = True
7273
}
7374
BotPlutusInterface.runPAB @GameContracts pabConf

examples/plutus-game/guess.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CONTRACT_INST_ID=$(curl --location --request POST 'localhost:9080/api/contract/a
66
"caID": {
77
"tag": "Guess",
88
"contents": {
9-
"guessGameId": 2,
9+
"guessGameId": 3,
1010
"guessSecret": "secret"
1111
}
1212
}

examples/plutus-game/lock.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CONTRACT_INST_ID=$(curl --location --request POST 'localhost:9080/api/contract/a
66
"caID": {
77
"tag": "Lock",
88
"contents": {
9-
"lockGameId": 2,
9+
"lockGameId": 3,
1010
"lockAmount": 1000000,
1111
"lockSecret": "secret"
1212
}

examples/plutus-game/plutus-game.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ library
8181
, cardano-crypto
8282
, cardano-ledger-alonzo
8383
, containers
84+
, bot-plutus-interface
8485
, data-default
8586
, data-default-class
8687
, directory

examples/plutus-game/protocol.json

Lines changed: 169 additions & 169 deletions
Large diffs are not rendered by default.

examples/plutus-nft/app/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ main = do
6161
, pcTipPollingInterval = 10_000_000
6262
, pcSlotConfig = def
6363
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
64+
, pcOwnStakePubKeyHash = Nothing
6465
, pcScriptFileDir = "./scripts"
6566
, pcSigningKeyFileDir = "./signing-keys"
6667
, pcTxFileDir = "./txs"

examples/plutus-transfer/app/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ main = do
6060
, pcTipPollingInterval = 10_000_000
6161
, pcSlotConfig = def
6262
, pcOwnPubKeyHash = "0f45aaf1b2959db6e5ff94dbb1f823bf257680c3c723ac2d49f97546"
63+
, pcOwnStakePubKeyHash = Nothing
6364
, pcScriptFileDir = "./scripts"
6465
, pcSigningKeyFileDir = "./signing-keys"
6566
, pcTxFileDir = "./txs"

src/BotPlutusInterface/Balance.hs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ balanceTxIO ::
7373
balanceTxIO pabConf ownPkh unbalancedTx =
7474
runEitherT $
7575
do
76-
utxos <- newEitherT $ CardanoCLI.utxosAt @w pabConf $ Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing
76+
utxos <- newEitherT $ CardanoCLI.utxosAt @w pabConf changeAddr
7777
privKeys <- newEitherT $ Files.readPrivateKeys @w pabConf
7878
let utxoIndex = fmap Tx.toTxOut utxos <> unBalancedTxUtxoIndex unbalancedTx
7979
requiredSigs = map Ledger.unPaymentPubKeyHash $ Map.keys (unBalancedTxRequiredSignatories unbalancedTx)
@@ -101,17 +101,19 @@ balanceTxIO pabConf ownPkh unbalancedTx =
101101
-- If we have change but no change UTxO, we need to add an output for it
102102
-- We'll add a minimal output, run the loop again so it gets minUTxO, then update change
103103
balancedTxWithChange <-
104-
if adaChange /= 0 && not (hasChangeUTxO ownPkh balancedTx)
105-
then fst <$> loop utxoIndex privKeys minUtxos (addOutput ownPkh balancedTx)
104+
if adaChange /= 0 && not (hasChangeUTxO changeAddr balancedTx)
105+
then fst <$> loop utxoIndex privKeys minUtxos (addOutput changeAddr balancedTx)
106106
else pure balancedTx
107107

108108
-- Get the updated change, add it to the tx
109109
let finalAdaChange = getAdaChange utxoIndex balancedTxWithChange
110-
fullyBalancedTx = addAdaChange ownPkh finalAdaChange balancedTxWithChange
110+
fullyBalancedTx = addAdaChange changeAddr finalAdaChange balancedTxWithChange
111111

112112
-- finally, we must update the signatories
113113
hoistEither $ addSignatories ownPkh privKeys requiredSigs fullyBalancedTx
114114
where
115+
changeAddr :: Address
116+
changeAddr = Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) (pabConf.pcOwnStakePubKeyHash)
115117
loop ::
116118
Map TxOutRef TxOut ->
117119
Map PubKeyHash DummyPrivKey ->
@@ -130,7 +132,7 @@ balanceTxIO pabConf ownPkh unbalancedTx =
130132

131133
-- Calculate fees by pre-balancing the tx, building it, and running the CLI on result
132134
txWithoutFees <-
133-
hoistEither $ balanceTxStep minUtxos utxoIndex ownPkh $ tx `withFee` 0
135+
hoistEither $ balanceTxStep minUtxos utxoIndex changeAddr $ tx `withFee` 0
134136

135137
exBudget <- newEitherT $ CardanoCLI.buildTx @w pabConf privKeys txWithoutFees
136138
nonBudgettedFees <- newEitherT $ CardanoCLI.calculateMinFee @w pabConf txWithoutFees
@@ -140,7 +142,7 @@ balanceTxIO pabConf ownPkh unbalancedTx =
140142
lift $ printLog @w Debug $ "Fees: " ++ show fees
141143

142144
-- Rebalance the initial tx with the above fees
143-
balancedTx <- hoistEither $ balanceTxStep minUtxos utxoIndex ownPkh $ tx `withFee` fees
145+
balancedTx <- hoistEither $ balanceTxStep minUtxos utxoIndex changeAddr $ tx `withFee` fees
144146

145147
if balancedTx == tx
146148
then pure (balancedTx, minUtxos)
@@ -175,13 +177,13 @@ calculateMinUtxos pabConf datums txOuts =
175177
balanceTxStep ::
176178
[(TxOut, Integer)] ->
177179
Map TxOutRef TxOut ->
178-
PubKeyHash ->
180+
Address ->
179181
Tx ->
180182
Either Text Tx
181-
balanceTxStep minUtxos utxos ownPkh tx =
183+
balanceTxStep minUtxos utxos changeAddr tx =
182184
Right (addLovelaces minUtxos tx)
183185
>>= balanceTxIns utxos
184-
>>= handleNonAdaChange ownPkh utxos
186+
>>= handleNonAdaChange changeAddr utxos
185187

186188
-- | Get change value of a transaction, taking inputs, outputs, mint and fees into account
187189
getChange :: Map TxOutRef TxOut -> Tx -> Value
@@ -288,10 +290,9 @@ addTxCollaterals utxos tx = do
288290
filterAdaOnly = Map.filter (isAdaOnly . txOutValue)
289291

290292
-- | Ensures all non ada change goes back to user
291-
handleNonAdaChange :: PubKeyHash -> Map TxOutRef TxOut -> Tx -> Either Text Tx
292-
handleNonAdaChange ownPkh utxos tx =
293-
let changeAddr = Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing
294-
nonAdaChange = getNonAdaChange utxos tx
293+
handleNonAdaChange :: Address -> Map TxOutRef TxOut -> Tx -> Either Text Tx
294+
handleNonAdaChange changeAddr utxos tx =
295+
let nonAdaChange = getNonAdaChange utxos tx
295296
outputs =
296297
case partition ((==) changeAddr . Tx.txOutAddress) $ txOutputs tx of
297298
([], txOuts) ->
@@ -307,38 +308,33 @@ handleNonAdaChange ownPkh utxos tx =
307308
then Right $ if Value.isZero nonAdaChange then tx else tx {txOutputs = outputs}
308309
else Left "Not enough inputs to balance tokens."
309310

310-
hasChangeUTxO :: PubKeyHash -> Tx -> Bool
311-
hasChangeUTxO ownPkh tx =
311+
hasChangeUTxO :: Address -> Tx -> Bool
312+
hasChangeUTxO changeAddr tx =
312313
any ((==) changeAddr . Tx.txOutAddress) $ txOutputs tx
313-
where
314-
changeAddr = Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing
315314

316315
-- | Adds ada change to a transaction, assuming there is already an output going to ownPkh. Otherwise, this is identity
317-
addAdaChange :: PubKeyHash -> Integer -> Tx -> Tx
318-
addAdaChange ownPkh change tx =
316+
addAdaChange :: Address -> Integer -> Tx -> Tx
317+
addAdaChange changeAddr change tx =
319318
tx
320319
{ txOutputs =
321320
case partition ((==) changeAddr . Tx.txOutAddress) $ txOutputs tx of
322321
(txOut@TxOut {txOutValue = v} : txOuts, txOuts') ->
323322
txOut {txOutValue = v <> Ada.lovelaceValueOf change} : (txOuts <> txOuts')
324323
_ -> txOutputs tx
325324
}
326-
where
327-
changeAddr = Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing
328325

329326
-- | Adds a 1 lovelace output to a transaction
330-
addOutput :: PubKeyHash -> Tx -> Tx
331-
addOutput ownPkh tx = tx {txOutputs = changeTxOut : txOutputs tx}
327+
addOutput :: Address -> Tx -> Tx
328+
addOutput changeAddr tx = tx {txOutputs = changeTxOut : txOutputs tx}
332329
where
333-
changeAddr = Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing
334330
changeTxOut =
335331
TxOut
336332
{ txOutAddress = changeAddr
337333
, txOutValue = Ada.lovelaceValueOf 1
338334
, txOutDatumHash = Nothing
339335
}
340336

341-
{- | Add the required signatorioes to the transaction. Be aware the the signature itself is invalid,
337+
{- | Add the required signatories to the transaction. Be aware the the signature itself is invalid,
342338
and will be ignored. Only the pub key hashes are used, mapped to signing key files on disk.
343339
-}
344340
addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash] -> Tx -> Either Text Tx

src/BotPlutusInterface/CardanoCLI.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import Ledger.Tx (
7676
TxInType (..),
7777
TxOut (..),
7878
TxOutRef (..),
79+
txId,
7980
)
8081
import Ledger.TxId (TxId (..))
8182
import Ledger.Value (Value)
@@ -176,7 +177,7 @@ calculateMinFee pabConf tx =
176177
, cmdArgs =
177178
mconcat
178179
[ ["transaction", "calculate-min-fee"]
179-
, ["--tx-body-file", txFilePath pabConf "raw" tx]
180+
, ["--tx-body-file", txFilePath pabConf "raw" (txId tx)]
180181
, ["--tx-in-count", showText $ length $ txInputs tx]
181182
, ["--tx-out-count", showText $ length $ txOutputs tx]
182183
, ["--witness-count", showText $ length $ txSignatures tx]
@@ -295,7 +296,7 @@ buildTx pabConf privKeys tx = do
295296
, ["--fee", showText . getLovelace . fromValue $ txFee tx]
296297
, mconcat
297298
[ ["--protocol-params-file", pabConf.pcProtocolParamsFile]
298-
, ["--out-file", txFilePath pabConf "raw" tx]
299+
, ["--out-file", txFilePath pabConf "raw" (txId tx)]
299300
]
300301
]
301302

@@ -318,9 +319,9 @@ signTx pabConf tx pubKeys =
318319
opts =
319320
mconcat
320321
[ ["transaction", "sign"]
321-
, ["--tx-body-file", txFilePath pabConf "raw" tx]
322+
, ["--tx-body-file", txFilePath pabConf "raw" (txId tx)]
322323
, signingKeyFiles
323-
, ["--out-file", txFilePath pabConf "signed" tx]
324+
, ["--out-file", txFilePath pabConf "signed" (txId tx)]
324325
]
325326

326327
budgetFromConfig :: PABConfig -> ExBudget -> ExBudget
@@ -343,7 +344,7 @@ submitTx pabConf tx =
343344
"cardano-cli"
344345
( mconcat
345346
[ ["transaction", "submit"]
346-
, ["--tx-file", txFilePath pabConf "signed" tx]
347+
, ["--tx-file", txFilePath pabConf "signed" (txId tx)]
347348
, networkOpt pabConf
348349
]
349350
)
@@ -480,8 +481,8 @@ networkOpt pabConf = case pabConf.pcNetwork of
480481
Mainnet -> ["--mainnet"]
481482

482483
txOutRefToCliArg :: TxOutRef -> Text
483-
txOutRefToCliArg (TxOutRef (TxId txId) txIx) =
484-
encodeByteString (fromBuiltin txId) <> "#" <> showText txIx
484+
txOutRefToCliArg (TxOutRef (TxId tId) txIx) =
485+
encodeByteString (fromBuiltin tId) <> "#" <> showText txIx
485486

486487
flatValueToCliArg :: (CurrencySymbol, TokenName, Integer) -> Text
487488
flatValueToCliArg (curSymbol, name, amount)

0 commit comments

Comments
 (0)