waves_logo Docs
  • Getting Started
    Getting Started
  • How-to Guides
    • Reading Blockchain Data
      Reading Blockchain Data
    • Creating & Broadcasting Transactions
      Creating & Broadcasting Transactions
    • Tokenisation
      Tokenisation
    • Airdrop
      Airdrop
    • Payments
      Payments
    • Buying & Selling Tokens
      Buying & Selling Tokens
    • Creating Crypto Trading Bot
      Creating Crypto Trading Bot
    • Simple Voting
      Simple Voting
    • Reading Band’s Price Data
      Reading Band’s Price Data
    How-to Guides
  • Waves Smart Contracts
    Waves Smart Contracts
  • 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
    • What is a Smart Asset
      What is a Smart Asset
    Smart Asset
  • dApp
    • Creating & Launching dApp
      Creating & Launching dApp
    dApp
  • Articles on Smart Contracts
    Articles on Smart Contracts
  • Tools
    • Waves IDE
      Waves IDE
    • Visual Studio Code Extension
      Visual Studio Code Extension
    • Surfboard
      Surfboard
    • Ride REPL
      Ride REPL
    Tools
  • API & SDK
    • Waves data service API
      Waves data service API
    • Waves Games
      • Waves Games API
        Waves Games API
      • Examples
        Examples
      Waves Games
    API & SDK
  • Client libraries
    • Signer
      Signer
    • PyWaves
      PyWaves
    • WavesJ
      WavesJ
    • WavesCS
      WavesCS
    • WavesC
      WavesC
    • GoWaves
      GoWaves
    • WavesRS
      WavesRS
    • Waves transactions
      Waves transactions
    • Community libraries
      Community libraries
    Client libraries
      • English
      • Русский
      On this page
        • Attaching an Account Script to an Account
        • Account Script Structure
        • Expression
        • Smart accounts and trading
        • Smart Account Script Examples
        • Smart Account Fees
      waves_logo Docs

          # What is Smart Account

          The functionality of a regular account only allows you to verify that the transaction released from it was actually sent from this account.

          Аttaching an account script to an account extends its functionality. It enables checking outgoing transactions for compliance with the conditions specified in the script. An account with a script attached to it is called a smart account. Only those transactions that have been validated can be sent from the smart account. For example, an account owner can set a rule according to which transactions can be sent from the address only if the blockchain height exceeds N. Another example — an account owner can allow sending only certain types of transactions. Or disable any validation other than the rule that all transactions sent from the address should be considered valid.

          The following parameters can be used for checks:

          • Transaction Signature.
          • Transaction proof.
          • The current blockchain height.
          • Arbitrary data existing in the blockchain, for example, oracle data.

          # Attaching an Account Script to an Account

          As we mentioned before, an account without a script validates transactions using the transaction validation mechanism. The operation of this mechanism is equivalent to the operation of the following script:

          sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPk)
          

          To attach your own script to an account, you need to send a set script transaction from it. Only one script can be attached to an account. “Detaching” a script from a smart account or replacing the old account script with a new one is possible unless the old script forbids it. To "detach" a script or replace it with a new one, you will need to send a new set script transaction. The transaction fee for setting the script is 0.01 WAVES.

          # Account Script Structure

          # Directive

          The directive should be placed at the very beginning of the script. Review the example directive:

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

          The given directive consists of three annotations and provides the compiler with the following information:

          • the script will use the third version of the library of standard functions
          • the type of script content is Expression
          • the script context will be the account context. In particular, this means that this variable type will be Address.

          If the directive is missing, then default annotations values will be used:

          • STDLIB_VERSION 2
          • CONTENT_TYPE EXPRESSION
          • SCRIPT_TYPE ACCOUNT

          # Expression

          The expression checks the transactions sent by the account for compliance with the specified conditions. If the conditions are not met, the transaction will not be sent. Possible results of the expression are

          • true (transaction is allowed)
          • false (transaction is not allowed)
          • error

          An account script may contain several expressions.

          # Smart accounts and trading

          Along with transactions, smart contracts allow to set rules (limitations) for the account trading operations. Examples of these rules are listed below.

          # Smart Account Script Examples

          # Buying or Selling only BTC

          An account with the script below can make sales transactions only in relation to BTC:

          let cooperPubKey = base58'BVqYXrapgJP9atQccdBPAgJPwHDKkh6A8'
          let BTCId = base58'8LQW8f7P5d5PZM7GtZEBgaqRPGSzS3DfPuiXrURJ4AJS'
          match tx {
             case o: Order =>
                sigVerify(o.bodyBytes, o.proofs[0], cooperPubKey ) && (o.assetPair.priceAsset == BTCId || o.assetPair.amountAsset == BTCId)
             case _ => sigVerify(tx.bodyBytes, tx.proofs[0], cooperPubKey )
          }
          

          # Purchase of a Certain Asset

          The script below allows making purchases from your account

          • only a given asset
          • for a given price only
          • only for WAVES
          let myAssetId = base58'8LQW8f7P5d5PZM7GtZEBgaqRPGSzS3DfPuiXrURJ4AJS'
          let cooperPubKey = base58'BVqYXrapgJP9atQccdBPAgJPwHDKkh6A8'
          match tx {
             case o: Order =>
                sigVerify(o.bodyBytes, o.proofs[0], cooperPubKey ) && o.assetPair.priceAsset == null && o.assetPair.amountAsset == myAssetId && o.price == 500000 && o.amount == 1000 && o.orderType == Buy
             case _ => sigVerify(tx.bodyBytes, tx.proofs[0], cooperPubKey )
          }
          

          # Smart Account Fees

          If the transaction is sent from a smart account, the transaction fee is increased by 0.004 WAVES. So if the transaction fee is 0.001 WAVES, the owner of the smart account will pay 0.001 + 0.004 = 0.005 WAVES.

          Waves Smart Contracts
          Creating smart account
          Waves Smart Contracts
          Creating smart account