Skip to content

Commit

Permalink
Have Initial validator check values
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1bo committed Jan 26, 2022
1 parent 2ee2400 commit 13d2835
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
5 changes: 4 additions & 1 deletion hydra-plutus/src/Hydra/Contract/Commit.hs
Expand Up @@ -81,8 +81,11 @@ compiledValidator = $$(PlutusTx.compile [||validator||])
validatorScript :: Script
validatorScript = unValidatorScript $ Scripts.validatorScript typedValidator

validatorHash :: ValidatorHash
validatorHash = Scripts.validatorHash typedValidator

address :: Address
address = scriptHashAddress $ Scripts.validatorHash typedValidator
address = scriptHashAddress validatorHash

datum :: DatumType Commit -> Datum
datum a = Datum (toBuiltinData a)
Expand Down
38 changes: 27 additions & 11 deletions hydra-plutus/src/Hydra/Contract/Initial.hs
Expand Up @@ -9,9 +9,9 @@ module Hydra.Contract.Initial where
import Ledger hiding (validatorHash)
import PlutusTx.Prelude

import Ledger.Typed.Scripts (TypedValidator, ValidatorType, ValidatorTypes (..))
import qualified Hydra.Contract.Commit as Commit
import Ledger.Typed.Scripts (TypedValidator, ValidatorTypes (..))
import qualified Ledger.Typed.Scripts as Scripts
import PlutusTx (CompiledCode)
import qualified PlutusTx
import PlutusTx.IsData.Class (ToData (..))

Expand All @@ -21,20 +21,36 @@ instance Scripts.ValidatorTypes Initial where
type DatumType Initial = PubKeyHash
type RedeemerType Initial = ()

validator :: DatumType Initial -> RedeemerType Initial -> ScriptContext -> Bool
validator _datum _redeemer _ctx = True
validator ::
-- | Commit validator
ValidatorHash ->
DatumType Initial ->
RedeemerType Initial ->
ScriptContext ->
Bool
validator commitValidator _datum _redeemer context@ScriptContext{scriptContextTxInfo = txInfo} =
checkOutputValue
where
checkOutputValue =
traceIfFalse "checkOutputValue" $
valueProduced txInfo == initialValue + committedValue

initialValue =
maybe mempty (txOutValue . txInInfoResolved) $ findOwnInput context

compiledValidator :: CompiledCode (ValidatorType Initial)
compiledValidator = $$(PlutusTx.compile [||validator||])
committedValue = valueLockedBy txInfo commitValidator

{- ORMOLU_DISABLE -}
typedValidator :: TypedValidator Initial
typedValidator = Scripts.mkTypedValidator @Initial
compiledValidator
$$(PlutusTx.compile [|| wrap ||])
typedValidator =
Scripts.mkTypedValidator @Initial
compiledValidator
$$(PlutusTx.compile [||wrap||])
where
compiledValidator =
$$(PlutusTx.compile [||validator||])
`PlutusTx.applyCode` PlutusTx.liftCode Commit.validatorHash

wrap = Scripts.wrapValidator @(DatumType Initial) @(RedeemerType Initial)
{- ORMOLU_ENABLE -}

-- | Get the actual plutus script. Mainly used to serialize and use in
-- transactions.
Expand Down

0 comments on commit 13d2835

Please sign in to comment.