Skip to content

Commit

Permalink
Merge pull request #5 from adm-metaex/feature/docs
Browse files Browse the repository at this point in the history
rust fmt file with the reformatting
  • Loading branch information
kstepanovdev authored Jul 3, 2024
2 parents 237e096 + 036713f commit 0946937
Show file tree
Hide file tree
Showing 35 changed files with 157 additions and 174 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Users can:
* Run rust based tests - `cargo test-bpf`
* To install the typescript client, do - `yarn add @blockworks-foundation/voter-stake-registry-client`
* usage
* Run `cargo +nightly fmt` before pushing your changes

## Node/Typescript
* Built and developed using - node (`v16.13.1`)
Expand Down
4 changes: 2 additions & 2 deletions program-states/src/state/deposit_entry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::state::lockup::{Lockup, LockupKind};

use anchor_lang::prelude::*;

/// Bookkeeping for a single deposit for a given mint and lockup schedule.
Expand Down Expand Up @@ -64,7 +63,8 @@ impl DepositEntry {
#[cfg(test)]
mod tests {
use super::*;
use crate::state::{LockupKind::Constant, LockupPeriod, VotingMintConfig};
use crate::state::LockupKind::Constant;
use crate::state::{LockupPeriod, VotingMintConfig};

#[test]
pub fn far_future_lockup_start_test() -> Result<()> {
Expand Down
17 changes: 7 additions & 10 deletions programs/voter-stake-registry/src/cpi_instructions.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use anchor_lang::prelude::borsh;
use anchor_lang::Key;
use anchor_lang::{prelude::Pubkey, AnchorDeserialize, AnchorSerialize};
use anchor_lang::prelude::{borsh, Pubkey};
use anchor_lang::{AnchorDeserialize, AnchorSerialize, Key};
use mplx_staking_states::state::LockupPeriod;
use solana_program::{
account_info::AccountInfo,
entrypoint::ProgramResult,
instruction::{AccountMeta, Instruction},
program::{invoke, invoke_signed},
system_program,
};
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
use solana_program::instruction::{AccountMeta, Instruction};
use solana_program::program::{invoke, invoke_signed};
use solana_program::system_program;

pub const REWARD_CONTRACT_ID: Pubkey =
solana_program::pubkey!("BF5PatmRTQDgEKoXR7iHRbkibEEi83nVM38cUKWzQcTR");
Expand Down
6 changes: 3 additions & 3 deletions programs/voter-stake-registry/src/instructions/claim.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::borrow::Borrow;

use crate::borsh::BorshDeserialize;
use crate::cpi_instructions;
use anchor_lang::prelude::*;
use anchor_spl::token::{Token, TokenAccount};
use mplx_staking_states::error::VsrError;
use solana_program::program::get_return_data;
use std::borrow::Borrow;

#[derive(Accounts)]
pub struct Claim<'info> {
Expand All @@ -23,7 +22,8 @@ pub struct Claim<'info> {
pub vault: UncheckedAccount<'info>,

/// CHECK: mining PDA will be checked in the rewards contract
/// PDA(["mining", mining owner <aka voter_authority in our case>, reward_pool], reward_program)
/// PDA(["mining", mining owner <aka voter_authority in our case>, reward_pool],
/// reward_program)
#[account(mut)]
pub deposit_mining: UncheckedAccount<'info>,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use anchor_lang::prelude::*;
use mplx_staking_states::{
error::VsrError,
state::{DepositEntry, Voter},
};
use mplx_staking_states::error::VsrError;
use mplx_staking_states::state::{DepositEntry, Voter};

#[derive(Accounts)]
pub struct CloseDepositEntry<'info> {
Expand Down
9 changes: 3 additions & 6 deletions programs/voter-stake-registry/src/instructions/close_voter.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::ops::DerefMut;

use crate::clock_unix_timestamp;
use anchor_lang::prelude::*;
use anchor_spl::token::Transfer;
use anchor_spl::token::{self, CloseAccount, Token, TokenAccount};
use anchor_spl::token::{self, CloseAccount, Token, TokenAccount, Transfer};
use bytemuck::bytes_of_mut;
use mplx_staking_states::error::VsrError;
use mplx_staking_states::state::{Registrar, Voter};
use mplx_staking_states::voter_seeds;

use crate::clock_unix_timestamp;
use std::ops::DerefMut;

// Remaining accounts must be all the token token accounts owned by voter, he wants to close,
// they should be writable so that they can be closed and sol required for rent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use anchor_lang::prelude::*;
use anchor_spl::token::Mint;
use mplx_staking_states::{
error::VsrError,
state::{Registrar, VotingMintConfig},
};
use mplx_staking_states::error::VsrError;
use mplx_staking_states::state::{Registrar, VotingMintConfig};

// Remaining accounts must be all the token mints that have registered
// as voting mints, including the newly registered one.
Expand All @@ -25,10 +23,11 @@ pub struct ConfigureVotingMint<'info> {
///
/// * `idx`: index of the rate to be set
/// * `digit_shift`: how many digits to shift the native token amount, see below
/// * `baseline_vote_weight_scaled_factor`: vote weight factor for all funds in vault, in 1/1e9 units
/// * `baseline_vote_weight_scaled_factor`: vote weight factor for all funds in vault, in 1/1e9
/// units
/// * `max_extra_lockup_vote_weight_scaled_factor`: max extra weight for lockups, in 1/1e9 units
/// * `lockup_saturation_secs`: lockup duration at which the full vote weight
/// bonus is given to locked up deposits
/// * `lockup_saturation_secs`: lockup duration at which the full vote weight bonus is given to
/// locked up deposits
///
/// This instruction can be called several times for the same mint and index to
/// change the voting mint configuration.
Expand All @@ -49,28 +48,31 @@ pub struct ConfigureVotingMint<'info> {
/// do your own checking too.
///
/// If you use a single mint, prefer digit_shift=0 and baseline_vote_weight_scaled_factor +
/// max_extra_lockup_vote_weight_scaled_factor <= 1e9. That way you won't have issues with overflow no
/// matter the size of the mint's supply.
/// max_extra_lockup_vote_weight_scaled_factor <= 1e9. That way you won't have issues with overflow
/// no matter the size of the mint's supply.
///
/// Digit shifting is particularly useful when using several voting token mints
/// that have a different number of decimals. It can be used to align them to
/// a common number of decimals.
///
/// Example: If you have token A with 6 decimals and token B with 9 decimals, you
/// could set up:
/// * A with digit_shift=0, baseline_vote_weight_scaled_factor=2e9, max_extra_lockup_vote_weight_scaled_factor=0
/// * B with digit_shift=-3, baseline_vote_weight_scaled_factor=1e9, max_extra_lockup_vote_weight_scaled_factor=1e9
/// * A with digit_shift=0, baseline_vote_weight_scaled_factor=2e9,
/// max_extra_lockup_vote_weight_scaled_factor=0
/// * B with digit_shift=-3, baseline_vote_weight_scaled_factor=1e9,
/// max_extra_lockup_vote_weight_scaled_factor=1e9
///
/// That would make 1.0 decimaled tokens of A as valuable as 2.0 decimaled tokens
/// of B when unlocked. B tokens could be locked up to double their vote weight. As
/// long as A's and B's supplies are below 2^63, there could be no overflow.
///
/// Note that in this example, you need 1000 native B tokens before receiving 1
/// unit of vote weight. If the supplies were significantly lower, you could use
/// * A with digit_shift=3, baseline_vote_weight_scaled_factor=2e9, max_extra_lockup_vote_weight_scaled_factor=0
/// * B with digit_shift=0, baseline_vote_weight_scaled_factor=1e9, max_extra_lockup_vote_weight_scaled_factor=1e9
/// * A with digit_shift=3, baseline_vote_weight_scaled_factor=2e9,
/// max_extra_lockup_vote_weight_scaled_factor=0
/// * B with digit_shift=0, baseline_vote_weight_scaled_factor=1e9,
/// max_extra_lockup_vote_weight_scaled_factor=1e9
/// to not lose precision on B tokens.
///
pub fn configure_voting_mint(
ctx: Context<ConfigureVotingMint>,
idx: u16,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::clock_unix_timestamp;
use anchor_lang::prelude::*;
use anchor_spl::associated_token::AssociatedToken;
use anchor_spl::token::{Mint, Token, TokenAccount};
use mplx_staking_states::error::VsrError;
use mplx_staking_states::state::{DepositEntry, Lockup, LockupKind, Voter};
use mplx_staking_states::state::{LockupPeriod, Registrar};

use crate::clock_unix_timestamp;
use mplx_staking_states::state::{
DepositEntry, Lockup, LockupKind, LockupPeriod, Registrar, Voter,
};

#[derive(Accounts)]
pub struct CreateDepositEntry<'info> {
Expand Down Expand Up @@ -47,8 +47,8 @@ pub struct CreateDepositEntry<'info> {
///
/// - `deposit_entry_index`: deposit entry to use
/// - `kind`: Type of lockup to use.
/// - `start_ts`: Start timestamp in seconds, defaults to current clock.
/// The lockup will end after `start + LockupPeriod::to_ts + COOLDOWNS_SECS.
/// - `start_ts`: Start timestamp in seconds, defaults to current clock. The lockup will end after
/// `start + LockupPeriod::to_ts + COOLDOWNS_SECS.
///
/// Note that tokens will already be locked before start_ts, it only defines
/// the vesting start time and the anchor for the periods computation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::cpi_instructions;
use anchor_lang::prelude::*;
use anchor_spl::token::Mint;
use anchor_spl::token::Token;
use anchor_spl::token::{Mint, Token};
use mplx_staking_states::error::VsrError;
use mplx_staking_states::state::Registrar;
use spl_governance::state::realm;
use std::mem::size_of;

use crate::cpi_instructions;

#[derive(Accounts)]
pub struct CreateRegistrar<'info> {
/// The voting registrar. There can only be a single registrar
Expand Down
14 changes: 7 additions & 7 deletions programs/voter-stake-registry/src/instructions/create_voter.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::cpi_instructions;
use crate::voter::VoterWeightRecord;
use anchor_lang::prelude::*;
use anchor_lang::solana_program::sysvar::instructions as tx_instructions;
use mplx_staking_states::error::VsrError;
use mplx_staking_states::state::Registrar;
use mplx_staking_states::state::Voter;
use mplx_staking_states::state::{Registrar, Voter};
use std::mem::size_of;

use crate::cpi_instructions;
use crate::voter::VoterWeightRecord;

#[derive(Accounts)]
pub struct CreateVoter<'info> {
/// Also, Registrar plays the role of deposit_authority on the Rewards Contract,
Expand Down Expand Up @@ -51,12 +49,14 @@ pub struct CreateVoter<'info> {
pub instructions: UncheckedAccount<'info>,

/// CHECK: Reward Pool PDA will be checked in the rewards contract
/// PDA(["reward_pool", deposit_authority <aka registrar in our case>, fill_authority], reward_program)
/// PDA(["reward_pool", deposit_authority <aka registrar in our case>, fill_authority],
/// reward_program)
#[account(mut)]
pub reward_pool: UncheckedAccount<'info>,

/// CHECK: mining PDA will be checked in the rewards contract
/// PDA(["mining", mining owner <aka voter_authority in our case>, reward_pool], reward_program)
/// PDA(["mining", mining owner <aka voter_authority in our case>, reward_pool],
/// reward_program)
#[account(mut)]
pub deposit_mining: UncheckedAccount<'info>,

Expand Down
9 changes: 3 additions & 6 deletions programs/voter-stake-registry/src/instructions/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::clock_unix_timestamp;
use anchor_lang::prelude::*;
use anchor_spl::token::{self, Token, TokenAccount};
use mplx_staking_states::{
error::VsrError,
state::{LockupKind, LockupPeriod, Registrar, Voter},
};

use crate::clock_unix_timestamp;
use mplx_staking_states::error::VsrError;
use mplx_staking_states::state::{LockupKind, LockupPeriod, Registrar, Voter};

#[derive(Accounts)]
pub struct Deposit<'info> {
Expand Down
17 changes: 7 additions & 10 deletions programs/voter-stake-registry/src/instructions/extend_deposit.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use crate::clock_unix_timestamp;
use crate::cpi_instructions::extend_deposit;
use anchor_lang::prelude::*;
use anchor_spl::token::{self, Token, TokenAccount, Transfer};
use mplx_staking_states::error::VsrError;
use mplx_staking_states::state::LockupKind;
use mplx_staking_states::state::LockupPeriod;
use mplx_staking_states::state::Registrar;
use mplx_staking_states::state::Voter;

use crate::clock_unix_timestamp;
use crate::cpi_instructions::extend_deposit;
use mplx_staking_states::state::{LockupKind, LockupPeriod, Registrar, Voter};

#[derive(Accounts)]
pub struct RestakeDeposit<'info> {
Expand Down Expand Up @@ -43,7 +39,8 @@ pub struct RestakeDeposit<'info> {
pub reward_pool: UncheckedAccount<'info>,

/// CHECK: mining PDA will be checked in the rewards contract
/// PDA(["mining", mining owner <aka voter_authority in our case>, reward_pool], reward_program)
/// PDA(["mining", mining owner <aka voter_authority in our case>, reward_pool],
/// reward_program)
#[account(mut)]
pub deposit_mining: UncheckedAccount<'info>,

Expand All @@ -65,8 +62,8 @@ impl<'info> RestakeDeposit<'info> {

/// Prolongs the deposit
///
/// The deposit will be restaked with the same lockup period as it was previously in case it's not expired.
/// If the deposit has expired, it can be restaked with any LockupPeriod.
/// The deposit will be restaked with the same lockup period as it was previously in case it's not
/// expired. If the deposit has expired, it can be restaked with any LockupPeriod.
/// The deposit entry must have been initialized with create_deposit_entry.
///
/// `deposit_entry_index`: Index of the deposit entry.
Expand Down
20 changes: 9 additions & 11 deletions programs/voter-stake-registry/src/instructions/lock_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use crate::{clock_unix_timestamp, cpi_instructions};
use anchor_lang::prelude::*;
use mplx_staking_states::error::VsrError;
use mplx_staking_states::state::LockupKind;
use mplx_staking_states::state::Registrar;
use mplx_staking_states::state::Voter;

use crate::clock_unix_timestamp;
use crate::cpi_instructions;
use mplx_staking_states::state::{LockupKind, Registrar, Voter};

#[derive(Accounts)]
pub struct LockTokens<'info> {
Expand All @@ -23,12 +19,14 @@ pub struct LockTokens<'info> {
pub voter_authority: Signer<'info>,

/// CHECK: Reward Pool PDA will be checked in the rewards contract
/// PDA(["reward_pool", deposit_authority <aka registrar in our case>, fill_authority], reward_program)
/// PDA(["reward_pool", deposit_authority <aka registrar in our case>, fill_authority],
/// reward_program)
#[account(mut)]
pub reward_pool: UncheckedAccount<'info>,

/// CHECK: mining PDA will be checked in the rewards contract
/// PDA(["mining", mining owner <aka voter_authority in our case>, reward_pool], reward_program)
/// PDA(["mining", mining owner <aka voter_authority in our case>, reward_pool],
/// reward_program)
#[account(mut)]
pub deposit_mining: UncheckedAccount<'info>,

Expand All @@ -39,9 +37,9 @@ pub struct LockTokens<'info> {

/// Transfers unlocked tokens from the source deposit entry to the target deposit entry.
///
/// Transfers token from one DepositEntry that is not LockupKind::None to another that is LockupKind::Constant.
/// In terms of business logic that means we want to deposit some tokens on DAO,
/// then we want to lock them up in order to receice rewards
/// Transfers token from one DepositEntry that is not LockupKind::None to another that is
/// LockupKind::Constant. In terms of business logic that means we want to deposit some tokens on
/// DAO, then we want to lock them up in order to receice rewards
pub fn lock_tokens(
ctx: Context<LockTokens>,
source_deposit_entry_index: u8,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::clock_unix_timestamp;
use crate::events::*;

use anchor_lang::prelude::*;
use mplx_staking_states::state::LockupKind;
use mplx_staking_states::state::Registrar;
use mplx_staking_states::state::Voter;
use mplx_staking_states::state::{LockupKind, Registrar, Voter};

#[derive(Accounts)]
pub struct LogVoterInfo<'info> {
Expand Down
3 changes: 2 additions & 1 deletion programs/voter-stake-registry/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub use deposit::*;
pub use extend_deposit::*;
pub use lock_tokens::*;
pub use log_voter_info::*;
use solana_program::{clock::Clock, sysvar::Sysvar};
use solana_program::clock::Clock;
use solana_program::sysvar::Sysvar;
pub use unlock_tokens::*;
pub use update_voter_weight_record::*;
pub use withdraw::*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::clock_unix_timestamp;
use anchor_lang::prelude::*;
use mplx_staking_states::error::VsrError;
use mplx_staking_states::state::Registrar;
use mplx_staking_states::state::Voter;
use mplx_staking_states::state::COOLDOWN_SECS;

use crate::clock_unix_timestamp;
use mplx_staking_states::state::{Registrar, Voter, COOLDOWN_SECS};

#[derive(Accounts)]
pub struct UnlockTokens<'info> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::voter::VoterWeightRecord;
use anchor_lang::prelude::*;
use mplx_staking_states::state::{Registrar, Voter};

use crate::voter::VoterWeightRecord;

#[derive(Accounts)]
pub struct UpdateVoterWeightRecord<'info> {
pub registrar: AccountLoader<'info, Registrar>,
Expand Down
7 changes: 3 additions & 4 deletions programs/voter-stake-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ declare_id!("9XZ7Ku7FYGVk3veKba6BRKTFXoYJyh4b4ZHC6MfaTUE8");
///
/// - Create a SPL governance realm.
/// - Create a governance registry account.
/// - Add exchange rates for any tokens one wants to deposit. For example,
/// if one wants to vote with tokens A and B, where token B has twice the
/// voting power of token A, then the exchange rate of B would be 2 and the
/// exchange rate of A would be 1.
/// - Add exchange rates for any tokens one wants to deposit. For example, if one wants to vote with
/// tokens A and B, where token B has twice the voting power of token A, then the exchange rate of
/// B would be 2 and the exchange rate of A would be 1.
/// - Create a voter account.
/// - Deposit tokens into this program, with an optional lockup period.
/// - Vote.
Expand Down
Loading

0 comments on commit 0946937

Please sign in to comment.