Smart Contracts: Rust Cake!
By Mels Dees

May 08, 2020

Writing a smart contract on Dusk Network is a piece of (Rust and) cake!, and because user friendliness is central to our design, we dive a little deeper into our new sugared syntax and show how to use this Rust Cake! and Bake smart contracts.

Rusk VM and the Rust language
All Dusk Network contracts are running in our virtual machine (VM), named Rusk, and is powered by WASM (WebAssembly) and the Rust programming language. What makes Rust special and extremely suitable for our VM, is the fact that it has a static garbage collector (* bear with us, we’ll explain in a bit), which brings very predictable performance and small code size to the table. In other words, Rust is extremely fast, and secure and predictable in its performance.

*During code development, developers need to take into consideration how much memory is needed. Normally, this is done through automatic memory management, while in Rust this ‘garbage collection’ is done statically. Therefore, with Rust there is less risk of memory overflow, meaning developers don't need to worry about allocating memory, and can instead focus on the task at hand: development.

Rusk and Cake
“A rusk is a hard, dry biscuit or a twice-baked bread. In some cultures, rusk is made of cake, rather than bread: this is sometimes referred to as cake rusk.” “It also simply sounds an awful lot like Dusk”.

The sugared syntax Cake! is only applicable to the Rust programming language, and is meant to make it easier for Rust developers to create smart contracts on Dusk Network. In the demo we show what the actual code of a smart contract (without Cake! applied) looks like, and it’s not exactly user friendly. The sugared syntax (or syntactic sugar) makes it easier to read or to express, effectively “sweetening” the language for human use.

Advantages of Cake!
Our sugared syntax is made to look and feel like regular Rust code, easing the reading and development of code for Rust developers. Cake! is not applicable to other languages, but this does not take away that other coding languages can be used for smart contract creation, as long as they compile to WebAssembly. In addition, it has versioning (see version = “0.0.1” in image below), which brings the benefit of being able to keep track of application builds.

Rust version Cake! version
Cake! provides abstraction over the Application Binary Interface (ABI). There is no need to change the Virtual Machine (VM) to make any improvement to our contracting. The ABI is a specification for a specific hardware platform and connects with the operating system. It is one step beyond the Application Program Interface (API), which defines the calls from the application to the operating system.

Below we've included a screenshot of the code of a testing program. On the left is the code without Cake!, and on the right is the code written with Cake!. There is a substantial difference in size and ease of writing the code.

Cake! and Bake
And while Cake! reduces the amount of code that one needs to produce. Bake helps manage the dependencies between contracts and is thus particularly helpful when you have inter-contract calls. Bake is predominantly used to create a hash of the contract that you are baking. Contract hashes can be used to create multi-contract interoperability where contracts reference one another (as dependencies). The result of the Cargo Bake function is:

  • the version of the contract
  • the contract name
  • the opcode for the public method, and;
  • the contract hash

For example, you can create a security token contract in which the whitelist is part of the smart contract, or you can create two contracts, one being the whitelist contract, and the other the security token contract. In the latter scenario, bake is used to manage inter contract calls, meaning the security token contract (or a multitude of security token contracts) can interact with the whitelist contract.

This feature can of course also be used to develop many other types of applications (dApps) that require inter-contract dependencies. We will explain more about this in future demos.

A cake! baking example
In the demo we show how cake! translates the code into something simpler and how to bake with contract dependencies.

The repository can be found here: https://github.com/dusk-network/cake-rusk

Next steps
Cake! will evolve into a complete Domain Specific Language (DSL) in due time. Before that happens we intend to extend cake! by bringing abstraction over storage’s ABI (set, list, etc.). The cargo bake function will manage contract dependencies, and could in turn even be used to deploy and verify contracts.

Related articles

Share this post

Subscribe to our newsletter

Dusk on GitHub Download Whitepaper