Skip to content

Instantly share code, notes, and snippets.

@bwbush
Created January 10, 2021 18:01
Show Gist options
  • Save bwbush/347c41c7fd5396feff2c7e2ecf568baf to your computer and use it in GitHub Desktop.
Save bwbush/347c41c7fd5396feff2c7e2ecf568baf 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 DataScript type
-- * The Redeemer type
--
-- And add function implementations (and rename them to
-- something suitable) for the endpoints:
-- * publish
-- * redeem
import qualified Language.PlutusTx as PlutusTx
import Language.PlutusTx.Prelude
import Ledger (Address, DataScript (DataScript), PendingTx,
RedeemerScript (RedeemerScript), ValidatorScript (ValidatorScript),
compileScript, scriptAddress, lifted)
import Ledger.Value (Value)
import Playground.Contract
import Wallet (MonadWallet, WalletAPI, WalletDiagnostics, collectFromScript,
defaultSlotRange, payToScript_, startWatching)
-- | These are the data script and redeemer types. We are using an integer
-- value for both, but you should define your own types.
data DataValue = DataValue Integer
PlutusTx.makeLift ''DataValue
data RedeemerValue = RedeemerValue Integer
PlutusTx.makeLift ''RedeemerValue
-- | This method is the spending validator (which gets lifted to
-- its on-chain representation).
validateSpend :: DataValue -> RedeemerValue -> PendingTx -> Bool
validateSpend _dataValue _redeemerValue _ = error () -- Please provide an implementation.
-- | This function lifts the validator previously defined to
-- the on-chain representation.
contractValidator :: ValidatorScript
contractValidator =
ValidatorScript ($$(Ledger.compileScript [|| validateSpend ||]))
-- | Helper function used to build the DataScript.
mkDataScript :: Integer -> DataScript
mkDataScript =
DataScript . lifted . DataValue
-- | Helper function used to build the RedeemerScript.
mkRedeemerScript :: Integer -> RedeemerScript
mkRedeemerScript =
RedeemerScript . lifted . RedeemerValue
-- | The address of the contract (the hash of its validator script).
contractAddress :: Address
contractAddress = Ledger.scriptAddress contractValidator
-- | The "publish" contract endpoint.
publish :: MonadWallet m => Integer -> Value -> m ()
publish dataValue lockedFunds =
payToScript_ defaultSlotRange contractAddress lockedFunds (mkDataScript dataValue)
-- | The "redeem" contract endpoint.
redeem :: (WalletAPI m, WalletDiagnostics m) => Integer -> m ()
redeem redeemerValue = do
let redeemer = mkRedeemerScript redeemerValue
collectFromScript defaultSlotRange contractValidator redeemer
-- | The "start" contract endpoint, telling the wallet to start watching
-- the address of the script.
start :: MonadWallet m => m ()
start =
startWatching contractAddress
$(mkFunctions ['publish, 'redeem, 'start])
[0,[{"wallets":[{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}},{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}}],"signatures":[{"functionName":"publish","argumentSchema":[{"tag":"FormSchemaInt"},{"tag":"FormSchemaValue"}]},{"functionName":"redeem","argumentSchema":[{"tag":"FormSchemaInt"}]},{"functionName":"start","argumentSchema":[]},{"functionName":"payToWallet_","argumentSchema":[{"tag":"FormSchemaValue"},{"contents":[["getWallet",{"tag":"FormSchemaInt"}]],"tag":"FormSchemaObject"}]}],"currencies":[{"knownTokens":[{"unTokenName":""}],"hash":"","friendlyName":"Ada"}],"actions":[{"simulatorWallet":{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}},"functionSchema":{"functionName":"start","argumentSchema":[]},"tag":"Action"},{"simulatorWallet":{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}},"functionSchema":{"functionName":"publish","argumentSchema":[{"contents":1,"tag":"FormInt"},{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},8]]]],"tag":"FormValue"}]},"tag":"Action"},{"simulatorWallet":{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}},"functionSchema":{"functionName":"redeem","argumentSchema":[{"contents":1,"tag":"FormInt"}]},"tag":"Action"}]}]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment