Skip to content

Commit 06335e2

Browse files
author
gege251
committed
Validate range before prebalancing tx
1 parent a2d2896 commit 06335e2

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/BotPlutusInterface/CardanoCLI.hs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ module BotPlutusInterface.CardanoCLI (
1212
validatorScriptFilePath,
1313
unsafeSerialiseAddress,
1414
policyScriptFilePath,
15-
validateRange,
1615
utxosAt,
1716
queryTip,
1817
) where
@@ -56,7 +55,7 @@ import Ledger.Ada qualified as Ada
5655
import Ledger.Address (Address (..))
5756
import Ledger.Crypto (PubKey, PubKeyHash)
5857
import Ledger.Interval (
59-
Extended (Finite, NegInf, PosInf),
58+
Extended (Finite),
6059
Interval (Interval),
6160
LowerBound (LowerBound),
6261
UpperBound (UpperBound),
@@ -438,14 +437,6 @@ exBudgetToCliArg (ExBudget (ExCPU steps) (ExMemory memory)) =
438437
showText :: forall (a :: Type). Show a => a -> Text
439438
showText = Text.pack . show
440439

441-
validateRange :: SlotRange -> Bool
442-
validateRange (Interval (LowerBound PosInf _) _) = False
443-
validateRange (Interval _ (UpperBound NegInf _)) = False
444-
validateRange (Interval (LowerBound (Finite lowerBound) _) (UpperBound (Finite upperBound) _))
445-
| lowerBound >= upperBound = False
446-
| otherwise = True
447-
validateRange _ = True
448-
449440
-- -- TODO: There is some issue with this function, the generated wallet key is incorrect
450441
-- toWalletKey :: Wallet -> Text
451442
-- toWalletKey =

src/BotPlutusInterface/PreBalance.hs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ import Ledger.Ada qualified as Ada
3030
import Ledger.Address (Address (..))
3131
import Ledger.Constraints.OffChain (UnbalancedTx (..), fromScriptOutput)
3232
import Ledger.Crypto (PrivateKey, PubKeyHash)
33+
import Ledger.Interval (
34+
Extended (Finite, NegInf, PosInf),
35+
Interval (Interval),
36+
LowerBound (LowerBound),
37+
UpperBound (UpperBound),
38+
)
3339
import Ledger.Scripts (Datum, DatumHash)
3440
import Ledger.Time (POSIXTimeRange)
3541
import Ledger.TimeSlot (posixTimeRangeToContainedSlotRange)
@@ -67,8 +73,12 @@ preBalanceTxIO pabConf ownPkh unbalancedTx =
6773
utxos <- lift $ CardanoCLI.utxosAt @w pabConf $ Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing
6874
privKeys <- newEitherT $ Files.readPrivateKeys @w pabConf
6975
let utxoIndex = fmap Tx.toTxOut utxos <> fmap (Ledger.toTxOut . fromScriptOutput) (unBalancedTxUtxoIndex unbalancedTx)
70-
tx = addValidRange (unBalancedTxValidityTimeRange unbalancedTx) (unBalancedTxTx unbalancedTx)
7176
requiredSigs = map Ledger.unPaymentPubKeyHash $ Map.keys (unBalancedTxRequiredSignatories unbalancedTx)
77+
tx <-
78+
hoistEither $
79+
addValidRange
80+
(unBalancedTxValidityTimeRange unbalancedTx)
81+
(unBalancedTxTx unbalancedTx)
7282

7383
lift $ printLog @w Debug $ show utxoIndex
7484

@@ -262,9 +272,19 @@ addSignatories ownPkh privKeys pkhs tx =
262272
tx
263273
(ownPkh : pkhs)
264274

265-
addValidRange :: POSIXTimeRange -> Tx -> Tx
275+
addValidRange :: POSIXTimeRange -> Tx -> Either Text Tx
266276
addValidRange timeRange tx =
267-
tx {txValidRange = posixTimeRangeToContainedSlotRange def timeRange}
277+
if validateRange timeRange
278+
then Right $ tx {txValidRange = posixTimeRangeToContainedSlotRange def timeRange}
279+
else Left "Invalid validity interval."
280+
281+
validateRange :: forall (a :: Type). Ord a => Interval a -> Bool
282+
validateRange (Interval (LowerBound PosInf _) _) = False
283+
validateRange (Interval _ (UpperBound NegInf _)) = False
284+
validateRange (Interval (LowerBound (Finite lowerBound) _) (UpperBound (Finite upperBound) _))
285+
| lowerBound >= upperBound = False
286+
| otherwise = True
287+
validateRange _ = True
268288

269289
showText :: forall (a :: Type). Show a => a -> Text
270290
showText = Text.pack . show

0 commit comments

Comments
 (0)