Created
July 11, 2022 05:23
-
-
Save mayorityz/25ea3e4ba6c379ac4eff4fc34b19d27b to your computer and use it in GitHub Desktop.
Plutus Playground Smart Contract
This file contains 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
import Control.Monad (void) | |
import Ledger (Address, ScriptContext) | |
import Ledger.Constraints qualified as Constraints | |
import Ledger.Typed.Scripts qualified as Scripts | |
import Ledger.Value (Value) | |
import Playground.Contract | |
import Plutus.Contract | |
import PlutusTx qualified | |
import PlutusTx.Prelude hiding (Applicative (..)) | |
-- | 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.ToData, PlutusTx.FromData, PlutusTx.UnsafeFromData) | |
PlutusTx.makeLift ''MyDatum | |
newtype MyRedeemer = MyRedeemer Integer deriving newtype (PlutusTx.ToData, PlutusTx.FromData, PlutusTx.UnsafeFromData) | |
PlutusTx.makeLift ''MyRedeemer | |
-- | This method is the spending validator (which gets lifted to | |
-- its on-chain representation). | |
validateSpend :: MyDatum -> MyRedeemer -> ScriptContext -> Bool | |
validateSpend _myDataValue _myRedeemerValue _ = error () -- Please provide an implementation. | |
-- | The address of the contract (the hash of its validator script). | |
contractAddress :: Address | |
contractAddress = Scripts.validatorAddress starterInstance | |
data Starter | |
instance Scripts.ValidatorTypes 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.TypedValidator Starter | |
starterInstance = Scripts.mkTypedValidator @Starter | |
$$(PlutusTx.compile [|| validateSpend ||]) | |
$$(PlutusTx.compile [|| wrap ||]) where | |
wrap = Scripts.wrapValidator @MyDatum @MyRedeemer | |
-- | The schema of the contract, with two endpoints. | |
type Schema = | |
Endpoint "publish" (Integer, Value) | |
.\/ Endpoint "redeem" Integer | |
contract :: AsContractError e => Contract () Schema e () | |
contract = selectList [publish, redeem] | |
-- | The "publish" contract endpoint. | |
publish :: AsContractError e => Promise () Schema e () | |
publish = endpoint @"publish" $ \(i, lockedFunds) -> do | |
let tx = Constraints.mustPayToTheScript (MyDatum i) lockedFunds | |
void $ submitTxConstraints starterInstance tx | |
-- | The "redeem" contract endpoint. | |
redeem :: AsContractError e => Promise () Schema e () | |
redeem = endpoint @"redeem" $ \myRedeemerValue -> do | |
unspentOutputs <- utxosAt 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 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":""},100000000]]]]}},{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},100000000]]]]}}],"simulationName":"Publish/Redeem","simulationId":1,"simulationActions":[{"tag":"CallEndpoint","caller":{"getWallet":1},"argumentValues":{"endpointDescription":{"getEndpointDescription":"publish"},"argument":{"contents":[{"contents":12345,"tag":"FormIntegerF"},{"contents":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},20000000]]]]},"tag":"FormValueF"}],"tag":"FormTupleF"}}},{"tag":"AddBlocks","blocks":1},{"tag":"CallEndpoint","caller":{"getWallet":2},"argumentValues":{"endpointDescription":{"getEndpointDescription":"redeem"},"argument":{"contents":12345,"tag":"FormIntegerF"}}},{"tag":"AddBlocks","blocks":1}]},{"simulationWallets":[{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},100000000]]]]}},{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},100000000]]]]}}],"simulationName":"Pay To Wallet","simulationId":2,"simulationActions":[{"tag":"PayToWallet","sender":{"getWallet":1},"recipient":{"getWallet":2},"amount":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},20000000]]]]}},{"tag":"AddBlocks","blocks":1}]}]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment