Skip to content

Move to relude #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: gergely/vasil
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bot-plutus-interface.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ library
, process
, QuickCheck
, regex-compat
, relude
, row-types
, serialise
, servant
Expand Down Expand Up @@ -230,6 +231,7 @@ test-suite bot-plutus-interface-test
, prettyprinter
, QuickCheck
, quickcheck-instances
, relude
, row-types
, serialise
, servant
Expand Down
3 changes: 1 addition & 2 deletions src/BotPlutusInterface.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ module BotPlutusInterface (runPAB) where
import BotPlutusInterface.Server qualified as Server
import BotPlutusInterface.Types (PABConfig (..))
import Data.Aeson (FromJSON)
import Data.Kind (Type)
import Network.Wai.Handler.Warp (run)
import Plutus.PAB.Effects.Contract.Builtin (HasDefinitions)
import Prelude
import Relude hiding (state)

runPAB :: forall (t :: Type). (HasDefinitions t, FromJSON t) => PABConfig -> IO ()
runPAB pabConf = do
Expand Down
25 changes: 9 additions & 16 deletions src/BotPlutusInterface/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,14 @@ import BotPlutusInterface.Types (
import Cardano.Api (ExecutionUnitPrices (ExecutionUnitPrices))
import Cardano.Api.Shelley (ProtocolParameters (protocolParamPrices))
import Control.Lens (folded, to, (^..))
import Control.Monad (foldM, void)
import Control.Monad (foldM)
import Control.Monad.Freer (Eff, Member)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Either (EitherT, firstEitherT, hoistEither, newEitherT, runEitherT)
import Control.Monad.Trans.Either (EitherT, firstEitherT, newEitherT, runEitherT)
import Control.Monad.Trans.Except (throwE)
import Data.Bifunctor (bimap)
import Data.Coerce (coerce)
import Data.Kind (Type)
import Data.List qualified as List
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Maybe (fromMaybe, mapMaybe)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text (unpack)
import GHC.Real (Ratio ((:%)))
import Ledger qualified
import Ledger.Ada qualified as Ada
Expand Down Expand Up @@ -80,7 +73,7 @@ import Plutus.V1.Ledger.Api (
TokenName (..),
)
import Prettyprinter (pretty, viaShow, (<+>))
import Prelude
import Relude

-- Config for balancing a `Tx`.
data BalanceConfig = BalanceConfig
Expand Down Expand Up @@ -139,7 +132,7 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
lift $ printBpiLog @w (Debug [TxBalancingLog]) $ viaShow utxoIndex

-- We need this folder on the CLI machine, which may not be the local machine
lift $ createDirectoryIfMissingCLI @w False (Text.unpack "pcTxFileDir")
lift $ createDirectoryIfMissingCLI @w False (unpack "pcTxFileDir")

tx <-
newEitherT $
Expand Down Expand Up @@ -237,7 +230,7 @@ utxosAndCollateralAtAddress ::
Eff effs (Either Text (Map TxOutRef Tx.ChainIndexTxOut, Maybe CollateralUtxo))
utxosAndCollateralAtAddress balanceCfg _pabConf changeAddr =
runEitherT $ do
utxos <- firstEitherT (Text.pack . show) $ newEitherT $ queryNode @w (UtxosAt changeAddr)
utxos <- firstEitherT show $ newEitherT $ queryNode @w (UtxosAt changeAddr)
inMemCollateral <- lift $ getInMemCollateral @w

-- check if `bcHasScripts` is true, if this is the case then we search of
Expand Down Expand Up @@ -379,7 +372,7 @@ handleNonAdaChange balanceCfg changeAddr utxos tx = runEitherT $ do
}

newOutputWithMinAmt <-
firstEitherT (Text.pack . show) $
firstEitherT show $
newEitherT $
queryNode @w (MinUtxo newOutput)

Expand Down Expand Up @@ -440,7 +433,7 @@ addOutput changeAddr tx =
}

changeTxOutWithMinAmt <-
firstEitherT (Text.pack . show) $
firstEitherT show $
newEitherT $
queryNode @w (MinUtxo changeTxOut)

Expand Down Expand Up @@ -470,7 +463,7 @@ addValidRange _ (Left _) = pure $ Left "BPI is not using CardanoBuildTx"
addValidRange timeRange (Right tx) =
if validateRange timeRange
then
bimap (Text.pack . show) (setRange tx)
bimap show (setRange tx)
<$> posixTimeRangeToContainedSlotRange @w timeRange
else pure $ Left "Invalid validity interval."
where
Expand Down
9 changes: 2 additions & 7 deletions src/BotPlutusInterface/BodyBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ import BotPlutusInterface.Files (
import BotPlutusInterface.Types (PABConfig, TxFile (Raw))
import Control.Monad.Freer (Eff, Member)
import Control.Monad.Trans.Either (firstEitherT, newEitherT, runEitherT)
import Data.Kind (Type)
import Data.Map (Map)
import Data.Text (Text)
import Data.Text qualified as Text
import Ledger (ExBudget, Tx, txId)
import Ledger.Crypto (PubKeyHash)
import Prelude
import Relude

{- | Build and save raw transaction (transaction body) with estimated execution budgets using `CardanoCLI`.
It builds first transaction body with 0 budget for all spending inputs and minting policies,
Expand All @@ -43,7 +40,7 @@ buildAndEstimateBudget pabConf privKeys tx = runEitherT $ do
buildDraftTxBody = newEitherT $ CardanoCLI.buildTx @w pabConf privKeys mempty tx

estimateBudgetByDraftBody path =
firstEitherT toText . newEitherT $ estimateBudget @w (Raw path)
firstEitherT show . newEitherT $ estimateBudget @w (Raw path)

buildBodyUsingEstimatedBudget exBudget =
newEitherT $
Expand All @@ -52,5 +49,3 @@ buildAndEstimateBudget pabConf privKeys tx = runEitherT $ do
privKeys
exBudget
tx

toText = Text.pack . show
7 changes: 2 additions & 5 deletions src/BotPlutusInterface/CardanoAPI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import Cardano.Prelude (maybeToEither)
import Cardano.Slotting.EpochInfo (hoistEpochInfo)
import Cardano.Slotting.Time (SystemStart, toRelativeTime)
import Control.Monad.Trans.Except (runExcept)
import Data.Bifunctor (first)
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Time (UTCTime, secondsToNominalDiffTime)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Ledger qualified
Expand All @@ -28,7 +25,7 @@ import Plutus.Script.Utils.Scripts qualified as ScriptUtils
import Plutus.V1.Ledger.Api (Credential (..))
import Plutus.V2.Ledger.Tx qualified as V2
import PlutusTx.Prelude qualified as PlutusTx
import Prelude
import Relude

fromCardanoTxOut :: CApi.TxOut CApi.CtxUTxO CApi.BabbageEra -> Either TxApi.FromCardanoError ChainIndexTxOut
fromCardanoTxOut (CApi.TxOut caddr val cdatum _refScript) = do
Expand Down Expand Up @@ -71,7 +68,7 @@ fromCardanoEpochInfo ::
CApi.EraHistory mode ->
EpochInfo (Either Text)
fromCardanoEpochInfo (CApi.EraHistory _ interpreter) =
hoistEpochInfo (first (Text.pack . show) . runExcept) $
hoistEpochInfo (first show . runExcept) $
Consensus.interpreterToEpochInfo interpreter

posixTimeToSlot ::
Expand Down
34 changes: 11 additions & 23 deletions src/BotPlutusInterface/CardanoCLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,15 @@ import BotPlutusInterface.Types (
)
import BotPlutusInterface.UtxoParser qualified as UtxoParser
import Cardano.Api.Shelley (NetworkId (Mainnet, Testnet), NetworkMagic (..), serialiseAddress)
import Control.Monad (join)
import Control.Monad.Freer (Eff, Member)
import Data.Aeson qualified as JSON
import Data.Aeson.Extras (encodeByteString)
import Data.Attoparsec.Text (parseOnly)
import Data.Bifunctor (first)
import Data.Bool (bool)
import Data.ByteString.Lazy.Char8 qualified as Char8
import Data.Either.Combinators (mapLeft)
import Data.Hex (hex)
import Data.Kind (Type)
import Data.List (sort)
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text.Encoding (decodeUtf8)
import Ledger (Slot (Slot), SlotRange)
import Ledger qualified
import Ledger.Ada (fromValue, getLovelace)
Expand Down Expand Up @@ -80,7 +71,7 @@ import Plutus.V1.Ledger.Api (
TokenName (..),
)
import PlutusTx.Builtins (fromBuiltin)
import Prelude
import Relude

-- | Getting information of the latest block
queryTip ::
Expand Down Expand Up @@ -112,9 +103,9 @@ calculateMinFee pabConf tx =
mconcat
[ ["transaction", "calculate-min-fee"]
, ["--tx-body-file", txFilePath pabConf "raw" (txId tx)]
, ["--tx-in-count", showText $ length $ txInputs tx]
, ["--tx-out-count", showText $ length $ txOutputs tx]
, ["--witness-count", showText $ length $ txSignatures tx]
, ["--tx-in-count", show $ length $ txInputs tx]
, ["--tx-out-count", show $ length $ txOutputs tx]
, ["--witness-count", show $ length $ txSignatures tx]
, ["--protocol-params-file", pabConf.pcProtocolParamsFile]
, networkOpt pabConf
]
Expand Down Expand Up @@ -159,7 +150,7 @@ buildTx pabConf privKeys txBudget tx = do
, -- TODO: Removed for now, as the main iohk branch doesn't support metadata yet
-- , metadataOpts pabConf (txMetadata tx)
requiredSigners
, ["--fee", showText . getLovelace . fromValue $ txFee tx]
, ["--fee", show . getLovelace . fromValue $ txFee tx]
, mconcat
[ ["--protocol-params-file", pabConf.pcProtocolParamsFile]
, ["--out-file", txFilePath pabConf "raw" (txId tx)]
Expand Down Expand Up @@ -300,11 +291,11 @@ validRangeOpts (Interval lowerBound upperBound) =
mconcat
[ case lowerBound of
LowerBound (Finite (Slot x)) closed ->
["--invalid-before", showText (bool (x + 1) x closed)]
["--invalid-before", show (bool (x + 1) x closed)]
_ -> []
, case upperBound of
UpperBound (Finite (Slot x)) closed ->
["--invalid-hereafter", showText (bool x (x + 1) closed)]
["--invalid-hereafter", show (bool x (x + 1) closed)]
_ -> []
]

Expand Down Expand Up @@ -332,20 +323,20 @@ txOutOpts pabConf datums =

networkOpt :: PABConfig -> [Text]
networkOpt pabConf = case pabConf.pcNetwork of
Testnet (NetworkMagic t) -> ["--testnet-magic", showText t]
Testnet (NetworkMagic t) -> ["--testnet-magic", show t]
Mainnet -> ["--mainnet"]

txOutRefToCliArg :: TxOutRef -> Text
txOutRefToCliArg (TxOutRef (TxId tId) txIx) =
encodeByteString (fromBuiltin tId) <> "#" <> showText txIx
encodeByteString (fromBuiltin tId) <> "#" <> show txIx

flatValueToCliArg :: (CurrencySymbol, TokenName, Integer) -> Text
flatValueToCliArg (curSymbol, name, amount)
| curSymbol == Ada.adaSymbol = amountStr
| Text.null tokenNameStr = amountStr <> " " <> curSymbolStr
| otherwise = amountStr <> " " <> curSymbolStr <> "." <> tokenNameStr
where
amountStr = showText amount
amountStr = show amount
curSymbolStr = encodeByteString $ fromBuiltin $ unCurrencySymbol curSymbol
tokenNameStr = decodeUtf8 $ hex $ fromBuiltin $ unTokenName name

Expand All @@ -361,10 +352,7 @@ unsafeSerialiseAddress network address =

exBudgetToCliArg :: ExBudget -> Text
exBudgetToCliArg (ExBudget (ExCPU steps) (ExMemory memory)) =
"(" <> showText steps <> "," <> showText memory <> ")"

showText :: forall (a :: Type). Show a => a -> Text
showText = Text.pack . show
"(" <> show steps <> "," <> show memory <> ")"

-- TODO: Removed for now, as the main iohk branch doesn't support metadata yet
-- metadataOpts :: PABConfig -> Maybe BuiltinByteString -> [Text]
Expand Down
6 changes: 2 additions & 4 deletions src/BotPlutusInterface/CardanoNode/Effects.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ import Cardano.Ledger.Shelley.API.Wallet (
import Control.Lens (folded, to, (^..))
import Control.Monad.Freer (Eff, Members, interpret, runM, send, type (~>))
import Control.Monad.Freer.Reader (Reader, ask, runReader)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Either (firstEitherT, hoistEither, newEitherT, runEitherT)
import Data.Map (Map)
import Control.Monad.Trans.Either (firstEitherT, newEitherT, runEitherT)
import Data.Map qualified as Map
import Data.Set qualified as Set
import Ledger qualified
Expand All @@ -53,7 +51,7 @@ import Ledger.Tx (ChainIndexTxOut (..))
import Ledger.Tx.CardanoAPI qualified as TxApi
import Ledger.Validation (Coin (Coin))
import Plutus.V2.Ledger.Tx qualified as V2
import Prelude
import Relude hiding (Reader, ask, runReader)

data NodeQuery a where
UtxosAt :: Address -> NodeQuery (Either NodeQueryError (Map V2.TxOutRef ChainIndexTxOut))
Expand Down
8 changes: 3 additions & 5 deletions src/BotPlutusInterface/CardanoNode/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ import Control.Monad.Freer.Reader (Reader, ask)
import Control.Monad.Trans.Class
import Control.Monad.Trans.Either
import Control.Monad.Trans.Except (throwE)
import Data.Text (Text)
import Data.Text qualified as Text
import Relude hiding (Reader, ask)
import System.Environment (getEnv)
import Prelude

{- | Error returned in case any error happened querying local node
(wraps whatever received in `Text`)
Expand All @@ -46,7 +44,7 @@ queryInCardanoMode ::
queryInCardanoMode query =
runEitherT $ do
conn <- lift $ ask @NodeConn
firstEitherT (NodeQueryError . Text.pack . show) $
firstEitherT (NodeQueryError . show) $
newEitherT $
send $
CApi.queryNodeLocalState conn Nothing query
Expand Down Expand Up @@ -88,4 +86,4 @@ connectionInfo pabConf =
epochSlots = CApi.EpochSlots 21600

toQueryError :: Show e => e -> NodeQueryError
toQueryError = NodeQueryError . Text.pack . show
toQueryError = NodeQueryError . show
3 changes: 1 addition & 2 deletions src/BotPlutusInterface/ChainIndex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import BotPlutusInterface.Types (
PABConfig,
readCollateralUtxo,
)
import Data.Kind (Type)
import Network.HTTP.Client (
ManagerSettings (managerResponseTimeout),
defaultManagerSettings,
Expand All @@ -28,14 +27,14 @@ import Plutus.ChainIndex.Api (
)
import Plutus.ChainIndex.Client qualified as ChainIndexClient
import Plutus.Contract.Effects (ChainIndexQuery (..), ChainIndexResponse (..))
import Relude
import Servant.Client (
ClientError (FailureResponse),
ClientM,
ResponseF (Response, responseStatusCode),
mkClientEnv,
runClientM,
)
import Prelude

handleChainIndexReq :: forall (w :: Type). ContractEnvironment w -> ChainIndexQuery -> IO ChainIndexResponse
handleChainIndexReq contractEnv@ContractEnvironment {cePABConfig} =
Expand Down
Loading