Skip to content

Instantly share code, notes, and snippets.

@Sinha-Ujjawal
Last active August 24, 2021 11:24
Show Gist options
  • Save Sinha-Ujjawal/92e5272debb3c8fb044636734946da93 to your computer and use it in GitHub Desktop.
Save Sinha-Ujjawal/92e5272debb3c8fb044636734946da93 to your computer and use it in GitHub Desktop.
Plutus Playground Smart Contract
-- 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
-- | 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 x) (MyRedeemer y) _ = x == y -- Please provide an implementation.
-- | 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 [])
[0,[{"simulationWallets":[{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},100]]]]}},{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},100]]]]}}],"simulationName":"Simulation 1","simulationId":1,"simulationActions":[{"caller":{"getWallet":1},"argumentValues":{"endpointDescription":{"getEndpointDescription":"publish"},"argument":{"contents":[{"s":1,"e":4,"c":[12345],"tag":"FormIntegerF"},{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},0]]]],"tag":"FormValueF"}],"tag":"FormTupleF"}},"tag":"CallEndpoint"},{"blocks":1,"tag":"AddBlocks"},{"caller":{"getWallet":2},"argumentValues":{"endpointDescription":{"getEndpointDescription":"redeem"},"argument":{"s":1,"e":4,"c":[12345],"tag":"FormIntegerF"}},"tag":"CallEndpoint"},{"blocks":1,"tag":"AddBlocks"}]}]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment