Skip to content

Instantly share code, notes, and snippets.

@deanmlittle
Last active August 24, 2023 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deanmlittle/316a7dc00ca2fb14bf7b11eed9864a0c to your computer and use it in GitHub Desktop.
Save deanmlittle/316a7dc00ca2fb14bf7b11eed9864a0c to your computer and use it in GitHub Desktop.
Anchor hash-based social content ranking
use anchor_lang::prelude::*;
// This is your program's public key and it will update
// automatically when you build the project.
declare_id!("HL1HGm4o5ygkrMYcyS3WYLK3XXPVLyvp9bPksHQZUeWb");
#[program]
mod hello_anchor {
use super::*;
pub fn initialize(ctx: Context<Initialize>, _hash: Vec<u8>) -> Result<()> {
ctx.accounts.vote_account.votes = 0i64;
ctx.accounts.vote_account.bump = *ctx.bumps.get("vote_account").unwrap();
Ok(())
}
pub fn upvote(ctx: Context<VoteInteraction>, _hash: Vec<u8>) -> Result<()> {
ctx.accounts.vote_account.votes+=1;
Ok(())
}
pub fn downvote(ctx: Context<VoteInteraction>, _hash: Vec<u8>) -> Result<()> {
ctx.accounts.vote_account.votes-=1;
Ok(())
}
}
#[derive(Accounts)]
#[instruction(_hash: Vec<u8>)]
pub struct Initialize<'info> {
#[account(mut)]
pub owner: Signer<'info>,
#[account(init, seeds = [b"vote", _hash.as_slice().as_ref()], bump, payer = owner, space = 8 + 9 + 32)]
pub vote_account: Account<'info, VoteCount>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
#[instruction(_hash: Vec<u8>)]
pub struct VoteInteraction<'info> {
#[account(mut)]
pub owner: Signer<'info>,
#[account(mut, seeds = [b"vote", _hash.as_slice().as_ref()], bump = vote_account.bump)]
pub vote_account: Account<'info, VoteCount>
}
#[account]
pub struct VoteCount {
votes: i64,
bump: u8
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment