added helper functions to treasury
This commit is contained in:
parent
b03674c58c
commit
67335813cf
1 changed files with 71 additions and 5 deletions
|
|
@ -12,8 +12,19 @@ module Agora.Treasury where
|
||||||
|
|
||||||
import GHC.Generics qualified as GHC
|
import GHC.Generics qualified as GHC
|
||||||
import Generics.SOP
|
import Generics.SOP
|
||||||
import Plutarch.Api.V1.Contexts (PScriptContext)
|
import Plutarch.Api.V1.Contexts (
|
||||||
|
PScriptContext,
|
||||||
|
PTuple,
|
||||||
|
PTxInfo (PTxInfo),
|
||||||
|
)
|
||||||
|
import Plutarch.Api.V1.Maybe (PMaybeData (PDJust))
|
||||||
|
import Plutarch.Api.V1.Scripts (PDatum, PDatumHash)
|
||||||
|
import Plutarch.Api.V1.Tx (
|
||||||
|
PTxInInfo (PTxInInfo),
|
||||||
|
PTxOut (PTxOut),
|
||||||
|
)
|
||||||
import Plutarch.Api.V1.Value (PCurrencySymbol, PValue)
|
import Plutarch.Api.V1.Value (PCurrencySymbol, PValue)
|
||||||
|
import Plutarch.Builtin (pforgetData)
|
||||||
import Plutarch.DataRepr (
|
import Plutarch.DataRepr (
|
||||||
PDataFields,
|
PDataFields,
|
||||||
PIsDataReprInstances (PIsDataReprInstances),
|
PIsDataReprInstances (PIsDataReprInstances),
|
||||||
|
|
@ -31,13 +42,68 @@ treasuryV ::
|
||||||
:--> PAsData PScriptContext
|
:--> PAsData PScriptContext
|
||||||
:--> PUnit
|
:--> PUnit
|
||||||
)
|
)
|
||||||
treasuryV = plam $ \_d r ctx' -> P.do
|
treasuryV = plam $ \d r ctx' -> P.do
|
||||||
ctx <- pletFields @["txInfo", "purpose"] ctx'
|
ctx <- pletFields @["txInfo", "purpose"] ctx'
|
||||||
|
PTxInfo txInfo' <- pmatch $ pfromData ctx.txInfo
|
||||||
pmatch (pfromData r) $ \case
|
pmatch (pfromData r) $ \case
|
||||||
|
PWitnessTreasury _ -> P.do
|
||||||
|
txInfo <- pletFields @'["inputs", "outputs", "data"] txInfo'
|
||||||
|
|
||||||
|
-- inputs :: Term s ( PAsData PBuiltinList )
|
||||||
|
let inputs = txInfo.inputs
|
||||||
|
|
||||||
|
-- dat :: Term s (PAsData PBuiltinList)
|
||||||
|
let dat = pfield @"data" # txInfo'
|
||||||
|
|
||||||
|
-- dh :: Term s PDatumHash
|
||||||
|
let dH = getTrDatumHash # d # dat
|
||||||
|
|
||||||
|
pconstant ()
|
||||||
|
|
||||||
-- Validation for receiving funds.
|
-- Validation for receiving funds.
|
||||||
PReceiveFunds _ -> pconstant ()
|
PReceiveFunds _ -> P.do
|
||||||
-- Validation for witnessing transaction.
|
pconstant ()
|
||||||
PWitnessTreasury _ -> pconstant ()
|
|
||||||
|
{- | Plutarch level function that, given a treasury datum and a
|
||||||
|
list of the transaction's data, will find its hash.
|
||||||
|
-}
|
||||||
|
getTrDatumHash ::
|
||||||
|
Term
|
||||||
|
s
|
||||||
|
( PAsData PTreasuryDatum
|
||||||
|
:--> PBuiltinList (PAsData (PTuple PDatumHash PDatum))
|
||||||
|
:--> PDatumHash
|
||||||
|
)
|
||||||
|
getTrDatumHash = plam $ \d l -> P.do
|
||||||
|
let t = phead #$ pfilter # (matchDatums # d) # l
|
||||||
|
pfield @"_0" # t
|
||||||
|
where
|
||||||
|
matchDatums ::
|
||||||
|
Term
|
||||||
|
s
|
||||||
|
( PAsData PTreasuryDatum
|
||||||
|
:--> PAsData (PTuple PDatumHash PDatum)
|
||||||
|
:--> PBool
|
||||||
|
)
|
||||||
|
matchDatums = plam $ \d t' ->
|
||||||
|
let t = pfield @"_1" # t'
|
||||||
|
in (pforgetData d) #== (pforgetData t)
|
||||||
|
|
||||||
|
getValAtDHash ::
|
||||||
|
Term
|
||||||
|
s
|
||||||
|
( PDatumHash
|
||||||
|
:--> PBuiltinList (PAsData PTxOut)
|
||||||
|
:--> PValue
|
||||||
|
)
|
||||||
|
getValAtDHash = plam $ \dh outs -> P.do
|
||||||
|
let matchingOut = phead #$ pfilter # (matchHashes # dh) # outs
|
||||||
|
pfield @"value" # matchingOut
|
||||||
|
where
|
||||||
|
matchHashes :: Term s (PDatumHash :--> PAsData PTxOut :--> PBool)
|
||||||
|
matchHashes = plam $ \dh out' -> P.do
|
||||||
|
PDJust dh' <- pmatch $ pfield @"datumHash" # pfromData out'
|
||||||
|
dh #== pfield @"_0" # dh'
|
||||||
|
|
||||||
{- | Plutarch level type representing datum of the treasury.
|
{- | Plutarch level type representing datum of the treasury.
|
||||||
Contains:
|
Contains:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue