Skip to content

Commit

Permalink
Proposal submission mock (#14)
Browse files Browse the repository at this point in the history
* added propose functionality to mock contract

* Update README.md
  • Loading branch information
Orland0x authored Feb 2, 2022
1 parent c70c649 commit 474511a
Show file tree
Hide file tree
Showing 5 changed files with 12,470 additions and 9,143 deletions.
Binary file modified VotingDemo/.yarn/install-state.gz
Binary file not shown.
3 changes: 3 additions & 0 deletions VotingDemo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VotingDemo deployed at: 0x0180abab6c7c3983e886bd4f8ca6090e068cf10f14be6ae9919e7b0c654d28c1

Example propose, vote, and calls in the scripts/vote.js
84 changes: 71 additions & 13 deletions VotingDemo/contracts/starknet/VotingDemo.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,35 @@

from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.math import assert_nn
from starkware.cairo.common.hash import hash2
from starkware.starknet.common.syscalls import get_caller_address

#No signature verification or double voting prevention


#stores owner of contract
@storage_var
func owner_account_store() -> (key : felt):
func owner_store() -> (key : felt):
end
#mapping stores 1 if the proposal has been initialized, otherwise 0
@storage_var
func proposal_id_store(proposal_id : felt) -> (bool : felt):
end
#double mapping that stores a counter for each vote type (1,2,3) for every proposal
@storage_var
func choices_store(proposal_id : felt, choice : felt) -> (num : felt):
end
#event emitted after each proposal is created
@event
func vote_received(proposal_id : felt, address : felt, choice : felt):
func proposal_created(proposal_id : felt, proposer_address : felt):
end
#event emitted after ech vote is received
@event
func vote_received(proposal_id : felt, voter_address : felt, choice : felt):
end
@constructor
Expand All @@ -23,9 +40,9 @@ func constructor{
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(
owner_account : felt
owner : felt
):
owner_account_store.write(owner_account)
owner_store.write(owner)
return ()
end

Expand All @@ -35,10 +52,10 @@ func get_owner{
pedersen_ptr : HashBuiltin*,
range_check_ptr
}() -> (
owner_account : felt
owner : felt
):
let (owner_account) = owner_account_store.read()
return (owner_account)
let (owner) = owner_store.read()
return (owner)
end

@external
Expand All @@ -47,16 +64,41 @@ func change_owner{
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(
new_owner_account : felt
new_owner : felt
):
let (caller_account) = get_caller_address()
let (owner_account) = owner_account_store.read()
assert caller_account = owner_account
owner_account_store.write(new_owner_account)
let (caller) = get_caller_address()
let (owner) = owner_store.read()
assert caller = owner
owner_store.write(new_owner)

return ()
end

@external
func propose{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(
execution_hash : felt,
metadata_hash : felt
):

#the proposal id is the hash of the execution_hash and the metadata_hash
let (proposal_id) = hash2{hash_ptr=pedersen_ptr}(execution_hash, metadata_hash)
let (init) = proposal_id_store.read(proposal_id)
#check that the proposal has not already been initialized
assert init = 0
#initialize proposal
proposal_id_store.write(proposal_id, 1)

let (caller) = get_caller_address()
#emit proposal creation event
proposal_created.emit(proposal_id, caller)
return ()
end


@external
func vote{
syscall_ptr : felt*,
Expand All @@ -81,6 +123,21 @@ func vote{
return ()
end

@view
func get_proposal_id{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(
execution_hash : felt,
metadata_hash : felt
) -> (
proposal_id : felt
):
let (proposal_id) = hash2{hash_ptr=pedersen_ptr}(execution_hash, metadata_hash)
return (proposal_id)
end

@view
func get_num_choice{
syscall_ptr : felt*,
Expand All @@ -94,4 +151,5 @@ func get_num_choice{
):
let (num_choice) = choices_store.read(proposal_id, choice)
return (num_choice)
end

end
47 changes: 34 additions & 13 deletions VotingDemo/scripts/vote.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,68 @@ const { getSelectorFromName } = stark;
async function main() {

const VOTING_CONTRACT_ADDRESS =
"0x0414130848ed5100d0c3bb0dafc3cf2a6de63b4ea49862ee5c38ccec2145cbda";
"0x0180abab6c7c3983e886bd4f8ca6090e068cf10f14be6ae9919e7b0c654d28c1";


//The owner of the voting contract
const ACCOUNT_CONTRACT_ADDRESS =
"0x069053b14d69a52aebc20833452df0da83ed20a43396ae7bb922b6eeba56c9de";


//create proposal, emits event containing proposal id and proposer address
var addTokenResponse = await defaultProvider.addTransaction({
type: "INVOKE_FUNCTION",
contract_address: VOTING_CONTRACT_ADDRESS,
entry_point_selector: getSelectorFromName("propose"),
calldata: ["1234", "5678"],
});
console.log(addTokenResponse);



//cast vote, emits an event containing vote info
// const addTokenResponse = await defaultProvider.addTransaction({
// type: "INVOKE_FUNCTION",
// contract_address: VOTING_CONTRACT_ADDRESS,
// entry_point_selector: getSelectorFromName("vote"),
// calldata: ["0", "1234", "2", "5678"],
// });
// console.log(addTokenResponse);
var addTokenResponse = await defaultProvider.addTransaction({
type: "INVOKE_FUNCTION",
contract_address: VOTING_CONTRACT_ADDRESS,
entry_point_selector: getSelectorFromName("vote"),
calldata: ["60265779274270434537323828408977526710079971372926889746684550312238229785", "33333", "2", "7777"],
});
console.log(addTokenResponse);


//Gets the number of votes for each of the 3 choices:

//get proposal id so that you can query the contract for vote info
var out = await defaultProvider.callContract({
contract_address: VOTING_CONTRACT_ADDRESS,
entry_point_selector: getSelectorFromName("get_proposal_id"),
calldata: ["1234", "5678"],
});
console.log(out.result[0])



//Gets the number of votes for each of the 3 choices for the given proposal id:
var out = await defaultProvider.callContract({
contract_address: VOTING_CONTRACT_ADDRESS,
entry_point_selector: getSelectorFromName("get_num_choice"),
calldata: ["0", "1"],
calldata: ["60265779274270434537323828408977526710079971372926889746684550312238229785", "1"],
});
console.log(out.result[0])

var out = await defaultProvider.callContract({
contract_address: VOTING_CONTRACT_ADDRESS,
entry_point_selector: getSelectorFromName("get_num_choice"),
calldata: ["0", "2"],
calldata: ["60265779274270434537323828408977526710079971372926889746684550312238229785", "2"],
});
console.log(out.result[0])

var out = await defaultProvider.callContract({
contract_address: VOTING_CONTRACT_ADDRESS,
entry_point_selector: getSelectorFromName("get_num_choice"),
calldata: ["0", "3"],
calldata: ["60265779274270434537323828408977526710079971372926889746684550312238229785", "3"],
});
console.log(out.result[0])


}

main()
Expand Down
Loading

0 comments on commit 474511a

Please sign in to comment.