From dba8b3c68b59f97f774e908ab3eb46e8eef0767c Mon Sep 17 00:00:00 2001 From: fanghr Date: Fri, 20 May 2022 18:22:33 +0800 Subject: [PATCH] add `isPubKey` && utilize it in the `TreasuryWithdrawal` effect --- agora/Agora/Effect/TreasuryWithdrawal.hs | 10 ++-------- agora/Agora/Utils.hs | 14 ++++++++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/agora/Agora/Effect/TreasuryWithdrawal.hs b/agora/Agora/Effect/TreasuryWithdrawal.hs index 6bb2851..5bf451c 100644 --- a/agora/Agora/Effect/TreasuryWithdrawal.hs +++ b/agora/Agora/Effect/TreasuryWithdrawal.hs @@ -18,7 +18,7 @@ import GHC.Generics qualified as GHC import Generics.SOP (Generic, I (I)) import Agora.Effect (makeEffect) -import Agora.Utils (findTxOutByTxOutRef, paddValue, tcassert, tclet, tcmatch) +import Agora.Utils (findTxOutByTxOutRef, isPubKey, paddValue, tcassert, tclet, tcmatch) import Plutarch.Api.V1 ( PCredential (..), PTuple, @@ -140,12 +140,6 @@ treasuryWithdrawalValidator currSymbol = makeEffect currSymbol $ treasuryInputValuesSum = sumValues #$ ofTreasury # inputValues treasuryOutputValuesSum = sumValues #$ ofTreasury # outputValues receiverValuesSum = sumValues # datum.receivers - isPubkey = plam $ \cred -> - pmatch cred $ - \case - PPubKeyCredential _ -> pcon PTrue - PScriptCredential _ -> pcon PFalse - -- Constraints outputContentMatchesRecivers = pall # plam (\out -> pelem # out # outputValues) @@ -165,7 +159,7 @@ treasuryWithdrawalValidator currSymbol = makeEffect currSymbol $ ( \((pfield @"_0" #) . pfromData -> cred) -> cred #== pfield @"credential" # effInput.address #|| pelem # cred # datum.treasuries - #|| isPubkey # pfromData cred + #|| isPubKey # pfromData cred ) # inputValues diff --git a/agora/Agora/Utils.hs b/agora/Agora/Utils.hs index 63cf022..1a6424b 100644 --- a/agora/Agora/Utils.hs +++ b/agora/Agora/Utils.hs @@ -60,6 +60,7 @@ module Agora.Utils ( pmergeBy, phalve, isScriptAddress, + isPubKey, ) where -------------------------------------------------------------------------------- @@ -623,10 +624,15 @@ scriptHashFromAddress = phoistAcyclic $ -- | Return true if the given address is a script address. isScriptAddress :: Term s (PAddress :--> PBool) isScriptAddress = phoistAcyclic $ - plam $ \addr -> - pmatch (pfromData $ pfield @"credential" # addr) $ \case - PScriptCredential _ -> pconstant True - _ -> pconstant False + plam $ \addr -> pnot #$ isPubKey #$ pfromData $ pfield @"credential" # addr + +-- | Return true if the given credential is a pub-key-hash. +isPubKey :: Term s (PCredential :--> PBool) +isPubKey = phoistAcyclic $ + plam $ \cred -> + pmatch cred $ \case + PScriptCredential _ -> pconstant False + _ -> pconstant True -- | Find all TxOuts sent to an Address findOutputsToAddress :: Term s (PBuiltinList (PAsData PTxOut) :--> PAddress :--> PBuiltinList (PAsData PTxOut))