Skip to content

Commit

Permalink
Fix AdditionalUtxos example by locking spent utxos
Browse files Browse the repository at this point in the history
  • Loading branch information
errfrom committed Sep 25, 2023
1 parent 854a4aa commit 2909b93
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions examples/AdditionalUtxos.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Contract.Config (ContractParams, testnetNamiConfig)
import Contract.Log (logInfo')
import Contract.Monad (Contract, launchAff_, liftedE, runContract)
import Contract.PlutusData (Datum, PlutusData(Integer), unitRedeemer)
import Contract.ScriptLookups (ScriptLookups, mkUnbalancedTx)
import Contract.ScriptLookups (ScriptLookups, UnbalancedTx, mkUnbalancedTx)
import Contract.ScriptLookups (datum, unspentOutputs, validator) as Lookups
import Contract.Scripts (Validator, ValidatorHash, validatorHash)
import Contract.Sync (withoutSync)
Expand All @@ -25,6 +25,7 @@ import Contract.Transaction
, createAdditionalUtxos
, signTransaction
, submit
, withBalancedTx
)
import Contract.TxConstraints
( DatumPresence(DatumInline, DatumWitness)
Expand Down Expand Up @@ -58,14 +59,19 @@ contract testAdditionalUtxoOverlap = withoutSync do
logInfo' "Running Examples.AdditionalUtxos"
validator <- alwaysSucceedsScriptV2
let vhash = validatorHash validator
{ additionalUtxos, datum } <- payToValidator vhash testAdditionalUtxoOverlap
spendFromValidator validator additionalUtxos datum
{ unbalancedTx, datum } <- payToValidator vhash
withBalancedTx unbalancedTx \balancedTx -> do
balancedSignedTx <- signTransaction balancedTx
txHash <- submit balancedSignedTx
when testAdditionalUtxoOverlap $ awaitTxConfirmed txHash
logInfo' "Successfully locked two outputs at the validator address."

additionalUtxos <- createAdditionalUtxos balancedSignedTx
spendFromValidator validator additionalUtxos datum

payToValidator
:: ValidatorHash
-> Boolean
-> Contract { additionalUtxos :: UtxoMap, datum :: Datum }
payToValidator vhash testAdditionalUtxoOverlap = do
:: ValidatorHash -> Contract { unbalancedTx :: UnbalancedTx, datum :: Datum }
payToValidator vhash = do
scriptRef <- liftEffect (NativeScriptRef <$> randomSampleOne arbitrary)
let
value :: Value
Expand All @@ -85,14 +91,7 @@ payToValidator vhash testAdditionalUtxoOverlap = do
lookups = Lookups.datum datum

unbalancedTx <- liftedE $ mkUnbalancedTx lookups constraints
balancedTx <- liftedE $ balanceTx unbalancedTx
balancedSignedTx <- signTransaction balancedTx
txHash <- submit balancedSignedTx
when testAdditionalUtxoOverlap $ awaitTxConfirmed txHash
logInfo' "Successfully locked two outputs at the validator address."

additionalUtxos <- createAdditionalUtxos balancedSignedTx
pure { additionalUtxos, datum }
pure { unbalancedTx, datum }

spendFromValidator :: Validator -> UtxoMap -> Datum -> Contract Unit
spendFromValidator validator additionalUtxos datum = do
Expand Down

0 comments on commit 2909b93

Please sign in to comment.