Remote State Fetch in Scilla

x <- & c.f : Remote fetch. Fetch the value of the contract field f at address c, and store it into the local variable x. Note that the type of c must be an address type containing the field f.
ByStr20 with contract field f1 : t1, field f2 : t2, ... end: A ByStr20 which, when interpreted as a network address, refers to the address of a contract containing the mutable fields f1 of type t1, f2 of type t2, and so on. The contract in question may define more fields than the ones specified in the type, but the fields specified in the type must be defined in the contract.
scilla_version 0import BoolUtils IntUtils ListUtils PairUtilslibrary QVotelet zero = Uint32 0 
let uint128_zero = Uint128 0
let one = Uint32 1
let bool_true = True

let pos_uint128_sqrt: Uint128 -> Uint128 =
fun(i: Uint128) =>
let uint = builtin to_uint128 i in
match uint with
| Some c =>
let sqrt = builtin isqrt c in
let uint = builtin to_uint128 sqrt in
match uint with
| Some cast_int =>
cast_int
| None =>
Uint128 0
end
| None =>
Uint128 0
end
let uint128_credit_to_vote: Uint128 -> Uint128 =
fun(i : Uint128) =>
pos_uint128_sqrt i

let uint32_to_uint128: Uint32 -> Uint128 =
fun(i : Uint32) =>
let res = builtin to_uint128 i in
match res with
| Some c => c
| None => Uint128 0
end
let check_valid_times =
fun(current: BNum) =>
fun(decision: BNum) =>
builtin blt current decision

let get_val =
fun (some_val: Option Uint128) =>
match some_val with
| Some val => val
| None => uint128_zero
end
(* registration codes *)
let owner_register_success_code = Uint32 1
(* vote codes *)
let vote_success_code = Uint32 2
(* events *)
let vote_success_event = {_eventname: "vote_success"; code: vote_success_code}
let owner_register_event_success = {_eventname: "owner_register_success"; code: owner_register_success_code}
(* Error events *)
type Error =
| CodeSenderIsNotOwner
| CodeTransitionOwnerRegisterMutipleCall
| CodeNotInTime
| CodeSenderNotRegisteredToVote
(* Library functions *)
let make_error_event =
fun (result : Error) =>
let result_code =
match result with
| CodeSenderIsNotOwner => Int32 -1
| CodeTransitionOwnerRegisterMutipleCall => Int32 -2
| CodeNotInTime => Int32 -3
| CodeSenderNotRegisteredToVote => Int32 -4
end
in
{ _exception : "Error"; code : result_code }
contract QVote
(
owner: ByStr20,
expiration_block: BNum, (* last block at which votes are accepted *)
name: String, (* decision name *)
description: String,
token: ByStr20 with contract field balances: Map ByStr20 Uint128 end
)
field votes : Map String Uint128 = Emp String Uint128(* Emit Errors *)
procedure EmitError(err : Error)
e = make_error_event err;
throw e
end
(*
@notice: vote on the decision
@param: option: Option to vote for
*)
transition vote(option: String)
blk <- & BLOCKNUMBER;
in_time = check_valid_times blk expiration_block;
match in_time with
| False =>
e = CodeNotInTime;
EmitError e
| True =>
(* get voter balance *)
voter_balance <- & token.balances[_sender];
match voter_balance with
| Some voter_b =>
(* voter balance to uint128 *)
voter_votes_uint128 = uint128_credit_to_vote voter_b;

(* do voting stuff *)
some_prev_vote <- votes[option];
prev_vote = get_val some_prev_vote;
new_vote = builtin add prev_vote voter_votes_uint128;
votes[option] := new_vote;

e = vote_success_event;
event e
| None =>
e = CodeSenderNotRegisteredToVote;
EmitError e
end
end
end
token: ByStr20 with contract field balances: Map ByStr20 Uint128 end
voter_balance <- & token.balances[_sender];

--

--

--

A blockchain dev | Zilliqa Developer Ambassador | RedChillies Labs, Inc. | Pastel Soft | JS Security Technologies | AART

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

8 Agile Testing Best Practices That Will Help To Boost Efficiency And Productivity Of Your Team

I’m a manager, why should I care about agile software development?

Routing Protocol

AI on AWS by Vimal Daga Sir

Golang: pointers — detailed overview

Pinky’s Palace: v3 | Vulnhub Walkthrough

Testing Flutter Applications

Our Top 5 Workplace Productivity Apps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bibek Koirala

Bibek Koirala

A blockchain dev | Zilliqa Developer Ambassador | RedChillies Labs, Inc. | Pastel Soft | JS Security Technologies | AART

More from Medium

Smart Contracts on TON

Deploying Blockscout Explorer for Etho Protocol

How to set up a private Ethereum Blockchain Network, based on Proof of Authority consensus…

Are “Smart Contracts” Contracts?