Skip to content

Commit

Permalink
Rework setup environment process (put it in separate command and chec…
Browse files Browse the repository at this point in the history
…k objects before sending transactions) #30
  • Loading branch information
Semen Medvedev committed Apr 29, 2022
1 parent 5bc8bbe commit 2c190ee
Show file tree
Hide file tree
Showing 17 changed files with 1,124 additions and 255 deletions.
2 changes: 2 additions & 0 deletions addin-fixed-weights/program/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use cfg_if::cfg_if;
macro_rules! voter_weight_array {
($identifier:ident, [ $(($value_pubkey:expr,$value_weight:expr),)* ]) => {
/// Voter Weight List
#[no_mangle]
#[used]
pub static $identifier: [(::solana_program::pubkey::Pubkey,u64); [$(($value_pubkey,$value_weight),)*].len()] = [
$((::solana_program::pubkey!($value_pubkey),$value_weight),)*
];
Expand Down
3 changes: 3 additions & 0 deletions governance-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ edition = "2021"
solana-sdk = "1.9"
solana-client = "1.9"
borsh = "0.9.1"
goblin = "0.4.2"
log = "0.4.11"
thiserror = "1.0"
spl-token = { version = "3.3", path = "../solana-program-library/token/program", features = [ "no-entrypoint" ] }
spl-associated-token-account = "1"
spl-governance = { version = "2.2", path = "../solana-program-library/governance/program" }
Expand Down
66 changes: 65 additions & 1 deletion governance-lib/src/addin_fixed_weights.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use {
crate::{
client::Client,
client::{Client, ClientResult},
realm::Realm,
errors::GovernanceLibError,
},
borsh::{BorshSchema,BorshDeserialize},
goblin::elf::Elf,
solana_sdk::{
borsh::{get_packed_len, try_from_slice_unchecked},
pubkey::Pubkey,
signer::Signer,
instruction::Instruction,
Expand All @@ -17,6 +21,12 @@ pub struct AddinFixedWeights<'a> {
pub program_id: Pubkey,
}

#[derive(Clone,Debug,BorshSchema,BorshDeserialize)]
pub struct VoterWeight {
pub voter: Pubkey,
pub weight: u64,
}

impl<'a> AddinFixedWeights<'a> {
pub fn new(client: &'a Client, program_id: Pubkey) -> Self {
AddinFixedWeights {
Expand Down Expand Up @@ -60,6 +70,29 @@ impl<'a> AddinFixedWeights<'a> {
Ok(max_voter_weight_record_pubkey)
}

pub fn get_voter_weight_record_address(&self, realm: &Realm, token_owner: &Pubkey) -> Pubkey {
spl_governance_addin_fixed_weights::instruction::get_voter_weight_address(
&self.program_id,
&realm.realm_address,
&realm.community_mint,
token_owner).0
}

pub fn setup_voter_weight_record_instruction(&self, realm: &Realm, token_owner: &Pubkey) -> Instruction {
let (voter_weight_record_pubkey,_): (Pubkey,u8) = spl_governance_addin_fixed_weights::instruction::get_voter_weight_address(
&self.program_id,
&realm.realm_address,
&realm.community_mint,
token_owner);
spl_governance_addin_fixed_weights::instruction::setup_voter_weight_record(
&self.program_id,
&realm.realm_address,
&realm.community_mint,
token_owner,
&self.client.payer.pubkey(),
)
}

pub fn setup_voter_weight_record(&self, realm: &Realm, token_owner: &Pubkey) -> Result<Pubkey,()> {
let (voter_weight_record_pubkey,_): (Pubkey,u8) = spl_governance_addin_fixed_weights::instruction::get_voter_weight_address(
&self.program_id,
Expand Down Expand Up @@ -93,4 +126,35 @@ impl<'a> AddinFixedWeights<'a> {
}
Ok(voter_weight_record_pubkey)
}

pub fn get_voter_list(&self) -> ClientResult<Vec<VoterWeight>> {
let program_data = &self.client.get_program_data(&self.program_id)?;
let elf = Elf::parse(program_data).expect("Can't parse Elf data");
for sym in elf.dynsyms.iter() {
let name = String::from(&elf.dynstrtab[sym.st_name]);
if name == "VOTER_LIST" {
let end = program_data.len();
let from: usize = usize::try_from(sym.st_value).map_err(|_| GovernanceLibError::InvalidElfData("Unable to cast usize".to_string()))?;
let to: usize = usize::try_from(sym.st_value + sym.st_size).map_err(|_| GovernanceLibError::InvalidElfData("Unable to cast usize".to_string()))?;
if to < end && from < end {
let item_len:usize = get_packed_len::<VoterWeight>();
if (to-from) % item_len != 0 {
return Err(GovernanceLibError::InvalidElfData("Invalid length of VOTER_LIST".to_string()));
}
let buf = &program_data[from..to];
let items_count = (to-from) / item_len;
let mut result = Vec::new();
for i in 0..items_count {
let item = try_from_slice_unchecked::<VoterWeight>(&buf[i*item_len..(i+1)*item_len])?;
result.push(item);
}
return Ok(result);
}
else {
return Err(GovernanceLibError::InvalidElfData(format!("{} is out of bounds", name)));
}
}
}
panic!("Can't find VOTER_LIST symbol in Elf data");
}
}
71 changes: 1 addition & 70 deletions governance-lib/src/addin_vesting.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
client::Client,
client::{Client, ClientResult},
realm::Realm,
},
solana_sdk::{
Expand Down Expand Up @@ -79,73 +79,4 @@ impl<'a> AddinVesting<'a> {
&realm.community_mint,
)
}

/* pub fn setup_max_voter_weight_record(&self, realm: &Realm) -> Result<Pubkey, ()> {
use spl_governance_addin_fixed_weights::instruction;
let (max_voter_weight_record_pubkey,_): (Pubkey,u8) = instruction::get_max_voter_weight_address(
&self.program_id,
&realm.address,
&realm.community_mint,
);
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.client.payer.pubkey(),
);
let transaction: Transaction =
Transaction::new_signed_with_payer(
&[
setup_max_voter_weight_record_instruction,
],
Some(&self.client.payer.pubkey()),
&[
self.client.payer,
],
self.client.solana_client.get_latest_blockhash().unwrap(),
);
self.client.solana_client.send_and_confirm_transaction(&transaction)
.map_err(|_|())?;
}
Ok(max_voter_weight_record_pubkey)
}
pub fn setup_voter_weight_record(&self, realm: &Realm, token_owner: &Pubkey) -> Result<Pubkey,()> {
let (voter_weight_record_pubkey,_): (Pubkey,u8) = spl_governance_addin_fixed_weights::instruction::get_voter_weight_address(
&self.program_id,
&realm.address,
&realm.community_mint,
token_owner);
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.client.payer.pubkey(),
);
let transaction: Transaction =
Transaction::new_signed_with_payer(
&[
setup_voter_weight_record_instruction,
],
Some(&self.client.payer.pubkey()),
&[
self.client.payer,
],
self.client.solana_client.get_latest_blockhash().unwrap(),
);
self.client.solana_client.send_and_confirm_transaction(&transaction).unwrap();
}
Ok(voter_weight_record_pubkey)
}*/
}
Loading

0 comments on commit 2c190ee

Please sign in to comment.