@@ -182,28 +182,32 @@ mkChain tracer queryTimeHandle wallet ctx LocalChainState{getLatest} submitTx =
182
182
, draftCommitTx = \ headId commitBlueprintTx amount -> do
183
183
ChainStateAt {spendableUTxO} <- atomically getLatest
184
184
let CommitBlueprintTx {lookupUTxO} = commitBlueprintTx
185
- traverse (finalizeTx wallet ctx spendableUTxO lookupUTxO) $
185
+ traverse (finalizeTx wallet ctx spendableUTxO lookupUTxO) $ do
186
+ checkAmount lookupUTxO amount
186
187
commit' ctx headId spendableUTxO commitBlueprintTx amount
187
188
, draftDepositTx = \ headId pparams commitBlueprintTx deadline amount -> do
188
189
let CommitBlueprintTx {lookupUTxO} = commitBlueprintTx
189
190
ChainStateAt {spendableUTxO} <- atomically getLatest
190
191
TimeHandle {currentPointInTime} <- queryTimeHandle
191
192
-- XXX: What an error handling mess
192
- runExceptT $ do
193
- liftEither $ rejectLowDeposits pparams lookupUTxO amount
194
- (currentSlot, currentTime) <- case currentPointInTime of
195
- Left failureReason -> throwError FailedToConstructDepositTx {failureReason}
196
- Right (s, t) -> pure (s, t)
197
- -- NOTE: Use a smaller upper bound than maxGraceTime to allow for
198
- -- shorter than 200 slot deposit periods. This is only important on
199
- -- fast moving networks (e.g. in testing). XXX: Making maxGraceTime
200
- -- configurable would avoid this.
201
- let untilDeadline = diffUTCTime deadline currentTime
202
- let graceTime = maxGraceTime `min` untilDeadline / 2
203
- -- -- NOTE: But also not make it smaller than 10 slots.
204
- let validBeforeSlot = currentSlot + fromInteger (truncate graceTime `max` 10 )
205
- lift . finalizeTx wallet ctx spendableUTxO lookupUTxO $
206
- depositTx (networkId ctx) headId commitBlueprintTx validBeforeSlot deadline amount
193
+ runExceptT $
194
+ do
195
+ liftEither $ do
196
+ checkAmount lookupUTxO amount
197
+ rejectLowDeposits pparams lookupUTxO amount
198
+ (currentSlot, currentTime) <- case currentPointInTime of
199
+ Left failureReason -> throwError FailedToConstructDepositTx {failureReason}
200
+ Right (s, t) -> pure (s, t)
201
+ -- NOTE: Use a smaller upper bound than maxGraceTime to allow for
202
+ -- shorter than 200 slot deposit periods. This is only important on
203
+ -- fast moving networks (e.g. in testing). XXX: Making maxGraceTime
204
+ -- configurable would avoid this.
205
+ let untilDeadline = diffUTCTime deadline currentTime
206
+ let graceTime = maxGraceTime `min` untilDeadline / 2
207
+ -- -- NOTE: But also not make it smaller than 10 slots.
208
+ let validBeforeSlot = currentSlot + fromInteger (truncate graceTime `max` 10 )
209
+ lift . finalizeTx wallet ctx spendableUTxO lookupUTxO $
210
+ depositTx (networkId ctx) headId commitBlueprintTx validBeforeSlot deadline amount
207
211
, -- Submit a cardano transaction to the cardano-node using the
208
212
-- LocalTxSubmission protocol.
209
213
submitTx
@@ -228,6 +232,15 @@ rejectLowDeposits pparams utxo amount = do
228
232
[] -> pure ()
229
233
(e : _) -> Left e
230
234
235
+ checkAmount :: UTxO. UTxO -> Maybe Coin -> Either (PostTxError Tx ) ()
236
+ checkAmount utxo amount =
237
+ case amount of
238
+ Nothing -> pure ()
239
+ Just amt -> do
240
+ let totalLovelace = UTxO. totalLovelace utxo
241
+ when (totalLovelace < amt) $
242
+ Left (AmountTooLow {providedValue = amt, totalUTxOValue = totalLovelace} :: PostTxError Tx )
243
+
231
244
-- | Balance and sign the given partial transaction.
232
245
finalizeTx ::
233
246
MonadThrow m =>
0 commit comments