Skip to content

Commit 8524fef

Browse files
committed
moving custom toCardanoTxOut to another module
1 parent 5db75f0 commit 8524fef

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/BotPlutusInterface/CardanoAPI.hs

+21
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ module BotPlutusInterface.CardanoAPI (
66
fromCardanoSlotNo,
77
fromCardanoEpochInfo,
88
posixTimeToSlot,
9+
toCardanoTxOut',
910
) where
1011

1112
import Cardano.Api qualified as CApi
13+
import Cardano.Api.Shelley qualified as CApi.S
1214
import Cardano.Ledger.Slot (EpochInfo)
1315
import Cardano.Prelude (maybeToEither)
1416
import Cardano.Slotting.EpochInfo (hoistEpochInfo)
@@ -99,3 +101,22 @@ convertOutputDatum = \case
99101
V2.NoOutputDatum -> Nothing
100102
V2.OutputDatumHash dh -> Just (dh, Nothing)
101103
V2.OutputDatum d -> Just (ScriptUtils.datumHash d, Just d)
104+
105+
{- | Custom version of `toCardanoTxOut` from `plutus-ledger`
106+
which doesn't throw an error in case `Value` has 0 Ada
107+
-}
108+
toCardanoTxOut' ::
109+
CApi.S.NetworkId ->
110+
( Maybe ScriptUtils.DatumHash ->
111+
Either TxApi.ToCardanoError (CApi.S.TxOutDatum ctx CApi.S.BabbageEra)
112+
) ->
113+
Ledger.TxOut ->
114+
Either TxApi.ToCardanoError (CApi.S.TxOut ctx CApi.S.BabbageEra)
115+
toCardanoTxOut' networkId fromHash (Ledger.TxOut addr value datumHash) =
116+
CApi.TxOut <$> TxApi.toCardanoAddressInEra networkId addr
117+
<*> toCardanoTxOutValue' value
118+
<*> fromHash datumHash
119+
<*> pure CApi.S.ReferenceScriptNone
120+
where
121+
toCardanoTxOutValue' v = do
122+
CApi.TxOutValue CApi.MultiAssetInBabbageEra <$> TxApi.toCardanoValue v

src/BotPlutusInterface/Effects.hs

+2-14
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module BotPlutusInterface.Effects (
3535
calcMinUtxo,
3636
) where
3737

38+
import BotPlutusInterface.CardanoAPI qualified as BPI.CApi
3839
import BotPlutusInterface.CardanoNode.Effects (NodeQuery, runNodeQuery)
3940
import BotPlutusInterface.ChainIndex (handleChainIndexReq)
4041
import BotPlutusInterface.Collateral (withCollateralHandling)
@@ -86,8 +87,6 @@ import Data.Text (Text)
8687
import Data.Text qualified as Text
8788
import Ledger qualified
8889
import Ledger.Ada qualified as Ada
89-
import Ledger.Tx (TxOut (TxOut))
90-
import Ledger.Tx.CardanoAPI (toCardanoAddressInEra, toCardanoValue)
9190
import Ledger.Tx.CardanoAPI qualified as TxApi
9291
import Ledger.Validation (Coin (Coin))
9392
import Plutus.Contract.Effects (ChainIndexQuery, ChainIndexResponse)
@@ -296,7 +295,7 @@ calcMinUtxo pabconf txout = do
296295

297296
ctxout <-
298297
mapLeft (Text.pack . show) $
299-
toCardanoTxOutPossibleZeroAda netId TxApi.toCardanoTxOutDatumHash txout
298+
BPI.CApi.toCardanoTxOut' netId TxApi.toCardanoTxOutDatumHash txout
300299

301300
let (Coin minTxOut) =
302301
evaluateMinLovelaceOutput pparamsInEra $
@@ -307,17 +306,6 @@ calcMinUtxo pabconf txout = do
307306
if missingLovelace > 0
308307
then calcMinUtxo pabconf (txout {Ledger.txOutValue = Ledger.txOutValue txout <> Ada.toValue missingLovelace})
309308
else return txout
310-
where
311-
-- We need to redefine this to remove error reporting with 0 ada outputs.
312-
toCardanoTxOutValuePossibleZeroAda value = do
313-
-- when (Ada.fromValue value == mempty) (Left OutputHasZeroAda)
314-
CApi.TxOutValue CApi.MultiAssetInBabbageEra <$> toCardanoValue value
315-
316-
toCardanoTxOutPossibleZeroAda networkId fromHash (TxOut addr value datumHash) =
317-
CApi.TxOut <$> toCardanoAddressInEra networkId addr
318-
<*> toCardanoTxOutValuePossibleZeroAda value
319-
<*> fromHash datumHash
320-
<*> pure CApi.S.ReferenceScriptNone
321309

322310
-- Couldn't use the template haskell makeEffect here, because it caused an OverlappingInstances problem.
323311
-- For some reason, we need to manually propagate the @w@ type variable to @send@

0 commit comments

Comments
 (0)