| title | author |
|---|---|
M08 - Bring it on |
Walker Leite |
In this module we'll build the most common off-chain code for a smart contract on Cardano.
To run this presentation type (you will need nix):
../../slide README.md- LovelaceAcademy Discord
- StackExchange (:bulb: use the tag lovelace-academy)
- Plutonomicon Discord
- Plutus
- You should be familiar with EUTxO Model (module 1);
- You should be able to serialize plutus code (module 6);
- Nix
- You should be familiar with Nix Language (module 3);
- You should be familiar with Nix Flakes (module 3);
- PureScript
- You should be familiar with Foreign Function Interface (module 7);
- You should be familiar with MonadEffect, MonadAff (module 7);
- You should be familiar with Halogen (module 7);
Optional, but recommended:
Frontend:
(Browser) Wallet - CTL - CSL
|
Backend: |
|
Ogmius (WebSocket) -|- Ogmius Datum Cache
|
Kopu - CTL is a Purescript library/interface for building smart contract transactions on Cardano.
- CSL is a WASM library to serialize and unserialize plutus compatible data. We don't use it directly.
- Ogmius is a service to query the blockchain using websocket protocol. We don't use it directly.
- Ogmius Datum Cache is a service to store datum values for V1 (non-vasil) contracts. We don't use it directly.
- Kopu: is a chain-index to query the blockchain using HTTP protocol. We don't use it directly.
The Contract is a newtype wrapper around QueryM, which is a ReaderT on QueryEnv over Aff:
newtype Contract (r :: Row Type) (a :: Type)
-- constructor
Contract (QueryMExtended r Aff a)
newtype QueryMExtended (r :: Row Type) (m :: Type -> Type) (a :: Type)
-- constructor
QueryMExtended (ReaderT (QueryEnv r) m a)The r :: Row Type defines the type of extraConfig field of the underlying given value for ReaderT:
type QueryEnv (r :: Row Type) = { config :: QueryConfig, extraConfig :: Record r, runtime :: QueryRuntime }Whenever we define a Contract type signature, we need to pass the extraConfig type or a placeholder and the returning value of the monad:
type ExtraConfig = { foo :: String }
submitTx :: Contract ExtraConfig TransactionHash
submitTx = do
cfg <- ask
logInfo' $ "Foo: " <> show cfg.foo
txId <- submit ?bsTx
logInfo' $ "Tx ID: " <> show txId
pure txIdTo run, we need to pass a valid config (with extraConfig set):
main :: Effect Unit
main = launchAff_ do
txId <- runContract {..., extraConfig = { foo = "bar" } } submitTx
log $ show txIdlaunchAff_ :: forall a. Aff a -> Effect Unit
runContract :: forall (r :: Row Type) (a :: Type). ConfigParams r -> Contract r a -> Aff alock :: forall r. Int -> Contract r TransactionHash
lock amount = do
validator <- ?toValidator :: Contract r Validator
let lookups = SL.validator validator
buildBalanceSignAndSubmitTx lookups ?constraintsnewtype Validator = Validator PlutusScripttoValidator :: Either Error Validator
toValidator = wrap <$> ?parseScript💡 Notice
ErrormatchingMonadError Error (Contract r).
parseScript :: Either Error PlutusScript
parseScript = plutusV2Script
<$> (lmap (error <<< printTextEnvelopeDecodeError) $ textEnvelopeBytes ?script PlutusScriptV2)data TextEnvelopeType = PlutusScriptV1 | PlutusScriptV2
textEnvelopeBytes :: String -> TextEnvelopeType -> Either TextEnvelopeDecodeError ByteArray
printTextEnvelopeDecodeError :: TextEnvelopeDecodeError -> String
lmap :: forall f a b c. Bifunctor f => (a -> b) -> f a c -> f b c
plutusV2Script :: ByteArray -> PlutusScript// Script.js
exports.script = require("Scripts/always-succeeds-v2.plutus");
⚠️ Your loader must know how torequirea plutus file
-- Script.purs
foreign import script :: StringUser Story: As a donator I want to give any amount of ADAs to the first visitor
- Donator integrates his wallet
- Donator locks
nADA intoScript Address - Visitor see avaliable
nADA - Visitor integrates his wallet
- Visitor grabs all ADA
cd modules/M08-bring-it-on
nix flake init -t github:LovelaceAcademy/nix-templates#pix-ctl-full
git init
git add --all
nix develop
npm install
git add --all
git commit -m "Initial commit"- Create
donator.{skey,vkey,addr}with givencardano-cli - Create
visitor.{skey,vkey,addr}with givencardano-cli - Send test ADA to donator and visitor address Faucet
