Use TransactionBuilder in the Rewards transaction

This commit is contained in:
Greg Fitzgerald 2019-02-28 11:34:32 -07:00 committed by Grimes
parent 8d004ee947
commit 485ccd20e4
3 changed files with 35 additions and 14 deletions

View File

@ -1,6 +1,23 @@
use crate::rewards_program;
use serde_derive::{Deserialize, Serialize};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::transaction_builder::BuilderInstruction;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum RewardsInstruction {
RedeemVoteCredits,
}
impl RewardsInstruction {
pub fn new_redeem_vote_credits(
vote_id: Pubkey,
rewards_id: Pubkey,
to_id: Pubkey,
) -> BuilderInstruction {
BuilderInstruction::new(
rewards_program::id(),
&RewardsInstruction::RedeemVoteCredits,
vec![(vote_id, true), (rewards_id, false), (to_id, false)],
)
}
}

View File

@ -5,10 +5,11 @@ use crate::rewards_instruction::RewardsInstruction;
use crate::rewards_program;
use solana_sdk::hash::Hash;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Keypair;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_transaction::SystemTransaction;
use solana_sdk::transaction::{Instruction, Transaction};
use solana_sdk::vote_program::{self, VoteInstruction};
use solana_sdk::transaction::Transaction;
use solana_sdk::transaction_builder::TransactionBuilder;
use solana_sdk::vote_program::VoteInstruction;
pub struct RewardsTransaction {}
@ -38,16 +39,12 @@ impl RewardsTransaction {
last_id: Hash,
fee: u64,
) -> Transaction {
Transaction::new_with_instructions(
&[vote_keypair],
&[rewards_id, to_id],
last_id,
fee,
vec![rewards_program::id(), vote_program::id()],
vec![
Instruction::new(0, &RewardsInstruction::RedeemVoteCredits, vec![0, 1, 2]),
Instruction::new(1, &VoteInstruction::ClearCredits, vec![0]),
],
)
let vote_id = vote_keypair.pubkey();
TransactionBuilder::new(fee)
.push(RewardsInstruction::new_redeem_vote_credits(
vote_id, rewards_id, to_id,
))
.push(VoteInstruction::new_clear_credits(vote_id))
.sign(&[vote_keypair], last_id)
}
}

View File

@ -4,6 +4,7 @@
use crate::account::{Account, KeyedAccount};
use crate::native_program::ProgramError;
use crate::pubkey::Pubkey;
use crate::transaction_builder::BuilderInstruction;
use bincode::{deserialize, serialize_into, serialized_size, ErrorKind};
use log::*;
use std::collections::VecDeque;
@ -78,6 +79,12 @@ pub enum VoteInstruction {
ClearCredits,
}
impl VoteInstruction {
pub fn new_clear_credits(vote_id: Pubkey) -> BuilderInstruction {
BuilderInstruction::new(id(), &VoteInstruction::ClearCredits, vec![(vote_id, true)])
}
}
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct VoteState {
pub votes: VecDeque<Lockout>,