Chapter 2: Intermediate

Lesson 8: Message – Success

While the last lesson may seem complex at first, the same format will be used again and again for sending messages, and you’ll get used to the syntax pretty soon.

In fact, we’ll be using the format once more in this lesson too. Now, that we’ve sent a message to the user for an error in the last lesson (telling the user that the sender wallet address wasn’t the same as the owner wallet address and therefore the deposit couldn’t be accepted,) we’ll also send a message for success.

So, this time we’ll focus on the true condition of the branch.

We have a task for you!

  • In the library section, declare a Uint32 type of variable with variable name accepted_code and value 0.
  • Then, in the True branch, we have to accept the money which has been sent. So use the accept keyword.
  • Now, in the next line 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 have 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 forst step of this task to indicate that sender is the owner, i.e.,accepted_code.
  • Finally, after you have defined msg, put this message in the list defined in the previous lesson (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

    34

    35

    36

    37

    38

    39

    40

    41

    42

    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
    (* Start typing from the line below. declare a Uint32 type of variable with variable name “accepted_code” and value “0” *)
    let accepted_code = Uint32 0
    contract SocialMediaPayment (owner: ByStr20)
    transition deposit()
    sender_is_owner = builtin eq _sender owner;
    match sender_is_owner with
    | False =>
    msg = { _tag: "";
    _recipient: _sender;
    _amount: zero;
    code: not_owner_code};
    msgs = one_msg msg;
    send msgs
    | True =>
    (* Start typing from the line below for task 2, 3 and 4*)
    accept;
    msg = { _tag: "";
    _recipient: _sender;
    _amount: zero;
    code: accepted_code};
    msgs = one_msg msg;
    send msgs
    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
    (* Start typing from the line below. declare a Uint32 type of variable with variable name “accepted_code” and value “0” *)
    
    
    
    contract SocialMediaPayment (owner: ByStr20)
    
    transition deposit()
      sender_is_owner = builtin eq _sender owner;
      match sender_is_owner with
      | False =>
    
        msg = { _tag: "";
                _recipient: _sender;
                _amount: zero;
                code: not_owner_code};
        msgs = one_msg msg;
        send msgs
    
      | True =>
        (* Start typing from the line below for task 2, 3 and 4*)
    
      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
    30
    31
    32
    33
    34
    35