Chapter 2: Intermediate

Lesson 7: Message – Error

Now that we have defined a code that implies that the sender to deposit transition is not the owner of the smart contract, we’ll have to send that code as a response. We’ll do this through sending a message using the instruction send.

Send is used to send messages to other accounts, either in order to invoke transitions on another smart contract, or to transfer money to user accounts. To construct a message we use the given syntax.

A message must contain the compulsory fields _tag,_recipient and _amount. The _recipient field is the blockchain address (of type ByStr20) that the message is to be sent to, and the _amount field is the number of ZIL to be transferred to that account. The value of the _tag field is the name of the transition (of type String) that is to be invoked on the _recipient contract. If _recipient is a user account, then the value of _tag can be set to be "" (the empty string). In fact, if the _recipient is a user account, then the value of _tag is ignored.

In addition to the compulsory fields the message may contain other fields, such as code above. However, if the message recipient is a contract, the additional fields must have the same names and types as the parameters of the transition being invoked on the recipient contract.

Sending a message is done using the send instruction, which takes a list of messages as a parameter. Since we currently only send one message at a time, we use the library function one_msg from the previous lesson.

To send out a message, we first construct the message, insert it into a list, and send it.

The details of this include understanding the list functionality for Scilla. In our program, it will only be used in this one instance, so rather than going in the details, we’ll simply be using the above format. However, for more details, please feel free to read it at the list section of the scilla documentation.

We have a task for you!

  • In the False branch of the conditions, declare a variable msg with the following values:
  • For _tag, the value should be empty, i.e., "".
  • For the _recipient, we want to send the message to the initial sender, so the value should be: _sender.
  • For the _amount, the value should be zero. Since we’ve defined it as a Uint128 variable in the library already, use that variable named zero.
  • For the _code, use the code that we have defined in the library to indicate that sender is not the owner, i.e.,not_owner_code.
  • Finally, after you have defined msg, put this message in the list defined in the step one (i.e. one_msg variable type) and then send it.
  • Your Workspace

    Show Solution

    Solution

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    scilla_version 0
    import BoolUtils
    library SocialMediaPayment
    let one_msg =
    fun (msg: Message) =>
    let nil_msg = Nil {Message} in
    Cons {Message} msg nil_msg
    let zero = Uint128 0
    let not_owner_code = Uint32 1
    contract SocialMediaPayment (owner: ByStr20)
    transition deposit()
    sender_is_owner = builtin eq _sender owner;
    match sender_is_owner with
    | False =>
    (* Start typing from the line below. Define the variable msg below with the values given in the task*)
    msg = { _tag: "";
    _recipient: _sender;
    _amount: zero;
    code: not_owner_code};
    (* Start typing from the line below. Put the message a list using one_msg and send it. *)
    msgs = one_msg msg;
    send msgs
    | True =>
    end
    end
    scilla_version 0
    import BoolUtils
    
    library SocialMediaPayment
    
    let one_msg = 
      fun (msg: Message) => 
        let nil_msg = Nil {Message} in
        Cons {Message} msg nil_msg
    
    let zero = Uint128 0
    let not_owner_code = Uint32 1
    
    contract SocialMediaPayment (owner: ByStr20)
    
    transition deposit()
      sender_is_owner = builtin eq _sender owner;
      match sender_is_owner with
      | False =>
        (* Start typing from the line below. Define the variable msg below with the values given in the second point of the task*)
    
    
        (* Start typing from the line below. Copy the two lines given in the third point of the task below. *)
    
    
      | True =>
    
      end
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29