Skip to content

Commit 885372f

Browse files
committed
Add awaitTxConfirmedUntilSlot and move the initial wait for the chain-index response to the contract
Change-type: patch Signed-off-by: Giovanni Garufi <giovanni@mlabs.city>
1 parent fd77f79 commit 885372f

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
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/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

src/BotPlutusInterface/Contract.hs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -179,41 +179,33 @@ awaitTxStatusChange contractEnv txId = do
179179
-- The depth (in blocks) after which a transaction cannot be rolled back anymore (from Plutus.ChainIndex.TxIdState)
180180
let chainConstant = 8
181181

182-
ciTxState <- findChainIndexTxLoop
183-
case ciTxState of
182+
mTx <- queryChainIndexForTxState
183+
case mTx of
184184
Nothing -> pure Unknown
185185
Just txState -> do
186-
awaitNBlocks @w contractEnv chainConstant
186+
awaitNBlocks @w contractEnv (chainConstant + 1)
187187
-- Check if the tx is still present in chain-index, in case of a rollback
188188
-- we might not find it anymore.
189-
ciTxState' <- findChainIndexTxLoop
189+
ciTxState' <- queryChainIndexForTxState
190190
case ciTxState' of
191191
Nothing -> pure Unknown
192192
Just _ -> do
193193
blk <- fromInteger <$> currentBlock contractEnv
194194
-- This will set the validity correctly based on the txState.
195-
-- The tx will always be committed, as we wait for chainConstant blocks
195+
-- The tx will always be committed, as we wait for chainConstant + 1 blocks
196196
let status = transactionStatus blk txState txId
197197
pure $ fromRight Unknown status
198198
where
199199
-- Attempts to find the tx in chain index. If the tx does not appear after
200200
-- 5 blocks we give up
201-
findChainIndexTxLoop :: Eff effs (Maybe TxIdState)
202-
findChainIndexTxLoop = go 0
203-
where
204-
go :: Int -> Eff effs (Maybe TxIdState)
205-
go n = do
206-
mTx <- join . preview _TxIdResponse <$> (queryChainIndex @w $ TxFromTxId txId)
207-
case mTx of
208-
Just tx -> do
209-
blk <- fromInteger <$> currentBlock contractEnv
210-
pure . Just $ fromTx blk tx
211-
Nothing -> do
212-
if n >= 5
213-
then pure Nothing
214-
else do
215-
_ <- awaitNBlocks @w contractEnv 1
216-
go (n + 1)
201+
queryChainIndexForTxState :: Eff effs (Maybe TxIdState)
202+
queryChainIndexForTxState = do
203+
mTx <- join . preview _TxIdResponse <$> (queryChainIndex @w $ TxFromTxId txId)
204+
case mTx of
205+
Just tx -> do
206+
blk <- fromInteger <$> currentBlock contractEnv
207+
pure . Just $ fromTx blk tx
208+
Nothing -> pure Nothing
217209

218210
-- | This will FULLY balance a transaction
219211
balanceTx ::

src/BotPlutusInterface/Helpers.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module BotPlutusInterface.Helpers (awaitTxConfirmedUntilSlot) where
2+
3+
import Control.Lens (review)
4+
import Data.Text (pack)
5+
import Ledger (Slot, TxId)
6+
import Plutus.Contract.Error (AsContractError, _OtherContractError)
7+
import Plutus.Contract.Request (RollbackState (Unknown), awaitTxStatusChange, currentSlot, waitNSlots)
8+
import Plutus.Contract.Types (Contract, throwError)
9+
import Prelude
10+
11+
awaitTxConfirmedUntilSlot :: forall w s e. (AsContractError e) => TxId -> Slot -> Contract w s e ()
12+
awaitTxConfirmedUntilSlot i maxSlot = go 0
13+
where
14+
go :: Integer -> Contract w s e ()
15+
go n = do
16+
mTx <- awaitTxStatusChange i
17+
case mTx of
18+
Unknown -> do
19+
curSlot <- currentSlot
20+
if curSlot >= maxSlot
21+
then throwError @e $ review _OtherContractError $ pack $ "Could not find tx after maxAttempts. ID: " ++ show i
22+
else do
23+
_ <- waitNSlots 20
24+
go (n + 1)
25+
_ -> pure ()

0 commit comments

Comments
 (0)