Chapter 2: Intermediate

Lesson 5: Library

Now, we have two conditional branches.

For each of them, a different behaviour will be programmed. Also, once the programmed action is finished, we’d want to generate some kind of a message regarding that execution.

For the branch, True, which implies that sender and owner addresses are the same, we will use the accept keyword, to accept the payment sent.

But when the condition yields False, we don’t want to accept the payment, but we still want to have some action to signal that the execution of the program has finished.

For doing this, we’ll be sending a message to the sender.

We’ll learn more details about how to send such messages in the coming lessons, but for now let’s first define a code that indicates that the sender is not the owner.

We can define this code in the library section of our code.

A library is declared in the preamble of a contract using the keyword library followed by the name of the library. In our current example a library declaration would look as follows:

This library will be used to store program constants (with a scope that covers the entire contract) and also some utility functions using the let x = y in expr construct. We’ll discuss utility functions later.

You can see the details of syntax in the Cheat Sheet in the Task section below.

For now, we need to define a program constant (or what we’ll later refer to as code) for the error message that says that the sender is not the owner.

We have a task for you!

  • Declare the library before the contract, and give it the same name as that of the contract, i.e., SocialMediaPayment.
  • In the library section, declare a Uint32 type of variable with variable name not_owner_code and value 1.
  • For the coming lessons, we’ll also need to define a code for the unsigned integer value 0 . In the library section, define a Uint128 type of variable with variable name zero and value 0.
  • 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

    scilla_version 0
    import BoolUtils
    (* Start typing from the line below. First define the library *)
    library SocialMediaPayment
    (* Now define the variable zero for Uint128 0 *)
    let zero = Uint128 0
    (* Now, in the line below, define the variable not_owner_code of type Uint32 and value 1 *)
    let not_owner_code = Uint32 1
    contract SocialMediaPayment (owner: ByStr20)
    transition deposit()
    sender_is_owner = builtin eq _sender owner;
    match sender_is_owner with
    | False =>
    | True =>
    end
    end
    scilla_version 0
    import BoolUtils
    (* Start typing from the line below. First define the library *)
    
    (* Now define the variable zero for Uint128 0 *)
    
    
    (* Now, in the line below, define the variable not_owner_code of type Uint32 and value 1 *)
    
    
    contract SocialMediaPayment (owner: ByStr20)
    
    transition deposit()
      sender_is_owner = builtin eq _sender owner;
      match sender_is_owner with
      | False =>
    
      | True =>
    
      end
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21