The Hitchhiker’s Guide to Waves Smart Contracts. Part 1

Inal Kardanov
Waves Protocol
Published in
5 min readSep 14, 2018

--

On September 10 Waves Platform released a new version of node, which has the support of Smart Contracts in their first implementation. In this article, we’re going to focus on the idea of Waves Smart Accounts and what makes it different than other existing solutions. We will begin with a brief introduction with some definitions of smart contracts then we’re going to see the difference between Bitcoin script, Ethereum Smart Contract and Waves Smart Accounts. After that, we will provide some use-cases implementation using our solution and explain more about our RIDE language.

So let’s prepare a cup of coffee and begin our journey!

Intro

Blockchain word has been around for a while and every day we’re hearing this word more and more. Usually, in the second or third sentence about the blockchain we hear about smart contracts, so they’re tightly coupled.

The definition of a Smart Contract

Most of us already heard about smart contracts and the first thing that comes to our mind when we say a smart contract is Ethereum, it is a little unfair, because the first proof-of-concept of a smart contract is Bitcoin Script. Actually, there are many different blockchain platforms which implemented the idea of a smart contract and the definition of the smart contract was introduced 20 years ago by Nick Szabo.

We can define a smart contract as follows:

  • A smart contract is a program that runs on the blockchain and has its correct execution driven by the consensus protocol. A contract can encode any set of rules represented in its programming language. Therefore, smart contracts can implement a wide range of applications, including financial instruments and autonomous governance applications.
  • The idea of a smart contract as defined by Nick Szabo is to satisfy common contractual conditions, to minimize exceptions both malicious and accidental, and minimize the need for trusted intermediaries.

Bitcoin Script

Now let’s understand the concept of Bitcoin script — the language which is used for creating scripts in Bitcoin Network. Basically, the script is a Forth-like bytecode stack-based language but what makes bitcoin script different that is designed purposely so the script execution is guaranteed to terminate. Some characteristics that are available in Forth but are not available in Script include variables, arrays, functions, and loops. Bitcoin Script does not provide any primitives that allow random write access to either of the stacks. A very important thing to understand — there are no accounts in the Bitcoin network, there are only inputs and outputs. Every transaction is a list of outputs, which refers to inputs. Bitcoin Script can be attached to any input and it defines the rule of converting an input to an output (sending bitcoins to person, for example).

Bitcoin Script key features:

  • Script language includes the necessary cryptographic operations and primitives, has many different opcodes (many of them are disabled), but there are no loops. Script memory access is stack-based.
  • A script always terminates. The execution time is bounded above by the length of the script after the instruction pointer and this limitation prevents denial of service attacks on the nodes which are validating the blocks.

Why do we need smart contracts

Since the first day of development, we had in our mind the main goal which is to cover two types of tasks for smart contracts:

  1. Simple and well-defined tasks like multisignature and atomic swap.
  2. Dapps with custom logic and almost limitless possibilities.

There is another common use case for smart contracts — tokens. For example, in Ethereum Network the vast majority of working contracts are ERC20 standard tokens. But in our case, it is not relevant, because we have lightweight tokens in the core (more details here). It is not necessary to create a contract to issue a token, you just have to send one transaction of the predefined type.

With our first implementation of smart contracts, we decided to cover first types of tasks, i.e. multisignatures, atomic swaps, escrows. This will provide functionality for implementing the most-needed scenarios as well as more elaborate protections for coins and with our data transaction, there will be a way to post Oracle data to the blockchain.

In the early stages of development we have created a list of requirements for such smart contracts:

  1. Maximum security. Smart contracts for simple tasks should be simple, secure and easy to start. Every month we see news like this, we want to avoid stupid bugs for simple tasks. Smart contracts language should help to avoid bugs.
  2. No need for gas. To do this, the script must be executed for a predictable amount of time.

About Waves Blockchain

Before moving on to the technical details of the implementation, let’s talk a little bit about Waves blockchain, it will be important for further understanding.

  1. There are currently 12 different types of transactions in the Waves Blockchain.
  1. There are no inputs and outputs (as in Bitcoin), but accounts. Every transaction is performed by one account.
  2. By default, the correctness of the transaction is determined by the current blockchain state and the validity of the signature. JSON representation of the transaction is quite simple:
JSON of transfer transaction in Waves Network

What is Smart Account?

The latest release of Waves Node makes Smart Accounts enabled. I will try to give you an intuition what is a smart account.

Basically, the smart account is an account with attached transactions checking script. In other words, a script which is attached to an account so the account can validate every transaction before confirming it.

Facts about Waves Smart Accounts (NO GAS is the best fact)

  1. The Waves smart account can check if the transaction meets certain conditions which are defined in a script before the transaction is submitted to be included in the next generated block.
  2. It will enable users to use a script on their accounts that will allow them to control all outgoing transactions so they can get the benefits of such smart accounts in different situations including 2FA, Multisig, escrow and oracles among others.
  3. It can be used to set different rules on the checking process of transaction validity. For example, the user can set a rule that transactions from an address can be sent only if the block height is greater than N or the user can set a rule that all transactions from the address are valid (even without signature validation).
  4. Smart accounts are predicated based on: the transaction type and fields, the Height or timestamp and finally the Data on the blockchain (Data transactions and Oracles).

In next part I will discuss about RIDE smart contracts language, it’s idea and technical details about costs, standard library and we will write our first smart contract for Waves using IDE.

Stay tuned!

P.S. You can find a lot of details and explanations, including Smart Accounts White Paper on Waves website.

--

--

Inal Kardanov
Waves Protocol

Co-founder & CTO of Billy. Software engineer. Blockchain, ML&AI developer. All opinions are my own.