Skip to content

Commit

Permalink
Fix deprecation warnings (#221)
Browse files Browse the repository at this point in the history
* Use GHC.IsList.toList / fromList for Value
* Fix remaining deprecation warnings
  • Loading branch information
j-mueller authored Oct 16, 2024
1 parent 10cc195 commit a5a54b8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/base/lib/Convex/BuildTx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-deprecations #-} -- see https://github.com/j-mueller/sc-tools/issues/213
{-| Building transactions
-}
module Convex.BuildTx(
Expand Down Expand Up @@ -142,6 +141,7 @@ import Data.List (nub)
import qualified Data.Map as Map
import Data.Maybe (fromJust)
import qualified Data.Set as Set
import GHC.IsList (IsList (fromList))
import qualified PlutusLedgerApi.V1 as Plutus

type TxBody era = C.TxBodyContent C.BuildTx era
Expand Down Expand Up @@ -445,7 +445,7 @@ mintPlutus script red assetName quantity =
-}
assetValue :: ScriptHash -> C.AssetName -> C.Quantity -> C.Value
assetValue hsh assetName quantity =
C.valueFromList [(C.AssetId (C.PolicyId hsh) assetName, quantity)]
fromList [(C.AssetId (C.PolicyId hsh) assetName, quantity)]

mintPlutusRef :: forall redeemer lang era m.
(Plutus.ToData redeemer, MonadBuildTx era m, C.HasScriptLanguageInEra lang era, C.IsBabbageBasedEra era)
Expand Down
6 changes: 3 additions & 3 deletions src/base/lib/Convex/PlutusLedger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-deprecations #-} -- see https://github.com/j-mueller/sc-tools/issues/213
{-| Translating between cardano-api/cardano-ledger and plutus representations
-}
module Convex.PlutusLedger(
Expand Down Expand Up @@ -76,6 +75,7 @@ import Data.ByteString.Short (fromShort)
import qualified Data.ByteString.Short as Short
import Data.Functor ((<&>))
import Data.Time.Clock.POSIX (POSIXTime)
import GHC.IsList (IsList (fromList, toList))
import PlutusLedgerApi.Common (SerialisedScript)
import qualified PlutusLedgerApi.V1 as PV1
import qualified PlutusLedgerApi.V1.Scripts as P
Expand Down Expand Up @@ -221,7 +221,7 @@ unTransTxOutValue value = C.TxOutValueShelleyBased C.ShelleyBasedEraConway . C.t

unTransValue :: PV1.Value -> Either C.SerialiseAsRawBytesError C.Value
unTransValue =
fmap C.valueFromList . traverse toSingleton . Value.flattenValue
fmap fromList . traverse toSingleton . Value.flattenValue
where
toSingleton (cs, tn, q) =
unTransAssetId (Value.assetClass cs tn) <&> (, C.Quantity q)
Expand All @@ -231,7 +231,7 @@ transValue =
let t (assetId, C.Quantity quantity) =
let Value.AssetClass (sym, tn) = transAssetId assetId
in (sym, Map.singleton tn quantity)
in PV1.Value . Map.safeFromList . fmap t . C.valueToList
in PV1.Value . Map.safeFromList . fmap t . toList

unTransPlutusScript
:: C.SerialiseAsRawBytes plutusScript
Expand Down
4 changes: 2 additions & 2 deletions src/base/lib/Convex/Utxos.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

-- FIXME (koslambrou) Remove once we have the newtype for 'AnyCardanoEra TxOut'.
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_GHC -Wno-deprecations #-} -- see https://github.com/j-mueller/sc-tools/issues/213
{-# LANGUAGE TypeApplications #-}

module Convex.Utxos(
Expand Down Expand Up @@ -108,6 +107,7 @@ import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as Text
import GHC.IsList (IsList (toList))
import GHC.Word (Word32)
import Prelude hiding (null)
import Prettyprinter (Doc, Pretty (..), hang, parens,
Expand Down Expand Up @@ -489,7 +489,7 @@ prettyValue :: C.Value -> [Doc ann]
prettyValue vl =
let k (C.AdaAssetId, l) = "Ada" <+> prettyAda l
k (C.AssetId p n, C.Quantity q) = prettyPolicy p n <+> pretty q
in k <$> C.valueToList vl
in k <$> toList vl

invBalanceChange :: BalanceChanges -> BalanceChanges
invBalanceChange = BalanceChanges . Map.map C.negateValue . tbBalances
Expand Down
20 changes: 10 additions & 10 deletions src/coin-selection/lib/Convex/CoinSelection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-deprecations #-} -- see https://github.com/j-mueller/sc-tools/issues/213
{-| Building cardano transactions from tx bodies
-}
module Convex.CoinSelection(
Expand Down Expand Up @@ -105,6 +104,7 @@ import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as Text
import GHC.Generics (Generic)
import GHC.IsList (IsList (fromList, toList))

type ERA = ConwayEra

Expand Down Expand Up @@ -221,7 +221,7 @@ balanceTransactionBody
TrailingChange -> txb & appendTxOut change

txbody0 <-
balancingError . first C.TxBodyError $ C.createAndValidateTransactionBody C.shelleyBasedEra $ csiTxBody & addChangeOutput changeOutputSmall
balancingError . first C.TxBodyError $ C.createTransactionBody C.shelleyBasedEra $ csiTxBody & addChangeOutput changeOutputSmall

exUnitsMap <- balancingError . first C.TxBodyErrorValidityInterval $
C.evaluateTransactionExecutionUnits C.cardanoEra
Expand All @@ -242,13 +242,13 @@ balanceTransactionBody
let txbodycontent1' = txbodycontent1 & set L.txFee (Coin (2^(32 :: Integer) - 1)) & over L.txOuts (|> changeOutputLarge)

-- append output instead of prepending
txbody1 <- balancingError . first C.TxBodyError $ C.createAndValidateTransactionBody C.shelleyBasedEra txbodycontent1'
txbody1 <- balancingError . first C.TxBodyError $ C.createTransactionBody C.shelleyBasedEra txbodycontent1'

let !t_fee = C.calculateMinTxFee C.shelleyBasedEra (C.unLedgerProtocolParameters protocolParams) csiUtxo txbody1 numWits
traceWith tracer Txfee{fee = C.lovelaceToQuantity t_fee}

let txbodycontent2 = txbodycontent1 & set L.txFee t_fee & appendTxOut csiChangeLatestEraOutput
txbody2 <- balancingError . first C.TxBodyError $ C.createAndValidateTransactionBody C.shelleyBasedEra txbodycontent2
txbody2 <- balancingError . first C.TxBodyError $ C.createTransactionBody C.shelleyBasedEra txbodycontent2

let unregPoolStakeBalance = C.maryEraOnwardsConstraints @era C.maryBasedEra unregBalance protocolParams txbodycontent2

Expand Down Expand Up @@ -669,7 +669,7 @@ addOwnInput builder allUtxos =
let availableUTxOs =
List.sortOn
( length
. (\(C.InAnyCardanoEra _ (C.TxOut _ txOutValue _ _)) -> C.valueToList (C.txOutValueToValue txOutValue))
. (\(C.InAnyCardanoEra _ (C.TxOut _ txOutValue _ _)) -> toList (C.txOutValueToValue txOutValue))
. fst
. snd
)
Expand Down Expand Up @@ -739,7 +739,7 @@ balancePositive
-> m (TxBuilder era, C.TxOut C.CtxTx era)
balancePositive dbg poolIds ledgerPPs utxo_ returnUTxO0 walletUtxo collateralUTxOs txBuilder0 = inMary @era $ do
let txBodyContent0 = BuildTx.buildTx txBuilder0
txb <- either (throwError . bodyError) pure (C.createAndValidateTransactionBody C.shelleyBasedEra txBodyContent0)
txb <- either (throwError . bodyError) pure (C.createTransactionBody C.shelleyBasedEra txBodyContent0)
let bal = C.evaluateTransactionBalance C.shelleyBasedEra (C.unLedgerProtocolParameters ledgerPPs) poolIds mempty mempty utxo_ txb & view L._TxOutValue
available = Utxos.removeUtxos (spentTxIns txBodyContent0) walletUtxo

Expand Down Expand Up @@ -779,12 +779,12 @@ addInputsForAssets dbg txBal availableUtxo collateralUtxo txBuilder =
return (txBuilder, mempty)
| otherwise -> do
let missingAssets = fmap (second abs) $ fst $ splitValue txBal
traceWith dbg (MissingAssets $ C.valueFromList missingAssets)
traceWith dbg (MissingAssets $ fromList missingAssets)
case Wallet.selectMixedInputsCovering availableUtxo missingAssets of
Nothing ->
let total = availableUtxo <> collateralUtxo in
case Wallet.selectMixedInputsCovering total missingAssets of
Nothing -> throwError (NotEnoughMixedOutputsFor (C.valueFromList missingAssets) (Utxos.totalBalance total) txBal)
Nothing -> throwError (NotEnoughMixedOutputsFor (fromList missingAssets) (Utxos.totalBalance total) txBal)
Just (total_, ins) -> pure (txBuilder <> BuildTx.liftTxBodyEndo (over L.txIns (<> fmap spendPubKeyTxIn ins)), total_)
Just (total, ins) -> pure (txBuilder <> BuildTx.liftTxBodyEndo (over L.txIns (<> fmap spendPubKeyTxIn ins)), total)

Expand All @@ -802,7 +802,7 @@ addOutputForNonAdaAssets ::
-- ^ The balance of the transaction
(C.TxOut C.CtxTx era, C.Quantity)
-- ^ The modified change output and the lovelace portion of the change output's value. If no output was added then the amount will be 0.
addOutputForNonAdaAssets pparams returnUTxO (C.valueFromList . snd . splitValue -> positives)
addOutputForNonAdaAssets pparams returnUTxO (fromList . snd . splitValue -> positives)
| isNothing (C.valueToLovelace positives) =
let (vlWithoutAda :: C.Value) = positives & set (L._Value . at C.AdaAssetId) Nothing
output =
Expand All @@ -815,7 +815,7 @@ splitValue :: C.Value -> ([(C.AssetId, C.Quantity)], [(C.AssetId, C.Quantity)])
splitValue =
let p (_, q) = q < 0
f (_, q) = q /= 0
in List.partition p . List.filter f . C.valueToList
in List.partition p . List.filter f . toList

{-| Take the tx body and produce a 'CSInputs' value for coin selection,
using the @MonadBlockchain@ effect to query any missing UTxO information.
Expand Down
6 changes: 3 additions & 3 deletions src/optics/lib/Convex/CardanoApi/Lenses.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wno-deprecations #-} -- see https://github.com/j-mueller/sc-tools/issues/213
{-| Lenses for @cardano-api@ types
-}
module Convex.CardanoApi.Lenses(
Expand Down Expand Up @@ -134,6 +133,7 @@ import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Proxy (Proxy (..))
import Data.Word (Word64)
import GHC.IsList (IsList (fromList, toList))
import PlutusLedgerApi.V1 (PubKeyHash (..))
import qualified PlutusLedgerApi.V1 as PV1
import PlutusLedgerApi.V1.Interval (Closure, Extended (..),
Expand Down Expand Up @@ -380,8 +380,8 @@ _TxInsReferenceIso = iso from to where
_Value :: Iso' Value (Map AssetId Quantity)
_Value = iso from to where
-- the 'Value' constructor is not exposed so we have to take the long way around
from = Map.fromList . C.valueToList
to = C.valueFromList . Map.toList
from = Map.fromList . toList
to = fromList . Map.toList

_AssetId :: Prism' C.AssetId (C.PolicyId, C.AssetName)
_AssetId = prism' from to where
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/lib/Convex/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-deprecations #-} -- see https://github.com/j-mueller/sc-tools/issues/213
{-| Primitive wallet
-}
module Convex.Wallet(
Expand Down Expand Up @@ -47,6 +46,7 @@ import Data.Maybe (fromMaybe, mapMaybe)
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as Text
import GHC.IsList (IsList (toList))


newtype Wallet = Wallet { getWallet :: SigningKey PaymentKey }
Expand Down Expand Up @@ -153,7 +153,7 @@ selectMixedInputsCovering UtxoSet{_utxos} xs =
all (\(assetId, quantity) -> C.selectAsset candidateVl assetId >= quantity) xs
requiredAssets = foldMap (\(a, _) -> Set.singleton a) xs
relevantValue (txIn, C.InAnyCardanoEra _ (C.TxOut _ (C.txOutValueToValue -> value) _ _)) =
let providedAssets = foldMap (Set.singleton . fst) (C.valueToList value)
let providedAssets = foldMap (Set.singleton . fst) (toList value)
in if Set.null (Set.intersection requiredAssets providedAssets)
then Nothing
else Just (value, txIn)
Expand Down

0 comments on commit a5a54b8

Please sign in to comment.