|
1 | | -module BotPlutusInterface.Helpers (awaitTxConfirmedUntilSlot) where |
| 1 | +module BotPlutusInterface.Helpers (awaitTxConfirmedUntilTime) where |
2 | 2 |
|
3 | 3 | import Control.Lens (review) |
| 4 | +import Control.Monad (void) |
| 5 | +import Data.Kind (Type) |
| 6 | +import Data.Row (Row) |
4 | 7 | import Data.Text (pack) |
5 | | -import Ledger (Slot, TxId) |
| 8 | +import Ledger (POSIXTime, TxId) |
6 | 9 | import Plutus.Contract.Error (AsContractError, _OtherContractError) |
7 | | -import Plutus.Contract.Request (RollbackState (Unknown), awaitTxStatusChange, currentSlot, waitNSlots) |
| 10 | +import Plutus.Contract.Request (RollbackState (Unknown), awaitTxStatusChange, currentTime, waitNSlots) |
8 | 11 | import Plutus.Contract.Types (Contract, throwError) |
9 | 12 | import Prelude |
10 | 13 |
|
11 | | -awaitTxConfirmedUntilSlot :: forall w s e. (AsContractError e) => TxId -> Slot -> Contract w s e () |
12 | | -awaitTxConfirmedUntilSlot txId maxSlot = go 0 |
13 | | - where |
14 | | - go :: Integer -> Contract w s e () |
15 | | - go n = do |
16 | | - mTx <- awaitTxStatusChange txId |
17 | | - case mTx of |
18 | | - Unknown -> do |
19 | | - curSlot <- currentSlot |
20 | | - if curSlot > maxSlot |
21 | | - then |
22 | | - throwError @e $ |
23 | | - review _OtherContractError $ |
24 | | - pack $ |
25 | | - "Could not find transaction - " ++ show txId ++ " - before slot " ++ show maxSlot |
26 | | - else do |
27 | | - _ <- waitNSlots 20 |
28 | | - go (n + 1) |
29 | | - _ -> pure () |
| 14 | +awaitTxConfirmedUntilTime :: forall (w :: Type) (s :: Row Type) (e :: Type). (AsContractError e) => TxId -> POSIXTime -> Contract w s e () |
| 15 | +awaitTxConfirmedUntilTime txId maxTime = do |
| 16 | + mTx <- awaitTxStatusChange txId |
| 17 | + case mTx of |
| 18 | + Unknown -> do |
| 19 | + curTime <- currentTime |
| 20 | + if curTime > maxTime |
| 21 | + then |
| 22 | + throwError $ |
| 23 | + review _OtherContractError $ |
| 24 | + pack $ |
| 25 | + "Could not find transaction - " ++ show txId ++ " - before " ++ show maxTime |
| 26 | + else do |
| 27 | + void $ waitNSlots 20 |
| 28 | + awaitTxConfirmedUntilTime txId maxTime |
| 29 | + _ -> pure () |
0 commit comments