Last active
March 9, 2021 16:02
-
-
Save ElGatoLoco/4dd94b60e28aec42868e1a9bf1e5ce5b to your computer and use it in GitHub Desktop.
Plutus playground starter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This is a starter contract, based on the Game contract, | |
-- containing the bare minimum required scaffolding. | |
-- | |
-- What you should change to something more suitable for | |
-- your use case: | |
-- * The MyDatum type | |
-- * The MyMyRedeemerValue type | |
-- | |
-- And add function implementations (and rename them to | |
-- something suitable) for the endpoints: | |
-- * publish | |
-- * redeem | |
import Control.Monad (void) | |
import Language.Plutus.Contract | |
import qualified Language.PlutusTx as PlutusTx | |
import Language.PlutusTx.Prelude hiding (Applicative (..)) | |
import Ledger (Address, ValidatorCtx, scriptAddress) | |
import qualified Ledger.Constraints as Constraints | |
import qualified Ledger.Typed.Scripts as Scripts | |
import Ledger.Value (Value) | |
import Playground.Contract | |
import qualified Prelude | |
-- | These are the data script and redeemer types. We are using an integer | |
-- value for both, but you should define your own types. | |
newtype MyDatum = MyDatum Integer deriving newtype PlutusTx.IsData | |
PlutusTx.makeLift ''MyDatum | |
newtype MyRedeemer = MyRedeemer Integer deriving newtype PlutusTx.IsData | |
PlutusTx.makeLift ''MyRedeemer | |
-- | This method is the spending validator (which gets lifted to | |
-- its on-chain representation). | |
validateSpend :: MyDatum -> MyRedeemer -> ValidatorCtx -> Bool | |
validateSpend (MyDatum myDataValue) (MyRedeemer myRedeemerValue) _ = myDataValue == myRedeemerValue | |
-- | The address of the contract (the hash of its validator script). | |
contractAddress :: Address | |
contractAddress = Ledger.scriptAddress (Scripts.validatorScript starterInstance) | |
data Starter | |
instance Scripts.ScriptType Starter where | |
type instance RedeemerType Starter = MyRedeemer | |
type instance DatumType Starter = MyDatum | |
-- | The script instance is the compiled validator (ready to go onto the chain) | |
starterInstance :: Scripts.ScriptInstance Starter | |
starterInstance = Scripts.validator @Starter | |
$$(PlutusTx.compile [|| validateSpend ||]) | |
$$(PlutusTx.compile [|| wrap ||]) where | |
wrap = Scripts.wrapValidator @MyDatum @MyRedeemer | |
-- | The schema of the contract, with two endpoints. | |
type Schema = | |
BlockchainActions | |
.\/ Endpoint "publish" PublishParams | |
.\/ Endpoint "redeem" RedeemParams | |
data PublishParams = PublishParams | |
{ myDataValue :: Integer | |
, lockedFunds :: Value | |
} | |
deriving stock (Prelude.Eq, Prelude.Show, Generic) | |
deriving anyclass (FromJSON, ToJSON, IotsType, ToSchema, ToArgument) | |
newtype RedeemParams = RedeemParams | |
{ myRedeemerValue :: Integer | |
} | |
deriving stock (Prelude.Eq, Prelude.Show, Generic) | |
deriving anyclass (FromJSON, ToJSON, IotsType, ToSchema, ToArgument) | |
contract :: AsContractError e => Contract Schema e () | |
contract = publish `select` redeem | |
-- | The "publish" contract endpoint. | |
publish :: AsContractError e => Contract Schema e () | |
publish = do | |
PublishParams i lockedFunds <- endpoint @"publish" | |
let tx = Constraints.mustPayToTheScript (MyDatum i) lockedFunds | |
void $ submitTxConstraints starterInstance tx | |
-- | The "redeem" contract endpoint. | |
redeem :: AsContractError e => Contract Schema e () | |
redeem = do | |
RedeemParams myRedeemerValue <- endpoint @"redeem" | |
unspentOutputs <- utxoAt contractAddress | |
let redeemer = MyRedeemer myRedeemerValue | |
tx = collectFromScript unspentOutputs redeemer | |
void $ submitTxConstraintsSpending starterInstance unspentOutputs tx | |
endpoints :: AsContractError e => Contract Schema e () | |
endpoints = contract | |
mkSchemaDefinitions ''Schema | |
$(mkKnownCurrencies []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment