Skip to content

Commit b795553

Browse files
committed
post-merge fixes
- still one test fails
1 parent c257661 commit b795553

File tree

8 files changed

+30
-50
lines changed

8 files changed

+30
-50
lines changed

src/BotPlutusInterface/Balance.hs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import Control.Monad.Trans.Except (throwE)
4141
import Data.Bifunctor (bimap)
4242
import Data.Coerce (coerce)
4343
import Data.Kind (Type)
44-
import Data.List ((\\))
4544
import Data.List qualified as List
4645
import Data.Map (Map)
4746
import Data.Map qualified as Map
@@ -152,7 +151,7 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
152151

153152
-- Get current Ada change
154153
let adaChange = getAdaChange utxoIndex balancedTx
155-
bTx = fst <$> balanceTxLoop utxoIndex privKeys minUtxos (addOutput changeAddr balancedTx)
154+
bTx = balanceTxLoop utxoIndex privKeys (addOutput changeAddr balancedTx)
156155

157156
-- Checks if there's ada change left, if there is then we check
158157
-- if `bcSeparateChange` is true, if this is the case then we create a new UTxO at
@@ -188,16 +187,9 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
188187
Map TxOutRef TxOut ->
189188
Map PubKeyHash DummyPrivKey ->
190189
Tx ->
191-
EitherT Text (Eff effs) (Tx, [(TxOut, Integer)])
192-
balanceTxLoop utxoIndex privKeys prevMinUtxos tx = do
190+
EitherT Text (Eff effs) Tx
191+
balanceTxLoop utxoIndex privKeys tx = do
193192
void $ lift $ Files.writeAll @w pabConf tx
194-
nextMinUtxos <-
195-
newEitherT $
196-
calculateMinUtxos @w pabConf (Tx.txData tx) $ Tx.txOutputs tx \\ map fst prevMinUtxos
197-
198-
let minUtxos = prevMinUtxos ++ nextMinUtxos
199-
200-
lift $ printBpiLog @w (Debug [TxBalancingLog]) $ "Min utxos:" <+> pretty minUtxos
201193

202194
-- Calculate fees by pre-balancing the tx, building it, and running the CLI on result
203195
txWithoutFees <-
@@ -212,11 +204,11 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
212204
lift $ printBpiLog @w (Debug [TxBalancingLog]) $ "Fees:" <+> pretty fees
213205

214206
-- Rebalance the initial tx with the above fees
215-
balancedTx <- newEitherT $ balanceTxStep @w balanceCfg minUtxos utxoIndex changeAddr $ tx `withFee` fees
207+
balancedTx <- newEitherT $ balanceTxStep @w balanceCfg utxoIndex changeAddr $ tx `withFee` fees
216208

217209
if balancedTx == tx
218-
then pure (balancedTx, minUtxos)
219-
else balanceTxLoop utxoIndex privKeys minUtxos balancedTx
210+
then pure balancedTx
211+
else balanceTxLoop utxoIndex privKeys balancedTx
220212

221213
-- `utxosAndCollateralAtAddress` returns all the utxos that can be used as an input of a `Tx`,
222214
-- i.e. we filter out `CollateralUtxo` present at the user's address, so it can't be used as input of a `Tx`.
@@ -275,14 +267,13 @@ balanceTxStep ::
275267
forall (w :: Type) (effs :: [Type -> Type]).
276268
Member (PABEffect w) effs =>
277269
BalanceConfig ->
278-
[(TxOut, Integer)] ->
279270
Map TxOutRef TxOut ->
280271
Address ->
281272
Tx ->
282273
Eff effs (Either Text Tx)
283-
balanceTxStep balanceCfg minUtxos utxos changeAddr tx =
274+
balanceTxStep balanceCfg utxos changeAddr tx =
284275
runEitherT $
285-
(newEitherT . balanceTxIns @w utxos) (addLovelaces minUtxos tx)
276+
(newEitherT . balanceTxIns @w utxos) tx
286277
>>= hoistEither . handleNonAdaChange balanceCfg changeAddr utxos
287278

288279
-- | Get change value of a transaction, taking inputs, outputs, mint and fees into account
@@ -305,23 +296,6 @@ getAdaChange utxos = lovelaceValue . getChange utxos
305296
getNonAdaChange :: Map TxOutRef TxOut -> Tx -> Value
306297
getNonAdaChange utxos = Ledger.noAdaValue . getChange utxos
307298

308-
-- | Add min lovelaces to each tx output
309-
addLovelaces :: [(TxOut, Integer)] -> Tx -> Tx
310-
addLovelaces minLovelaces tx =
311-
let lovelacesAdded =
312-
map
313-
( \txOut ->
314-
let outValue = txOutValue txOut
315-
lovelaces = Ada.getLovelace $ Ada.fromValue outValue
316-
minUtxo = fromMaybe 0 $ lookup txOut minLovelaces
317-
in txOut
318-
{ txOutValue =
319-
outValue <> Ada.lovelaceValueOf (max 0 (minUtxo - lovelaces))
320-
}
321-
)
322-
$ txOutputs tx
323-
in tx {txOutputs = lovelacesAdded}
324-
325299
balanceTxIns ::
326300
forall (w :: Type) (effs :: [Type -> Type]).
327301
Member (PABEffect w) effs =>

src/BotPlutusInterface/ChainIndex.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ handleChainIndexReq contractEnv@ContractEnvironment {cePABConfig} =
5757
TxOutFromRef txOutRef ->
5858
TxOutRefResponse <$> chainIndexQueryOne cePABConfig (ChainIndexClient.getTxOut txOutRef)
5959
UnspentTxOutFromRef txOutRef ->
60-
UnspentTxOutResponse <$> chainIndexQueryOne pabConf (ChainIndexClient.getUnspentTxOut txOutRef)
60+
UnspentTxOutResponse <$> chainIndexQueryOne cePABConfig (ChainIndexClient.getUnspentTxOut txOutRef)
6161
UnspentTxOutSetAtAddress page credential ->
6262
UnspentTxOutsAtResponse
6363
<$> chainIndexQueryMany
64-
pabConf
64+
cePABConfig
6565
(ChainIndexClient.getUnspentTxOutsAtAddress (QueryAtAddressRequest (Just page) credential))
6666
TxFromTxId txId ->
6767
TxIdResponse <$> chainIndexQueryOne cePABConfig (ChainIndexClient.getTx txId)

src/BotPlutusInterface/Contract.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
22
{-# LANGUAGE RankNTypes #-}
33
{-# OPTIONS_GHC -Wno-orphans #-}
4+
{-# LANGUAGE ViewPatterns #-}
45

56
module BotPlutusInterface.Contract (runContract, handleContract) where
67

@@ -348,7 +349,8 @@ balanceTx ::
348349
ContractEnvironment w ->
349350
UnbalancedTx ->
350351
Eff effs BalanceTxResponse
351-
balanceTx contractEnv unbalancedTx = do
352+
balanceTx _ (UnbalancedTx (Left _) _ _ _) = pure $ BalanceTxFailed $ OtherError "CardanoBuildTx is not supported"
353+
balanceTx contractEnv unbalancedTx@(UnbalancedTx (Right tx') _ _ _) = do
352354
let pabConf = contractEnv.cePABConfig
353355

354356
result <- handleCollateral @w contractEnv
@@ -360,13 +362,13 @@ balanceTx contractEnv unbalancedTx = do
360362
eitherBalancedTx <-
361363
Balance.balanceTxIO' @w
362364
Balance.defaultBalanceConfig
363-
{ Balance.bcHasScripts = Balance.txUsesScripts (unBalancedTxTx unbalancedTx)
365+
{ Balance.bcHasScripts = Balance.txUsesScripts tx'
364366
}
365367
pabConf
366368
pabConf.pcOwnPubKeyHash
367369
unbalancedTx
368370

369-
pure $ either (BalanceTxFailed . InsufficientFunds) (BalanceTxSuccess . EmulatorTx) eitherBalancedTx
371+
pure $ either (BalanceTxFailed . OtherError) (BalanceTxSuccess . EmulatorTx) eitherBalancedTx
370372

371373
fromCardanoTx :: CardanoTx -> Tx.Tx
372374
fromCardanoTx (CardanoApiTx _) = error "Cannot handle cardano api tx"
@@ -575,7 +577,7 @@ makeCollateral cEnv = runEitherT $ do
575577
pabConf
576578
pabConf.pcOwnPubKeyHash unbalancedTx
577579

578-
wbr <- lift $ writeBalancedTx cEnv (Right balancedTx)
580+
wbr <- lift $ writeBalancedTx cEnv (EmulatorTx balancedTx)
579581
case wbr of
580582
WriteBalancedTxFailed e -> throwE . T.pack $ "Failed to create collateral output: " <> show e
581583
WriteBalancedTxSuccess cTx -> do

src/BotPlutusInterface/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ import Plutus.PAB.Effects.Contract.Builtin (
6666
SomeBuiltin (SomeBuiltin),
6767
endpointsToSchemas,
6868
)
69-
import Plutus.V1.Ledger.Ada qualified as Ada
7069
import Prettyprinter (Pretty (pretty), (<+>))
7170
import Prettyprinter qualified as PP
7271
import Servant.Client (BaseUrl (BaseUrl), Scheme (Http))
7372
import Wallet.Types (ContractInstanceId (..))
7473
import Prelude
74+
import Ledger.Ada qualified as Ada
7575

7676
data PABConfig = PABConfig
7777
{ -- | Calling the cli through ssh when set to Remote

src/PlutusConfig/Cardano/Api/Shelley.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Cardano.Api.Shelley (ProtocolParameters (..))
2323
-- )
2424
import Data.Aeson qualified as JSON
2525
import Data.ByteString.Lazy qualified as LazyByteString
26+
import Control.Exception (IOException, catch)
2627

2728
-- import Data.Default (def)
2829
-- import Data.Text qualified as Text
@@ -279,7 +280,9 @@ import Prelude
279280
-- pure ProtocolParameters {..}
280281

281282
readProtocolParametersJSON :: FilePath -> IO (Either String ProtocolParameters)
282-
readProtocolParametersJSON fn = (JSON.eitherDecode <$> LazyByteString.readFile fn) `catch` (\(e :: IOException) -> pure $ Left (show e))
283+
readProtocolParametersJSON fn =
284+
(JSON.eitherDecode <$> LazyByteString.readFile fn)
285+
`catch` (\(e :: IOException) -> pure $ Left (show e))
283286

284287
writeProtocolParametersJSON :: FilePath -> ProtocolParameters -> IO ()
285288
writeProtocolParametersJSON fn params =

test/Spec/BotPlutusInterface/Collateral.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import Ledger qualified
1212
import Ledger.Ada qualified as Ada
1313
import Ledger.Constraints qualified as Constraints
1414
import Ledger.Scripts qualified as Scripts
15-
import Ledger.Tx (CardanoTx, TxOut (TxOut), TxOutRef (TxOutRef))
15+
import Ledger.Tx (CardanoTx, TxOut (TxOut), TxOutRef (TxOutRef), ChainIndexTxOut (PublicKeyChainIndexTxOut))
1616
import Ledger.Tx qualified as Tx
17-
import Ledger.TxId qualified as TxId
17+
import Ledger.Tx qualified as TxId
1818
import Ledger.Value qualified as Value
1919
import NeatInterpolation (text)
2020
import Plutus.Contract (
@@ -50,6 +50,7 @@ import System.IO.Unsafe (unsafePerformIO)
5050
import Test.Tasty (TestTree, testGroup)
5151
import Test.Tasty.HUnit (Assertion, assertEqual, testCase)
5252
import Prelude
53+
import Plutus.ChainIndex (OutputDatum(NoOutputDatum))
5354

5455
tests :: TestTree
5556
tests =
@@ -65,9 +66,9 @@ tests =
6566
testTxUsesCollateralCorrectly :: Assertion
6667
testTxUsesCollateralCorrectly = do
6768
let txOutRef1 = TxOutRef "e406b0cf676fc2b1a9edb0617f259ad025c20ea6f0333820aa7cef1bfe7302e5" 0
68-
txOut1 = TxOut pkhAddr1 (Ada.lovelaceValueOf 10_000_000) Nothing
69+
txOut1 = PublicKeyChainIndexTxOut pkhAddr1 (Ada.lovelaceValueOf 10_000_000) NoOutputDatum Nothing
6970
txOutRef2 = TxOutRef "d406b0cf676fc2b1a9edb0617f259ad025c20ea6f0333820aa7cef1bfe7302e4" 0
70-
txOut2 = TxOut pkhAddr1 (Ada.lovelaceValueOf 90_000_000) Nothing
71+
txOut2 = PublicKeyChainIndexTxOut pkhAddr1 (Ada.lovelaceValueOf 90_000_000) NoOutputDatum Nothing
7172
cenv' = def {ceCollateral = CollateralVar $ unsafePerformIO $ newTVarIO Nothing}
7273
initState = def & utxos .~ [(txOutRef1, txOut1), (txOutRef2, txOut2)] & contractEnv .~ cenv' & collateralUtxo .~ Nothing
7374

@@ -106,7 +107,7 @@ testTxUsesCollateralCorrectly = do
106107
testTxCreatesCollateralCorrectly :: Assertion
107108
testTxCreatesCollateralCorrectly = do
108109
let txOutRef1 = TxOutRef "d406b0cf676fc2b1a9edb0617f259ad025c20ea6f0333820aa7cef1bfe7302e4" 0
109-
txOut1 = TxOut pkhAddr1 (Ada.lovelaceValueOf 90_000_000) Nothing
110+
txOut1 = PublicKeyChainIndexTxOut pkhAddr1 (Ada.lovelaceValueOf 90_000_000) NoOutputDatum Nothing
110111
cenv' = def {ceCollateral = CollateralVar $ unsafePerformIO $ newTVarIO Nothing}
111112
initState = def & utxos .~ [(txOutRef1, txOut1)] & contractEnv .~ cenv' & collateralUtxo .~ Nothing
112113

test/Spec/BotPlutusInterface/Contract.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ mintTokens = do
447447
txOut = PublicKeyChainIndexTxOut pkhAddr1 (Ada.lovelaceValueOf 1_000_000) NoOutputDatum Nothing
448448
initState = def & utxos <>~ [(txOutRef, txOut)]
449449
inTxId = encodeByteString $ fromBuiltin $ Tx.getTxId $ Tx.txOutRefId txOutRef
450-
collateralTxId = encodeByteString $ fromBuiltin $ TxId.getTxId theCollateralTxId
450+
collateralTxId = encodeByteString $ fromBuiltin $ Tx.getTxId theCollateralTxId
451451

452452
mintingPolicy :: Scripts.MintingPolicy
453453
mintingPolicy =
@@ -621,7 +621,7 @@ redeemFromValidator = do
621621
(Right validator)
622622
initState = def & utxos <>~ [(txOutRef, txOut), (txOutRef', txOut')]
623623
inTxId = encodeByteString $ fromBuiltin $ Tx.getTxId $ Tx.txOutRefId txOutRef
624-
collateralTxId = encodeByteString $ fromBuiltin $ TxId.getTxId theCollateralTxId
624+
collateralTxId = encodeByteString $ fromBuiltin $ Tx.getTxId theCollateralTxId
625625

626626
validator :: Scripts.Validator
627627
validator =

test/Spec/MockContract.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ instance Monoid w => Default (MockContractState w) where
287287
_utxos =
288288
[
289289
( collateralTxOutRef theCollateralUtxo
290-
, TxOut pkhAddr1 (Ada.lovelaceValueOf $ toInteger $ pcCollateralSize def) Nothing
290+
, PublicKeyChainIndexTxOut pkhAddr1 (Ada.lovelaceValueOf $ toInteger $ pcCollateralSize def) NoOutputDatum Nothing
291291
)
292292
]
293293
, _tip = Tip 1000 (BlockId "ab12") 4

0 commit comments

Comments
 (0)