Skip to content

Commit 5c4cb93

Browse files
authored
Merge pull request #108 from mlabs-haskell/collateral-ignore
Ignore collateral handling logic for simple transactions
2 parents 6de6e56 + 18b9ea5 commit 5c4cb93

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

src/BotPlutusInterface/Balance.hs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
{-# LANGUAGE NamedFieldPuns #-}
23

34
module BotPlutusInterface.Balance (
45
balanceTxStep,
@@ -281,17 +282,25 @@ balanceTxIns utxos tx = do
281282
(suboptimally we just pick a random utxo from the tx inputs)
282283
-}
283284
addTxCollaterals :: Map TxOutRef TxOut -> Tx -> Either Text Tx
284-
addTxCollaterals utxos tx = do
285-
let txIns = mapMaybe (rightToMaybe . txOutToTxIn) $ Map.toList $ filterAdaOnly utxos
286-
txIn <- findPubKeyTxIn txIns
287-
pure $ tx {txCollateral = Set.singleton txIn}
285+
addTxCollaterals utxos tx =
286+
if not $ usesScripts tx
287+
then Right tx
288+
else do
289+
let txIns = mapMaybe (rightToMaybe . txOutToTxIn) $ Map.toList $ filterAdaOnly utxos
290+
txIn <- findPubKeyTxIn txIns
291+
pure $ tx {txCollateral = Set.singleton txIn}
288292
where
289293
findPubKeyTxIn = \case
290294
x@(TxIn _ (Just ConsumePublicKeyAddress)) : _ -> Right x
291295
x@(TxIn _ Nothing) : _ -> Right x
292296
_ : xs -> findPubKeyTxIn xs
293297
_ -> Left "There are no utxos to be used as collateral"
294298
filterAdaOnly = Map.filter (isAdaOnly . txOutValue)
299+
usesScripts Tx {txInputs, txMintScripts} =
300+
not (null txMintScripts)
301+
|| any
302+
(\TxIn {txInType} -> case txInType of Just ConsumeScriptAddress {} -> True; _ -> False)
303+
(Set.toList txInputs)
295304

296305
-- | Ensures all non ada change goes back to user
297306
handleNonAdaChange :: Address -> Map TxOutRef TxOut -> Tx -> Either Text Tx

test/Spec/BotPlutusInterface/Contract.hs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ sendAda = do
132132
, [text|
133133
cardano-cli transaction build-raw --alonzo-era
134134
--tx-in ${inTxId}#0
135-
--tx-in-collateral ${inTxId}#0
136135
--tx-out ${addr2}+1000
137136
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
138137
--fee 0
@@ -157,7 +156,6 @@ sendAda = do
157156
, [text|
158157
cardano-cli transaction build-raw --alonzo-era
159158
--tx-in ${inTxId}#0
160-
--tx-in-collateral ${inTxId}#0
161159
--tx-out ${addr1}+50
162160
--tx-out ${addr2}+1000
163161
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
@@ -197,7 +195,6 @@ sendAdaNoChange = do
197195
, [text|
198196
cardano-cli transaction build-raw --alonzo-era
199197
--tx-in ${inTxId}#0
200-
--tx-in-collateral ${inTxId}#0
201198
--tx-out ${addr2}+1000
202199
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
203200
--fee 200
@@ -246,7 +243,6 @@ sendAdaStaking = do
246243
, [text|
247244
cardano-cli transaction build-raw --alonzo-era
248245
--tx-in ${inTxId}#0
249-
--tx-in-collateral ${inTxId}#0
250246
--tx-out ${addr2Staking}+1000
251247
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
252248
--fee 0
@@ -270,7 +266,6 @@ sendAdaStaking = do
270266
, [text|
271267
cardano-cli transaction build-raw --alonzo-era
272268
--tx-in ${inTxId}#0
273-
--tx-in-collateral ${inTxId}#0
274269
--tx-out ${addr2Staking}+1000
275270
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
276271
--fee 200
@@ -323,7 +318,6 @@ multisigSupport = do
323318
, [text|
324319
cardano-cli transaction build-raw --alonzo-era
325320
--tx-in ${inTxId}#0
326-
--tx-in-collateral ${inTxId}#0
327321
--tx-out ${addr2}+1000
328322
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
329323
--required-signer ./signing-keys/signing-key-${pkh3'}.skey
@@ -374,7 +368,6 @@ withoutSigning = do
374368
, [text|
375369
cardano-cli transaction build-raw --alonzo-era
376370
--tx-in ${inTxId}#0
377-
--tx-in-collateral ${inTxId}#0
378371
--tx-out ${addr2}+1000
379372
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
380373
--required-signer-hash ${pkh3'}
@@ -401,7 +394,6 @@ sendTokens = do
401394
Nothing
402395
initState = def & utxos .~ [(txOutRef1, txOut1), (txOutRef2, txOut2)]
403396
inTxId1 = encodeByteString $ fromBuiltin $ TxId.getTxId $ Tx.txOutRefId txOutRef1
404-
inTxId2 = encodeByteString $ fromBuiltin $ TxId.getTxId $ Tx.txOutRefId txOutRef2
405397

406398
contract :: Contract () (Endpoint "SendAda" ()) Text CardanoTx
407399
contract = do
@@ -419,7 +411,6 @@ sendTokens = do
419411
, [text|
420412
cardano-cli transaction build-raw --alonzo-era
421413
--tx-in ${inTxId1}#0
422-
--tx-in-collateral ${inTxId2}#1
423414
--tx-out ${addr1}+50 + 95 abcd1234.74657374546F6B656E
424415
--tx-out ${addr2}+1000 + 5 abcd1234.74657374546F6B656E
425416
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
@@ -445,7 +436,6 @@ sendTokensWithoutName = do
445436
Nothing
446437
initState = def & utxos .~ [(txOutRef1, txOut1), (txOutRef2, txOut2)]
447438
inTxId1 = encodeByteString $ fromBuiltin $ TxId.getTxId $ Tx.txOutRefId txOutRef1
448-
inTxId2 = encodeByteString $ fromBuiltin $ TxId.getTxId $ Tx.txOutRefId txOutRef2
449439

450440
contract :: Contract () (Endpoint "SendAda" ()) Text CardanoTx
451441
contract = do
@@ -463,7 +453,6 @@ sendTokensWithoutName = do
463453
, [text|
464454
cardano-cli transaction build-raw --alonzo-era
465455
--tx-in ${inTxId1}#0
466-
--tx-in-collateral ${inTxId2}#1
467456
--tx-out ${addr1}+50 + 95 abcd1234
468457
--tx-out ${addr2}+1000 + 5 abcd1234
469458
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
@@ -608,7 +597,6 @@ spendToValidator = do
608597
, [text|
609598
cardano-cli transaction build-raw --alonzo-era
610599
--tx-in ${inTxId}#0
611-
--tx-in-collateral ${inTxId}#0
612600
--tx-out ${valAddr'}+500
613601
--tx-out-datum-embed-file ./result-scripts/datum-${datumHash'}.json
614602
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
@@ -621,7 +609,6 @@ spendToValidator = do
621609
, [text|
622610
cardano-cli transaction build-raw --alonzo-era
623611
--tx-in ${inTxId}#0
624-
--tx-in-collateral ${inTxId}#0
625612
--tx-out ${addr1}+200
626613
--tx-out ${valAddr'}+500
627614
--tx-out-datum-embed-file ./result-scripts/datum-${datumHash'}.json
@@ -790,7 +777,6 @@ withValidRange = do
790777
, [text|
791778
cardano-cli transaction build-raw --alonzo-era
792779
--tx-in ${inTxId}#0
793-
--tx-in-collateral ${inTxId}#0
794780
--tx-out ${addr2}+1000
795781
--invalid-before 47577202
796782
--invalid-hereafter 50255602
@@ -804,7 +790,6 @@ withValidRange = do
804790
, [text|
805791
cardano-cli transaction build-raw --alonzo-era
806792
--tx-in ${inTxId}#0
807-
--tx-in-collateral ${inTxId}#0
808793
--tx-out ${addr2}+1000
809794
--invalid-before 47577202
810795
--invalid-hereafter 50255602

0 commit comments

Comments
 (0)