waves_logo Docs
  • Overview
    Overview
  • How-to Guides
    • Reading Blockchain Data
      Reading Blockchain Data
    • Creating & Broadcasting Transactions
      Creating & Broadcasting Transactions
    • Tokenization
      Tokenization
    • Airdrop
      Airdrop
    • Payments
      Payments
    • Exchange Tokens
      Exchange Tokens
    • Simple Voting
      Simple Voting
    • List as argument
      List as argument
    How-to Guides
  • Waves Smart Contracts
    Waves Smart Contracts
  • dApp
    • Creating & Launching dApp
      Creating & Launching dApp
    dApp
  • Smart Account
    • Creating smart account
      Creating smart account
    • Creating and deploying a script manually
      Creating and deploying a script manually
    • Video tutorials
      • Introduction to the Waves blockchain, Waves Smart Accounts and Waves Smart Assets
        Introduction to the Waves blockchain, Waves Smart Accounts and Waves Smart Assets
      • Waves Smart Account with multisignature
        Waves Smart Account with multisignature
      • Waves Smart Account with escrow service
        Waves Smart Account with escrow service
      • Creating multisignature account via Waves IDE tools
        Creating multisignature account via Waves IDE tools
      • Creating multisignature account via Waves Client
        Creating multisignature account via Waves Client
      • Waves console explained
        Waves console explained
      Video tutorials
    Smart Account
  • Smart Asset
    Smart Asset
  • Developer Tools
    • Waves IDE
      Waves IDE
    • Visual Studio Code Extension
      Visual Studio Code Extension
    • Surfboard
      Surfboard
    • Ride REPL
      Ride REPL
    Developer Tools
  • Signer ◆
    Signer ◆
  • Waves API
    • Data Service API
      Data Service API
    • Node REST API
      Node REST API
    • Node gRPC Server
      Node gRPC Server
    • Blockchain Updates
      Blockchain Updates
    Waves API
  • Client Libraries
    • Waves C#
      • Install SDK
        Install SDK
      • Run Code Sample
        • Send Transactions
          Send Transactions
        • Use Crypto Utilities
          Use Crypto Utilities
        • Interact With Node
          Interact With Node
        • Set Up Smart Contracts
          Set Up Smart Contracts
        Run Code Sample
      Waves C#
    • Gowaves
      • Install SDK
        Install SDK
      Gowaves
    • WavesJ
      • Install SDK
        Install SDK
      WavesJ
    • Ts-lib-crypto
      • Install SDK
        Install SDK
      Ts-lib-crypto
    • Waves-PHP
      • Install SDK
        Install SDK
      Waves-PHP
    • Waves-python
      • Install SDK
        Install SDK
      Waves-python
    • Waves-rust
      • Install SDK
        Install SDK
      Waves-rust
    Client Libraries
      • English
      • Русский
      On this page
        • Structure of dApp Script
        • Data Accessible by dApp
        • Assigning dApp Script to Account
        • Limitations
        • Examples
        • Waves Tech Blog Articles
      waves_logo Docs

          # What is dApp

          dApp is Waves account assigned with dApp script.

          dApp script is a Ride script that contains callable functions that can be called externally by the invoke script transaction.

          An invoke script transaction contains:

          • dApp address;
          • name of the callable function and the argument values;
          • in addition, an invoke script transaction can contain payments to be credited to the dApp balance.

          Invoke script transaction example

          You can use callable functions to:

          • Add, modify or delete dApp account data storage entries.
          • Transfer, issue, reissue, burn tokens.
          • Setup sponsorship.

          Available script actions depend on Standard library version used.

          # Structure of dApp Script

          dApp script comprises one or more callable functions.

          In addition, dApp script can comprise a verifier function that checks transactions and orders that are sent from dApp account.

          # Directives

          Each Ride script should start with directives. Here is the set of directives for the dApp script:

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

          These directives tell the compiler that:

          • The script uses Standard library version 8.
          • Type of the script is dApp.
          • The script will be assigned to an account (not asset).

          # Script Context

          Script context includes built-in variables and built-in functions. In addition, user variables and functions could be declared between directives and callable function. These variables and functions are accessible within the entire script.

          Example:

          let someConstant = 42
          func doSomething() = {
              1+1
          }
          

          # Callable Functions

          Callable function can be called externally by the invoke script transaction. The callable function should be marked with the @Callable(i) annotation, where i is an Invocation structure that contains invoke script transaction fields that are available to the callable function.

          Callable function result is a set of script actions that are performed on the blockchain: adding entries to the account data storages, token transfers and others. The result format and the possible actions depend on the Standard library version used.

          For a detailed description, see the Callable Function article.

          In the example below the callable function transfers 1 WAVES to an account that called it and records the request information in the account data storage. If the same account tries to call the function again, the callable function does nothing.

          @Callable(i)
          func faucet () = {
              let isKnownCaller =  match getBoolean(this, toBase58String(i.caller.bytes)) {
                  case hist: Boolean =>
                      hist
                  case _ =>
                      false
              }
              if (!isKnownCaller) then 
                  (
                     [
                         BooleanEntry(toBase58String(i.caller.bytes), true),
                         ScriptTransfer(i.caller, 100000000, unit)
                     ],
                     unit
                  )
              else ([],unit)
          }
          

          # Verifier Function

          Verifier function checks transactions and orders that are sent from dApp account (in other words it does the same as the account script). The verifier function should be marked with the @Verifier(tx) annotation, where tx is the transaction or the order that that the function is currently checking.

          For a detailed description, see the Verifier Function article.

          In the example below the verifier function allows transfer transactions and denies orders and other types of transactions. The match operator is used to specify verification rules depending on the type of transaction (or order).

          @Verifier(tx)
          func verify() = {
              match tx {
                  case ttx:TransferTransaction => sigVerify(ttx.bodyBytes, ttx.proofs[0], ttx.senderPublicKey)
                  case _ => false
              }
          }
          

          dApp that has no verifier function performs default verification, that is, checking that the transaction or the order is indeed signed by this account.

          # Data Accessible by dApp

          dApps can read the following blockchain data:

          • Entries in account data storages (both dApp's account and any other account).
          • Balances of accounts.
          • Parameters of assets.
          • Blockchain height.
          • Headers of blocks.
          • Transfer transactions (by transaction ID).

          Appropriate fuctions are described in the Account Data Storage Functions and Blockchain Functions articles.

          Furthermore:

          • The callable function has access to some fields of the transaction that called the dApp script. See the Invocation article for the fields description.
          • The verifier function has access to the fields of the outgoing transaction or order, including proofs.

          # Assigning dApp Script to Account

          To assign dApp script to an account, you have to send a Set Script transaction from this account.

          There are the following options to send the transaction:

          • In Waves IDE create or import an account, open the dApp script and click Deploy.
          • Using client libraries. See some examples of sending a transaction in the Creating & Broadcasting Transactions article.

          Set script transaction example

          The fee for the Set Script transaction is 0.01 WAVES.

          If the script contains a verifier function and the complexity of the function exceeds the sender complexity threshold, the minimum fee for each transaction sent from the dApp account is increased by 0.004 WAVES.

          # Limitations

          Limitations on the size, complexity of the script, as well as on functions and variables are given in the Limitations article.

          # Examples

          Find dApp script examples:

          • In the How-to Guides chapter.
          • In Waves IDE in the Library menu.
          • In Github repository ride-examples .

          For tutorial on creating dApp, see the Creating & Launching dApp article.

          # Waves Tech Blog Articles

          • Why use the Waves protocol for DeFi applications? (Mar 25, 2021)
          • 5 things I wish I’d known before starting to develop dApps (Jun 17, 2020)
          • How to build a dApp for team motivation (Jun 9, 2020)
          • How to avoid common mistakes in dApp development (May 8, 2020)
          • What are Smart Contracts and how to use them in your app (Mar 19, 2020)
          Waves Smart Contracts
          Creating & Launching dApp
          Waves Smart Contracts
          Creating & Launching dApp