Skip to content

Commit cd192e5

Browse files
authored
Merge pull request #156 from mlabs-haskell/adjust-collateral-creation
Adjusting collateral creation
2 parents 74e9d95 + 2f4b4c5 commit cd192e5

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,11 @@ The fake PAB consists of the following modules:
167167
- adding collaterals,
168168
- modifying tx outs to contain the minimum amount of lovelaces
169169
- balancing non ada outputs
170+
171+
## Collateral handling
172+
173+
Current version handles collateral under the hood.
174+
175+
Before contract being executed, single transaction submitted to create collateral UTxO at "own" address. Default amount for collateral UTxO is 10 Ada. It also can be set via config.
176+
177+
BPI identifies collateral UTxO by its value. If "own" address already has UTxO with amount set in config, this UTxO will be used as collateral. UTxO that was picked as collateral is stored in memory, so each time BPI will use same UTxO for collateral. Also, collateral is not returned among other UTxOs inside `Contract` monad, e.g. from eDSL functions like `utxosAt`. So it is safe to use such functions without the risk of consuming collateral by accident.

src/BotPlutusInterface/Contract.hs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import BotPlutusInterface.Types (
4242
Tip (block, slot),
4343
TxFile (Signed),
4444
collateralValue,
45+
pcCollateralSize,
46+
pcOwnPubKeyHash,
4547
)
4648
import Cardano.Api (
4749
AsType (..),
@@ -105,7 +107,27 @@ runContract ::
105107
Contract w s e a ->
106108
IO (Either e a)
107109
runContract contractEnv (Contract effs) = do
108-
runM $ handlePABEffect @w contractEnv $ raiseEnd $ handleContract contractEnv effs
110+
-- try to create collateral before any contract is executed
111+
res <- crateCollateralUtxo
112+
case res of
113+
Left e -> error $ mkError e
114+
Right () -> runUserContract
115+
where
116+
crateCollateralUtxo =
117+
runM $ handlePABEffect @w contractEnv (handleCollateral contractEnv)
118+
119+
runUserContract =
120+
runM $
121+
handlePABEffect @w contractEnv $
122+
raiseEnd $
123+
handleContract contractEnv effs
124+
125+
mkError e =
126+
let collateralAmt = pcCollateralSize $ cePABConfig contractEnv
127+
in "Tried to create collateral UTxO with " <> show collateralAmt
128+
<> " lovealces, but failed:\n"
129+
<> show e
130+
<> "\nContract execution aborted."
109131

110132
handleContract ::
111133
forall (w :: Type) (e :: Type) (a :: Type).
@@ -503,37 +525,53 @@ handleCollateral ::
503525
ContractEnvironment w ->
504526
Eff effs (Either WAPI.WalletAPIError ())
505527
handleCollateral cEnv = do
528+
let ownPkh = pcOwnPubKeyHash $ cePABConfig cEnv
506529
result <- (fmap swapEither . runEitherT) $
507530
do
508531
let helperLog :: PP.Doc () -> ExceptT CollateralUtxo (Eff effs) ()
509532
helperLog msg = newEitherT $ Right <$> printBpiLog @w (Debug [CollateralLog]) msg
510533

511534
collateralNotInMem <-
512535
newEitherT $
513-
maybeToLeft "Collateral UTxO not found in contract env."
536+
maybeToLeft ("PKH: " <> pretty ownPkh <> ". Collateral UTxO not found in contract env.")
514537
<$> getInMemCollateral @w
515538

516539
helperLog collateralNotInMem
517540

518541
collateralNotInWallet <- newEitherT $ swapEither <$> findCollateralAtOwnPKH cEnv
519542

520543
helperLog
521-
("Collateral UTxO not found or failed to be found in wallet: " <> pretty collateralNotInWallet)
544+
( "PKH: " <> pretty ownPkh <> ". Collateral UTxO not found or failed to be found in wallet: "
545+
<> pretty collateralNotInWallet
546+
)
522547

523-
helperLog "Creating collateral UTxO."
548+
helperLog ("PKH: " <> pretty ownPkh <> ". Creating collateral UTxO.")
524549

525550
notCreatedCollateral <- newEitherT $ swapEither <$> makeCollateral @w cEnv
526551

527552
helperLog
528-
("Failed to create collateral UTxO: " <> pretty notCreatedCollateral)
553+
( "PKH: " <> pretty ownPkh <> ". Failed to create collateral UTxO: "
554+
<> pretty notCreatedCollateral
555+
)
529556

530-
pure ("Failed to create collateral UTxO: " <> show notCreatedCollateral)
557+
pure
558+
( "PKH: " <> show ownPkh <> ". Failed to create collateral UTxO: "
559+
<> show notCreatedCollateral
560+
)
531561

532562
case result of
533563
Right collteralUtxo ->
534564
setInMemCollateral @w collteralUtxo
535-
>> Right <$> printBpiLog @w (Debug [CollateralLog]) "successfully set the collateral utxo in env."
536-
Left err -> pure $ Left $ WAPI.OtherError $ T.pack $ "Failed to make collateral: " <> show err
565+
>> Right
566+
<$> printBpiLog @w
567+
(Debug [CollateralLog])
568+
("PKH: " <> pretty ownPkh <> ". Successfully set the collateral utxo in env.")
569+
Left err ->
570+
pure $
571+
Left $
572+
WAPI.OtherError $
573+
T.pack $
574+
"PKH: " <> show ownPkh <> ". Failed to make collateral: " <> show err
537575

538576
{- | Create collateral UTxO by submitting Tx.
539577
Then try to find created UTxO at own PKH address.

0 commit comments

Comments
 (0)