Chapter 1: Elementary

Lesson 4: Transition

The smart contract code on blockchain needs to be able to interact with the external world for the contract to be actually useful. It needs some interfaces so that the commands, data or tokens can be sent to the smart contract or be requested from it.

This is achieved by having transitions in the smart contract. Transitions are similar to functions or methods in other languages.

A transition is declared using the keyword transition. The end of a transition scope is declared using the keyword end. The transition keyword is followed by the transition name.

Then follow the input parameters within (). Each input parameter is separated by a comma.

We take a look at a simple setHello transition that takes a msg argument of type String.

In order to use a smart contract, a transition within the smart contract will have to be called. It can be called directly or by another program or smart contract.

We have a task for you!

So far, we have a fixed value for our username variable, which is "Alice". Let’s have an option that the name could be changed. To change a mutable variable, we’ll need a transition.

Declare a transition with the name changeName. You don’t need to pass any variables to it right now, so the brackets after the name will be empty.

There will be no code in the body of the transition at the moment. We’ll fill it in later. For now, just declare an empty transition named changeName, and close it by using the keyword end.

Your Workspace

Show Solution

Solution

1

2

3

4

5

6

7

8

scilla_version 0
contract SocialMediaPayment (owner: ByStr20)
field username: String = "Alice"
(* Start typing from the line below *)
transition changeName()
end
scilla_version 0

contract SocialMediaPayment (owner: ByStr20)
field username: String = "Alice"

(* Start typing from the line below *)
1
2
3
4
5
6