solana-with-rpc-optimizations/programs/vote_api/src/vote_instruction.rs

54 lines
1.7 KiB
Rust
Raw Normal View History

2019-03-02 13:51:26 -08:00
use crate::id;
use serde_derive::{Deserialize, Serialize};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::transaction_builder::BuilderInstruction;
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Vote {
// TODO: add signature of the state here as well
/// A vote for height slot_height
pub slot_height: u64,
}
impl Vote {
pub fn new(slot_height: u64) -> Self {
Self { slot_height }
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum VoteInstruction {
/// Initialize the VoteState for this `vote account`
2019-03-05 09:53:10 -08:00
/// * Instruction::keys[0] - the new "vote account" to be associated with the delegate
2019-03-02 13:51:26 -08:00
InitializeAccount,
/// `Delegate` or `Assign` a vote account to a particular node
2019-03-02 13:51:26 -08:00
DelegateStake(Pubkey),
Vote(Vote),
/// Clear the credits in the vote account
/// * Transaction::keys[0] - the "vote account"
ClearCredits,
}
impl VoteInstruction {
pub fn new_clear_credits(vote_id: Pubkey) -> BuilderInstruction {
BuilderInstruction::new(id(), &VoteInstruction::ClearCredits, vec![(vote_id, true)])
}
pub fn new_delegate_stake(vote_id: Pubkey, delegate_id: Pubkey) -> BuilderInstruction {
BuilderInstruction::new(
id(),
&VoteInstruction::DelegateStake(delegate_id),
vec![(vote_id, true)],
)
}
2019-03-05 09:53:10 -08:00
pub fn new_initialize_account(vote_id: Pubkey) -> BuilderInstruction {
2019-03-02 13:51:26 -08:00
BuilderInstruction::new(
id(),
&VoteInstruction::InitializeAccount,
2019-03-05 09:53:10 -08:00
vec![(vote_id, false)],
2019-03-02 13:51:26 -08:00
)
}
pub fn new_vote(vote_id: Pubkey, vote: Vote) -> BuilderInstruction {
BuilderInstruction::new(id(), &VoteInstruction::Vote(vote), vec![(vote_id, true)])
}
}