-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathvote.rs
43 lines (43 loc) · 1.27 KB
/
vote.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
use anchor_lang::prelude::*;
declare_id!("HC2oqz2p6DEWfrahenqdq2moUcga9c9biqRBcdK3XKU1");
#[program]
pub mod vote_program {
use super::*;
pub fn initialize(ctx: Context<InitializeContext>) -> Result<()> {
ctx.accounts.state.vote = 0;
Ok(())
}
pub fn upvote(ctx: Context<UpvoteContext>) -> Result<()> {
ctx.accounts.state.vote = ctx.accounts.state.vote + 1;
Ok(())
}
pub fn downvote(ctx: Context<DownvoteContext>) -> Result<()> {
ctx.accounts.state.vote = ctx.accounts.state.vote - 1;
Ok(())
}
}
#[derive(Accounts)]
pub struct InitializeContext<'info> {
#[account(init, payer = user, space = 17, seeds = [b"vote"], bump)]
pub state: Account<'info, VoteState>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct UpvoteContext<'info> {
#[account(mut, seeds = [b"vote"], bump)]
pub state: Account<'info, VoteState>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct DownvoteContext<'info> {
#[account(mut, seeds = [b"vote"], bump)]
pub state: Account<'info, VoteState>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct VoteState {
pub vote: i64,
pub bump: u8,
}