How to create a CorDapp in 3 steps
When you embark on writing your first CorDapp, it is a significant undertaking for the first time around. There are a lot of moving parts and concepts to learn, some of them may be completely unfamiliar such as distributed systems and cryptography.
At Web3 Labs, we strive to make the developer experience as smooth as possible. By utilising multiple years experience building developer tools for Ethereum, we have now applied our expertise to Corda, creating web3j-corda. web3j-corda drastically simplifies the steps to get started with creating your first CorDapp.
This blog will take you through all you need to know before you have your first corDapp up and running in less than 30 minutes.
Step 1: Installation
First, we need to ensure we have all the prerequisites are installed, which are:
Then let’s install web3j-corda using
curl -L https://getcorda.web3j.io | bash
If you are on windows run:
Set-ExecutionPolicy Bypass -Scope Process -Force;iex ((New-Object
This will install the latest version of web3j-corda to the $HOME/.web3j-corda directory. It will be added directly to your $PATH. So the next time you start the terminal, it will be ready for you to use right away. But if you want to use it in the current terminal, simply run
Step 2: Create your CorDapp
Now let us create our first CorDapp using web3j-corda. For that, we have to run the new command. This creates a template CorDapp for you to get going with. The following are the different options it takes:
- -n — name of the cordapp we want to create
- -o — the output directory in which we want to cordapp to be generated
- -p — the package name of the cordapp client
Let’s run the following:
web3j-corda new -n test -o ~/clients/corda/test/t1 -p org.example
This generates a Gradle project with the given input. We can then open the created project in the IDE of our choice.
The project contains 3 main modules:
- Contracts: It defines the contractual validity of the CorDapp. You can read more about it here. States are created in the same module, they are the actual information stored in the ledger.
- Workflows: The flows enable automated agreeing to the ledger states. Detailed documentation can be found on the Corda website.
- Clients: The client defines classes that can be used to interact with the CorDapp in a production environment.
All the above modules have been pre-defined by web3j-corda, such that it can be used out of the box without any change to be made to individual classes or methods.
For the example above, we have generated all the code for the CorDapp including a default contract, state and workflow. We have even defined client that can be used we can interact with the CorDapp defined.
The generated client code is annotated with REST API definitions so that it can be used to connect to the Corda network.
We have defined all the necessary interfaces to start a flow defined in the Workflows module. The client is a facade to REST endpoints exposed by the Corda application. You can also find all the flows defined and the respective inputs and outputs to the flow.
We define a test network using docker to spin up 1 party node. And a network-map to create a compatibility zone. Also, we expose a braid server for each party node which exposes OpenAPI definition which helps us communicate over REST endpoints.
Step 3: Run the CorDapp
Now that we understand everything that is generated, we have our first CorDapp up all stitched up to our needs. We can run the test by running ./gradlew test (or by using Intellij IDEA) to verify the start-up of network and starting a flow — which does the most basic call to the flow and verifies the output.
Ensure that you have docker installed and running. In the test here, we are going to start a party node. It will take some time to start-up the node, verify the certificates with network-map and start the network.
We have the test working. And hooray you have got your first corDapp up and running as well! Getting productive with Corda doesn’t need to be difficult if you utilise the right tools. The three steps we’ve demonstrated will allow you to understand the core concepts and most importantly provide the right foundations to be productive on the Corda platform.
Enjoy your Corda journey, see you next time with more things you can do with web3j-corda.