This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
instruction_api.rs
172 lines (157 loc) · 5.29 KB
/
instruction_api.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use borsh::{BorshDeserialize, BorshSerialize};
use anchor_lang::prelude::*;
use solana_program::instruction::Instruction;
#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub enum VotingInstruction {
/// Starts the voting by creating and populating a VotingState account.
///
///
/// Accounts expected:
///
/// 0. `[writable, signer]` The voting owner account.
/// 1. `[writable]` The voting state account.
InitVoting,
/// Makes the voter eligible for voting by creating a VoterVotes account.
///
///
/// Accounts expected:
///
/// 0. `[writable, signer]` The voting owner account.
/// 1. `[]` The voting state account.
/// 2. `[writable]` The voter votes account.
/// 3. `[]` The system program.
AddVoter {
voter_pubkey: Pubkey,
voter_votes_bump_seed: u8,
},
/// Creates a new Party account with the requested name
/// and increments the parties counter in the VotingState account.
///
///
/// Accounts expected:
///
/// 0. `[writable, signer]` The fee payer account.
/// 1. `[writable]` The party account.
/// 2. `[writable]` The voting state account.
/// 3. `[]` The system program.
AddParty { name: String, party_bump_seed: u8 },
/// Votes the provided party and creates a VoterVoted account.
///
///
/// Accounts expected:
///
/// 0. `[writable, signer]` The voter account.
/// 1. `[]` The voting state account.
/// 2. `[writable]` The voter voted account.
/// 3. `[writable]` The voter votes account.
/// 4. `[writable]` The party account.
/// 5. `[]` The system program.
Vote {
/// The party will receive one negative or positive vote.
positive: bool,
voter_votes_bump_seed: u8,
},
}
fn voting_state_pubkey(voting_owner_pubkey: &Pubkey) -> Pubkey {
Pubkey::create_with_seed(voting_owner_pubkey, "voting_state", &crate::id())
.expect("failed to create voting_state_pubkey")
}
pub fn init_voting(voting_owner_pubkey: &Pubkey) -> Instruction {
let account_metas = vec![
AccountMeta::new(*voting_owner_pubkey, true),
AccountMeta::new(voting_state_pubkey(voting_owner_pubkey), false),
];
Instruction::new_with_borsh(crate::id(), &VotingInstruction::InitVoting, account_metas)
}
pub fn add_voter(voting_owner_pubkey: &Pubkey, voter_pubkey: &Pubkey) -> (Instruction, Pubkey) {
let voting_state_pubkey = voting_state_pubkey(voting_owner_pubkey);
let seeds = &[
b"voter_votes".as_ref(),
voter_pubkey.as_ref(),
voting_state_pubkey.as_ref(),
];
let (voter_votes_pubkey, voter_votes_bump_seed) =
Pubkey::find_program_address(seeds, &crate::id());
let account_metas = vec![
AccountMeta::new(*voting_owner_pubkey, true),
AccountMeta::new_readonly(voting_state_pubkey, false),
AccountMeta::new(voter_votes_pubkey, false),
AccountMeta::new_readonly(System::id(), false),
];
let ix = Instruction::new_with_borsh(
crate::id(),
&VotingInstruction::AddVoter {
voter_pubkey: *voter_pubkey,
voter_votes_bump_seed,
},
account_metas,
);
(ix, voter_votes_pubkey)
}
pub fn add_party(
fee_payer: &Pubkey,
party_name: &str,
party_count: u32,
voting_state_pubkey: &Pubkey,
) -> (Instruction, Pubkey) {
let new_party_index_bytes = party_count.to_le_bytes();
let seeds = &[
b"party",
new_party_index_bytes.as_ref(),
voting_state_pubkey.as_ref(),
];
let (party_pubkey, party_bump_seed) = Pubkey::find_program_address(seeds, &crate::id());
let account_metas = vec![
AccountMeta::new(*fee_payer, true),
AccountMeta::new(party_pubkey, false),
AccountMeta::new(*voting_state_pubkey, false),
AccountMeta::new_readonly(System::id(), false),
];
let ix = Instruction::new_with_borsh(
crate::id(),
&VotingInstruction::AddParty {
name: party_name.to_owned(),
party_bump_seed,
},
account_metas,
);
(ix, party_pubkey)
}
pub fn vote(
voter_pubkey: &Pubkey,
voting_state_pubkey: &Pubkey,
party_pubkey: &Pubkey,
positive: bool,
) -> (Instruction, Pubkey, Pubkey) {
let seeds = &[
b"voter_votes".as_ref(),
voter_pubkey.as_ref(),
voting_state_pubkey.as_ref(),
];
let voter_votes_pubkey = Pubkey::find_program_address(seeds, &crate::id()).0;
let seeds = &[
b"voter_voted".as_ref(),
voter_pubkey.as_ref(),
party_pubkey.as_ref(),
voting_state_pubkey.as_ref(),
];
let (voter_voted_pubkey, voter_votes_bump_seed) =
Pubkey::find_program_address(seeds, &crate::id());
let account_metas = vec![
AccountMeta::new(*voter_pubkey, true),
AccountMeta::new_readonly(*voting_state_pubkey, false),
AccountMeta::new(voter_voted_pubkey, false),
AccountMeta::new(voter_votes_pubkey, false),
AccountMeta::new(*party_pubkey, false),
AccountMeta::new_readonly(System::id(), false),
];
let ix = Instruction::new_with_borsh(
crate::id(),
&VotingInstruction::Vote {
positive,
voter_votes_bump_seed,
},
account_metas,
);
(ix, voter_votes_pubkey, voter_voted_pubkey)
}