44[[writing-smart-contracts]]
55== Writing smart contracts
66
7+ [[chapter8-prefaces]]
78=== Prefaces
89
10+ [[chapter8-plutus-preface]]
911==== Plutus preface
1012
1113_Prof Philip Wadler(((Wadler, Philip))), FRS_ +
@@ -93,6 +95,7 @@ experience – it's not often that you can suggest a practical
9395application of a fifty-year-old theory and have people take you
9496seriously!
9597
98+ [[chapter8-marlowe-preface]]
9699==== Marlowe preface
97100
98101_Prof Simon Thompson(((Thompson, Simon)))_ +
@@ -183,7 +186,7 @@ refine the core language evolution to better support its aim of
183186providing a high-level, chain-agnostic language for blockchain financial
184187smart contracts. Please join us on this journey!
185188
186-
189+ [[chapter8-smart-contract-programming-languages]]
187190=== Smart contract programming languages
188191
189192Cardano(((Cardano))) smart contract languages can be grouped into three categories:
@@ -192,6 +195,7 @@ Cardano(((Cardano))) smart contract languages can be grouped into three categori
192195* Compiled languages(((language, compiled))) that can be compiled to a native language
193196* Interpreted languages(((language, interpreted))) that are interpreted by a compiled language.
194197
198+ [[chapter8-native-languages]]
195199==== Native languages
196200
197201The Cardano node can only process native languages. Currently, there are
@@ -225,6 +229,7 @@ Solidity(((Solidity))) languages is presented in section
225229The security advantages of Cardano's EUTXO model over Ethereum's account-based
226230model are discussed in section <<Cardano security>>.
227231
232+ [[chapter8-compiled-languages]]
228233==== Compiled languages
229234
230235Smart contract developers do not write code directly in Plutus. Instead,
@@ -343,6 +348,7 @@ with links to the official page and a white paper.
343348showcase] page: tags projects by groups
344349and adds a short description to each.
345350
351+ [[chapter8-interpreted-languages]]
346352==== Interpreted languages
347353
348354The third category of smart contract languages in Cardano consists of
@@ -376,6 +382,7 @@ following domains:
376382Section <<Marlowe security and best practices>> explores the security
377383and best practices of Marlowe in more detail.
378384
385+ [[chapter8-on-chain-and-off-chain-code]]
379386==== On-chain and off-chain code
380387
381388Sometimes, smart contract code is referred to as on-chain code(((on-chain, code))) because
@@ -431,8 +438,10 @@ showcases how to write off-chain code for smart contracts in subsections
431438We provide a link to a repository that contains the presented MeshJS code
432439examples and also contains Lucid Evolution code examples.
433440
441+ [[chapter8-smart-contract-case-studies]]
434442=== Smart contract case studies
435443
444+ [[chapter8-world-mobile-token-smart-contracts]]
436445==== World Mobile Token smart contracts
437446
438447The https://worldmobile.io/en[World Mobile] company offers an
@@ -528,6 +537,7 @@ encryption(((encryption, private/public))) provided by Cardano
528537and restricts spending to only unspent transaction outputs(((transaction, output))),
529538making the reward system more deterministic.
530539
540+ [[chapter8-cardano-addresses]]
531541=== Cardano addresses
532542
533543A blockchain address serves as a communication link between the
@@ -576,6 +586,7 @@ _bootstrap addresses_ and _script addresses_. The
576586https://docs.cardano.org/learn/cardano-addresses/[Cardano addresses]
577587documentation page provides more information about address categories.
578588
589+ [[chapter8-binary-format]]
579590==== Binary format
580591
581592Under the hood, a Cardano address is a sequence of bytes that conforms
@@ -649,8 +660,10 @@ https://cips.cardano.org/cips/cip19/#binaryformat[Cardano Improvement Proposal 1
649660(CIP-19), which defines the technical details of Cardano
650661addresses.
651662
663+ [[chapter8-marlowe-smart-contracts]]
652664=== Marlowe smart contracts
653665
666+ [[chapter8-about-marlowe]]
654667==== About Marlowe
655668
656669Marlowe(((Marlowe))) is a domain-specific language (DSL) for building
@@ -741,6 +754,7 @@ ecosystem as a community-based project. The Marlowe repositories will be
741754transferred to the https://github.com/marlowe-lang[marlowe-lang]
742755GitHub, and continued development will take place there.
743756
757+ [[chapter8-developer-tools-and-services]]
744758==== Developer tools and services
745759
746760Marlowe provides a set of open-source tools that help create, test, and
@@ -934,6 +948,7 @@ documentation] provides extensive explanations, links to learning
934948resources, and access to tutorials and community resources from the top
935949bar.
936950
951+ [[chapter8-marlowe-runtime-architecture]]
937952==== Marlowe Runtime architecture
938953
939954Below is the Marlowe Runtime(((Marlowe, runtime))) architecture:
@@ -973,6 +988,7 @@ Please note that the Marlowe Runtime architecture may evolve. Refer to
973988the https://docs.marlowe-lang.org/docs/platform-and-architecture/architecture[Marlowe
974989documentation] for the latest version.
975990
991+ [[chapter8-contract-examples]]
976992==== Contract examples
977993
978994Marlowe is designed to create the following building blocks of financial
@@ -1345,6 +1361,7 @@ Swap Prototype] – a decentralized platform for users to list tokens for
13451361swap, specifying the desired return. Interested parties can accept
13461362offers, resulting in a token swap.
13471363
1364+ [[chapter8-integrating-with-plinth]]
13481365==== Integrating with Plinth
13491366
13501367Marlowe contracts can be integrated with Plinth smart contracts or other
@@ -1379,6 +1396,7 @@ Plinth script does not interfere with Marlowe validation
13791396* The Marlowe validators do not need to be modified to run alongside a
13801397Plinth script that holds the role token.
13811398
1399+ [[chapter8-future-of-marlowe]]
13821400==== Future of Marlowe
13831401
13841402The Marlowe Foundation(((Marlowe, Foundation))) will continue supporting
@@ -1421,6 +1439,7 @@ businesses, developers, infrastructure providers, and stake pool operators(((SPO
14211439* Marlowe oracle protocol
14221440* Outreach
14231441
1442+ [[chapter8-marlowe-dapp-starter-kit]]
14241443===== Marlowe DApp starter kit
14251444
14261445The Marlowe DApp starter kit(((DSK))) (DSK) will consolidate the documentation,
@@ -1432,6 +1451,7 @@ pre-built binaries to ensure ease of implementation. The initial DSK
14321451will be delivered early in the project, with incremental enhancements
14331452added as new technologies are developed.
14341453
1454+ [[chapter8-validator-enhancements]]
14351455===== Validator enhancements
14361456
14371457Targeted changes will be implemented to the current validator to
@@ -1446,6 +1466,7 @@ their real-world application potential, will be demonstrated through
14461466DApp prototypes, fully documented and accessible via the marlowe-ts-sdk
14471467for developers to build upon.
14481468
1469+ [[chapter8-configurable-runtime-fee-mechanism]]
14491470===== Configurable runtime fee mechanism
14501471
14511472To encourage infrastructure providers and SPOs to adopt Marlowe, a
@@ -1456,6 +1477,7 @@ validation layer in the TypeScript client library. This layer will
14561477ensure secure interactions, even with untrusted backends, facilitating a
14571478wider distribution and adoption of Marlowe technology.
14581479
1480+ [[chapter8-marlowe-oracle-protocol]]
14591481===== Marlowe oracle protocol
14601482
14611483The Marlowe oracle protocol will be formalized and expanded with a
@@ -1470,6 +1492,7 @@ Aiken or Plinth, allowing scripts to efficiently manage oracle data.
14701492This enhancement will not only ensure compatibility with current
14711493technologies but also open up numerous new applications on Cardano.
14721494
1495+ [[chapter8-outreach]]
14731496===== Outreach
14741497
14751498The Marlowe 2025 project aims to enhance community engagement through a
@@ -1489,6 +1512,7 @@ conceptual track aimed at fundamental language modifications. This track
14891512will be organized through the creation of Marlowe improvement proposals
14901513(MIPs), in collaboration with the wider community.
14911514
1515+ [[chapter8-impact-of-marlowe]]
14921516==== Impact of Marlowe
14931517
14941518Marlowe has the potential to evolve into a smart contract technology
@@ -1515,6 +1539,7 @@ the initial onboarding process to a single npm(((npm))) install command, a surge
15151539in Marlowe's usage is anticipated. In doing this, it will benefit a
15161540range of different stakeholders in different ways.
15171541
1542+ [[chapter8-stakeholder-beneficiaries]]
15181543===== Stakeholder beneficiaries
15191544
15201545Marlowe is essential for the Cardano infrastructure. The addition of
@@ -1543,8 +1568,10 @@ the Marlowe special interest group and existing forums, will help forge
15431568a vibrant and sustainable ecosystem, positioning Marlowe as a
15441569cornerstone of the Cardano economy.
15451570
1571+ [[chapter8-plutus-smart-contracts]]
15461572=== Plutus smart contracts
15471573
1574+ [[chapter8-overview-and-learning-resources]]
15481575==== Overview and learning resources
15491576
15501577The _Plutus smart contracts(((smart contract)))_ section presents the rules
@@ -1644,7 +1671,7 @@ under an open-source license.
16441671course, that teaches the basics of blockchain and Cardano.
16451672
16461673Cardano's hard fork combinator(((HFC))) technology, briefly introduced in section
1647- <<cardano-node-layers, Cardano node layers>>, subsection *Consensus and storage layer* ,
1674+ <<cardano-node-layers, Cardano node layers>>, subsection _Consensus and storage layer_ ,
16481675enables the Cardano node(((Cardano node))) to continue processing scripts written in earlier
16491676versions of Plutus even after a hard fork introduces a new Plutus version.
16501677If you encounter smart contract code examples written in PlutusV1(((PlutusV1)))
@@ -1654,6 +1681,7 @@ previous Plutus Pioneer program iterations:
16541681* https://github.com/input-output-hk/plutus-pioneer-program/tree/third-iteration[third-iteration] - PlutusV1 examples
16551682* https://github.com/input-output-hk/plutus-pioneer-program/tree/fourth-iteration[fourth-iteration] - PlutusV2 examples.
16561683
1684+ [[chapter8-plutus-in-comparison-to-bitcoin-script-and-solidity]]
16571685==== Plutus in comparison to Bitcoin Script and Solidity
16581686
16591687This section compares the basic characteristics of smart contracts in
@@ -1782,6 +1810,7 @@ resources. The https://github.com/IntersectMBO/plutus[Plutus]
17821810GitHub repository also provides links to specifications, design documents,
17831811academic papers, and talks.
17841812
1813+ [[chapter8-plinth-in-comparison-to-aiken]]
17851814==== Plinth in comparison to Aiken
17861815
17871816Plinth(((Plinth))) enables developers to write and compile their on-chain
@@ -1890,6 +1919,7 @@ For those who prefer to learn from a book, the
18901919https://book.io/book/i-can-aiken/[I can Aiken] book, written by John
18911920Greene provides comprehensive information and learning examples.
18921921
1922+ [[chapter8-setting-up-a-plinth-development-environment]]
18931923==== Setting up a Plinth development environment
18941924
18951925For your development environment, you can use one of the following
@@ -1953,6 +1983,7 @@ Additionally, the Plinth libraries are not hosted on Hackage, which means the on
19531983to query Plinth types from a REPL is to build it with a Cabal file that imports
19541984those libraries.
19551985
1986+ [[chapter8-simple-validation-scripts]]
19561987==== Simple validation scripts
19571988
19581989In this section, we will look at basic Plinth validation scripts. All
@@ -2821,6 +2852,7 @@ chapter, including blueprint and cabal configuration files, can be found at the
28212852https://github.com/input-output-hk/plutus-pioneer-program/tree/plinth-plutusV3[plinth-plutusV3]
28222853branch of the Plutus pioneer program.
28232854
2855+ [[chapter8-script-context-explained]]
28242856==== Script context explained
28252857
28262858In this section, we will examine the [purple]#ScriptContext# data type in more detail.
@@ -2848,6 +2880,7 @@ In the previous code examples, the validation logic did not use the transaction
28482880information. However, in most validator scripts, they are
28492881used. First, we look at the script information data type.
28502882
2883+ [[chapter8-script-information]]
28512884===== Script information
28522885
28532886The [purple]#ScriptInfo# data type is defined as:
@@ -2986,6 +3019,7 @@ following script purposes:
29863019In the <<Plutus smart contracts>> section, we will focus on the spending
29873020and minting script purposes.
29883021
3022+ [[chapter8-decoding-the-script-information-in-untyped-form]]
29893023===== Decoding the script information in untyped form
29903024
29913025Before we move on to explaining the transaction information data type we look
@@ -3059,6 +3093,7 @@ in smart contracts:
30593093* Representing the current state of the UTXO
30603094* Defining metadata and/or configurations.
30613095
3096+ [[chapter8-transaction-information]]
30623097===== Transaction information
30633098
30643099Let us look at the transaction information(((transaction, information)))
@@ -3405,6 +3440,7 @@ donation. Both are of type [purple]#Maybe V2.Lovelace#. Learn more about
34053440governance features, including the topics of voting and proposal submission
34063441in chapter <<Cardano governance>>.
34073442
3443+ [[chapter8-time-dependent-and-parameterized-validators]]
34083444==== Time-dependent and parameterized validators
34093445
34103446This section demonstrates a smart contract representing a vesting
@@ -3656,6 +3692,7 @@ function to the vesting parameter in order to wrap the validator before compilin
36563692This concludes the procedure of writing a parameterized contract. Next, we look at
36573693off-chain code that interacts with the validators presented in this section.
36583694
3695+ [[chapter8-off-chain-code-with-meshjs]]
36593696==== Off-chain code with MeshJS
36603697
36613698The blockchain is passive – it only acts when a user interacts with it.
@@ -4335,6 +4372,7 @@ More information about MeshJS is available at:
43354372In the next section, we show how to write a minting policy and off-chain code
43364373that interacts with the policy.
43374374
4375+ [[chapter8-minting-policies-and-native-tokens]]
43384376==== Minting policies and native tokens
43394377
43404378In Plutus, a minting policy(((minting policy))) or minting script defines the
@@ -4926,6 +4964,7 @@ explanations] page, which also covers native tokens
49264964* https://iohk.io/en/research/library/papers/native-custom-tokens-in-the-extended-utxo-model/[Native
49274965Custom Tokens in the Extended UTXO Model] scientific paper.
49284966
4967+ [[chapter8-plutusv3-features]]
49294968==== PlutusV3 features
49304969
49314970PlutusV3(((PlutusV3))) focuses on performance throughput, smart contract size,
@@ -4995,8 +5034,10 @@ allows for better code optimization. This is discussed further in section
49955034<<Simple validation scripts>>. The
49965035https://plutus.cardano.intersectmbo.org/docs/working-with-scripts/optimizing-scripts-with-asData[Plinth user guide] also provides information on that.
49975036
5037+ [[chapter8-smart-contract-security]]
49985038=== Smart contract security
49995039
5040+ [[chapter8-cardano-security]]
50005041==== Cardano security
50015042
50025043The security of smart contracts is an important topic, since these programs
@@ -5279,7 +5320,7 @@ smooth transitions during hard forks. If a hard fork upgrades the Plutus
52795320language to a new major version, smart contracts using an older version
52805321of Plutus can still be processed. You can read more about the HFC in
52815322<<cardano-node-layers, Cardano node layers>> section, subsection
5282- **Consensus and storage layer** .
5323+ _Consensus and storage layer_ .
52835324
52845325Cardano smart contracts see a predictable number of inputs and can only
52855326produce a set number of outputs that are always the same given the same
@@ -5307,6 +5348,7 @@ security features of the Plutus and Marlowe smart contract languages are
53075348further discussed in the remaining sections of the <<Smart contract security>>
53085349section.
53095350
5351+ [[chapter8-plutus security]]
53105352==== Plutus security
53115353
53125354Cardano offers Plinth(((Plinth))), previously called PlutusTx, which is a
@@ -5631,7 +5673,7 @@ made some of their audit reports freely available to the public:
56315673Other auditing companies that might not have their auditing reports grouped
56325674into a single repository, are also worth exploring.
56335675
5634- [[marlowe-security]]
5676+ [[chapter8- marlowe-security]]
56355677==== Marlowe security and best practices
56365678
56375679*Marlowe security*
0 commit comments