Create a smart contract on Zilliqa in 12 simple steps with the new Scilla IDE

We’ve been hard at work to make developing smart contracts on Zilliqa easier and more intuitive. This is the first of an upcoming series of tutorials on how to develop for Zilliqa with our new tools. If there is anything you want to learn more of, let us know in the comments!

Kicking off the development of smart contracts can be a hassle, as there are several tools that you have to install even before you can even start coding. You also need to keep updated with any tools that change midway during development. In order to make developing smart contracts more painless, we have launched a web-based Interactive Development Environment (IDE) that comes with all the necessary features required to manually create and test contracts.

This web IDE is created by Ovidiu Miclea and it can be found at https://ide.zilliqa.com/.

This web IDE will also be the primary portal for learning Scilla, a safe-by-design smart contract language designed for Zilliqa. Scilla is a principled domain-specific language that has security features and best practices built-in. You can learn more about why we created a new language and Scilla’s design principles here.

Features included in this IDE:

  • Persistent Memory: All contract codes and contract states are stored within your browser’s cookies until you clear them with the IDE’s Refresh button.
  • Testing Accounts: The IDE is equipped with 20 different accounts that are preloaded with 1 billion test ZIL tokens. You can use these accounts to Deploy and Call contracts written in the IDE and check if your contracts are working as intended.
  • Automatic Filtering of Transitions (i.e. Functions/Methods): Filters out the callable transitions available within the contract automatically after you deploy it.
  • Static Type Checking: Type-check for errors in variables within your contract on-the-fly and ensure that the contract is Type-Safe before deployment.
  • Automatic Block-time: This IDE simulates a blockchain (literally a chain of blocks with timestamp on them) and sets an arbitrary block interval at 10 seconds each. You can use this block-time as your timestamp for your time-lock or escrow contracts.
Savant IDE Overlay, fully packaged for development

The steps for developing a new smart contract typically goes like this:

[1] Create new Scilla file by using + NEW CONTRACT and name your project.

[2] Code in Scilla, by referencing documentation or the sample contracts available on the left tab.

[3]SAVE and CHECK your contract for type and syntax errors.

[4] Debug using the red error pointers highlighted in the coding environment.

Accounts and Addresses at your command on the right tab

[5] Choose an arbitrary account.

[6] At the DEPLOY tab, key in the initial parameter (and immutable variables if any) of the contract.

[7] DEPLOY the contract.

[8] Choose an arbitrary account.

[9] At the CALL tab, key in the message parameters (and mutable variables if any) to pass to the transition of the contract.

[10] CALL the contract.

[11] At the STATE tab, check the previous and final states of the contract to see if the logic flow is correct.

[12] Repeat step 5 to step 11 for each test case of your smart contract.

NOTE:

  • Events are emitted if your transition includes it. It is recommended to use events for the checking of the logical flow of your contract. You can check out the CrowdFunding sample contract to understand how to write an event within your transition.
  • You can also trigger the Native Data Type function in the `STATE` tab to convert Scilla format into JavaScript format. This enables you to read the state of the contract easily, without needing to open dropdown tabs one by one.
Savant IDE Workflow for the CrowdFunding contract

Walkthrough of a Simple HelloWorld contract deployment

A smart contract can, generally, be viewed as the code for accessing a highly-secured and immutable database. It is typically used to store important information, such as ownership and total supply, on the blockchain.

When the smart contract is deployed on the blockchain, its state is initialised. The contract state will then only mutate when a user with the right permissions calls a transition (function) within the smart contract. Note that every transaction that changes the contract state cannot be reversed on a blockchain.

There are two forms of variables in a smart contract that can be stored on the blockchain:

  1. Immutable variables that do not change throughout the lifespan of the contract, as declared within the parenthesis ( ).
  2. Mutable variables, as declared with field.

In this HelloWorld Contract example, we have an immutable variable of type ByStr20, called owner, that stores the owner’s account address. It does not change throughout the lifespan of the contract. We also have a mutable variable of type String, which changes only when the owner of the contract invokes the setHello transition.

Deploying and Calling HelloWorld

To deploy this HelloWorld contract, we simply need to key in the owner field as the initial parameters. To call the deployed contract, we just need to define the message parameters for msg.

Hope this quick walkthrough was a helpful introduction to building a simple smart contract on our new IDE. If you have any questions, you can find us on Gitter or let us know in the comments. Stay tuned to more tutorials and tool walkthroughs coming up soon!