In this tutorial, we demonstrate how to create a parametric blockchain with Deku-P, using the Deku TypeScript-SDK. To do so, we are going to create a state machine using Typescript, execute it within Deku blockchain, and interact with it!
Prerequisites
Running Deku
1. Install Nix by following the official guide. Additionally, you must enable the flakes feature:
Nix automatically installs all the dependencies and devtools needed for development, including Tilt, Docker, etc.
Note: Nix is the tool used by marigold to create reproductible environment, other distribution models will be available for the release
2. Install direnv and don’t forget to “hook direnv to your shell”
3. You can clone the github directory and use the branch doc/deku-pTutorial, on your terminal type:
You can follow our readme on how to compile Deku.
Init empty typescript application
- We will use Typescript as a main language for our blockchain application. Create the folder for your new project within the examples folder, in the terminal it looks like:
npm init will initialize your project it will create a package.json file
- Run npm install typescript,.
As a good practice, we will add some compiler settings, within package.json specify to use the tsconfig.json containing all the configurations for your typescript project (created at next step).
Create a new file tsconfig.json at the same level of your previously created package.json
You can copy the content of our tsconfig.json
Install deku-sdk manually by running:
Write your application!
Create an index.ts file. This file will contain all the business of your application, which will be your state machine.
Write the blockchain state machine
Let's start to write our business: print out Hello World (yes, I know, this is crazy business) in the Deku-P virtual machine.
Inside index.ts we import the library deku-sdk to play with the Deku VMs state.
Please add the // @ts-ignore before the import, to avoid an error due to our tsconfig.json
- main: takes two parameters:
- initial state of your VM
- transition function, to execute when there is a new operation submitted
- get: retrieves the value from the local state, it will take a single parameter key, and will return the stored value.
- set: set a value in a Deku state for a given key. It takes two parameters:
- key: a key of the state
- value: a value is a string encoded in JSON format
- transition: is a JSON received from the chain, it contains
The complete index.ts example:
- The transition function:
The tx which is a transaction will allow you to retrieve and save the state of your application.
- The main function declares the initial state:
- myState: "": it is the initial state, it is a JSON object with the tz1xxx address as key
- transition: to update the state
Submit your first operation
We provide 2 CLI commands to try out your state.
The first one is deku-cli mock-transaction which needs to arguments:
- A wallet.json:
you just need a public tz1xxx address and its related secret, something like:
You can generate one by running:
Then you can use the deku-cli mock-transaction command where arguments are:
- The payload we want to submit, in this case, it is an empty string.
- The file index.js (transpiled one) which we have written before.
As you have noticed, the VM is “mocked”, which means it is not really running. The command mock-transaction is the fastest way to check your state is working as you want, and the state is not “persisted”.
We will see in the following section how to start a VM or a Deku cluster.
Before that, let’s make our state a bit more complex.
Provide the operation
Instead of giving an empty string, we will provide Hello World from the dApp to the Deku-P cluster.
Let's modify the code in the function transition in index.ts as follow:
We will simply provide it as the second argument of deku-cli mock-transaction command:
Since the mock-transaction command doesn’t persist the state, we can’t see the several changes.
Let’s use deku-cli submit-transaction to see state evolution!
The result should be printed in the first terminal (where ./tutorial.sh is running)
Now let’s change this value:
And… we're done! The complete source code of this basic dApp is available here.
This is great!
We did our first interaction with Deku-P! Of course, you must be thinking:
"But this is only from the command-line… We need to do the same from the outside world, like a web application!"
In the second part of this tutorial (Create a cookie game Blockchain app with Deku-P), we will implement a cookie-clicker game as our state machine!
We will provide you the technical steps with the needed explanation, to interact with the vm-state from your Web application.
If you want to know more about Marigold, please follow us on social media (Twitter, Reddit, Linkedin)!