Last active
March 24, 2021 18:02
-
-
Save ngoluuduythai/bd16e0729fa23a854b5a8461de6fa966 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
-- 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. | |
-- create new type MyDatum wrap Integer deriving (like extend) | |
-- public class myDatum extends Data { | |
-- public myDatum(Integer i){ | |
-- this.i = i; | |
-- } | |
-- } | |
-- | A generic "data" type. | |
-- | |
-- The main constructor 'Constr' represents a datatype value in sum-of-products | |
-- form: @Constr i args@ represents a use of the @i@th constructor along with its arguments. | |
-- | |
-- The other constructors are various primitives. | |
-- data Data = | |
-- Constr Integer [Data] | |
-- | Map [(Data, Data)] | |
-- | List [Data] | |
-- | I Integer | |
-- | B BS.ByteString | |
-- deriving stock (Show, Eq, Ord, Generic) | |
-- deriving anyclass (Serialise, NFData) | |
-- PlutusTx the libs and compiler for compiling Haskell into PlutusCore | |
-- to form the on-chain part of a contract application | |
-- PlutusCore is The programing language in which scripts on the cardano blockchain written | |
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). | |
-- Validator script used to lock/unclock a transaction | |
-- MyDatum data the person locking ADA sends (number ada lock and key redeem) | |
-- MyRedeemer data the person trying to unclock the ADA sends. | |
-- Validation Context information of the transactions | |
-- take MyDatum, MyRedeemer, ValidatorCtx return Bool (transaction success or not) | |
validateSpend :: MyDatum -> MyRedeemer -> ValidatorCtx -> Bool | |
validateSpend _myDataValue _myRedeemerValue _ = error () -- 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 []) |
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":""},100]]]]}},{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},100]]]]}}],"simulationName":"Publish/Redeem","simulationId":1,"simulationActions":[{"caller":{"getWallet":1},"argumentValues":{"endpointDescription":{"getEndpointDescription":"publish"},"argument":{"contents":[{"s":1,"e":4,"c":[12345],"tag":"FormIntegerF"},{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},20]]]],"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"}]},{"simulationWallets":[{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},100]]]]}},{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},100]]]]}}],"simulationName":"Pay To Wallet","simulationId":2,"simulationActions":[{"sender":{"getWallet":1},"recipient":{"getWallet":2},"amount":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},24]]]]},"tag":"PayToWallet"},{"blocks":1,"tag":"AddBlocks"}]}]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment