waves_logo Docs
  • Ride Programming Language
    Ride Programming Language
  • Getting started
    Getting started
  • Base concepts
    • Definition
      Definition
    • Expression
      Expression
    Base concepts
  • Comments
    Comments
  • Constants
    Constants
  • Data types
    • Boolean
      Boolean
    • ByteVector
      ByteVector
    • Int
      Int
    • String
      String
    • Unit
      Unit
    • List
      List
    • Tuple
      Tuple
    • Union
      Union
    Data types
  • Exceptions
    Exceptions
  • Functions
    • Annotations
      Annotations
    • Built-in functions
      • Account data storage functions
        Account data storage functions
      • Blockchain functions
        Blockchain functions
      • Byte array functions
        Byte array functions
      • Converting functions
        Converting functions
      • Data transaction functions
        Data transaction functions
      • Decoding functions
        Decoding functions
      • Encoding functions
        Encoding functions
      • Exception functions
        Exception functions
      • Hashing functions
        Hashing functions
      • List functions
        List functions
      • Math functions
        Math functions
      • String functions
        String functions
      • Union functions
        Union functions
      • Verification functions
        Verification functions
      Built-in functions
    • Callable function
      Callable function
    • Verifier function
      Verifier function
    Functions
  • FOLD<N> Macro
    FOLD<N> Macro
  • Operators
    • match-case
      match-case
    Operators
  • Script
    • Directives
      Directives
    • Script body
      Script body
    • Script context
      Script context
    • Script types
      • Account script
        Account script
      • Asset script
        Asset script
      • dApp script
        dApp script
      Script types
    • Standard Library
      Standard Library
    Script
  • Structures
    • Script actions
      • BinaryEntry (v4)
        BinaryEntry (v4)
      • BooleanEntry (v4)
        BooleanEntry (v4)
      • Burn (v4)
        Burn (v4)
      • DataEntry (v3)
        DataEntry (v3)
      • DeleteEntry (v4)
        DeleteEntry (v4)
      • IntegerEntry (v4)
        IntegerEntry (v4)
      • Issue (v4)
        Issue (v4)
      • Reissue (v4)
        Reissue (v4)
      • ScriptTransfer (v3 and v4)
        ScriptTransfer (v3 and v4)
      • SponsorFee (v4)
        SponsorFee (v4)
      • StringEntry (v4)
        StringEntry (v4)
      Script actions
    • Script results (v3)
      • ScriptResult
        ScriptResult
      • TransferSet
        TransferSet
      • WriteSet
        WriteSet
      Script results (v3)
    • Common structures
      • Address
        Address
      • Alias
        Alias
      • Asset
        Asset
      • AssetPair
        AssetPair
      • AttachedPayment
        AttachedPayment
      • BalanceDetails
        BalanceDetails
      • BlockInfo
        BlockInfo
      • Invocation
        Invocation
      • Order
        Order
      • Transfer
        Transfer
      Common structures
    • Transaction structures
      • BurnTransaction
        BurnTransaction
      • CreateAliasTransaction
        CreateAliasTransaction
      • DataTransaction
        DataTransaction
      • ExchangeTransaction
        ExchangeTransaction
      • GenesisTransaction
        GenesisTransaction
      • InvokeScriptTransaction
        InvokeScriptTransaction
      • IssueTransaction
        IssueTransaction
      • LeaseCancelTransaction
        LeaseCancelTransaction
      • LeaseTransaction
        LeaseTransaction
      • MassTransferTransaction
        MassTransferTransaction
      • ReissueTransaction
        ReissueTransaction
      • SetAssetScriptTransaction
        SetAssetScriptTransaction
      • SetScriptTransaction
        SetScriptTransaction
      • SponsorFeeTransaction
        SponsorFeeTransaction
      • TransferTransaction
        TransferTransaction
      • UpdateAssetInfoTransaction
        UpdateAssetInfoTransaction
      Transaction structures
    Structures
  • Variables
    • Built-in variables
      Built-in variables
    Variables
  • Limitations
    • Complexity
      Complexity
    • Data Weight
      Data Weight
    Limitations
  • Ride Components
    Ride Components
  • Script performance tests
    Script performance tests
      • English
      • Русский
      On this page
        • Introduction
        • Overview
        • Disclaimer
        • “Hello world!”
        • Blockchain
        • Comments
        • Directives
        • Variables
        • Functions
        • Basic types
        • Special types
        • State reader functions
        • If
        • Exceptions
        • Predefined data structures
        • Loops with FOLD<N>
        • Annotations
      waves_logo Docs

          # Getting started

          # Introduction

          Ride is a purpose-designed programming language for smart contracts on the Waves blockchain. It was created to address many of the most serious shortcomings of other popular smart contract languages. The overall idea was to offer a straightforward functional language for dApp development on the Waves blockchain.

          Ride is easy to learn, especially for beginning developers. This brochure gives a comprehensive introduction to Ride, along with examples and further tools and resources.

          # Overview

          Ride is a statically-typed, lazy, functional, expression-based compiled programming language. It is designed for building developer-friendly decentralized applications (dApps).

          Ride is not Turing Complete and its execution engine (virtual machine) doesn’t have any concept of loops or possibility for recursions. Also, there are a number of limitations by design, helping to ensure execution is secure and straightforward. However, we recognize that iterations are necessary and have implemented them as FOLD macros (see below). One of the key features is that the execution cost is always predictable and known in advance, so all code executes as intended with no failed transactions recorded on-chain – removing a significant source of frustration.

          Despite being simple to use, however, Ride is powerful and offers wide-ranging functionality to developers. It’s broadly based on Scala and is also influenced by F# and the functional paradigm.

          Ride is simple and concise. It will take around an hour to read this brochure, after which you will know everything about the Ride and opportunities that it gives for dApps development.

          # Disclaimer

          Ride Standard Library (STDLIB) is under active development. At the time of publication, the most up-to-date version is STDLIB_VERSION 3, with STDLIB_VERSION 4 on the way. The brochure covers most of the projected features too. Those which are not part of STDLIB_VERSION 3 are marked with (*).

          # “Hello world!”

          Let’s start with a familiar example:

          func say() = {
            "Hello world!"
          }
          

          Functions in Ride are declared with func (see further below). Functions do have return types, this is inferred automatically by the compiler, so you don't have to declare them. In the case above the function say returns the string Hello World!. There is no return statement in the language because Ride is expression-based (everything is an expression), and the last statement is a result of the function.

          # Blockchain

          Ride was created specifically for execution within a blockchain environment and is optimised for this purpose. Because the blockchain is a shared ledger, located on many computers all around the world, it works a little differently to conventional programming languages.

          Since Ride is designed to be used inside the blockchain, there is no way to access the filesystem or display anything in the console. Instead, Ride functions can read data from the blockchain and return actions as a result, which can then be applied to the blockchain.

          # Comments

          You can add comments to your code much as you can with other languages such as Python:

          # This is a comment line
          # And there is no multiline comments
          "Hello world!" # You can write comments like here
          

          # Directives

          Every Ride script should start with directives for the compiler. At the time of publication, there are three types of directive, with different possible values.

          {-# STDLIB_VERSION 3 #-}
          {-# CONTENT_TYPE DAPP #-}
          {-# SCRIPT_TYPE ACCOUNT #-}
          

          STDLIB_VERSION sets the version of the standard library. The latest version currently in production is 3.

          CONTENT_TYPE sets the type of the file you're working on. There are different content types, DAPP and EXPRESSION. The DAPP type allows you to define functions and finish execution with certain transactions (changes to the blockchain), as well as using annotations. The EXPRESSION type should always return a boolean value, since it’s used as a predicate for transaction validation.

          SCRIPT_TYPE sets the entity type we want to add to the script to change its default behavior. Ride scripts can be attached to either an ACCOUNT or ASSET.

          Not all combinations of directives are correct. The example below won’t work, because DAPP content type is allowed only for accounts, while EXPRESSION type is allowed for assets and accounts.

          {-# STDLIB_VERSION 3 #-}
          {-# CONTENT_TYPE DAPP #-}
          {-# SCRIPT_TYPE ASSET #-} # dApp content type is not allowed for an asset
          

          # Variables

          Variables are declared and initialized with the let keyword. This is the only way to declare variables in Ride.

          let a = "Bob"
          let b = 1
          

          All variables in Ride are immutable. This means you cannot change the value of a variable after declaration.

          Ride is strongly typed and the variable's type is inferred from the value on the right hand side.

          Ride allows you to define variables globally, inside any function, or even inside a variable definition.

          func lazyIsGood() = {
            let a = "Bob"
            let b = {
               let x = 1
               “Alice”
              }  
            true
          }
          

          The function above will compile and return true as a result, but variable a won't be initialized because Ride is lazy, meaning that any unused variables will not be calculated.

          # Functions

          Functions in Ride can only be used after they are declared.

          func greet(name: String) = {
            "Hello, " + name
          }
          func add(a: Int, b: Int) = {
            func m(a:Int) = a
            m(a) + b
          }
          

          The type (Int, String, etc) comes after the argument’s name.

          As in many other languages, functions should not be overloaded. It helps to keep the code simple, readable and maintainable.

          func calc() = {
            42
          }
          func do() = { 
            let a = calc()
            true
          }
          

          The callable function will not be called either, because variable a is unused.

          Unlike most languages, variable shadowing is not allowed. Declaring a variable with a name that is already used in a parent scope will result in a compilation error.

          Functions should be defined before they are used.

          Functions can be invoked in prefix and postfix order:

          let list = [1, 2, 3]
          let a1 = list.size()
          let a2 = size(list)
          let b1 = getInteger(this, “key”)
          let b2 = this.getInteger(“key”)
          

          In these examples a1 is the same as a2 and b1 is the same as b2.

          # Basic types

          The main basic types and examples are listed below:

          Boolean    #   true
          String     #   "Hey"
          Int        #   1610
          ByteVector #   base58'...', base64'...', base16'...', fromBase58String("...") etc.
          

          We will explore Strings and special types further below.

          # Strings

          let name = "Bob"   # use "double" quotes only
          let coolName = name + " is cool!" # string concatenation by + sign
          name.indexOf("o")  # 1
          

          Like other data structures in Ride, strings are immutable. String data is encoded using UTF-8.

          Only double quotes can be used to denote strings. Strings are immutable, just like all other types. This means that the substring function is very efficient: no copying is performed and no extra allocations are required.

          All operators in Ride must have values of the same type on both sides. The following code will not compile because age is an int:

          let age = 21
          "Bob is " + age # won't compile
          

          To make it work we have to convert age to string:

          let age = 21
          "Alice is " + age.toString() # will work!
          

          # Special types

          Ride has few core types, which operate much as they do in Scala.

          # Unit

          There is no null in Ride, as is the case in many other languages. Usually, built-in functions return unit value of type unit instead of null.

          "String".indexOf("substring") == unit # true
          

          # Nothing

          Nothing is the 'bottom type' of Ride’s type system. No value can be of type Nothing, but an expression of type Nothing can be used everywhere. In functional languages, this is essential for support for throwing an exception:

          2 + throw() # the expression compiles because
              # there's a defined function +(Int, Int).
                # The type of the second operand is Nothing, 
                # which complies to any required type.
          

          # List

          let list = [16, 10, 1997, "birthday"]       # can contain different data types
          let second = list[1]                        # 10 - read second value from the list
          

          List doesn't have any fields, but there are functions in the standard library that make it easier to work with fields.

          let list = [16, 10, 1997, "birthday"]
          let last = list[(list.size() - 1)] # "birthday", postfix call of size() function
          let lastAgain = getElement(collection, size(collection) - 1) # the same as above
          

          .size() function returns the length of a list. Note that it's a read-only value, and it cannot be modified by the user. (Note also that last could be of more than one type, but this is only inferred when the variable is set.)

          let initList = [16, 10]                   # init value
          let newList = cons(1997, initList)        # [1997, 16, 10]
          let newList2 = 1997 :: initList           # [1997, 16, 10]
          let newList2 = initList :+ 1              # [16, 10, 1](* Available in STDLIB_VERSION 4)
          let newList2 = [4, 8, 15, 16] ++ [23, 42]     # [4 8 15 16 23 42](*)
          
          • To prepend an element to an existing list, use the cons function or :: operator
          • To append an element, use the :+ operator (*)
          • To concatenate 2 lists, use the ++ operator (*)

          # Union types & Type Matching

          let valueFromBlockchain = getString("3PHHD7dsVqBFnZfUuDPLwbayJiQudQJ9Ngf", "someKey") # Union(String | Unit)
          

          Union types are a very convenient way to work with abstractions. Union(String | Unit) shows that the value is an intersection of these types.

          The simplest example of Union types is given below (please bear in mind that defining custom user types in dApp code will be supported in future versions):

          type Human : { firstName: String, lastName: String, age: Int}
          type Cat : {name: String, age: Int }
          

          Union(Human | Cat) is an object with one field, age, but we can use pattern matching:

          Human | Cat => { age: Int }
          

          Pattern matching is designed to check a value against value type:

            let t = ...               # Cat | Human
            t.age                     # OK
            t.name                    # Compiler error
            let name = match t {      # OK
              case h: Human => h.firstName
              case c: Cat   => c.name
            }
          

          Type matching is a mechanism for:

          let amount = match tx {              # tx is a current outgoing transaction
            case t: TransferTransaction => t.amount
            case m: MassTransferTransaction => m.totalAmount
            case _ => 0
          }
          

          The code above shows an example of type matching. There are different types of transactions in Waves, and depending on the type, the real amount of transferred tokens can be stored in different fields. If a transaction is TransferTransaction or MassTransferTransaction we use the corresponding field, while in all other cases, we will get 0.

          # State reader functions

          let readOrZero = match getInteger(this, "someKey") { # reading data from state
              case a:Int => a
              case _ => 0
          }
          readOrZero + 1
          

          getString returns Union(String | Unit) because while reading data from the blockchain (the key-value state of accounts) some key-value pairs may not exist.

          let v = getInteger("3PHHD7dsVqBFnZfUuDPLwbayJiQudQJ9Ngf", "someKey")
          v + 1    # doesn’t compile, forcing a developer to foresee the possibility of non-existing value for the key
          v.valueOrErrorMessage(“oops”) +  1 # compiles and executes
          let realStringValue2 = getStringValue(this, "someKey")
          

          To get the real type and value from Union use the extract function, which will terminate the script in case of Unit value. Another option is to use specialized functions like getStringValue, getIntegerValue, etc.

          # If

          let amount = 1610
          if (amount > 42) then "I claim that amount is bigger than 42"
            else if (amount > 100500) then "Too big!"
            else "I claim something else"
          

          if statements are pretty straightforward and similar to most other languages, with an important difference from some: if is an expression, so it must have an else clause (the result is assignable to a variable).

          let a = 16
          let result = if (a > 0) then a / 10 else 0 #
          

          # Exceptions

          throw("Here is exception text")
          

          The throw function will terminate script execution immediately, with the provided text. There is no way to catch thrown exceptions.

          The idea of throw is to stop execution and send useful feedback to the user.

          let a = 12
          if (a != 100) then
            throw ("a is not 100, actual value is " + a.toString())
            else throw("A is 100")
          

          # Predefined data structures

          #LET THE HOLY WAR BEGIN

          Ride has many predefined data structures specific to the Waves blockchain, such as: Address, Alias, DataEntry, ScriptResult, Invocation, ScriptTransfer, TransferSet, WriteSet, AssetInfo, BlockInfo.

          let keyValuePair = DataEntry("someKey", "someStringValue")
          

          For example, DataEntry is a data structure which describes a key-value pair, e.g. for account storage.

          let transferSet = TransferSet([ScriptTransfer("3P23fi1qfVw6RVDn4CH2a5nNouEtWNQ4THs", amount, unit)])
          

          All data structures can be used for type checking, pattern matching and their constructors as well.

          # Loops with FOLD<N>

          Since Ride’s virtual machine doesn’t have any concept of loops, they are implemented at compiler level via the FOLD<N> macro. The macro behaves like the ‘fold’ function in other programming languages, taking the input arguments: collection for iteration, starting values of the accumulator and folding function.

          The important aspect is N - the maximum amount of interactions over collections. This is necessary for maintaining predictable computation costs.

          This code sums the numbers of the array:

          let a = [1, 2, 3, 4, 5]
          func foldFunc(acc: Int, e: Int) = acc + e
          FOLD<5>(a, 0, foldFunc) # returns 15
          

          FOLD<N> can also be used for filtering, mapping, and other operations. Here’s an example for map with reverse:

          let a = [1, 2, 3, 4, 5]
          func foldFunc(acc: List[Int], e: Int) = (e + 1) :: acc
          FOLD<5>(a, [], foldFunc) # returns [6, 5, 4, 3, 2]
          

          # Annotations

          Functions can be without annotations, or with @Callable or @Verifier annotations.

          func getPayment(i: Invocation) = {
            let pmt = i.payment.valueOrErrorMessage(“Payment must be attached”)
            if (isDefined(pmt.assetId)) then 
              throw("This function accepts waves tokens only")
            else
              pmt.amount
          }
          @Callable(i)
          func pay() = {
            let amount = getPayment(i)
            WriteSet([DataEntry(i.caller.bytes, amount)])
          }
          

          Annotations can bind some values to the function. In the example above, variable i was bound to the function pay and stored all the information about the fact of invocation (the caller’s public key, address, payment attached to the transaction, fee, transactionId etc.).

          Functions without annotations are not available from the outside. You can call them only inside other functions.

          @Verifier(tx)
          func verifier() = {
            match tx {
              case m: TransferTransaction => tx.amount <= 100 # can send up to 100 tokens
              case _ => false
            }
          }
          

          # @Verifier annotation

          @Verifier(tx)
          func verifier() = {
            match tx {
              case m: TransferTransaction => tx.amount <= 100 # can send up to 100 tokens
              case _ => false
            }
          }
          

          A function with the @Verifier annotation sets the rules for outgoing transactions of a decentralized application (dApp). Verifier functions cannot be called from the outside, but they are executed every time an attempt is made to send a transaction from a dApp.

          Verifier functions should always return a Boolean value as a result, depending on which a transaction will be recorded to the blockchain or not.

          Expression scripts (with directive {-# CONTENT_TYPE EXPRESSION #-}) along with functions annotated by @Verifier should always return a boolean value. Depending on that value the transaction will be accepted (in case of true) or rejected (in case of false) by the blockchain.

          @Verifier(tx)
          func verifier() = {
            sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)
          }
          

          The Verifier function binds variable tx, which is an object with all fields of the current outgoing transaction.

          A maximum of one @Verifier() function can be defined in each dApp script.

          # @Callable annotation

          Functions with the @Callable annotation can be called (or invoked) from outside of the blockchain. To call a callable function you have to send InvokeScriptTransaction.

          @Callable(i)
          func giveAway(age: Int) = {
            ScriptResult(
              WriteSet([DataEntry("age", age)]),
              TransferSet([ScriptTransfer(i.caller, age, unit)])
            )
          }
          

          Every caller of giveAway function will receive as many WAVES as his age and the dApp will store information about the fact of the transfer in its state.

          Ride Programming Language
          Base concepts
          Ride Programming Language
          Base concepts