How to build a Hello World CorDapp? (Part 2)

karthik Ganti
3 min readDec 1, 2020

--

In the previous tutorial we created a simple CorDapp which allowed a node to issue IOU’s. In this tutorial we will see how to add contract constraints. We will be using the same HelloWorld Cordapp. You can check out the part1 here.

So let’s get started.

Why do we need contract constraints?

The CorDapp as it stands so far does not impose any constraints on the evolution of IOUs on the blockchain over time. Anyone would be free to create IOUs of any value.

To rectify this, We must write a contract to impose rules on how an IOUState can change over time. In turn, this will require some small changes to the flow you defined earlier in writing the flow.

Writing the contract

In Corda, contracts are mechanism used to impose constraints on how states can evolve. Every state has an associated contract. A transaction is invalid if it does not satisfy the contract.

We will be imposing following constraints in our CorDapp:

  • A transaction involving IOUs must consume zero inputs and create one output of type IOUState.
  • The transaction should also include a Create command.
  • The value must be non-negative.
  • The lender and borrower cannot be the same entity.
  • The IOU’s lender and borrower must sign the transaction.

OK, enough theory let’s start writing the code :)

Create a java class IOUContract.java in com.template.contracts package and add the following code.

Update the flow

Now we will have to update the flow.

Update Responder flow

Redeploy CorDapps

We will have to run gradlew command to redeploy the cordapps.

Mac/Linux:

./gradlew clean deployNodes

Windows:

gradlew clean deployNodes

Output:

C:\Users\karthik\Documents\GitHub\cordapp-template-java>gradlew clean deployNodes
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :jar
Cordapp metadata not defined for this gradle build file. See https://docs.corda.net/head/cordapp-build-systems.html#separation-of-cordapp-contracts-flows-and-services
> Task :deployNodes
Running Cordform task
Deleting C:\Users\karthik\Documents\GitHub\cordapp-template-java\build\nodes
Bootstrapping local test network in C:\Users\karthik\Documents\GitHub\cordapp-template-java\build\nodes
Generating node directory for Notary
Generating node directory for PartyA
Generating node directory for PartyB
Waiting for all nodes to generate their node-info files...
... still waiting. If this is taking longer than usual, check the node logs.> Task :deployNodes
Distributing all node-info files to all nodes
Loading existing network parameters... none found
Gathering notary identities
Generating contract implementations whitelist
New NetworkParameters {
minimumPlatformVersion=8
notaries=[NotaryInfo(identity=O=Notary, L=London, C=GB, validating=false)]
maxMessageSize=10485760
maxTransactionSize=524288000
whitelistedContractImplementations {
}
eventHorizon=PT720H
packageOwnership {
}
modifiedTime=2020-12-01T06:36:15.890Z
epoch=1
}
Bootstrapping complete!
Run database schema migration scripts
Run database schema migration scripts
Run database schema migration scripts
BUILD SUCCESSFUL in 1m 38s
15 actionable tasks: 14 executed, 1 up-to-date

Run corda nodes:

Start the nodes by running the following command

Mac / Linux:

build/nodes/runnodes

Windoes:

build/nodes/runnodes.bat

Testing contract conditions:

Let’s test the flow by giving negative IOU value

start IOUFlow iouValue: -10, otherParty: "O=PartyB,L=New York,C=US"

The behavior is correct, Let’s give positive IOU value and check.

start IOUFlow iouValue: 50, otherParty: "O=PartyB,L=New York,C=US"

Let’s do a vault query at PartyB.

run vaultQuery contractStateType: com.template.states.IOUState

Similarly we can check other contract conditions. The node will throw error if it does not satisfy the contract constraints.

Conclusion

You have now updated your flow to verify the transaction and gather the lender’s signature. You now have the basic idea on how to build CorDapps.

--

--

karthik Ganti
karthik Ganti

Written by karthik Ganti

Hi, I am karthik. Full Stack Developer | Web3 Expert | Micorservices Developer | Exploring Gen AI | ReactJS Developer. https://github.com/hacktronaut

No responses yet