Skip to main content

Use Dubhe to develop a contract

Let’s develop one of the simplest examples, called Distributed Messaging, or dms for short. The contract will have a schema called message, which will have a field called content, which will store the message content. The contract will have a method called send, which will take a string as a parameter and store it in the content field of the mailbox schema. The contract will also have an event called MessageSent, which will be triggered when a message is sent. Dubhe generates some code based on the dubhe.config.ts configuration, so we just need to focus on this file.
Let’s go over what’s in dubheConfig
  • name: The name of the contract project.
  • description: The description of the contract project.
  • schemas: The schema of the contract project. Refer to the Schemas section for details of schema.
To complete dms contract I need a Schema to store the numbers, now let’s complete the Schema.
The name of this Schema is message, and he has only one number field with storage type StorageValue, which is used to store String. Now we need to generate the contract frame content using Dubhe’s most powerful feature.
Take a look at the file structure of dms, we don’t need to concern ourselves with anything else but our business code systems folder.
A Message Schema has been generated for you, now you need to write your own business code! We create a new move file called message in the Systems folder.
This can be easily achieved with the help of Dubhe’s code generation. The contents of the file:
Let’s add an error handler so that the length of the content doesn’t exceed 12, how to do error handling?
The error handling code has been added, now we also want to add events to the contract to tell us who sent what message.
Execute the schemagen command here.
Errors and events are already generated. First we do an error check, the input content must be greater than 0.
Finally we add events to the contract.
By this point we need to write the contents of the deploy_hook, which will be automatically executed for you when the contract is deployed, now we initialise the content in the store to Hello World!.
Let’s run the Build command to check for syntax errors.