refactoring

This commit is contained in:
Lars Brünjes 2021-05-17 19:49:33 +02:00
parent d9f053a00f
commit 1ab5906489
No known key found for this signature in database
GPG key ID: B488B9045DC1A087

View file

@ -75,6 +75,13 @@ PlutusTx.unstableMakeIsData ''GameRedeemer
lovelaces :: Value -> Integer lovelaces :: Value -> Integer
lovelaces = Ada.getLovelace . Ada.fromValue 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 #-} {-# INLINABLE mkGameValidator #-}
mkGameValidator :: Game -> ByteString -> ByteString -> GameDatum -> GameRedeemer -> ScriptContext -> Bool mkGameValidator :: Game -> ByteString -> ByteString -> GameDatum -> GameRedeemer -> ScriptContext -> Bool
mkGameValidator game bsZero' bsOne' dat red ctx = case (dat, red) of 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" _ -> traceError "expected exactly one game output"
outputDatum :: GameDatum outputDatum :: GameDatum
outputDatum = case m of outputDatum = case gameDatum ownOutput (`findDatum` info) of
Nothing -> traceError "game output datum not found" Nothing -> traceError "game output datum not found"
Just d -> d 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 :: ByteString -> ByteString -> GameChoice -> Bool
checkNonce bs nonce cSecond = sha2_256 (nonce `concatenate` cFirst) == bs 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 :: (TxOutRef, TxOutTx) -> Maybe (TxOutRef, TxOutTx, Maybe GameChoice)
f (oref, o) = do f (oref, o) = do
guard $ lovelaces (txOutValue $ txOutTxOut o) == 2 * fpStake fp guard $ lovelaces (txOutValue $ txOutTxOut o) == 2 * fpStake fp
dh <- txOutDatum $ txOutTxOut o dat <- gameDatum (txOutTxOut o) (`Map.lookup` txData (txOutTxTx o))
Datum d <- Map.lookup dh $ txData $ txOutTxTx o
dat <- PlutusTx.fromData d
case dat of case dat of
GameDatum bs' mc GameDatum bs' mc
| bs' == bs && (isNothing mc || mc == Just c) -> return (oref, o, 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 :: (TxOutRef, TxOutTx) -> Maybe (TxOutRef, TxOutTx, ByteString)
f (oref, o) = do f (oref, o) = do
guard $ lovelaces (txOutValue $ txOutTxOut o) == spStake sp guard $ lovelaces (txOutValue $ txOutTxOut o) == spStake sp
dh <- txOutDatum $ txOutTxOut o dat <- gameDatum (txOutTxOut o) (`Map.lookup` txData (txOutTxTx o))
Datum d <- Map.lookup dh $ txData $ txOutTxTx o
dat <- PlutusTx.fromData d
case dat of case dat of
GameDatum bs Nothing -> return (oref, o, bs) GameDatum bs Nothing -> return (oref, o, bs)
_ -> Nothing _ -> Nothing