improve efficiency of authorityTokenPolicy
This commit is contained in:
parent
c4db658afb
commit
4fbd4b98f1
2 changed files with 42 additions and 70 deletions
|
|
@ -13,8 +13,7 @@ module Agora.AuthorityToken (
|
||||||
|
|
||||||
import Agora.Utils (
|
import Agora.Utils (
|
||||||
passert,
|
passert,
|
||||||
pnegativeSymbolValueOf,
|
psymbolValueOf',
|
||||||
ppositiveSymbolValueOf,
|
|
||||||
)
|
)
|
||||||
import Plutarch.Api.V1 (
|
import Plutarch.Api.V1 (
|
||||||
PCredential (..),
|
PCredential (..),
|
||||||
|
|
@ -35,6 +34,7 @@ import Plutarch.Api.V2 (
|
||||||
)
|
)
|
||||||
import Plutarch.Extra.AssetClass (PAssetClassData, ptoScottEncoding)
|
import Plutarch.Extra.AssetClass (PAssetClassData, ptoScottEncoding)
|
||||||
import "liqwid-plutarch-extra" Plutarch.Extra.List (plookupAssoc)
|
import "liqwid-plutarch-extra" Plutarch.Extra.List (plookupAssoc)
|
||||||
|
import Plutarch.Extra.Maybe (pfromJust)
|
||||||
import Plutarch.Extra.ScriptContext (pisTokenSpent)
|
import Plutarch.Extra.ScriptContext (pisTokenSpent)
|
||||||
import Plutarch.Extra.Sum (PSum (PSum))
|
import Plutarch.Extra.Sum (PSum (PSum))
|
||||||
import "liqwid-plutarch-extra" Plutarch.Extra.TermCont (
|
import "liqwid-plutarch-extra" Plutarch.Extra.TermCont (
|
||||||
|
|
@ -160,10 +160,8 @@ authorityTokenPolicy =
|
||||||
|
|
||||||
let ownSymbol = pfromData $ pfield @"_0" # ownSymbol'
|
let ownSymbol = pfromData $ pfield @"_0" # ownSymbol'
|
||||||
|
|
||||||
applySymbolValueOf <- pletC $ plam $ \f -> f # ownSymbol # txInfo.mint
|
PPair mintedATs burntATs <-
|
||||||
|
pmatchC $ pfromJust #$ psymbolValueOf' # ownSymbol # txInfo.mint
|
||||||
mintedATs <- pletC $ applySymbolValueOf # ppositiveSymbolValueOf
|
|
||||||
let burntATs = applySymbolValueOf # pnegativeSymbolValueOf
|
|
||||||
|
|
||||||
pure $
|
pure $
|
||||||
popaque $
|
popaque $
|
||||||
|
|
|
||||||
|
|
@ -33,34 +33,30 @@ module Agora.Utils (
|
||||||
passert,
|
passert,
|
||||||
pisNothing,
|
pisNothing,
|
||||||
pisDNothing,
|
pisDNothing,
|
||||||
ppositiveSymbolValueOf,
|
psymbolValueOf',
|
||||||
pnegativeSymbolValueOf,
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Plutarch.Api.V1 (
|
import Plutarch.Api.V1 (
|
||||||
KeyGuarantees (Unsorted),
|
KeyGuarantees (Unsorted),
|
||||||
PPOSIXTime,
|
PPOSIXTime,
|
||||||
PRedeemer,
|
PRedeemer,
|
||||||
PTokenName,
|
|
||||||
PValidatorHash,
|
PValidatorHash,
|
||||||
)
|
)
|
||||||
import Plutarch.Api.V1.AssocMap (PMap, plookup)
|
import Plutarch.Api.V1.AssocMap (PMap, plookup)
|
||||||
import Plutarch.Api.V2 (
|
import Plutarch.Api.V2 (
|
||||||
AmountGuarantees,
|
AmountGuarantees,
|
||||||
PCurrencySymbol,
|
PCurrencySymbol,
|
||||||
PMap (PMap),
|
|
||||||
PMaybeData (PDNothing),
|
PMaybeData (PDNothing),
|
||||||
PScriptHash,
|
PScriptHash,
|
||||||
PScriptPurpose,
|
PScriptPurpose,
|
||||||
PValue (PValue),
|
PTokenName,
|
||||||
|
PValue,
|
||||||
)
|
)
|
||||||
import Plutarch.Extra.Applicative (PApplicative (ppure))
|
import Plutarch.Extra.Applicative (PApplicative (ppure))
|
||||||
import Plutarch.Extra.Category (PCategory (pidentity))
|
import Plutarch.Extra.Category (PCategory (pidentity))
|
||||||
import Plutarch.Extra.Functor (PFunctor (PSubcategory, pfmap))
|
import Plutarch.Extra.Functor (PFunctor (PSubcategory, pfmap))
|
||||||
import "liqwid-plutarch-extra" Plutarch.Extra.List (plookupAssoc)
|
import Plutarch.Extra.Maybe (pjust, pnothing)
|
||||||
import Plutarch.Extra.Maybe (pexpectJustC, pjust, pnothing)
|
|
||||||
import Plutarch.Extra.Ord (PComparator, POrdering (PLT), pcompareBy, pequateBy)
|
import Plutarch.Extra.Ord (PComparator, POrdering (PLT), pcompareBy, pequateBy)
|
||||||
import "liqwid-plutarch-extra" Plutarch.Extra.TermCont (pmatchC)
|
|
||||||
import Plutarch.Extra.Time (PCurrentTime (PCurrentTime))
|
import Plutarch.Extra.Time (PCurrentTime (PCurrentTime))
|
||||||
import Plutarch.Unsafe (punsafeCoerce)
|
import Plutarch.Unsafe (punsafeCoerce)
|
||||||
import PlutusLedgerApi.V2 (
|
import PlutusLedgerApi.V2 (
|
||||||
|
|
@ -426,67 +422,45 @@ pisDNothing = phoistAcyclic $
|
||||||
PDNothing _ -> pconstant True
|
PDNothing _ -> pconstant True
|
||||||
_ -> pconstant False
|
_ -> pconstant False
|
||||||
|
|
||||||
psymbolValueOfHelper ::
|
{- | Get the negative and positive amount of a particular 'CurrencySymbol', and
|
||||||
|
return nothing if it doesn't exist in the value.
|
||||||
|
|
||||||
|
@since 1.0.0
|
||||||
|
-}
|
||||||
|
psymbolValueOf' ::
|
||||||
forall
|
forall
|
||||||
(keys :: KeyGuarantees)
|
(keys :: KeyGuarantees)
|
||||||
(amounts :: AmountGuarantees)
|
(amounts :: AmountGuarantees)
|
||||||
(s :: S).
|
(s :: S).
|
||||||
Term
|
Term
|
||||||
s
|
s
|
||||||
( (PInteger :--> PBool)
|
( PCurrencySymbol
|
||||||
:--> PCurrencySymbol
|
:--> PValue keys amounts
|
||||||
:--> ( PValue keys amounts
|
:--> PMaybe
|
||||||
:--> PInteger
|
( PPair
|
||||||
)
|
-- Positive amount
|
||||||
|
PInteger
|
||||||
|
-- Negative amount
|
||||||
|
PInteger
|
||||||
|
)
|
||||||
)
|
)
|
||||||
psymbolValueOfHelper =
|
psymbolValueOf' = phoistAcyclic $
|
||||||
phoistAcyclic $
|
plam $ \sym value ->
|
||||||
plam $ \cond sym value'' -> unTermCont $ do
|
let tnMap = plookup # sym # pto value
|
||||||
PValue value' <- pmatchC value''
|
f =
|
||||||
PMap value <- pmatchC value'
|
plam $
|
||||||
m' <-
|
( pfoldr
|
||||||
pexpectJustC
|
# plam
|
||||||
0
|
( \x r ->
|
||||||
( plookupAssoc
|
let q = pfromData $ psndBuiltin # x
|
||||||
# pfstBuiltin
|
in pmatch r $ \(PPair p n) ->
|
||||||
# psndBuiltin
|
pif
|
||||||
# pdata sym
|
(0 #< q)
|
||||||
# value
|
(pcon $ PPair (p + q) n)
|
||||||
)
|
(pcon $ PPair p (n + q))
|
||||||
PMap m <- pmatchC (pfromData m')
|
)
|
||||||
pure $
|
# pcon (PPair 0 0)
|
||||||
pfoldr
|
#
|
||||||
# plam
|
|
||||||
( \x v ->
|
|
||||||
plet (pfromData $ psndBuiltin # x) $ \q ->
|
|
||||||
pif
|
|
||||||
(cond # q)
|
|
||||||
(q + v)
|
|
||||||
v
|
|
||||||
)
|
)
|
||||||
# 0
|
. pto
|
||||||
# m
|
in pfmap # f # tnMap
|
||||||
|
|
||||||
{- | The sum of positive entries belonging to a particular currency symbol.
|
|
||||||
|
|
||||||
@since 1.0.0
|
|
||||||
-}
|
|
||||||
ppositiveSymbolValueOf ::
|
|
||||||
forall
|
|
||||||
(keys :: KeyGuarantees)
|
|
||||||
(amounts :: AmountGuarantees)
|
|
||||||
(s :: S).
|
|
||||||
Term s (PCurrencySymbol :--> (PValue keys amounts :--> PInteger))
|
|
||||||
ppositiveSymbolValueOf = phoistAcyclic $ psymbolValueOfHelper #$ plam (0 #<)
|
|
||||||
|
|
||||||
{- | The sum of negative entries belonging to a particular currency symbol.
|
|
||||||
|
|
||||||
@since 1.0.0
|
|
||||||
-}
|
|
||||||
pnegativeSymbolValueOf ::
|
|
||||||
forall
|
|
||||||
(keys :: KeyGuarantees)
|
|
||||||
(amounts :: AmountGuarantees)
|
|
||||||
(s :: S).
|
|
||||||
Term s (PCurrencySymbol :--> (PValue keys amounts :--> PInteger))
|
|
||||||
pnegativeSymbolValueOf = phoistAcyclic $ psymbolValueOfHelper #$ plam (#< 0)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue