<h2>Starting the commercial paper class<aclass="headerlink"href="#starting-the-commercial-paper-class"title="Permalink to this headline">¶</a></h2>
<p>A smart contract is a class that implements the <codeclass="docutils literal"><spanclass="pre">Contract</span></code> interface. For now, they have to be a part of the main
codebase, as dynamic loading of contract code is not yet implemented. Therefore, we start by creating a file named
either <codeclass="docutils literal"><spanclass="pre">CommercialPaper.kt</span></code> or <codeclass="docutils literal"><spanclass="pre">CommercialPaper.java</span></code> in the src/contracts directory with the following contents:</p>
<p>Every contract must have at least a <codeclass="docutils literal"><spanclass="pre">getLegalContractReference()</span></code> and a <codeclass="docutils literal"><spanclass="pre">verify()</span></code> method. In Kotlin we express
a getter without a setter as an immutable property (val). The <em>legal contract reference</em> is supposed to be a hash
of a document that describes the legal contract and may take precedence over the code, in case of a dispute.</p>
<p>The verify method returns nothing. This is intentional: the function either completes correctly, or throws an exception,
in which case the transaction is rejected.</p>
<p>We also need to define a constant hash that would, in a real system, be the hash of the program bytecode. For now
we just set it to a dummy value as dynamic loading and sandboxing of bytecode is not implemented. This constant
isn’t shown in the code snippet but is called <codeclass="docutils literal"><spanclass="pre">CP_PROGRAM_ID</span></code>.</p>
<p>So far, so simple. Now we need to define the commercial paper <em>state</em>, which represents the fact of ownership of a
piece of issued paper.</p>
</div>
<divclass="section"id="states">
<h2>States<aclass="headerlink"href="#states"title="Permalink to this headline">¶</a></h2>
<p>A state is a class that stores data that is checked by the contract.</p>
<spanclass="k">return</span><spanclass="n">SecureHash</span><spanclass="o">.</span><spanclass="na">Companion</span><spanclass="o">.</span><spanclass="na">sha256</span><spanclass="o">(</span><spanclass="s">"java commercial paper (this should be a bytecode hash)"</span><spanclass="o">);</span>
<p>We define a class that implements the <codeclass="docutils literal"><spanclass="pre">ContractState</span></code> and <codeclass="docutils literal"><spanclass="pre">SerializableWithKryo</span></code> interfaces. The
latter is an artifact of how the prototype implements serialization and can be ignored for now: it wouldn’t work
like this in any final product.</p>
<p>The <codeclass="docutils literal"><spanclass="pre">ContractState</span></code> interface requires us to provide a <codeclass="docutils literal"><spanclass="pre">getProgramRef</span></code> method that is supposed to return a hash of
the bytecode of the contract itself. For now this is a dummy value and isn’t used: later on, this mechanism will change.
Beyond that it’s a freeform object into which we can put anything which can be serialized.</p>
<p>We have four fields in our state:</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">issuance</span></code>: a reference to a specific piece of commercial paper at a party</li>
<li><codeclass="docutils literal"><spanclass="pre">owner</span></code>: the public key of the current owner. This is the same concept as seen in Bitcoin: the public key has no
attached identity and is expected to be one-time-use for privacy reasons. However, unlike in Bitcoin, we model
ownership at the level of individual contracts rather than as a platform-level concept as we envisage many
(possibly most) contracts on the platform will not represent “owner/issuer” relationships, but “party/party”
relationships such as a derivative contract.</li>
<li><codeclass="docutils literal"><spanclass="pre">faceValue</span></code>: an <codeclass="docutils literal"><spanclass="pre">Amount</span></code>, which wraps an integer number of pennies and a currency.</li>
<li><codeclass="docutils literal"><spanclass="pre">maturityDate</span></code>: an <aclass="reference external"href="https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html">Instant</a>, which is a type
from the Java 8 standard time library. It defines a point on the timeline.</li>
</ul>
<p>States are immutable, and thus the class is defined as immutable as well. The <codeclass="docutils literal"><spanclass="pre">data</span></code> modifier in the Kotlin version
causes the compiler to generate the equals/hashCode/toString methods automatically, along with a copy method that can
be used to create variants of the original object. Data classes are similar to case classes in Scala, if you are
familiar with that language. The <codeclass="docutils literal"><spanclass="pre">withoutOwner</span></code> method uses the auto-generated copy method to return a version of
the state with the owner public key blanked out: this will prove useful later.</p>
<p>The Java code compiles to the same bytecode as the Kotlin version, but as you can see, is much more verbose.</p>
</div>
<divclass="section"id="commands">
<h2>Commands<aclass="headerlink"href="#commands"title="Permalink to this headline">¶</a></h2>
<p>The logic for a contract may vary depending on what stage of a lifecycle it is automating. So it can be useful to
pass additional data into the contract code that isn’t represented by the states which exist permanently in the ledger.</p>
<p>For this purpose we have commands. Often, they don’t need to contain any data at all, they just need to exist. A command
is a piece of data associated with some <em>signatures</em>. By the time the contract runs the signatures have already been
checked, so from the contract code’s perspective, a command is simply a data structure with a list of attached
public keys. Each key had a signature proving that the corresponding private key was used to sign.</p>
<p>The <codeclass="docutils literal"><spanclass="pre">object</span></code> keyword in Kotlin just defines a singleton object. As the commands don’t need any additional data in our
case, they can be empty and we just use their type as the important information. Java has no syntax for declaring
singletons, so we just define a class that considers any other instance to be equal and that’s good enough.</p>
</div>
<divclass="section"id="the-verify-function">
<h2>The verify function<aclass="headerlink"href="#the-verify-function"title="Permalink to this headline">¶</a></h2>
<p>The heart of a smart contract is the code that verifies a set of state transitions (a <em>transaction</em>). The function is
simple: it’s given a class representing the transaction, and if the function returns then the transaction is considered
acceptable. If it throws an exception, the transaction is rejected.</p>
<p>Each transaction can have multiple input and output states of different types. The set of contracts to run is decided
by taking the code references inside each state. Each contract is run only once. As an example, a contract that includes
2 cash states and 1 commercial paper state as input, and has as output 1 cash state and 1 commercial paper state, will
run two contracts one time each: Cash and CommercialPaper.</p>
<p>We start by using the <codeclass="docutils literal"><spanclass="pre">groupStates</span></code> method, which takes a type and a function. State grouping is a way of ensuring
your contract can handle multiple unrelated states of the same type in the same transaction, which is needed for
splitting/merging of assets, atomic swaps and so on. The second line does what the code suggests: it searches for
a command object that inherits from the <codeclass="docutils literal"><spanclass="pre">CommercialPaper.Commands</span></code> supertype, and either returns it, or throws an
exception if there’s zero or more than one such command.</p>
</div>
<divclass="section"id="using-state-groups">
<h2>Using state groups<aclass="headerlink"href="#using-state-groups"title="Permalink to this headline">¶</a></h2>
<p>The simplest way to write a smart contract would be to say that each transaction can have a single input state and a
single output state of the kind govered by that contract. This would be easy for the developer, but would prevent many
important use cases.</p>
<p>The next easiest way to write a contract would be to iterate over each input state and expect it to have an output
state. Now you can build a single transaction that, for instance, moves two different cash states in different currencies
simultaneously. But it gets complicated when you want to issue or exit one state at the same time as moving another.</p>
<p>Things get harder still once you want to split and merge states. We say states are <em>fungible</em> if they are
treated identically to each other by the recipient, despite the fact that they aren’t quite identical. Dollar bills are
fungible because even though one may be worn/a bit dirty and another may be crisp and new, they are still both worth
exactly $1. Likewise, ten $1 bills are almost exactly equivalent to one $10 bill. On the other hand, $10 and £10 are not
fungible: if you tried to pay for something that cost £20 with $10+£10 notes your trade would not be accepted.</p>
<p>To make all this easier the contract API provides a notion of groups. A group is a set of input states and output states
that should be checked for validity together.</p>
<p>Consider the following simplified currency trade transaction:</p>
<ulclass="simple">
<li><strong>Input</strong>: $12,000 owned by Alice (A)</li>
<li><strong>Input</strong>: $3,000 owned by Alice (A)</li>
<li><strong>Input</strong>: £10,000 owned by Bob (B)</li>
<li><strong>Output</strong>: £10,000 owned by Alice (B)</li>
<li><strong>Output</strong>: $15,000 owned by Bob (A)</li>
</ul>
<p>In this transaction Alice and Bob are trading $15,000 for £10,000. Alice has her money in the form of two different
inputs e.g. because she received the dollars in two payments. The input and output amounts do balance correctly, but
the cash smart contract must consider the pounds and the dollars separately because they are not fungible: they cannot
be merged together. So we have two groups: A and B.</p>
<p>The <codeclass="docutils literal"><spanclass="pre">TransactionForVerification.groupStates</span></code> method handles this logic for us: firstly, it selects only states of the
given type (as the transaction may include other types of state, such as states representing bond ownership, or a
multi-sig state) and then it takes a function that maps a state to a grouping key. All states that share the same key are
grouped together. In the case of the cash example above, the grouping key would be the currency.</p>
<p>In other kinds of contract, we don’t want CP to be fungible: merging and splitting is (in our example) not allowed.
So we just use a copy of the state minus the owner field as the grouping key.</p>
<p>Here are some code examples:</p>
<divclass="codeset container">
<divclass="highlight-kotlin"><divclass="highlight"><pre><span></span>// Type of groups is List<InOutGroup<State, Pair<PartyReference, Currency>>>
val groups = tx.groupStates() { it: Cash.State -> Pair(it.deposit, it.amount.currency) }
<p>The <codeclass="docutils literal"><spanclass="pre">groupStates</span></code> call uses the provided function to calculate a “grouping key”. All states that have the same
grouping key are placed in the same group. A grouping key can be anything that implements equals/hashCode, but it’s
always an aggregate of the fields that shouldn’t change between input and output. In the above example we picked the
fields we wanted and packed them into a <codeclass="docutils literal"><spanclass="pre">Pair</span></code>. It returns a list of <codeclass="docutils literal"><spanclass="pre">InOutGroup</span></code>, which is just a holder for the
inputs, outputs and the key that was used to define the group. In the Kotlin version we unpack these using destructuring
to get convenient access to the inputs, the outputs, the deposit data and the currency. The Java version is more
verbose, but equivalent.</p>
<p>The rules can then be applied to the inputs and outputs as if it were a single transaction. A group may have zero
inputs or zero outputs: this can occur when issuing assets onto the ledger, or removing them.</p>
<p>In this example, we do it differently and use the state class itself as the aggregator. We just
blank out fields that are allowed to change, making the grouping key be “everything that isn’t that”:</p>
<p>For large states with many fields that must remain constant and only one or two that are really mutable, it’s often
easier to do things this way than to specifically name each field that must stay the same. The <codeclass="docutils literal"><spanclass="pre">withoutOwner</span></code> function
here simply returns a copy of the object but with the <codeclass="docutils literal"><spanclass="pre">owner</span></code> field set to <codeclass="docutils literal"><spanclass="pre">NullPublicKey</span></code>, which is just a public key
of all zeros. It’s invalid and useless, but that’s OK, because all we’re doing is preventing the field from mattering
<spanclass="s">"the transaction is signed by the owner of the CP"</span><spanclass="k">by</span><spanclass="p">(</span><spanclass="n">command</span><spanclass="p">.</span><spanclass="n">signers</span><spanclass="p">.</span><spanclass="n">contains</span><spanclass="p">(</span><spanclass="n">input</span><spanclass="p">.</span><spanclass="n">owner</span><spanclass="p">))</span>
<spanclass="s">"the state is propagated"</span><spanclass="k">by</span><spanclass="p">(</span><spanclass="n">group</span><spanclass="p">.</span><spanclass="n">outputs</span><spanclass="p">.</span><spanclass="n">size</span><spanclass="p">==</span><spanclass="m">1</span><spanclass="p">)</span>
<spanclass="k">if</span><spanclass="p">(</span><spanclass="n">time</span><spanclass="p">==</span><spanclass="k">null</span><spanclass="p">)</span><spanclass="k">throw</span><spanclass="n">IllegalArgumentException</span><spanclass="p">(</span><spanclass="s">"Redemption transactions must be timestamped"</span><spanclass="p">)</span>
<spanclass="s">"the paper must have matured"</span><spanclass="k">by</span><spanclass="p">(</span><spanclass="n">time</span><spanclass="p">></span><spanclass="n">input</span><spanclass="p">.</span><spanclass="n">maturityDate</span><spanclass="p">)</span>
<spanclass="s">"the received amount equals the face value"</span><spanclass="k">by</span><spanclass="p">(</span><spanclass="n">received</span><spanclass="p">==</span><spanclass="n">input</span><spanclass="p">.</span><spanclass="n">faceValue</span><spanclass="p">)</span>
<spanclass="s">"the paper must be destroyed"</span><spanclass="k">by</span><spanclass="n">group</span><spanclass="p">.</span><spanclass="n">outputs</span><spanclass="p">.</span><spanclass="n">isEmpty</span><spanclass="p">()</span>
<spanclass="s">"the transaction is signed by the owner of the CP"</span><spanclass="k">by</span><spanclass="p">(</span><spanclass="n">command</span><spanclass="p">.</span><spanclass="n">signers</span><spanclass="p">.</span><spanclass="n">contains</span><spanclass="p">(</span><spanclass="n">input</span><spanclass="p">.</span><spanclass="n">owner</span><spanclass="p">))</span>
<spanclass="k">if</span><spanclass="p">(</span><spanclass="n">time</span><spanclass="p">==</span><spanclass="k">null</span><spanclass="p">)</span><spanclass="k">throw</span><spanclass="n">IllegalArgumentException</span><spanclass="p">(</span><spanclass="s">"Issuance transactions must be timestamped"</span><spanclass="p">)</span>
<spanclass="s">"the face value is not zero"</span><spanclass="k">by</span><spanclass="p">(</span><spanclass="n">output</span><spanclass="p">.</span><spanclass="n">faceValue</span><spanclass="p">.</span><spanclass="n">pennies</span><spanclass="p">></span><spanclass="m">0</span><spanclass="p">)</span>
<spanclass="s">"the maturity date is not in the past"</span><spanclass="k">by</span><spanclass="p">(</span><spanclass="n">time</span><spanclass="p"><</span><spanclass="n">output</span><spanclass="p">.</span><spanclass="n">maturityDate</span><spanclass="p">)</span>
<spanclass="c1">// Don't allow an existing CP state to be replaced by this issuance.</span>
<spanclass="s">"there is no input state"</span><spanclass="k">by</span><spanclass="n">group</span><spanclass="p">.</span><spanclass="n">inputs</span><spanclass="p">.</span><spanclass="n">isEmpty</span><spanclass="p">()</span>
<spanclass="p">}</span>
<spanclass="p">}</span>
<spanclass="c1">// TODO: Think about how to evolve contracts over time with new commands.</span>
<divclass="highlight-java"><divclass="highlight"><pre><span></span><spanclass="n">Instant</span><spanclass="n">time</span><spanclass="o">=</span><spanclass="n">tx</span><spanclass="o">.</span><spanclass="na">getTime</span><spanclass="o">();</span><spanclass="c1">// Can be null/missing.</span>
<spanclass="k">throw</span><spanclass="k">new</span><spanclass="n">IllegalStateException</span><spanclass="o">(</span><spanclass="s">"Failed requirement: the transaction is signed by the owner of the CP"</span><spanclass="o">);</span>
<spanclass="k">throw</span><spanclass="k">new</span><spanclass="n">IllegalStateException</span><spanclass="o">(</span><spanclass="s">"Failed requirement: the output state is the same as the input state except for owner"</span><spanclass="o">);</span>
<spanclass="k">throw</span><spanclass="k">new</span><spanclass="n">IllegalArgumentException</span><spanclass="o">(</span><spanclass="s">"Redemption transactions must be timestamped"</span><spanclass="o">);</span>
<spanclass="k">throw</span><spanclass="k">new</span><spanclass="n">IllegalStateException</span><spanclass="o">(</span><spanclass="s">"Failed requirement: no cash being redeemed"</span><spanclass="o">);</span>
<spanclass="k">throw</span><spanclass="k">new</span><spanclass="n">IllegalStateException</span><spanclass="o">(</span><spanclass="s">"Failed requirement: the paper must have matured"</span><spanclass="o">);</span>
<spanclass="k">throw</span><spanclass="k">new</span><spanclass="n">IllegalStateException</span><spanclass="o">(</span><spanclass="s">"Failed requirement: the received amount equals the face value"</span><spanclass="o">);</span>
<spanclass="k">throw</span><spanclass="k">new</span><spanclass="n">IllegalStateException</span><spanclass="o">(</span><spanclass="s">"Failed requirement: the paper must be destroyed"</span><spanclass="o">);</span>
<spanclass="c1">// .. etc .. (see Kotlin for full definition)</span>
<spanclass="o">}</span>
<spanclass="o">}</span>
</pre></div>
</div>
</div>
<p>This loop is the core logic of the contract.</p>
<p>The first line simply gets the timestamp out of the transaction. Timestamping of transactions is optional, so a time
may be missing here. We check for it being null later.</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">In the Kotlin version, as long as we write a comparison with the transaction time first, the compiler will
verify we didn’t forget to check if it’s missing. Unfortunately due to the need for smooth Java interop, this
check won’t happen if we write e.g. <codeclass="docutils literal"><spanclass="pre">someDate</span><spanclass="pre">></span><spanclass="pre">time</span></code>, it has to be <codeclass="docutils literal"><spanclass="pre">time</span><spanclass="pre"><</span><spanclass="pre">someDate</span></code>. So it’s good practice to
always write the transaction timestamp first.</p>
</div>
<p>The first line (first three lines in Java) impose a requirement that there be a single piece of commercial paper in
this group. We do not allow multiple units of CP to be split or merged even if they are owned by the same owner. The
<codeclass="docutils literal"><spanclass="pre">single()</span></code> method is a static <em>extension method</em> defined by the Kotlin standard library: given a list, it throws an
exception if the list size is not 1, otherwise it returns the single item in that list. In Java, this appears as a
regular static method of the type familiar from many FooUtils type singleton classes. In Kotlin, it appears as a
method that can be called on any JDK list. The syntax is slightly different but behind the scenes, the code compiles
to the same bytecodes.</p>
<p>Next, we check that the transaction was signed by the public key that’s marked as the current owner of the commercial
paper. Because the platform has already verified all the digital signatures before the contract begins execution,
all we have to do is verify that the owner’s public key was one of the keys that signed the transaction. The Java code
is straightforward. The Kotlin version looks a little odd: we have a <em>requireThat</em> construct that looks like it’s
built into the language. In fact <em>requireThat</em> is an ordinary function provided by the platform’s contract API. Kotlin
supports the creation of <em>domain specific languages</em> through the intersection of several features of the language, and
we use it here to support the natural listing of requirements. To see what it compiles down to, look at the Java version.
Each <codeclass="docutils literal"><spanclass="pre">"string"</span><spanclass="pre">by</span><spanclass="pre">(expression)</span></code> statement inside a <codeclass="docutils literal"><spanclass="pre">requireThat</span></code> turns into an assertion that the given expression is
true, with an exception being thrown that contains the string if not. It’s just another way to write out a regular
assertion, but with the English-language requirement being put front and center.</p>
<p>Next, we take one of two paths, depending on what the type of the command object is.</p>
<p>If the command is a <codeclass="docutils literal"><spanclass="pre">Move</span></code> command, then we simply verify that the output state is actually present: a move is not
allowed to delete the CP from the ledger. The grouping logic already ensured that the details are identical and haven’t
been changed, save for the public key of the owner.</p>
<p>If the command is a <codeclass="docutils literal"><spanclass="pre">Redeem</span></code> command, then the requirements are more complex:</p>
<olclass="arabic simple">
<li>We want to see that the face value of the CP is being moved as a cash claim against some party, that is, the
issuer of the CP is really paying back the face value.</li>
<li>The transaction must be happening after the maturity date.</li>
<li>The commercial paper must <em>not</em> be propagated by this transaction: it must be deleted, by the group having no
output state. This prevents the same CP being considered redeemable multiple times.</li>
</ol>
<p>To calculate how much cash is moving, we use the <codeclass="docutils literal"><spanclass="pre">sumCashOrNull</span></code> utility method. Again, this is an extension method,
so in Kotlin code it appears as if it was a method on the <codeclass="docutils literal"><spanclass="pre">List<Cash.State></span></code> type even though JDK provides no such
method. In Java we see its true nature: it is actually a static method named <codeclass="docutils literal"><spanclass="pre">CashKt.sumCashOrNull</span></code>. This method simply
returns an <codeclass="docutils literal"><spanclass="pre">Amount</span></code> object containing the sum of all the cash states in the transaction output, or null if there were
no such states <em>or</em> if there were different currencies represented in the outputs! So we can see that this contract
imposes a limitation on the structure of a redemption transaction: you are not allowed to move currencies in the same
transaction that the CP does not involve. This limitation could be addressed with better APIs, if it were to be a
real limitation.</p>
<p>Finally, we support an <codeclass="docutils literal"><spanclass="pre">Issue</span></code> command, to create new instances of commercial paper on the ledger. It likewise
enforces various invariants upon the issuance.</p>
<p>This contract is extremely simple and does not implement all the business logic a real commercial paper lifecycle
management program would. For instance, there is no logic requiring a signature from the issuer for redemption:
it is assumed that any transfer of money that takes place at the same time as redemption is good enough. Perhaps
that is something that should be tightened. Likewise, there is no logic handling what happens if the issuer has gone
bankrupt, if there is a dispute, and so on.</p>
<p>As the prototype evolves, these requirements will be explored and this tutorial updated to reflect improvements in the
<spanclass="n">expectFailureOfTx</span><spanclass="p">(</span><spanclass="m">1</span><spanclass="p">,</span><spanclass="s">"signed by the claimed issuer"</span><spanclass="p">)</span>
<spanclass="p">}</span>
<spanclass="p">}</span>
<spanclass="p">}</span>
</pre></div>
</div>
</div>
<p>We start by defining a commercial paper state. It will be owned by a pre-defined unit test party, affectionately
called <codeclass="docutils literal"><spanclass="pre">MEGA_CORP</span></code> (this constant, along with many others, is defined in <codeclass="docutils literal"><spanclass="pre">TestUtils.kt</span></code>). Due to Kotin’s extensive
type inference, many types are not written out explicitly in this code and it has the feel of a scripting language.
But the types are there, and you can ask IntelliJ to reveal them by pressing Alt-Enter on a “val” or “var” and selecting
“Specify type explicitly”.</p>
<p>There are a few things that are unusual here:</p>
<ulclass="simple">
<li>We can specify quantities of money by writing 1000.DOLLARS or 1000.POUNDS</li>
<li>We can specify quantities of time by writing 7.days</li>
<li>We can add quantities of time to the TEST_TX_TIME constant, which merely defines an arbitrary java.time.Instant</li>
</ul>
<p>If you examine the code in the actual repository, you will also notice that it makes use of method names with spaces
in them by surrounding the name with backticks, rather than using underscores. We don’t show this here as it breaks the
<p>The <codeclass="docutils literal"><spanclass="pre">1000.DOLLARS</span></code> construct is quite simple: Kotlin allows you to define extension functions on primitive types like
Int or Double. So by writing 7.days, for instance, the compiler will emit a call to a static method that takes an int
and returns a <codeclass="docutils literal"><spanclass="pre">java.time.Duration</span></code>.</p>
<p>As this is JUnit, we must remember to annotate each test method with @Test. Let’s examine the contents of the first test.
We are trying to check that it’s not possible for just anyone to issue commercial paper in MegaCorp’s name. That would
be bad!</p>
<p>The <codeclass="docutils literal"><spanclass="pre">transactionGroup</span></code> function works the same way as the <codeclass="docutils literal"><spanclass="pre">requireThat</span></code> construct above.</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">This DSL is an example of what Kotlin calls a type safe builder, which you can read about in <aclass="reference external"href="https://kotlinlang.org/docs/reference/type-safe-builders.html">the
documentation for builders</a>. You can mix and match
ordinary code inside such DSLs so please read the linked page to make sure you fully understand what they are capable
of.</p>
</div>
<p>The code block that follows it is run in the scope of a freshly created <codeclass="docutils literal"><spanclass="pre">TransactionGroupForTest</span></code> object, which assists
you with building little transaction graphs and verifying them as a whole. Here, our “group” only actually has a
single transaction in it, with a single output, no inputs, and an Issue command signed by <codeclass="docutils literal"><spanclass="pre">DUMMY_PUBKEY_1</span></code> which is just
an arbitrary public key. As the paper claims to be issued by <codeclass="docutils literal"><spanclass="pre">MEGA_CORP</span></code>, this doesn’t match and should cause a
failure. The <codeclass="docutils literal"><spanclass="pre">expectFailureOfTx</span></code> method takes a 1-based index (in this case we expect the first transaction to fail)
and a string that should appear in the exception message. Then it runs the <codeclass="docutils literal"><spanclass="pre">TransactionGroup.verify()</span></code> method to
invoke all the involved contracts.</p>
<p>It’s worth bearing in mind that even though this code may look like a totally different language to normal Kotlin or
Java, it’s actually not, and so you can embed arbitrary code anywhere inside any of these blocks.</p>
<p>Let’s set up a full trade and ensure it works:</p>
<divclass="codeset container">
<divclass="highlight-kotlin"><divclass="highlight"><pre><span></span><spanclass="c1">// Generate a trade lifecycle with various parameters.</span>
<p>In this example we see some new features of the DSL:</p>
<ulclass="simple">
<li>The <codeclass="docutils literal"><spanclass="pre">roots</span></code> construct. Sometimes you don’t want to write transactions that laboriously issue everything you need
in a formally correct way. Inside <codeclass="docutils literal"><spanclass="pre">roots</span></code> you can create a bunch of states without any contract checking what you’re
doing. As states may not exist outside of transactions, each line inside defines a fake/invalid transaction with the
given output states, which may be <em>labelled</em> with a short string. Those labels can be used later to join transactions
together.</li>
<li>The <codeclass="docutils literal"><spanclass="pre">.CASH</span></code> suffix. This is a part of the unit test DSL specific to the cash contract. It takes a monetary amount
like 1000.DOLLARS and then wraps it in a cash ledger state, with some fake data.</li>
<li>The owned_by <aclass="reference external"href="https://kotlinlang.org/docs/reference/functions.html#infix-notation">infix function</a>. This is just
a normal function that we’re allowed to write in a slightly different way, which returns a copy of the cash state
with the owner field altered to be the given public key. <codeclass="docutils literal"><spanclass="pre">ALICE</span></code> is a constant defined by the test utilities that
is, like <codeclass="docutils literal"><spanclass="pre">DUMMY_PUBKEY_1</span></code>, just an arbitrary keypair.</li>
<li>We are now defining several transactions that chain together. We can optionally label any output we create. Obviously
then, the <codeclass="docutils literal"><spanclass="pre">input</span></code> method requires us to give the label of some other output that it connects to.</li>
<li>The <codeclass="docutils literal"><spanclass="pre">transaction</span></code> function can also be given a time, to override the default timestamp on a transaction.</li>
</ul>
<p>The <codeclass="docutils literal"><spanclass="pre">trade</span></code> function is not itself a unit test. Instead it builds up a trade/transaction group, with some slight
differences depending on the parameters provided (Kotlin allows parameters to have default values). Then it returns
<spanclass="n">trade</span><spanclass="p">(</span><spanclass="n">redemptionTime</span><spanclass="p">=</span><spanclass="n">TEST_TX_TIME</span><spanclass="p">+</span><spanclass="m">2.</span><spanclass="n">days</span><spanclass="p">).</span><spanclass="n">expectFailureOfTx</span><spanclass="p">(</span><spanclass="m">3</span><spanclass="p">,</span><spanclass="s">"must have matured"</span><spanclass="p">)</span>
<spanclass="p">}</span>
</pre></div>
</div>
</div>
<p>That’s pretty simple: we just call <codeclass="docutils literal"><spanclass="pre">verify</span></code> in order to check all the transactions in the group. If any are invalid,
an exception will be thrown indicating which transaction failed and why. In the second case, we call <codeclass="docutils literal"><spanclass="pre">expectFailureOfTx</span></code>
again to ensure the third transaction fails with a message that contains “must have matured” (it doesn’t have to be
<h2>Adding a generation API to your contract<aclass="headerlink"href="#adding-a-generation-api-to-your-contract"title="Permalink to this headline">¶</a></h2>
<p>Contract classes <strong>must</strong> provide a verify function, but they may optionally also provide helper functions to simplify
their usage. A simple class of functions most contracts provide are <em>generation functions</em>, which either create or
modify a transaction to perform certain actions (an action is normally mappable 1:1 to a command, but doesn’t have to
be so).</p>
<p>Generation may involve complex logic. For example, the cash contract has a <codeclass="docutils literal"><spanclass="pre">generateSpend</span></code> method that is given a set of
cash states and chooses a way to combine them together to satisfy the amount of money that is being sent. In the
immutable-state model that we are using ledger entries (states) can only be created and deleted, but never modified.
Therefore to send $1200 when we have only $900 and $500 requires combining both states together, and then creating
two new output states of $1200 and $200 back to ourselves. This latter state is called the <em>change</em> and is a concept
that should be familiar to anyone who has worked with Bitcoin.</p>
<p>As another example, we can imagine code that implements a netting algorithm may generate complex transactions that must
be signed by many people. Whilst such code might be too big for a single utility method (it’d probably be sized more
like a module), the basic concept is the same: preparation of a transaction using complex logic.</p>
<p>For our commercial paper contract however, the things that can be done with it are quite simple. Let’s start with
<p>We take a reference that points to the issuing party (i.e. the caller) and which can contain any internal
bookkeeping/reference numbers that we may require. Then the face value of the paper, and the maturity date. It
returns a <codeclass="docutils literal"><spanclass="pre">TransactionBuilder</span></code>. A <codeclass="docutils literal"><spanclass="pre">TransactionBuilder</span></code> is one of the few mutable classes the platform provides.
It allows you to add inputs, outputs and commands to it and is designed to be passed around, potentially between
multiple contracts.</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">Generation methods should ideally be written to compose with each other, that is, they should take a
<codeclass="docutils literal"><spanclass="pre">TransactionBuilder</span></code> as an argument instead of returning one, unless you are sure it doesn’t make sense to
combine this type of transaction with others. In this case, issuing CP at the same time as doing other things
would just introduce complexity that isn’t likely to be worth it, so we return a fresh object each time: instead,
an issuer should issue the CP (starting out owned by themselves), and then sell it in a separate transaction.</p>
</div>
<p>The function we define creates a <codeclass="docutils literal"><spanclass="pre">CommercialPaper.State</span></code> object that mostly just uses the arguments we were given,
but it fills out the owner field of the state to be the same public key as the issuing party. If the caller wants
to issue CP onto the ledger that’s immediately owned by someone else, they’ll have to create the state themselves.</p>
<p>The returned partial transaction has a <codeclass="docutils literal"><spanclass="pre">WireCommand</span></code> object as a parameter. This is a container for any object
that implements the <codeclass="docutils literal"><spanclass="pre">Command</span></code> interface, along with a key that is expected to sign this transaction. In this case,
issuance requires that the issuing party sign, so we put the key of the party there.</p>
<p>The <codeclass="docutils literal"><spanclass="pre">TransactionBuilder</span></code> constructor we used above takes a variable argument list for convenience. You can pass in
any <codeclass="docutils literal"><spanclass="pre">ContractStateRef</span></code> (input), <codeclass="docutils literal"><spanclass="pre">ContractState</span></code> (output) or <codeclass="docutils literal"><spanclass="pre">Command</span></code> objects and it’ll build up the transaction
for you.</p>
<p>What about moving the paper, i.e. reassigning ownership to someone else?</p>
<p>Here, the method takes a pre-existing <codeclass="docutils literal"><spanclass="pre">TransactionBuilder</span></code> and adds to it. This is correct because typically
you will want to combine a sale of CP atomically with the movement of some other asset, such as cash. So both
generate methods should operate on the same transaction. You can see an example of this being done in the unit tests
for the commercial paper contract.</p>
<p>The paper is given to us as a <codeclass="docutils literal"><spanclass="pre">StateAndRef<CommercialPaper.State></span></code> object. This is exactly what it sounds like:
a small object that has a (copy of) a state object, and also the (txhash, index) that indicates the location of this
<p>Here we can see an example of composing contracts together. When an owner wishes to redeem the commercial paper, the
issuer (i.e. the caller) must gather cash from its wallet and send the face value to the owner of the paper.</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last"><strong>Exercise for the reader</strong>: In this early, simplified model of CP there is no built in support
for rollover. Extend the contract code to support rollover as well as redemption (reissuance of the paper with a
higher face value without any transfer of cash)</p>
</div>
<p>The <em>wallet</em> is a concept that may be familiar from Bitcoin and Ethereum. It is simply a set of cash states that are
owned by the caller. Here, we use the wallet to update the partial transaction we are handed with a movement of cash
from the issuer of the commercial paper to the current owner. If we don’t have enough quantity of cash in our wallet,
an exception is thrown. And then we add the paper itself as an input, but, not an output (as we wish to delete it
from the ledger permanently). Finally, we add a Redeem command that should be signed by the owner of the commercial
paper.</p>
<p>A <codeclass="docutils literal"><spanclass="pre">TransactionBuilder</span></code> is not by itself ready to be used anywhere, so first, we must convert it to something that
is recognised by the network. The most important next step is for the participating entities to sign it using the
<codeclass="docutils literal"><spanclass="pre">signWith()</span></code> method. This takes a keypair, serialises the transaction, signs the serialised form and then stores the
signature inside the <codeclass="docutils literal"><spanclass="pre">TransactionBuilder</span></code>. Once all parties have signed, you can call <codeclass="docutils literal"><spanclass="pre">TransactionBuilder.toSignedTransaction()</span></code>
to get a <codeclass="docutils literal"><spanclass="pre">SignedTransaction</span></code> object. This is an immutable form of the transaction that’s ready for <em>timestamping</em>,
which can be done using a <codeclass="docutils literal"><spanclass="pre">TimestamperClient</span></code>. To learn more about that, please refer to the
<aclass="reference internal"href="protocol-state-machines.html"><spanclass="doc">Protocol state machines</span></a> document.</p>
<p>You can see how transactions flow through the different stages of construction by examining the commercial paper
<h2>Non-asset-oriented based smart contracts<aclass="headerlink"href="#non-asset-oriented-based-smart-contracts"title="Permalink to this headline">¶</a></h2>
<p>It is important to distinguish between the idea of a legal contract vs a code contract. In this document we use the
term <em>contract</em> as a shorthand for code contract: a small module of widely shared, simultaneously executed business
logic that uses standardised APIs and runs in a sandbox.</p>
<p>Although this tutorial covers how to implement an owned asset, there is no requirement that states and code contracts
<em>must</em> be concerned with ownership of an asset. It is better to think of states as representing useful facts about the
world, and (code) contracts as imposing logical relations on how facts combine to produce new facts.</p>
<p>For example, in the case that the transfer of an asset cannot be performed entirely on-ledger, one possible usage of
the model is to implement a delivery-vs-payment lifecycle in which there is a state representing an intention to trade
and two other states that can be interpreted by off-ledger platforms as firm instructions to move the respective asset
or cash - and a final state in which the exchange is marked as complete. The key point here is that the two off-platform
instructions form pa rt of the same Transaction and so either both are signed (and can be processed by the off-ledger
systems) or neither are.</p>
<p>As another example, consider multi-signature transactions, a feature which is commonly used in Bitcoin to implement
various kinds of useful protocols. This technique allows you to lock an asset to ownership of a group, in which a
threshold of signers (e.g. 3 out of 4) must all sign simultaneously to enable the asset to move. It is initially
tempting to simply add this as another feature to each existing contract which someone might want to treat in this way.
But that could lead to unnecessary duplication of work.</p>
<p>A better approach is to model the fact of joint ownership as a new contract with its own state. In this approach, to
lock up your commercial paper under multi-signature ownership you would make a transaction that looks like this:</p>
<ulclass="simple">
<li><strong>Input</strong>: the CP state</li>
<li><strong>Output</strong>: a multi-sig state that contains the list of keys and the signing threshold desired (e.g. 3 of 4). The state has a hash of H.</li>
<li><strong>Output</strong>: the same CP state, with a marker that says a state with hash H must exist in any transaction that spends it.</li>
</ul>
<p>The CP contract then needs to be extended only to verify that a state with the required hash is present as an input.
The logic that implements measurement of the threshold, different signing combinations that may be allowed etc can then
be implemented once in a separate contract, with the controlling data being held in the named state.</p>
<p>Future versions of the prototype will explore these concepts in more depth.</p>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.