Chapter 3: Advanced

Lesson 9: Event

So far, we have primarily used messages to send notifications for the users. However, there is another way of sending information back.

A contract can also communicate to the outside world by emitting events.

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. On the other hand, events are dispatched signals that smart contracts can use to transmit data to client applications.

An event is a signal that gets stored on the blockchain for everyone to see. If a user uses a client application to invoke a transition on a contract, the client application can listen for events that the contract may emit, and alert the user.

event e: Emits a message e as an event. The given code emits an event with name e_name.

An emitted event must contain the compulsory field _eventname (of type String ), and may contain other entries as well. The value of the _eventname entry must be a string literal. All events with the same name must have the same entry names and types.

For the registration confirmation, we will be using an event.

We have a task for you!

  • After the previous step of updating the map entries, define an event e with _eventname equal to register_user.
  • For the second entry of the event, send the information about field user containing variable user_address.

  • Hint: the <entry>_2 from the format above will look like user: user_address in this case.

  • For the third entry of the event, send the information about field username containing variable twitter_username.
  • Finally, in the next line, emit the event using event e.
  • 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

    70

    71

    72

    73

    74

    75

    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
    let user_exists_code = Uint32 2
    let true = True
    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 =>
    msg = {_tag: ""; _recipient: _sender; _amount: zero; code: user_exists_code};
    msgs = one_msg msg;
    send msgs
    | False =>
    users[user_address] := twitter_username;
    used_usernames[twitter_username] := true
    (* Start typing from the line below *)
    e = {_eventname: "register_user";
    user: user_address;
    username: twitter_username };
    event e
    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
    let user_exists_code = Uint32 2
    let true = True
    
    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 =>
    
        msg = {_tag: ""; _recipient: _sender; _amount: zero; code: user_exists_code};
        msgs = one_msg msg;
        send msgs
    
      | False =>
    
        users[user_address] := twitter_username;
        used_usernames[twitter_username] := true
    
        (* Start typing from the line below *)
    
      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
    65
    66
    67
    68
    69
    70