Less code

This commit is contained in:
Greg Fitzgerald 2019-03-24 22:23:20 -06:00 committed by Grimes
parent aefa9891c0
commit caff603497
1 changed files with 20 additions and 29 deletions

View File

@ -1,12 +1,9 @@
//! The `vote_transaction` module provides functionality for creating vote transactions. //! The `vote_transaction` module provides functionality for creating vote transactions.
use crate::id;
use crate::vote_instruction::{Vote, VoteInstruction}; use crate::vote_instruction::{Vote, VoteInstruction};
use crate::vote_state::VoteState;
use solana_sdk::hash::Hash; use solana_sdk::hash::Hash;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil}; use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::transaction::Transaction; use solana_sdk::transaction::Transaction;
pub struct VoteTransaction {} pub struct VoteTransaction {}
@ -19,12 +16,13 @@ impl VoteTransaction {
recent_blockhash: Hash, recent_blockhash: Hash,
fee: u64, fee: u64,
) -> Transaction { ) -> Transaction {
let vote = Vote { slot }; let ix = VoteInstruction::new_vote(staking_account, Vote { slot });
let ix = VoteInstruction::new_vote(staking_account, vote); Transaction::new_signed_instructions(
let mut tx = Transaction::new(vec![ix]); &[authorized_voter_keypair],
tx.fee = fee; vec![ix],
tx.sign(&[authorized_voter_keypair], recent_blockhash); recent_blockhash,
tx fee,
)
} }
/// Fund or create the staking account with lamports /// Fund or create the staking account with lamports
@ -37,10 +35,7 @@ impl VoteTransaction {
) -> Transaction { ) -> Transaction {
let from_id = from_keypair.pubkey(); let from_id = from_keypair.pubkey();
let ixs = VoteInstruction::new_account(&from_id, staker_id, lamports); let ixs = VoteInstruction::new_account(&from_id, staker_id, lamports);
let mut tx = Transaction::new(ixs); Transaction::new_signed_instructions(&[from_keypair], ixs, recent_blockhash, fee)
tx.fee = fee;
tx.sign(&[from_keypair], recent_blockhash);
tx
} }
/// Fund or create the staking account with lamports /// Fund or create the staking account with lamports
@ -54,15 +49,17 @@ impl VoteTransaction {
) -> Transaction { ) -> Transaction {
let from_id = from_keypair.pubkey(); let from_id = from_keypair.pubkey();
let voter_id = voter_keypair.pubkey(); let voter_id = voter_keypair.pubkey();
let space = VoteState::max_size() as u64;
let create_ix = let mut ixs = VoteInstruction::new_account(&from_id, &voter_id, lamports);
SystemInstruction::new_program_account(&from_id, &voter_id, lamports, space, &id());
let init_ix = VoteInstruction::new_initialize_account(&voter_id);
let delegate_ix = VoteInstruction::new_delegate_stake(&voter_id, &delegate_id); let delegate_ix = VoteInstruction::new_delegate_stake(&voter_id, &delegate_id);
let mut tx = Transaction::new(vec![create_ix, init_ix, delegate_ix]); ixs.push(delegate_ix);
tx.fee = fee;
tx.sign(&[from_keypair, voter_keypair], recent_blockhash); Transaction::new_signed_instructions(
tx &[from_keypair, voter_keypair],
ixs,
recent_blockhash,
fee,
)
} }
/// Choose a voter id to accept signed votes from /// Choose a voter id to accept signed votes from
@ -73,10 +70,7 @@ impl VoteTransaction {
fee: u64, fee: u64,
) -> Transaction { ) -> Transaction {
let ix = VoteInstruction::new_authorize_voter(&vote_keypair.pubkey(), authorized_voter_id); let ix = VoteInstruction::new_authorize_voter(&vote_keypair.pubkey(), authorized_voter_id);
let mut tx = Transaction::new(vec![ix]); Transaction::new_signed_instructions(&[vote_keypair], vec![ix], recent_blockhash, fee)
tx.fee = fee;
tx.sign(&[vote_keypair], recent_blockhash);
tx
} }
/// Choose a node id to `delegate` or `assign` this vote account to /// Choose a node id to `delegate` or `assign` this vote account to
@ -87,9 +81,6 @@ impl VoteTransaction {
fee: u64, fee: u64,
) -> Transaction { ) -> Transaction {
let ix = VoteInstruction::new_delegate_stake(&vote_keypair.pubkey(), node_id); let ix = VoteInstruction::new_delegate_stake(&vote_keypair.pubkey(), node_id);
let mut tx = Transaction::new(vec![ix]); Transaction::new_signed_instructions(&[vote_keypair], vec![ix], recent_blockhash, fee)
tx.fee = fee;
tx.sign(&[vote_keypair], recent_blockhash);
tx
} }
} }