Skip to content

Commit

Permalink
feat: add tx-level-minter sample
Browse files Browse the repository at this point in the history
  • Loading branch information
hadelive committed Apr 27, 2024
1 parent ed765ac commit 9698cad
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions plutarch-design-pattern.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,6 @@ test-suite plutarch-design-pattern-test
other-modules:
Spec.MerkelizedValidatorSpec
Spec.StakeValidatorSpec
Spec.TxLevelMinterSpec

build-depends: plutarch-design-pattern
4 changes: 2 additions & 2 deletions src/Plutarch/TxLevelMinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Plutarch.TxLevelMinter (

import Plutarch.Api.V1.Address (PCredential (..))
import Plutarch.Api.V1.Value (pnormalize)
import Plutarch.Api.V2 (PCurrencySymbol (..), PScriptContext, PScriptPurpose (..), PStakeValidator, PTokenName)
import Plutarch.Api.V2 (PCurrencySymbol (..), PMintingPolicy, PScriptContext, PScriptPurpose (..), PTokenName)
import Plutarch.Api.V2.Contexts (PTxInfo)
import Plutarch.Builtin (pasInt)
import Plutarch.Monadic qualified as P
Expand Down Expand Up @@ -42,7 +42,7 @@ spend = phoistAcyclic $ plam $ \mintTN red ctx -> P.do
(pconstant ())
perror

mint :: Term s (PData :--> PCurrencySymbol :--> PTxInfo :--> PUnit) -> Term s PStakeValidator
mint :: Term s (PData :--> PCurrencySymbol :--> PTxInfo :--> PUnit) -> Term s PMintingPolicy
mint mintLogic =
plam $ \redeemer ctx -> unTermCont $ do
ctxF <- pletFieldsC @'["txInfo", "purpose"] ctx
Expand Down
12 changes: 6 additions & 6 deletions test/Spec/StakeValidatorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ module Spec.StakeValidatorSpec (
validator,
) where

import Plutarch.Api.V2 (PScriptContext, PStakeValidator, PStakingCredential, PValidator)
import Plutarch.Api.V2 (PStakeValidator, PStakingCredential, PValidator)
import Plutarch.Api.V2.Contexts (PTxInfo)
import Plutarch.Multivalidator qualified as Multivalidator
import Plutarch.Prelude
import Plutarch.StakeValidator qualified as StakeValidator

spend :: Term s PValidator
spend = StakeValidator.spend

withdrawLogic :: Term s (PData :--> PStakingCredential :--> PTxInfo :--> PUnit)
withdrawLogic =
plam $ \redData ownValidator txInfo -> unTermCont $ do
plam $ \_ _ _ -> unTermCont $ do
pure $ pconstant ()

withdraw :: Term s PStakeValidator
withdraw = phoistAcyclic $
plam $ \redeemer ctx -> unTermCont $ do
return $ StakeValidator.withdraw withdrawLogic redeemer ctx
withdraw = StakeValidator.withdraw withdrawLogic

validator :: Term s PStakeValidator
validator :: Term s PValidator
validator = Multivalidator.multivalidator withdraw spend
25 changes: 25 additions & 0 deletions test/Spec/TxLevelMinterSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Spec.TxLevelMinterSpec (
validator,
) where

import Plutarch.Api.V2 (PCurrencySymbol, PMintingPolicy, PValidator)
import Plutarch.Api.V2.Contexts (PTxInfo)
import Plutarch.Multivalidator qualified as Multivalidator
import Plutarch.Prelude
import Plutarch.TxLevelMinter qualified as TxLevelMinter

spend :: Term s PValidator
spend = phoistAcyclic $
plam $ \_ redeemer ctx -> unTermCont $ do
return (popaque $ TxLevelMinter.spend # pconstant "BEACON" # redeemer # ctx)

mintLogic :: Term s (PData :--> PCurrencySymbol :--> PTxInfo :--> PUnit)
mintLogic =
plam $ \_ _ _ -> unTermCont $ do
pure $ pconstant ()

mint :: Term s PMintingPolicy
mint = TxLevelMinter.mint mintLogic

validator :: Term s PValidator
validator = Multivalidator.multivalidator mint spend

0 comments on commit 9698cad

Please sign in to comment.