Chapter 3: Advanced

Lesson 4: Map – Existence of a value

In order to check whether or not the user id or wallet id being sent by a user have been previously registered, we’ll need to read the value from the map. In this particular case, we simply need to check whether such value exists or not in a given map.

This is also called In-place existence check. It check whether in map m, any value corresponding to key k exists or not and accordingly returns a Bool which in above example is stored in variable b.

Existence checks through nested maps is supported with the syntax v <- exists m[k1][k2][...]. If one or more of the intermediate key(s) do not exist in the corresponding map, the result is False.

1

b <- exists m[k]

We have a task for you!

In the transition register_user do the following two actions:

  • Check if the value of the variable user_address exists in the map users through the keyword exists, and then store the value in a new variable user_exists. This will all be done in a single line as shown in the example above.
  • Then, in the next line, check if the value of the variable twitter_username exists in the map used_usernames through the keyword exists, and then store the value in a new variable username_exists. This will all be done in a single line as shown in the example above.
  • 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

    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
    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)
    (* Start typing from the line below *)
    user_exists <- exists users[user_address];
    username_exists <- exists used_usernames[twitter_username]
    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
    
    
    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)
    
      (* Start typing from the line below *)
    
    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