Skip to content

Commit

Permalink
Utils: Lovelace+L.Coin + use the era is >= Shelley
Browse files Browse the repository at this point in the history
Signed-off-by: Clément Hurlin <clement.hurlin@moduscreate.com>
  • Loading branch information
smelc committed Apr 29, 2024
1 parent 52c760b commit 9f8553f
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions cardano-faucet/src/Cardano/Faucet/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

module Cardano.Faucet.Utils where

import Cardano.Api (TxIn, TxOut(TxOut), CtxUTxO, Lovelace, CardanoEra, TxFee, txFeesExplicitInEra, TxFee(TxFeeImplicit, TxFeeExplicit), anyCardanoEra, TxValidityLowerBound(TxValidityNoLowerBound), TxValidityUpperBound(TxValidityNoUpperBound), validityNoUpperBoundSupportedInEra)
import Cardano.Api (TxIn, TxOut(TxOut), CtxUTxO, CardanoEra, TxFee (..), anyCardanoEra, defaultTxValidityUpperBound, TxValidityLowerBound(TxValidityNoLowerBound), TxValidityUpperBound, ShelleyBasedEra)
import Cardano.Faucet.Misc
import Cardano.Faucet.Types
import Cardano.Prelude hiding ((%))
import Control.Concurrent.STM (TMVar, takeTMVar, putTMVar)
import Control.Monad.Trans.Except.Extra (left)
import Data.Map.Strict qualified as Map
import qualified Prelude
import qualified Cardano.Api.Ledger as L

computeUtxoStats :: Map TxIn (TxOut CtxUTxO era) -> UtxoStats
computeUtxoStats utxo = do
Expand Down Expand Up @@ -66,30 +67,20 @@ findUtxoOfSize utxoTMVar value = do
Nothing -> throwSTM $ FaucetWebErrorUtxoNotFound value

validateTxFee ::
CardanoEra era
-> Maybe Lovelace
ShelleyBasedEra era
-> Maybe L.Coin
-> ExceptT FaucetWebError IO (TxFee era)
validateTxFee era mfee = case (txFeesExplicitInEra era, mfee) of
(Left implicit, Nothing) -> return (TxFeeImplicit implicit)
(Right explicit, Just fee) -> return (TxFeeExplicit explicit fee)
(Right _, Nothing) -> txFeatureMismatch era
(Left _, Just _) -> txFeatureMismatch era
validateTxFee sbe mfee =
case mfee of
Nothing -> txFeatureMismatch sbe -- Fees are explicit since Shelley
Just fee -> return $ TxFeeExplicit sbe fee

txFeatureMismatch ::
CardanoEra era
ShelleyBasedEra era
-> ExceptT FaucetWebError IO a
txFeatureMismatch era = left (FaucetWebErrorFeatureMismatch (anyCardanoEra era))
txFeatureMismatch era = left $ FaucetWebErrorFeatureMismatch -- (anyCardanoEra era))

noBoundsIfSupported ::
CardanoEra era
-> ExceptT FaucetWebError IO (TxValidityLowerBound era, TxValidityUpperBound era)
noBoundsIfSupported era = (,)
<$> pure TxValidityNoLowerBound
<*> noUpperBoundIfSupported era

noUpperBoundIfSupported ::
CardanoEra era
-> ExceptT FaucetWebError IO (TxValidityUpperBound era)
noUpperBoundIfSupported era = case validityNoUpperBoundSupportedInEra era of
Nothing -> txFeatureMismatch era
Just supported -> return (TxValidityNoUpperBound supported)
ShelleyBasedEra era
-> (TxValidityLowerBound era, TxValidityUpperBound era)
noBoundsIfSupported sbe = (TxValidityNoLowerBound, defaultTxValidityUpperBound sbe)

0 comments on commit 9f8553f

Please sign in to comment.