Skip to content

Instantly share code, notes, and snippets.

@kujua
Last active January 17, 2022 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kujua/34b4564f1dc81ce18430b3c362942794 to your computer and use it in GitHub Desktop.
Save kujua/34b4564f1dc81ce18430b3c362942794 to your computer and use it in GitHub Desktop.
Plutus Playground Smart Contract
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 (..), const, (+), (.), (<>), ($))
import Control.Lens (makeClassyPrisms, prism', review)
import Control.Monad (void)
import Control.Monad.Error.Lens (catching, throwing, throwing_)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Default (Default (def))
import Ledger.TimeSlot (SlotConfig)
import Ledger.TimeSlot qualified as TimeSlot
-- import Plutus.Contract (AsContractError (_ContractError), ContractError, awaitTime, logInfo, mapError, selectList)
import Prelude (Maybe (..), const, show, ($), (+), (.), (<>))
data MyError =
Error1 Text
| Error2
| AContractError ContractError
deriving Show
makeClassyPrisms ''MyError
instance AsContractError MyError where
_ContractError = _AContractError
instance AsMyError Text where
_MyError = prism' (T.pack . show) (const Nothing)
throw :: AsMyError e => Text -> Contract () s e ()
throw e = do
logInfo @Text $ "throwError: " <> e
throwing _Error1 e
throwAndCatch :: AsMyError e => Text -> Contract () s e ()
throwAndCatch e =
let handleError1 :: Text -> Contract () s e ()
handleError1 t = logInfo $ "handleError: " <> t
in catching _Error1 (throw e) handleError1
catchContractError :: (AsMyError e) => SlotConfig -> Contract () s e ()
catchContractError slotCfg =
catching _AContractError
(void $ mapError (review _AContractError) $
awaitTime $ TimeSlot.scSlotZeroTime slotCfg + 10000)
(\_ -> throwing_ _Error2)
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
validateSpend :: MyDatum -> MyRedeemer -> ScriptContext -> Bool
validateSpend _myDataValue _myRedeemerValue _ = error ()
contractAddress :: Address
contractAddress = Scripts.validatorAddress starterInstance
data Starter
instance Scripts.ValidatorTypes Starter where
type instance RedeemerType Starter = MyRedeemer
type instance DatumType Starter = MyDatum
starterInstance :: Scripts.TypedValidator Starter
starterInstance = Scripts.mkTypedValidator @Starter
$$(PlutusTx.compile [|| validateSpend ||])
$$(PlutusTx.compile [|| wrap ||]) where
wrap = Scripts.wrapValidator @MyDatum @MyRedeemer
type Schema =
Endpoint "publish" (Integer, Value)
.\/ Endpoint "redeem" Integer
.\/ Endpoint "throwError" Text
.\/ Endpoint "catchError" Text
.\/ Endpoint "catchContractError" ()
contract :: AsContractError e => Contract () Schema e ()
contract = selectList [publish, redeem]
publish :: AsContractError e => Promise () Schema e ()
publish = endpoint @"publish" $ \(i, lockedFunds) -> do
let tx = Constraints.mustPayToTheScript (MyDatum i) lockedFunds
void $ submitTxConstraints starterInstance tx
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 [])
[0,[{"simulationWallets":[{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},100000000]]]]}},{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},100000000]]]]}}],"simulationName":"Simulation 1","simulationId":1,"simulationActions":[{"tag":"CallEndpoint","caller":{"getWallet":1},"argumentValues":{"endpointDescription":{"getEndpointDescription":"throwError"},"argument":{"contents":"ERROR 1","tag":"FormStringF"}}},{"tag":"CallEndpoint","caller":{"getWallet":2},"argumentValues":{"endpointDescription":{"getEndpointDescription":"throwError"},"argument":{"contents":"ERROR 2","tag":"FormStringF"}}}]}]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment