2019-03-02 13:51:26 -08:00
|
|
|
use crate::id;
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-03-19 12:03:20 -07:00
|
|
|
use solana_sdk::transaction::{AccountMeta, Instruction};
|
2019-03-02 13:51:26 -08:00
|
|
|
|
|
|
|
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub struct Vote {
|
|
|
|
// TODO: add signature of the state here as well
|
2019-03-05 14:18:29 -08:00
|
|
|
/// A vote for height slot
|
|
|
|
pub slot: u64,
|
2019-03-02 13:51:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Vote {
|
2019-03-05 14:18:29 -08:00
|
|
|
pub fn new(slot: u64) -> Self {
|
|
|
|
Self { slot }
|
2019-03-02 13:51:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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,
|
2019-03-04 17:50:19 -08:00
|
|
|
/// `Delegate` or `Assign` a vote account to a particular node
|
2019-03-02 13:51:26 -08:00
|
|
|
DelegateStake(Pubkey),
|
2019-03-06 13:33:12 -08:00
|
|
|
/// Authorize a voter to send signed votes.
|
|
|
|
AuthorizeVoter(Pubkey),
|
2019-03-02 13:51:26 -08:00
|
|
|
Vote(Vote),
|
|
|
|
/// Clear the credits in the vote account
|
|
|
|
/// * Transaction::keys[0] - the "vote account"
|
|
|
|
ClearCredits,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VoteInstruction {
|
2019-03-15 08:47:25 -07:00
|
|
|
pub fn new_clear_credits(vote_id: &Pubkey) -> Instruction {
|
2019-03-19 14:25:48 -07:00
|
|
|
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
2019-03-19 12:03:20 -07:00
|
|
|
Instruction::new(id(), &VoteInstruction::ClearCredits, account_metas)
|
2019-03-02 13:51:26 -08:00
|
|
|
}
|
2019-03-15 08:47:25 -07:00
|
|
|
pub fn new_delegate_stake(vote_id: &Pubkey, delegate_id: &Pubkey) -> Instruction {
|
2019-03-19 14:25:48 -07:00
|
|
|
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
2019-03-15 08:47:25 -07:00
|
|
|
Instruction::new(
|
2019-03-02 13:51:26 -08:00
|
|
|
id(),
|
2019-03-09 19:28:43 -08:00
|
|
|
&VoteInstruction::DelegateStake(*delegate_id),
|
2019-03-19 12:03:20 -07:00
|
|
|
account_metas,
|
2019-03-02 13:51:26 -08:00
|
|
|
)
|
|
|
|
}
|
2019-03-15 08:47:25 -07:00
|
|
|
pub fn new_authorize_voter(vote_id: &Pubkey, authorized_voter_id: &Pubkey) -> Instruction {
|
2019-03-19 14:25:48 -07:00
|
|
|
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
2019-03-15 08:47:25 -07:00
|
|
|
Instruction::new(
|
2019-03-06 13:33:12 -08:00
|
|
|
id(),
|
2019-03-09 19:28:43 -08:00
|
|
|
&VoteInstruction::AuthorizeVoter(*authorized_voter_id),
|
2019-03-19 12:03:20 -07:00
|
|
|
account_metas,
|
2019-03-06 13:33:12 -08:00
|
|
|
)
|
|
|
|
}
|
2019-03-15 08:47:25 -07:00
|
|
|
pub fn new_initialize_account(vote_id: &Pubkey) -> Instruction {
|
2019-03-19 14:25:48 -07:00
|
|
|
let account_metas = vec![AccountMeta::new(*vote_id, false)];
|
2019-03-19 12:03:20 -07:00
|
|
|
Instruction::new(id(), &VoteInstruction::InitializeAccount, account_metas)
|
2019-03-02 13:51:26 -08:00
|
|
|
}
|
2019-03-15 08:47:25 -07:00
|
|
|
pub fn new_vote(vote_id: &Pubkey, vote: Vote) -> Instruction {
|
2019-03-19 14:25:48 -07:00
|
|
|
let account_metas = vec![AccountMeta::new(*vote_id, true)];
|
2019-03-19 12:03:20 -07:00
|
|
|
Instruction::new(id(), &VoteInstruction::Vote(vote), account_metas)
|
2019-03-02 13:51:26 -08:00
|
|
|
}
|
|
|
|
}
|