From 1ab59064894593dde26be44c0694e5279dbf8bad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Br=C3=BCnjes?= Date: Mon, 17 May 2021 19:49:33 +0200 Subject: [PATCH] refactoring --- code/week07/src/Week07/EvenOdd.hs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/code/week07/src/Week07/EvenOdd.hs b/code/week07/src/Week07/EvenOdd.hs index 9b53bd9..7f2eebd 100644 --- a/code/week07/src/Week07/EvenOdd.hs +++ b/code/week07/src/Week07/EvenOdd.hs @@ -75,6 +75,13 @@ PlutusTx.unstableMakeIsData ''GameRedeemer lovelaces :: Value -> Integer lovelaces = Ada.getLovelace . Ada.fromValue +{-# INLINABLE gameDatum #-} +gameDatum :: TxOut -> (DatumHash -> Maybe Datum) -> Maybe GameDatum +gameDatum o f = do + dh <- txOutDatum o + Datum d <- f dh + PlutusTx.fromData d + {-# INLINABLE mkGameValidator #-} mkGameValidator :: Game -> ByteString -> ByteString -> GameDatum -> GameRedeemer -> ScriptContext -> Bool mkGameValidator game bsZero' bsOne' dat red ctx = case (dat, red) of @@ -114,15 +121,9 @@ mkGameValidator game bsZero' bsOne' dat red ctx = case (dat, red) of _ -> traceError "expected exactly one game output" outputDatum :: GameDatum - outputDatum = case m of + outputDatum = case gameDatum ownOutput (`findDatum` info) of Nothing -> traceError "game output datum not found" Just d -> d - where - m :: Maybe GameDatum - m = do - dh <- txOutDatum ownOutput - Datum d <- findDatum dh info - PlutusTx.fromData d checkNonce :: ByteString -> ByteString -> GameChoice -> Bool checkNonce bs nonce cSecond = sha2_256 (nonce `concatenate` cFirst) == bs @@ -219,9 +220,7 @@ firstGame fp = do f :: (TxOutRef, TxOutTx) -> Maybe (TxOutRef, TxOutTx, Maybe GameChoice) f (oref, o) = do guard $ lovelaces (txOutValue $ txOutTxOut o) == 2 * fpStake fp - dh <- txOutDatum $ txOutTxOut o - Datum d <- Map.lookup dh $ txData $ txOutTxTx o - dat <- PlutusTx.fromData d + dat <- gameDatum (txOutTxOut o) (`Map.lookup` txData (txOutTxTx o)) case dat of GameDatum bs' mc | bs' == bs && (isNothing mc || mc == Just c) -> return (oref, o, mc) @@ -292,9 +291,7 @@ secondGame sp = do f :: (TxOutRef, TxOutTx) -> Maybe (TxOutRef, TxOutTx, ByteString) f (oref, o) = do guard $ lovelaces (txOutValue $ txOutTxOut o) == spStake sp - dh <- txOutDatum $ txOutTxOut o - Datum d <- Map.lookup dh $ txData $ txOutTxTx o - dat <- PlutusTx.fromData d + dat <- gameDatum (txOutTxOut o) (`Map.lookup` txData (txOutTxTx o)) case dat of GameDatum bs Nothing -> return (oref, o, bs) _ -> Nothing