After finishing the code in the True
branch of the condition, let’s focus on the False
branch of the condition.
This branch implies that the user information has not previously existed in the maps, so what we now have to do is to register the user by updating their information in the maps of users
and used_usernames
.
To add these values, we’ll be using in place insert operations that have the given format.
Where m
is the map variable, k
is the key variable and v
is the value variable. Insertion into nested maps is supported with the syntax m[k1][k2][...] := v
. If the intermediate key(s) does not exist in the nested maps, they are freshly created along with the map values they are associated with.
true
which will be equal to True
.let true = True
in the library section. See the Cheat sheet below for a quick refresher.False
branch in the transition. There, using the format described above, insert the value variable twitter_username
with the corresponding key variable user_address
in the map users
.true
with the corresponding key variable twitter_username
in the map used_usernames
.Show Solution
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
(* Start typing from the line below to define the variable ‘user_exists_code’ of type Uint32 and value ‘2’*)
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 =>
(* Start typing from the line below *)
end
end
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869