Quadratic Voting in Scilla

Quadratic Voting vs Traditional Voting
Total credits needed for user = (Number of votes)^2
contract QVote 
(
owner: ByStr20,
expiration_block: BNum, (*last block at which votes are accepted*)
name: String, (* decision name *)
description: String
)
let register_address_list: List ByStr20 -> List Uint32 -> Map ByStr20 Uint32 = 
fun(addresses: List ByStr20) =>
fun(balances: List Uint32) =>
let init = Emp ByStr20 Uint32 in
let zip = @list_zip ByStr20 Uint32 in
let zipped = zip addresses balances in
let foldleft = @list_foldl (Pair ByStr20 Uint32) (Map ByStr20 Uint32) in
let f = @fst ByStr20 Uint32 in
let s = @snd ByStr20 Uint32 in
let insert =
fun(acc : Map ByStr20 Uint32) =>
fun(address_credits_p : Pair ByStr20 Uint32) =>
let address = f address_credits_p in
let balance = s address_credits_p in
builtin put acc address balance
in foldleft insert init zipped
contract QVote
(
owner: ByStr20,
expiration_block: BNum, (*last block at which votes are accepted*)
name: String, (* decision name *)
description: String
)
field voter_balances : Map ByStr20 Uint32 = Emp ByStr20 Uint32field owner_register_called : Bool = False(* Only owner can call this transition ONCE before expiration_block *)
transition owner_register(addresses : List ByStr20, credits : List Uint32)
blk <- & BLOCKNUMBER;
in_time = check_valid_times blk expiration_block;
match in_time with
| True =>
is_owner = builtin eq _sender owner;
match is_owner with
| False =>
e = CodeSenderIsNotOwner;
EmitError e
| True =>
(* to ensure addresses are registered only once *)
is_called <- owner_register_called;
match is_called with
| True =>
e = CodeTransitionOwnerRegisterMutipleCall;
EmitError e
| False =>
voter_balances_ = register_address_list addresses credits;
voter_balances := voter_balances_;
owner_register_called := bool_true;
e = owner_register_event_success;
event e
end
end
| False =>
e = CodeNotInTime;
EmitError e
end
end
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
contract QVote
(
owner: ByStr20,
expiration_block: BNum, (* last block at which votes are accepted *)
name: String, (* decision name *)
description: String
)
field votes : Map String Uint128 = Emp String Uint128(*
@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 <- voter_balances[_sender];
match voter_balance with
| Some voter_b =>
(* voter balance to uint128 *)
voter_balance_uint128 = uint32_to_uint128 voter_b;
voter_votes_uint128 = uint128_credit_to_vote voter_balance_uint128;

(* 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;
(* can vote only once *)
voter_balances[_sender] := zero;
e = vote_success_event;
event e
| None =>
e = CodeSenderNotRegisteredToVote;
EmitError e
end
end
end

--

--

--

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

How to Line Up Numbers on Decimal Points in MS Excel

Mycroft AI with remote microphone

PWNABLE.KR BOF WALKTHROUGH

OCP4.3 installation on RHV 4.3 with Bare-metal method

A step by step guide to the Counting Sort algorithm

anik/dataset — A PHP package to export DB tables data to CSV, import CSV data to DB tables.

Let’s Bulky WordPress Project Run On a Cloud With Galera, Nginx, Redis, HaProxy, andVarnish

Wordcloud Web App In 40 Lines of R

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

Ayurcann DD Overview (CSE: AYUR)

VIRTUAL AGENT DEVELOPMENT USING DIALOGFLOW

Baseball In a Pool?

Hackathon In Review: BIRS CIH Hackathon