Skip to content

Commit

Permalink
Implement quorum
Browse files Browse the repository at this point in the history
  • Loading branch information
pscott committed May 10, 2022
1 parent 339b15a commit 0503bfb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions contracts/starknet/lib/proposal.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from starkware.cairo.common.uint256 import Uint256

struct Proposal:
member execution_hash : Uint256
member quorum : Uint256
member start_timestamp : felt
member min_end_timestamp : felt
member max_end_timestamp : felt
Expand Down
29 changes: 28 additions & 1 deletion contracts/starknet/space.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from starkware.starknet.common.syscalls import get_caller_address, get_block_timestamp
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.uint256 import Uint256, uint256_add, uint256_lt
from starkware.cairo.common.uint256 import Uint256, uint256_add, uint256_lt, uint256_le
from starkware.cairo.common.bool import TRUE, FALSE
from starkware.cairo.common.hash_state import hash_init, hash_update
from starkware.cairo.common.math import (
assert_lt, assert_le, assert_nn, assert_not_zero, assert_lt_felt
Expand Down Expand Up @@ -35,6 +36,10 @@ end
func proposal_threshold() -> (threshold : Uint256):
end

@storage_var
func quorum() -> (value : Uint256):
end

@storage_var
func voting_strategies(index : felt) -> (voting_strategy_contract : felt):
end
Expand Down Expand Up @@ -273,6 +278,7 @@ func constructor{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_p
_max_voting_duration : felt,
_proposal_threshold : Uint256,
_controller : felt,
_quorum : felt,
_voting_strategies_len : felt,
_voting_strategies : felt*,
_authenticators_len : felt,
Expand Down Expand Up @@ -434,9 +440,12 @@ func propose{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr :
# Hash the execution params
let (hash) = hash_array(execution_params_len, execution_params)

let (_quorum) = quorum.read()

# Create the proposal and its proposal id
let proposal = Proposal(
execution_hash,
_quorum,
start_timestamp,
min_end_timestamp,
max_end_timestamp,
Expand Down Expand Up @@ -504,9 +513,27 @@ func finalize_proposal{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_c
# Count votes for
let (for) = vote_power.read(proposal_id, Choice.FOR)

# Count votes against
let (abstain) = vote_power.read(proposal_id, Choice.ABSTAIN)

# Count votes against
let (against) = vote_power.read(proposal_id, Choice.AGAINST)

let (partial_power, overflow1) = uint256_add(for, abstain)

let (total_power, overflow2) = uint256_add(partial_power, against)

let _quorum = proposal.quorum
let (is_lower_or_equal) = uint256_le(_quorum, total_power)

# If overflow1 or overflow2 happened, then quorum has necessarily been reached because `quorum` is by definition smaller or equal to Uint256::MAX.
# If `is_lower_or_equal` (meaning `_quorum` is smaller than `total_power`), then quorum has been reached (definition of quorum).
# So if `overflow1 || overflow2 || is_lower_or_equal`, we have reached quorum. If we sum them and find `0`, then they're all equal to 0, which means
# quorum has not been reached.
with_attr error_message("Quorum has not been reached"):
assert_not_zero(overflow1 + overflow2 + is_lower_or_equal)
end

# Set proposal outcome accordingly
let (has_passed) = uint256_lt(against, for)

Expand Down
2 changes: 2 additions & 0 deletions test/starknet/shared/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export async function vanillaSetup() {
_min_voting_duration: MIN_VOTING_DURATION,
_max_voting_duration: MAX_VOTING_DURATION,
_proposal_threshold: PROPOSAL_THRESHOLD,
_quorum: 0,
_controller: BigInt(account.starknetContract.address),
_voting_strategies: [voting_strategy],
_authenticators: [authenticator],
Expand Down Expand Up @@ -117,6 +118,7 @@ export async function ethTxAuthSetup(signer: SignerWithAddress) {
_min_voting_duration: MIN_VOTING_DURATION,
_max_voting_duration: MAX_VOTING_DURATION,
_proposal_threshold: PROPOSAL_THRESHOLD,
_quorum: 0,
_controller: 1,
_voting_strategies: [voting_strategy],
_authenticators: [authenticator],
Expand Down

0 comments on commit 0503bfb

Please sign in to comment.