Chapter 3: Advanced

Lesson 7: Messages for the True condition

In case already_exists variable is true, then we simply need to let the users know about that in a message.

We’ll be sending that message in the same way as we did in the second chapter.

We have a task for you!

  • First of all, we need to define the code for such an eventuality in the library. So, in the library section define a variable user_exists_code of Uint32 type and value 2.
  • In the True branch of the conditions, declare a variable msg with the following values:
  • For _tag, 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 0 variable in the library already, use the variable name zero.
  • For the _code, use the code that we have defined in the library to indicate that the user already exists, i.e., user_exists_code.
  • Finally, after you have defined msg, put this message in the list defined previously (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

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    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
    let accepted_code = Uint32 0
    (* Start typing from the line below to define the variable ‘user_exists_code’ of type Uint32 and value ‘2’*)
    let user_exists_code = Uint32 2
    contract SocialMediaPayment (owner: ByStr20)
    field users: Map ByStr20 String
    = Emp ByStr20 String
    field used_usernames: Map String Bool
    = Emp String Bool
    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 =>
    accept;
    msg = { _tag: "";
    _recipient: _sender;
    _amount: zero;
    code: accepted_code};
    msgs = one_msg msg;
    send msgs
    end
    end
    transition register_user (user_address: ByStr20, twitter_username: String)
    user_exists <- exists users[user_address];
    username_exists <- exists used_usernames[twitter_username]
    already_exists = orb user_exists username_exists
    match already_exists with
    | True =>
    (* Start typing from the line below *)
    msg = {_tag: ""; _recipient: _sender; _amount: zero; code: user_exists_code};
    msgs = one_msg msg;
    send msgs
    | False =>
    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
    let accepted_code = Uint32 0
    
    (* Start typing from the line below to define the variable ‘user_exists_code’ of type Uint32 and value ‘2’*)
    
    contract SocialMediaPayment (owner: ByStr20)
    
    
    field users: Map ByStr20 String
      = Emp ByStr20 String
    
    field used_usernames: Map String Bool
      = Emp String Bool
    
    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 =>
        accept;
        msg = { _tag: "";
                _recipient: _sender;
                _amount: zero;
                code: accepted_code};
        msgs = one_msg msg;
        send msgs
    
      end
    end
    
    transition register_user (user_address: ByStr20, twitter_username: String)
    
      user_exists <- exists users[user_address];
      username_exists <- exists used_usernames[twitter_username]
      already_exists = orb user_exists username_exists
    
      match already_exists with
      | True =>
    
        (* Start typing from the line below *)
    
      | False =>
    
      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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64