Last active
March 10, 2021 15:34
-
-
Save gclaramunt/a0ecef5b6fb8237709671aa585f6b9d9 to your computer and use it in GitHub Desktop.
Plutus Playground Smart Contract
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 qualified Ledger.Contexts as Validation | |
import Playground.Contract | |
import Plutus.V1.Ledger.Scripts (validatorHash) | |
-- | 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 _myDataValue _myRedeemerValue ctx@ValidatorCtx{valCtxTxInfo=txInfo} = | |
let | |
toPinlockValidator = Validation.valueLockedBy txInfo (validatorHash (Scripts.validatorScript bountyInstance) ) | |
in | |
toPinlockValidator == toPinlockValidator | |
------------ | |
data MathBounty | |
instance Scripts.ScriptType MathBounty where | |
type instance RedeemerType MathBounty = Integer | |
type instance DatumType MathBounty = Integer | |
validateSolution :: Integer -> Integer -> ValidatorCtx -> Bool | |
validateSolution y x _ = x*x == y | |
-- | The address of the bounty (the hash of its validator script) | |
bountyAddress :: Address | |
bountyAddress = Ledger.scriptAddress (Scripts.validatorScript bountyInstance) | |
-- | The script instance is the compiled validator (ready to go onto the chain) | |
bountyInstance :: Scripts.ScriptInstance MathBounty | |
bountyInstance = Scripts.validator @MathBounty | |
$$(PlutusTx.compile [|| validateSolution ||]) | |
$$(PlutusTx.compile [|| wrap ||]) where | |
wrap = Scripts.wrapValidator @Integer @Integer | |
------------ | |
-- | 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" (Integer, Value) | |
.\/ Endpoint "redeem" Integer | |
contract :: AsContractError e => Contract Schema e () | |
contract = publish `select` redeem | |
-- | The "publish" contract endpoint. | |
publish :: AsContractError e => Contract Schema e () | |
publish = do | |
(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 | |
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 []) |
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
[0,[{"simulationWallets":[{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}},{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}}],"simulationName":"Simulation 1","simulationId":1,"simulationActions":[{"caller":{"getWallet":1},"argumentValues":{"endpointDescription":{"getEndpointDescription":"publish"},"argument":{"contents":[{"s":1,"e":3,"c":[1234],"tag":"FormIntegerF"},{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},0]]]],"tag":"FormValueF"}],"tag":"FormTupleF"}},"tag":"CallEndpoint"}]}]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment