diff --git a/cardano-api/internal/Cardano/Api/Fees.hs b/cardano-api/internal/Cardano/Api/Fees.hs index 654ae049d4..032cabb100 100644 --- a/cardano-api/internal/Cardano/Api/Fees.hs +++ b/cardano-api/internal/Cardano/Api/Fees.hs @@ -272,7 +272,7 @@ estimateBalancedTxBody availableUTxOValue = mconcat [ totalUTxOValue - , negateValue (lovelaceToValue totalDeposits) + , negateValue (coinToValue totalDeposits) ] let change = toLedgerValue w $ calculateChangeValue sbe availableUTxOValue txbodycontent1 @@ -341,7 +341,7 @@ estimateBalancedTxBody , txTotalCollateral = reqCol } - let fakeUTxO = createFakeUTxO sbe txbodycontent1 $ selectLovelace availableUTxOValue + let fakeUTxO = createFakeUTxO sbe txbodycontent1 $ selectCoin availableUTxOValue balance = evaluateTransactionBalance sbe pparams poolids stakeDelegDeposits drepDelegDeposits fakeUTxO txbody2 -- check if the balance is positive or negative diff --git a/cardano-api/internal/Cardano/Api/Tx/Body.hs b/cardano-api/internal/Cardano/Api/Tx/Body.hs index f1fc18bb83..3ef72fe1b8 100644 --- a/cardano-api/internal/Cardano/Api/Tx/Body.hs +++ b/cardano-api/internal/Cardano/Api/Tx/Body.hs @@ -952,7 +952,7 @@ txOutValueToLovelace tv = txOutValueToValue :: TxOutValue era -> Value txOutValueToValue tv = case tv of - TxOutValueByron l -> lovelaceToValue l + TxOutValueByron l -> coinToValue l TxOutValueShelleyBased sbe v -> fromLedgerValue sbe v prettyRenderTxOut :: TxOutInAnyEra -> Text @@ -1683,7 +1683,7 @@ validateMintValue :: TxMintValue build era -> Either TxBodyError () validateMintValue txMintValue = case txMintValue of TxMintNone -> return () - TxMintValue _ v _ -> guard (selectLovelace v == 0) ?! TxBodyMintAdaError + TxMintValue _ v _ -> guard (selectCoin v == 0) ?! TxBodyMintAdaError inputIndexDoesNotExceedMax :: [(TxIn, a)] -> Either TxBodyError () inputIndexDoesNotExceedMax txIns = @@ -2171,8 +2171,8 @@ classifyRangeError :: TxOut CtxTx ByronEra -> TxBodyError classifyRangeError txout = case txout of TxOut (AddressInEra ByronAddressInAnyEra ByronAddress{}) (TxOutValueByron value) _ _ - | value < 0 -> TxBodyOutputNegative (lovelaceToQuantity value) (txOutInAnyEra ByronEra txout) - | otherwise -> TxBodyOutputOverflow (lovelaceToQuantity value) (txOutInAnyEra ByronEra txout) + | value < 0 -> TxBodyOutputNegative (coinToQuantity value) (txOutInAnyEra ByronEra txout) + | otherwise -> TxBodyOutputOverflow (coinToQuantity value) (txOutInAnyEra ByronEra txout) TxOut (AddressInEra ByronAddressInAnyEra (ByronAddress _)) (TxOutValueShelleyBased w _) _ _ -> case w of {} TxOut (AddressInEra (ShelleyAddressInEra sbe) ShelleyAddress{}) _ _ _ -> case sbe of {} diff --git a/cardano-api/internal/Cardano/Api/Value.hs b/cardano-api/internal/Cardano/Api/Value.hs index 10e0a1e45c..1b26bba494 100644 --- a/cardano-api/internal/Cardano/Api/Value.hs +++ b/cardano-api/internal/Cardano/Api/Value.hs @@ -27,10 +27,15 @@ module Cardano.Api.Value , calcMinimumDeposit -- ** Ada \/ L.Coin specifically + , quantityToCoin , quantityToLovelace + , coinToQuantity , lovelaceToQuantity + , selectCoin , selectLovelace + , coinToValue , lovelaceToValue + , valueToCoin , valueToLovelace -- ** Alternative nested representation @@ -124,11 +129,19 @@ instance Semigroup Quantity where instance Monoid Quantity where mempty = Quantity 0 +{-# DEPRECATED lovelaceToQuantity "Use 'coinToQuantity' instead." #-} lovelaceToQuantity :: L.Coin -> Quantity -lovelaceToQuantity (L.Coin x) = Quantity x +lovelaceToQuantity = coinToQuantity +coinToQuantity :: L.Coin -> Quantity +coinToQuantity (L.Coin x) = Quantity x + +{-# DEPRECATED quantityToLovelace "Use 'quantityToCoin' instead." #-} quantityToLovelace :: Quantity -> L.Coin -quantityToLovelace (Quantity x) = L.Coin x +quantityToLovelace = quantityToCoin + +quantityToCoin :: Quantity -> L.Coin +quantityToCoin (Quantity x) = L.Coin x newtype PolicyId = PolicyId {unPolicyId :: ScriptHash} deriving stock (Eq, Ord) @@ -243,22 +256,31 @@ negateLedgerValue sbe v = filterValue :: (AssetId -> Bool) -> Value -> Value filterValue p (Value m) = Value (Map.filterWithKey (\k _v -> p k) m) +{-# DEPRECATED selectLovelace "Use selectCoin instead." #-} selectLovelace :: Value -> L.Coin -selectLovelace = quantityToLovelace . flip selectAsset AdaAssetId +selectLovelace = selectCoin + +selectCoin :: Value -> L.Coin +selectCoin = quantityToLovelace . flip selectAsset AdaAssetId +{-# DEPRECATED lovelaceToValue "Use 'coinToValue' instead." #-} lovelaceToValue :: L.Coin -> Value -lovelaceToValue = Value . Map.singleton AdaAssetId . lovelaceToQuantity +lovelaceToValue = coinToValue coinToValue :: L.Coin -> Value -coinToValue = lovelaceToValue -- jky +coinToValue = Value . Map.singleton AdaAssetId . lovelaceToQuantity + +{-# DEPRECATED valueToLovelace "Use 'valueToCoin' instead." #-} +valueToLovelace :: Value -> Maybe L.Coin +valueToLovelace = valueToCoin -- | Check if the 'Value' consists of /only/ 'L.Coin' and no other assets, -- and if so then return the L.Coin. -- --- See also 'selectLovelace' to select the L.Coin quantity from the Value, +-- See also 'selectCoin' to select the L.Coin quantity from the Value, -- ignoring other assets. -valueToLovelace :: Value -> Maybe L.Coin -valueToLovelace v = +valueToCoin :: Value -> Maybe L.Coin +valueToCoin v = case valueToList v of [] -> Just (L.Coin 0) [(AdaAssetId, q)] -> Just (quantityToLovelace q) diff --git a/cardano-api/src/Cardano/Api.hs b/cardano-api/src/Cardano/Api.hs index 0baabb2da8..406af60a49 100644 --- a/cardano-api/src/Cardano/Api.hs +++ b/cardano-api/src/Cardano/Api.hs @@ -247,10 +247,15 @@ module Cardano.Api , fromLedgerValue -- ** Ada \/ Lovelace within multi-asset values + , quantityToCoin , quantityToLovelace + , coinToQuantity , lovelaceToQuantity + , selectCoin , selectLovelace + , coinToValue , lovelaceToValue + , valueToCoin , valueToLovelace -- * Blocks