From 0570ce08cc95afa2d2e8f4b40ef0ad09ad7a8473 Mon Sep 17 00:00:00 2001 From: nini-faroux Date: Thu, 13 Apr 2023 16:56:49 +0100 Subject: [PATCH] Find governor output, no longer require it to be only one --- agora/Agora/Effect/GovernorMutation.hs | 16 ++++-- agora/Agora/Utils.hs | 72 ++++++++++++++++++-------- 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/agora/Agora/Effect/GovernorMutation.hs b/agora/Agora/Effect/GovernorMutation.hs index a35d920..b52ae6b 100644 --- a/agora/Agora/Effect/GovernorMutation.hs +++ b/agora/Agora/Effect/GovernorMutation.hs @@ -27,7 +27,7 @@ import Agora.Governor ( ) import Agora.Proposal (PProposalId) import Agora.SafeMoney (AuthorityTokenTag, GovernorSTTag) -import Agora.Utils (pfindInputWithStateThreadToken) +import Agora.Utils (pfindInputWithStateThreadToken, pfindOutputWithStateThreadTokenAndAddress) import Generics.SOP qualified as SOP import Plutarch.Api.V1 (PCurrencySymbol) import Plutarch.Api.V2 ( @@ -55,7 +55,7 @@ import Plutarch.Extra.ScriptContext ( import Plutarch.Extra.Tagged (PTagged) import Plutarch.Lift (PConstantDecl, PLifted, PUnsafeLiftDecl) import PlutusTx qualified -import "liqwid-plutarch-extra" Plutarch.Extra.List (ptryFromSingleton) + import "liqwid-plutarch-extra" Plutarch.Extra.TermCont (pguardC, pletC, pletFieldsC) -------------------------------------------------------------------------------- @@ -204,6 +204,8 @@ mutateGovernorValidator = governorRef = pfield @"outRef" # governorInput + governorInputAddress = pfield @"address" #$ pfield @"resolved" # governorInput + governorInputDatum = ptrace "Resolve governor input datum" $ pfromData $ @@ -240,7 +242,7 @@ mutateGovernorValidator = governorRedeemer = pfromData $ passertPJust - # "Govenor redeemer should be resolved" + # "Governor redeemer should be resolved" #$ ptryFromRedeemer @(PAsData PGovernorRedeemer) # mkRecordConstr PSpending (#_0 .= governorRef) # txInfoF.redeemers @@ -252,8 +254,12 @@ mutateGovernorValidator = let governorOutput = - ptrace "Only governor output is allowed" $ - ptryFromSingleton # pfromData txInfoF.outputs + passertPJust + # "No governor output found" + #$ pfindOutputWithStateThreadTokenAndAddress + # pfromData gstSymbol + # governorInputAddress + # pfromData txInfoF.outputs governorOutputDatum = ptrace "Resolve governor outoput datum" $ diff --git a/agora/Agora/Utils.hs b/agora/Agora/Utils.hs index 5bd6b19..03aaefe 100644 --- a/agora/Agora/Utils.hs +++ b/agora/Agora/Utils.hs @@ -20,8 +20,9 @@ module Agora.Utils ( phashDatum, puncurryTuple, psubtractSortedValue, - pisSubValueOf, pfindInputWithStateThreadToken, + pfindOutputWithStateThreadTokenAndAddress, + pisSubValueOf, ) where import Plutarch.Api.V1 (AmountGuarantees (Positive), KeyGuarantees (Sorted)) @@ -30,10 +31,12 @@ import Plutarch.Api.V1.AssocMap qualified as AssocMap import Plutarch.Api.V1.Scripts (PDatumHash (PDatumHash)) import Plutarch.Api.V2 ( AmountGuarantees (NoGuarantees), + PAddress, PCurrencySymbol, PMaybeData (PDNothing), PTuple, PTxInInfo, + PTxOut, PValue, ) import Plutarch.Builtin (pforgetData, pserialiseData) @@ -178,6 +181,52 @@ psubtractSortedValue = phoistAcyclic $ plam $ \a b -> # (pfmap # pnegate) # pto b +{- | Find an input containing exactly one token with the given currency symbol + + @since 1.0.0 +-} +pfindInputWithStateThreadToken :: + forall tag. + ClosedTerm + ( PTagged tag PCurrencySymbol + :--> PBuiltinList PTxInInfo + :--> PMaybe PTxInInfo + ) +pfindInputWithStateThreadToken = plam $ \tokenSymbol inputs -> + pfind + # ( plam $ \input -> + ptaggedSymbolValueOf + # tokenSymbol + # (pfield @"value" # (pfield @"resolved" # input)) + #== 1 + ) + # inputs + +{- | Find an output containing exactly one token with the given currency symbol, + and with a PAddress that matches the given one. + + @since 1.0.0 +-} +pfindOutputWithStateThreadTokenAndAddress :: + forall tag. + ClosedTerm + ( PTagged tag PCurrencySymbol + :--> PAddress + :--> PBuiltinList PTxOut + :--> PMaybe PTxOut + ) +pfindOutputWithStateThreadTokenAndAddress = plam $ \tokenSymbol address outputs -> + pfind + # ( plam $ \output -> + ( ptaggedSymbolValueOf + # tokenSymbol + # (pfield @"value" # output) + #== 1 + ) + #&& (address #== (pfield @"address" # output)) + ) + # outputs + pisNonNegativeValue :: forall (kg :: KeyGuarantees) (am :: AmountGuarantees) (s :: S). Term s (PValue kg am :--> PBool) @@ -200,24 +249,3 @@ pisSubValueOf = phoistAcyclic $ plam $ \vl vr -> #$ psubtractSortedValue # vl # vr - -{- | Find an input containing exactly one token with the given currency symbol - - @since 1.0.0 --} -pfindInputWithStateThreadToken :: - forall tag. - ClosedTerm - ( PTagged tag PCurrencySymbol - :--> PBuiltinList PTxInInfo - :--> PMaybe PTxInInfo - ) -pfindInputWithStateThreadToken = plam $ \tokenSymbol inputs -> - pfind - # ( plam $ \input -> - ptaggedSymbolValueOf - # tokenSymbol - # (pfield @"value" # (pfield @"resolved" # input)) - #== 1 - ) - # inputs