Skip to content

Commit

Permalink
CI-Actions pass (#101)
Browse files Browse the repository at this point in the history
* bump package version

* fix doc list item missing indentation error

* fix formats and see if cargo test-sbf runs tests faster

* clean up repo with latest code & bonk-plugin
  • Loading branch information
crypt0miester authored Nov 13, 2024
1 parent b213571 commit 8aec2c0
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 80 deletions.
18 changes: 2 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"programs/token-haver",
"programs/token-voter"
]
resolver = "2"

[profile.release]
overflow-checks = true
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solana/governance-program-library",
"version": "0.17.7",
"version": "0.18.0",
"description": "Client for Governance Program Library which is a set of extensions for Solana's spl-governance program.",
"author": "Solana Maintainers <[email protected]>",
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct UpdateVoterWeightRecord<'info> {
pub system_program: Program<'info, System>,
}

pub fn update_voter_weight_record_handler<'info>(
pub fn update_voter_weight_record_handler(
ctx: Context<UpdateVoterWeightRecord>,
stake_receipts_count: u8,
action_target: Pubkey,
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn update_voter_weight_record_handler<'info>(
);

let input_voter_weight_record =
resolve_input_voter_weight(&input_voter_weight_account, &clone_record, &registrar)?;
resolve_input_voter_weight(&input_voter_weight_account, &clone_record, registrar)?;

voter_weight = voter_weight
.checked_add(input_voter_weight_record.get_voter_weight())
Expand All @@ -119,13 +119,13 @@ pub fn update_voter_weight_record_handler<'info>(

stake_deposit_record.deposits_len = new_deposit_len;

for mut stake_deposit_receipt_info in ctx.remaining_accounts {
for stake_deposit_receipt_info in ctx.remaining_accounts {
let vote_weight = resolve_stake_deposit_weight(
registrar,
proposal_info,
governance_info,
&governing_token_owner,
&mut stake_deposit_receipt_info,
stake_deposit_receipt_info,
&mut stake_deposit_record.deposits,
action,
action_target,
Expand All @@ -138,7 +138,8 @@ pub fn update_voter_weight_record_handler<'info>(
&& voter_weight_record.weight_action == Some(action)
{
voter_weight_record.voter_weight = voter_weight_record
.voter_weight.checked_sub(stake_deposit_record.previous_voter_weight)
.voter_weight
.checked_sub(stake_deposit_record.previous_voter_weight)
.unwrap();

voter_weight_record.voter_weight = voter_weight_record
Expand Down
11 changes: 6 additions & 5 deletions programs/bonk-plugin/src/state/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub fn resolve_governing_token_owner(
}

/// Resolves vote weight for the given stake deposit receipt
#[allow(clippy::too_many_arguments)]
pub fn resolve_stake_deposit_weight(
registrar: &Registrar,
proposal_info: &Option<UncheckedAccount>,
Expand All @@ -102,7 +103,7 @@ pub fn resolve_stake_deposit_weight(

let stake_deposit_receipt: StakeDepositReceipt =
StakeDepositReceipt::deserialize_checked(stake_deposit_receipt_info)?;

let stake_deposit_receipt_key = stake_deposit_receipt_info.key();

// voter_weight_record.governing_token_owner must be the owner of the stake deposit
Expand All @@ -127,9 +128,9 @@ pub fn resolve_stake_deposit_weight(
unique_stake_deposit_receipts.push(stake_deposit_receipt_key);

let stake_deposit_end_time = stake_deposit_receipt
.deposit_timestamp
.checked_add(stake_deposit_receipt.lockup_duration as i64)
.unwrap();
.deposit_timestamp
.checked_add(stake_deposit_receipt.lockup_duration as i64)
.unwrap();

let current_timestamp = Clock::get()?.unix_timestamp;

Expand Down Expand Up @@ -160,7 +161,7 @@ pub fn resolve_stake_deposit_weight(
}
}

Ok(stake_deposit_receipt.deposit_amount as u64)
Ok(stake_deposit_receipt.deposit_amount)
}

pub fn resolve_proposal_end_time(
Expand Down
28 changes: 13 additions & 15 deletions programs/bonk-plugin/src/utils/stake_deposit_receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,29 @@ pub struct StakeDepositReceipt {
pub claimed_amounts: [u128; 10],
}

impl StakeDepositReceipt {
impl StakeDepositReceipt {
pub const ACCOUNT_DISCRIMINATOR: [u8; 8] = [210, 98, 254, 196, 151, 68, 235, 0];

pub fn deserialize_checked(
stake_deposit_receipt_account_info: &AccountInfo
) -> Result<Self> {
pub fn deserialize_checked(stake_deposit_receipt_account_info: &AccountInfo) -> Result<Self> {
if stake_deposit_receipt_account_info.owner != &SPL_TOKEN_STAKING_PROGRAM_ID {
return Err(
anchor_lang::error!(anchor_lang::error::ErrorCode::AccountOwnedByWrongProgram)
.with_account_name("StakeDepositReceipt")
);
return Err(anchor_lang::error!(
anchor_lang::error::ErrorCode::AccountOwnedByWrongProgram
)
.with_account_name("StakeDepositReceipt"));
}

let stake_deposit_receipt_data = &stake_deposit_receipt_account_info.try_borrow_data()?;
let mut data = &mut stake_deposit_receipt_data.as_ref();
let data = &mut stake_deposit_receipt_data.as_ref();

let stake_deposit_receipt = Self::try_from_slice(&mut data)?;
let stake_deposit_receipt = Self::try_from_slice(data)?;

if stake_deposit_receipt.discriminator.to_le_bytes() != Self::ACCOUNT_DISCRIMINATOR {
return Err(
anchor_lang::error!(anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch)
.with_account_name("StakeDepositReceipt")
);
return Err(anchor_lang::error!(
anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch
)
.with_account_name("StakeDepositReceipt"));
}

Ok(stake_deposit_receipt)
}
}
}
20 changes: 9 additions & 11 deletions programs/bonk-plugin/src/utils/stake_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,21 @@ pub struct RewardPool {
}

impl StakePool {
pub const ACCOUNT_DISCRIMINATOR: [u8; 8] = [121, 34, 206, 21, 79, 127, 255, 28];
pub const ACCOUNT_DISCRIMINATOR: [u8; 8] = [121, 34, 206, 21, 79, 127, 255, 28];

pub fn deserialize_checked(
stake_pool_account_info: &AccountInfo
) -> Result<Self> {
pub fn deserialize_checked(stake_pool_account_info: &AccountInfo) -> Result<Self> {
let stake_pool_data = &stake_pool_account_info.try_borrow_data()?;
let mut data = &mut stake_pool_data.as_ref();
let data = &mut stake_pool_data.as_ref();

let stake_pool = Self::try_from_slice(&mut data)?;
let stake_pool = Self::try_from_slice(data)?;

if stake_pool.discriminator.to_le_bytes() != Self::ACCOUNT_DISCRIMINATOR {
return Err(
anchor_lang::error!(anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch)
.with_account_name("StakePool")
);
return Err(anchor_lang::error!(
anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch
)
.with_account_name("StakePool"));
}

Ok(stake_pool)
}
}
}
4 changes: 2 additions & 2 deletions programs/bonk-plugin/tests/update_voter_weight_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async fn test_update_voter_weight_record_create_proposal() -> Result<(), Transpo
Some(proposal_key),
&clone_keypair(&depositor),
governance_key,
&None
&None,
)
.await?;
bonk_plugin_test
Expand Down Expand Up @@ -290,7 +290,7 @@ async fn test_update_voter_weight_record_vote() -> Result<(), TransportError> {
Some(proposal_key),
&clone_keypair(&voter),
governance_key,
&Some(vec![stake_pool_reciept_voter])
&Some(vec![stake_pool_reciept_voter]),
)
.await?;

Expand Down
4 changes: 1 addition & 3 deletions programs/token-voter/src/instructions/close_voter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ pub struct CloseVoter<'info> {
/// Only accounts with no remaining deposits can be closed.
///
/// Tokens must be withdrawn first to be able to close voter and close the token accounts.
pub fn close_voter<'key, 'accounts, 'remaining, 'info>(
ctx: Context<'key, 'accounts, 'remaining, 'info, CloseVoter<'info>>,
) -> Result<()> {
pub fn close_voter<'info>(ctx: Context<'_, '_, '_, 'info, CloseVoter<'info>>) -> Result<()> {
let voter = &ctx.accounts.voter;
let voter_authority = &ctx.accounts.voter_authority;
let amount = voter.deposits.iter().fold(0u64, |sum, d| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn configure_mint_config(
ctx.accounts.realm_authority.key(),
TokenVoterError::InvalidRealmAuthority
);

let voting_mint_config = VotingMintConfig {
mint: mint.key(),
digit_shift,
Expand All @@ -85,10 +85,9 @@ pub fn configure_mint_config(
}

// Update MaxVoterWeightRecord.max_voter_weight
// recalculate the max voter weight as mint supply has possibly changed
// recalculate the max voter weight as mint supply has possibly changed
max_voter_weight_record.max_voter_weight = registrar.max_vote_weight()?;


max_voter_weight_record.max_voter_weight_expiry = None;

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ pub fn create_voter_weight_record(ctx: Context<CreateVoterWeightRecord>) -> Resu
voter.registrar = registrar.key();
voter.deposits = DepositEntry::init_deposits(registrar.max_mints as usize);


let voter_weight_record = &mut ctx.accounts.voter_weight_record;

voter_weight_record.account_discriminator =
Expand Down
4 changes: 2 additions & 2 deletions programs/token-voter/src/instructions/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ pub struct Deposit<'info> {
///
/// `deposit_entry_index`: Index of the deposit entry.
/// `amount`: Number of native tokens to transfer.
pub fn deposit<'key, 'accounts, 'remaining, 'info>(
ctx: Context<'key, 'accounts, 'remaining, 'info, Deposit<'info>>,
pub fn deposit<'info>(
ctx: Context<'_, '_, '_, 'info, Deposit<'info>>,
deposit_entry_index: u8,
amount: u64,
) -> Result<()> {
Expand Down
5 changes: 2 additions & 3 deletions programs/token-voter/src/instructions/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ pub struct Withdraw<'info> {
///
/// `deposit_entry_index`: The deposit entry to withdraw from.
/// `amount`: is in units of the native currency being withdrawn.
pub fn withdraw<'key, 'accounts, 'remaining, 'info>(
ctx: Context<'key, 'accounts, 'remaining, 'info, Withdraw<'info>>,
pub fn withdraw<'info>(
ctx: Context<'_, '_, '_, 'info, Withdraw<'info>>,
deposit_entry_index: u8,
amount: u64,
) -> Result<()> {
Expand Down Expand Up @@ -156,6 +156,5 @@ pub fn withdraw<'key, 'accounts, 'remaining, 'info>(
// since no other action other than deposit and withdraw could invalidate it
voter_weight_record.voter_weight_expiry = None;


Ok(())
}
12 changes: 5 additions & 7 deletions programs/token-voter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,25 @@ pub mod token_voter {
instructions::configure_mint_config(ctx, digit_shift)
}

pub fn deposit<'key, 'accounts, 'remaining, 'info>(
ctx: Context<'key, 'accounts, 'remaining, 'info, Deposit<'info>>,
pub fn deposit<'info>(
ctx: Context<'_, '_, '_, 'info, Deposit<'info>>,
deposit_entry_index: u8,
amount: u64,
) -> Result<()> {
log_version();
instructions::deposit(ctx, deposit_entry_index, amount)
}

pub fn withdraw<'key, 'accounts, 'remaining, 'info>(
ctx: Context<'key, 'accounts, 'remaining, 'info, Withdraw<'info>>,
pub fn withdraw<'info>(
ctx: Context<'_, '_, '_, 'info, Withdraw<'info>>,
deposit_entry_index: u8,
amount: u64,
) -> Result<()> {
log_version();
instructions::withdraw(ctx, deposit_entry_index, amount)
}

pub fn close_voter<'key, 'accounts, 'remaining, 'info>(
ctx: Context<'key, 'accounts, 'remaining, 'info, CloseVoter<'info>>,
) -> Result<()> {
pub fn close_voter<'info>(ctx: Context<'_, '_, '_, 'info, CloseVoter<'info>>) -> Result<()> {
log_version();
instructions::close_voter(ctx)
}
Expand Down
7 changes: 6 additions & 1 deletion programs/token-voter/src/state/deposit_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ impl DepositEntry {
reserved: [0; 38],
}
}

/// Initializes a vector of DepositEntry with a given length
pub fn init_deposits(length: usize) -> Vec<Self> {
vec![Self::new(); length]
Expand All @@ -52,3 +51,9 @@ impl DepositEntry {
Ok(vote_weight)
}
}

impl Default for DepositEntry {
fn default() -> Self {
Self::new()
}
}
7 changes: 3 additions & 4 deletions programs/token-voter/src/state/registrar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::{
error::TokenVoterError, id, state::VotingMintConfig,
vote_weight_record, max_voter_weight_record
error::TokenVoterError, id, max_voter_weight_record, state::VotingMintConfig,
vote_weight_record,
},
anchor_lang::{prelude::*, Discriminator},
solana_program::pubkey::PUBKEY_BYTES,
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Registrar {
.ok_or_else(|| error!(TokenVoterError::MintNotFound))
}

/// Returns the max vote weight based on the supply initially set for each mint
/// Returns the max vote weight based on the supply initially set for each mint
/// throws an error if the sum of the vote weights overflows
pub fn max_vote_weight(&self) -> Result<u64> {
self.voting_mint_configs
Expand Down Expand Up @@ -150,7 +150,6 @@ mod test {
assert_eq!(expected_space, actual_space);
}


#[test]
fn test_max_vote_weight() {
// Arrange
Expand Down
2 changes: 1 addition & 1 deletion programs/token-voter/src/state/voter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Voter {
);

let d = &mut self.deposits[index];
// if deposit_slot_hash is 0 then deposit is inactive
// if deposit_slot_hash is 0 then deposit is inactive
if d.deposit_slot_hash == 0 {
return Err(TokenVoterError::DepositIndexInactive.into());
}
Expand Down
1 change: 1 addition & 0 deletions programs/token-voter/src/tools/spl_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub fn transfer_checked_spl_tokens<'a>(

/// Transfers SPL Tokens checked from a token account owned by the provided PDA
/// authority with seeds
#[allow(clippy::too_many_arguments)]
pub fn transfer_spl_tokens_signed_checked<'a>(
source_info: &AccountInfo<'a>,
destination_info: &AccountInfo<'a>,
Expand Down

0 comments on commit 8aec2c0

Please sign in to comment.