Skip to content

Commit

Permalink
Value: deprecate some Lovelace functions. Advice to use Coin name ins…
Browse files Browse the repository at this point in the history
…tead.
  • Loading branch information
smelc committed Aug 8, 2024
1 parent 31e67e2 commit 3611d79
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cardano-api/internal/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ estimateBalancedTxBody
availableUTxOValue =
mconcat
[ totalUTxOValue
, negateValue (lovelaceToValue totalDeposits)
, negateValue (coinToValue totalDeposits)
]

let change = toLedgerValue w $ calculateChangeValue sbe availableUTxOValue txbodycontent1
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions cardano-api/internal/Cardano/Api/Tx/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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 {}

Expand Down
38 changes: 30 additions & 8 deletions cardano-api/internal/Cardano/Api/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3611d79

Please sign in to comment.