Skip to content

Commit c31302f

Browse files
committed
adjusting collateral creation
- try to create before running contract
1 parent 74e9d95 commit c31302f

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
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: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import BotPlutusInterface.Types (
4242
Tip (block, slot),
4343
TxFile (Signed),
4444
collateralValue,
45+
pcCollateralSize,
4546
)
4647
import Cardano.Api (
4748
AsType (..),
@@ -105,7 +106,27 @@ runContract ::
105106
Contract w s e a ->
106107
IO (Either e a)
107108
runContract contractEnv (Contract effs) = do
108-
runM $ handlePABEffect @w contractEnv $ raiseEnd $ handleContract contractEnv effs
109+
-- try to create collateral before any contract is executed
110+
res <- crateCollateralUtxo
111+
case res of
112+
Left e -> error $ mkError e
113+
Right () -> runUserContract
114+
where
115+
crateCollateralUtxo =
116+
runM $ handlePABEffect @w contractEnv (handleCollateral contractEnv)
117+
118+
runUserContract =
119+
runM $
120+
handlePABEffect @w contractEnv $
121+
raiseEnd $
122+
handleContract contractEnv effs
123+
124+
mkError e =
125+
let collateralAmt = pcCollateralSize $ cePABConfig contractEnv
126+
in "Tried to create collateral UTxO with " <> show collateralAmt
127+
<> " lovealces, but failed:\n"
128+
<> show e
129+
<> "\nContract execution aborted."
109130

110131
handleContract ::
111132
forall (w :: Type) (e :: Type) (a :: Type).

0 commit comments

Comments
 (0)