Chapter 3: Advanced

Lesson 3: Map – Declaration

Now, we want to limit the possibility of a same account creating multiple registrations.

In order to do so, we want to impose two conditions:

  • the same wallet id shouldn't be able to register twice.
  • the same twitter id shouldn’t be able to register twice.
  • In order to impose these conditions, we’ll first have to create two records, one of already registered wallet ids and second of already registered twitter ids.

    We’ll do so using map feature.

    A map of type map kt vt provides a key-value store where kt is the type of keys and vt is the type of values. kt may be any one of String, IntX, UintX, ByStrX or ByStr. vt may be any type except a function type.

    We often provide the key-value kt as an input and look for the vt as the output value. For eg, kt could be a bank account number and vt could be the corresponding bank balance.

    The declaration for an empty map is done as follows at the start of the contract.

    Then the values are added to that empty map. (You can also check the other ways of declaring map in the Cheat Sheet.)

    We have a task for you!

    After the start of the contract, declare two maps with no initial values:

  • A map with name users with key type ByStr20 and value type String.
  • Then declare another map with name used_usernames with key type String and value type Bool.
  • 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

    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)
    (* Start typing from the line below *)
    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)
    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)
    
    
    (* Start typing from the line below *)
    
    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)
    
    
    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