@@ -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
219211balanceTx ::
0 commit comments