diff --git a/programs/native/rewards_api/src/rewards_instruction.rs b/programs/native/rewards_api/src/rewards_instruction.rs index 1594b87c74..90ded34142 100644 --- a/programs/native/rewards_api/src/rewards_instruction.rs +++ b/programs/native/rewards_api/src/rewards_instruction.rs @@ -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)], + ) + } +} diff --git a/programs/native/rewards_api/src/rewards_transaction.rs b/programs/native/rewards_api/src/rewards_transaction.rs index 05739a2bb3..9a38b80acc 100644 --- a/programs/native/rewards_api/src/rewards_transaction.rs +++ b/programs/native/rewards_api/src/rewards_transaction.rs @@ -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) } } diff --git a/sdk/src/vote_program.rs b/sdk/src/vote_program.rs index 1c1f019c68..a3ece45ec4 100644 --- a/sdk/src/vote_program.rs +++ b/sdk/src/vote_program.rs @@ -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,