Chapter 1: Elementary

Lesson 8: Implicit variables

In addition to parameters that are explicitly declared in the definition, each transition also has the following implicit parameters available to it for each call.

The _sender variable contains the account address that triggered this transition. In case, the transition was called by a contract account instead of a user account, then _sender contains the contract address.

The _amount contains the incoming amount ZILs sent by the sender. This amount must be explicitly accepted using the accept statement within the transition. The money transfer does not happen if the transition does not execute accept.

We have a task for you!

Declare two new mutable variables in the contract:

  • user_address of type ByStr20 with value 0x1234567890123456789012345678901234567890
  • user_tokens of type Uint128 with value 0 in the contract.
  • Then in the transition, assign these variables the value of _sender and _amount respectively.

    Remember that the implicit variable _amount has to be explicitly accepted. Use a simple accept command in the first line of the transition changeName.

    Your Workspace

    Show Solution

    Solution

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    scilla_version 0
    contract SocialMediaPayment (owner: ByStr20)
    field username: String = "Alice"
    field user_address: ByStr20 = 0x1234567890123456789012345678901234567890
    field user_tokens: Uint128 = Uint128 0
    transition changeName(newname: String)
    username := newname;
    (*Use ‘accept’ command in the line below which will accept the amount sent to this transition.*)
    accept;
    (*Assign the value of the implicit variables to the new mutable variables in the lines below. You’ll need to use the semicolons to separate the lines in the transition*)
    user_address := _sender;
    user_tokens := _amount
    end
    scilla_version 0
    
    contract SocialMediaPayment (owner: ByStr20)
    field username: String = "Alice"
    (*Declare the two new mutable variables below. You don’t need to use any semicolons to separate the lines outside the transitions*)
    
    transition changeName(newname: String)
      username := newname
      (*Use ‘accept’ command in the line below which will accept the amount sent to this transition.*)
    
      (*Assign the value of the implicit variables to the new mutable variables in the lines below. You’ll need to use the semicolons to separate the lines in the transition*)
    
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13