Skip to content

Commit

Permalink
Rename interactor to client #30
Browse files Browse the repository at this point in the history
  • Loading branch information
Semen Medvedev committed Apr 29, 2022
1 parent f823b2d commit 305abdf
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 159 deletions.
32 changes: 16 additions & 16 deletions governance-lib/src/addin_fixed_weights.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
client::SplGovernanceInteractor,
client::Client,
realm::Realm,
},
solana_sdk::{
Expand All @@ -13,14 +13,14 @@ use {

#[derive(Debug)]
pub struct AddinFixedWeights<'a> {
interactor: &'a SplGovernanceInteractor<'a>,
client: &'a Client<'a>,
pub program_id: Pubkey,
}

impl<'a> AddinFixedWeights<'a> {
pub fn new(interactor: &'a SplGovernanceInteractor, program_id: Pubkey) -> Self {
pub fn new(client: &'a Client, program_id: Pubkey) -> Self {
AddinFixedWeights {
interactor,
client,
program_id,
}
}
Expand All @@ -33,28 +33,28 @@ impl<'a> AddinFixedWeights<'a> {
&realm.community_mint,
);

if !self.interactor.account_exists(&max_voter_weight_record_pubkey) {
if !self.client.account_exists(&max_voter_weight_record_pubkey) {
let setup_max_voter_weight_record_instruction: Instruction =
instruction::setup_max_voter_weight_record(
&self.program_id,
&realm.address,
&realm.community_mint,
&self.interactor.payer.pubkey(),
&self.client.payer.pubkey(),
);

let transaction: Transaction =
Transaction::new_signed_with_payer(
&[
setup_max_voter_weight_record_instruction,
],
Some(&self.interactor.payer.pubkey()),
Some(&self.client.payer.pubkey()),
&[
self.interactor.payer,
self.client.payer,
],
self.interactor.solana_client.get_latest_blockhash().unwrap(),
self.client.solana_client.get_latest_blockhash().unwrap(),
);

self.interactor.solana_client.send_and_confirm_transaction(&transaction)
self.client.solana_client.send_and_confirm_transaction(&transaction)
.map_err(|_|())?;
}
Ok(max_voter_weight_record_pubkey)
Expand All @@ -67,29 +67,29 @@ impl<'a> AddinFixedWeights<'a> {
&realm.community_mint,
token_owner);

if !self.interactor.account_exists(&voter_weight_record_pubkey) {
if !self.client.account_exists(&voter_weight_record_pubkey) {
let setup_voter_weight_record_instruction: Instruction =
spl_governance_addin_fixed_weights::instruction::setup_voter_weight_record(
&self.program_id,
&realm.address,
&realm.data.community_mint,
token_owner,
&self.interactor.payer.pubkey(),
&self.client.payer.pubkey(),
);

let transaction: Transaction =
Transaction::new_signed_with_payer(
&[
setup_voter_weight_record_instruction,
],
Some(&self.interactor.payer.pubkey()),
Some(&self.client.payer.pubkey()),
&[
self.interactor.payer,
self.client.payer,
],
self.interactor.solana_client.get_latest_blockhash().unwrap(),
self.client.solana_client.get_latest_blockhash().unwrap(),
);

self.interactor.solana_client.send_and_confirm_transaction(&transaction).unwrap();
self.client.solana_client.send_and_confirm_transaction(&transaction).unwrap();
}
Ok(voter_weight_record_pubkey)
}
Expand Down
38 changes: 19 additions & 19 deletions governance-lib/src/addin_vesting.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
client::SplGovernanceInteractor,
client::Client,
realm::Realm,
},
solana_sdk::{
Expand All @@ -22,15 +22,15 @@ use {

#[derive(Debug)]
pub struct AddinVesting<'a> {
interactor: &'a SplGovernanceInteractor<'a>,
client: &'a Client<'a>,
pub program_id: Pubkey,
}


impl<'a> AddinVesting<'a> {
pub fn new(interactor: &'a SplGovernanceInteractor, program_id: Pubkey) -> Self {
pub fn new(client: &'a Client, program_id: Pubkey) -> Self {
AddinVesting {
interactor,
client,
program_id,
}
}
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<'a> AddinVesting<'a> {
&source_token_authority,
source_token_account,
vesting_owner,
&self.interactor.payer.pubkey(),
&self.client.payer.pubkey(),
schedules,
)
}
Expand All @@ -76,9 +76,9 @@ impl<'a> AddinVesting<'a> {
&source_token_authority,
source_token_account,
vesting_owner,
&self.interactor.payer.pubkey(),
&self.client.payer.pubkey(),
schedules,
&realm.interactor.spl_governance_program_address,
&realm.client.spl_governance_program_address,
&realm.address,
&realm.community_mint,
)
Expand All @@ -92,28 +92,28 @@ impl<'a> AddinVesting<'a> {
&realm.community_mint,
);
if !self.interactor.account_exists(&max_voter_weight_record_pubkey) {
if !self.client.account_exists(&max_voter_weight_record_pubkey) {
let setup_max_voter_weight_record_instruction: Instruction =
instruction::setup_max_voter_weight_record(
&self.program_id,
&realm.address,
&realm.community_mint,
&self.interactor.payer.pubkey(),
&self.client.payer.pubkey(),
);
let transaction: Transaction =
Transaction::new_signed_with_payer(
&[
setup_max_voter_weight_record_instruction,
],
Some(&self.interactor.payer.pubkey()),
Some(&self.client.payer.pubkey()),
&[
self.interactor.payer,
self.client.payer,
],
self.interactor.solana_client.get_latest_blockhash().unwrap(),
self.client.solana_client.get_latest_blockhash().unwrap(),
);
self.interactor.solana_client.send_and_confirm_transaction(&transaction)
self.client.solana_client.send_and_confirm_transaction(&transaction)
.map_err(|_|())?;
}
Ok(max_voter_weight_record_pubkey)
Expand All @@ -126,29 +126,29 @@ impl<'a> AddinVesting<'a> {
&realm.community_mint,
token_owner);
if !self.interactor.account_exists(&voter_weight_record_pubkey) {
if !self.client.account_exists(&voter_weight_record_pubkey) {
let setup_voter_weight_record_instruction: Instruction =
spl_governance_addin_fixed_weights::instruction::setup_voter_weight_record(
&self.program_id,
&realm.address,
&realm.data.community_mint,
token_owner,
&self.interactor.payer.pubkey(),
&self.client.payer.pubkey(),
);
let transaction: Transaction =
Transaction::new_signed_with_payer(
&[
setup_voter_weight_record_instruction,
],
Some(&self.interactor.payer.pubkey()),
Some(&self.client.payer.pubkey()),
&[
self.interactor.payer,
self.client.payer,
],
self.interactor.solana_client.get_latest_blockhash().unwrap(),
self.client.solana_client.get_latest_blockhash().unwrap(),
);
self.interactor.solana_client.send_and_confirm_transaction(&transaction).unwrap();
self.client.solana_client.send_and_confirm_transaction(&transaction).unwrap();
}
Ok(voter_weight_record_pubkey)
}*/
Expand Down
33 changes: 17 additions & 16 deletions governance-lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,29 @@ use {

const MIN_COMMUNITY_WEIGHT_TO_CREATE_GOVERNANCE: u64 = 1;

pub struct SplGovernanceInteractor<'a> {
pub struct Client<'a> {
pub url: String,
pub solana_client: RpcClient,
pub payer: &'a Keypair,
pub solana_client: RpcClient,
pub spl_governance_program_address: Pubkey,
pub spl_governance_voter_weight_addin_address: Pubkey,
}

impl<'a> fmt::Debug for SplGovernanceInteractor<'a> {
impl<'a> fmt::Debug for Client<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("SplGovernanceInteractor")
f.debug_struct("Client")
.field("url", &self.url)
.field("payer", &self.payer.pubkey())
.field("program", &self.spl_governance_program_address)
.field("addin", &self.spl_governance_voter_weight_addin_address)
.finish()
}
}

impl<'a> SplGovernanceInteractor<'a> {
impl<'a> Client<'a> {

pub fn new(url: &str, program_address: Pubkey, addin_address: Pubkey, payer: &'a Keypair) -> Self {
SplGovernanceInteractor {
Client {
url: url.to_string(),
solana_client: RpcClient::new_with_commitment(url.to_string(),CommitmentConfig::confirmed()),
payer,
Expand All @@ -59,9 +60,9 @@ impl<'a> SplGovernanceInteractor<'a> {
}

pub fn send_and_confirm_transaction<T: Signers>(
&self,
instructions: &[Instruction],
signing_keypairs: &T,
&self,
instructions: &[Instruction],
signing_keypairs: &T,
) -> Result<Signature, ClientError> {
let mut transaction: Transaction =
Transaction::new_with_payer(
Expand All @@ -77,9 +78,9 @@ impl<'a> SplGovernanceInteractor<'a> {
}

pub fn get_account_data_pack<T: Pack + IsInitialized>(
&self,
owner_program_id: &Pubkey,
account_key: &Pubkey,
&self,
owner_program_id: &Pubkey,
account_key: &Pubkey,
) -> Result<Option<T>, ClientError> {
let account_info = &self.solana_client.get_account_with_commitment(
&account_key, self.solana_client.commitment())?.value;
Expand All @@ -104,9 +105,9 @@ impl<'a> SplGovernanceInteractor<'a> {
}

pub fn get_account_data<T: BorshDeserialize + IsInitialized>(
&self,
owner_program_id: &Pubkey,
account_key: &Pubkey,
&self,
owner_program_id: &Pubkey,
account_key: &Pubkey,
) -> Result<Option<T>, ClientError> {
let account_info = &self.solana_client.get_account_with_commitment(
&account_key, self.solana_client.commitment())?.value;
Expand Down Expand Up @@ -195,7 +196,7 @@ impl<'a> SplGovernanceInteractor<'a> {
Ok(
Realm {
//authority: realm_authority,
interactor: self,
client: self,
address: realm_pubkey,
community_mint: *community_mint_pubkey,
data: self.get_realm_v2(realm_name).unwrap(),
Expand Down
24 changes: 12 additions & 12 deletions governance-lib/src/governance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
client::SplGovernanceInteractor,
client::Client,
realm::Realm,
token_owner::TokenOwner,
proposal::Proposal,
Expand Down Expand Up @@ -32,44 +32,44 @@ pub struct Governance<'a> {
}

impl<'a> Governance<'a> {
pub fn get_interactor(&self) -> &SplGovernanceInteractor<'a> {self.realm.interactor}
pub fn get_client(&self) -> &Client<'a> {self.realm.client}

pub fn get_proposals_count(&self) -> u32 {
let data: GovernanceV2 = self.get_interactor().get_account_data(
&self.get_interactor().spl_governance_program_address,
let data: GovernanceV2 = self.get_client().get_account_data(
&self.get_client().spl_governance_program_address,
&self.address).unwrap().unwrap();
data.proposals_count
}

pub fn get_proposal_address(&self, proposal_index: u32) -> Pubkey {
get_proposal_address(
&self.get_interactor().spl_governance_program_address,
&self.get_client().spl_governance_program_address,
&self.address,
&self.realm.community_mint,
&proposal_index.to_le_bytes())
}

pub fn get_proposal_v2(&self, proposal_pubkey: Pubkey) -> ProposalV2 {
let mut dt: &[u8] = &self.get_interactor().solana_client.get_account_data(&proposal_pubkey).unwrap();
let mut dt: &[u8] = &self.get_client().solana_client.get_account_data(&proposal_pubkey).unwrap();
ProposalV2::deserialize(&mut dt).unwrap()
}

// Note: Only governance PDA via a proposal can authorize change to its own config
pub fn set_governance_config_instruction(&self, config: GovernanceConfig) -> Instruction {
set_governance_config(
&self.get_interactor().spl_governance_program_address,
&self.get_client().spl_governance_program_address,
&self.address,
config)
}

pub fn create_proposal<'b:'a>(&'b self, create_authority: &Keypair, token_owner: &TokenOwner, proposal_name: &str, proposal_description: &str, proposal_index: u32) -> Result<Proposal<'a>,ClientError> {
let proposal_address: Pubkey = self.get_proposal_address(proposal_index);
let payer = &self.get_interactor().payer;
let payer = &self.get_client().payer;

if !self.get_interactor().account_exists(&proposal_address) {
if !self.get_client().account_exists(&proposal_address) {
let create_proposal_instruction: Instruction =
create_proposal(
&self.get_interactor().spl_governance_program_address,
&self.get_client().spl_governance_program_address,
&self.address,
&token_owner.token_owner_record_address,
&create_authority.pubkey(),
Expand All @@ -96,10 +96,10 @@ impl<'a> Governance<'a> {
create_authority,
payer,
],
self.get_interactor().solana_client.get_latest_blockhash().unwrap(),
self.get_client().solana_client.get_latest_blockhash().unwrap(),
);

self.get_interactor().solana_client.send_and_confirm_transaction(&transaction)?;
self.get_client().solana_client.send_and_confirm_transaction(&transaction)?;
}
Ok(
Proposal {
Expand Down
Loading

0 comments on commit 305abdf

Please sign in to comment.