Skip to content

Commit

Permalink
feat: use constants from external crate
Browse files Browse the repository at this point in the history
  • Loading branch information
n00m4d committed Sep 16, 2024
1 parent c187cba commit de0c59c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 31 deletions.
2 changes: 2 additions & 0 deletions clients/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ impl LeafSchemaEvent {
}
}
}

pub const MAX_ACC_PROOFS_SIZE: u32 = 17;
1 change: 1 addition & 0 deletions programs/bubblegum/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ spl-associated-token-account = { version = ">= 1.1.3, < 3.0", features = ["no-en
spl-token = { version = ">= 3.5.0, < 5.0", features = ["no-entrypoint"] }
mplx-staking-states = { git = "https://github.com/adm-metaex/mplx-staking.git" }
mplx-rewards = { git = "https://github.com/adm-metaex/mplx-rewards.git", features = ["no-entrypoint"] }
mpl-common-constants = { git = "https://github.com/adm-metaex/mpl-common-constants.git", branch = "main", features = ["mainnet"] }

[dev-dependencies]
async-trait = "0.1.71"
Expand Down
2 changes: 2 additions & 0 deletions programs/bubblegum/program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ pub enum BubblegumError {
StakingVoterAuthorityMismatch,
#[msg("Invalid mining owner")]
MiningOwnerMismatch,
#[msg("Cannot create pubkey from string")]
PubkeyReconstruction,
}

// Converts certain Token Metadata errors into Bubblegum equivalents
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use std::str::FromStr;

use anchor_lang::{prelude::*, solana_program::clock::Clock, system_program::System};
use mplx_staking_states::state::{registrar::Registrar, Voter, REGISTRAR_DISCRIMINATOR};
use spl_account_compression::{program::SplAccountCompression, Noop};

use crate::{
error::BubblegumError,
state::{
TreeConfig, FEE_RECEIVER, MINIMUM_WEIGHTED_STAKE, PROTOCOL_FEE_PER_1024_ASSETS, REALM,
REALM_GOVERNING_MINT, VOTER_DISCRIMINATOR,
TreeConfig, MINIMUM_WEIGHTED_STAKE, PROTOCOL_FEE_PER_1024_ASSETS, VOTER_DISCRIMINATOR,
},
};

use mpl_common_constants::constants::{DAO_GOVERNING_MINT, DAO_PUBKEY, FEE_RECEIVER};

const DISCRIMINATOR_LEN: usize = REGISTRAR_DISCRIMINATOR.len();
#[derive(Accounts)]
pub struct FinalizeTreeWithRoot<'info> {
Expand Down Expand Up @@ -57,7 +60,7 @@ pub(crate) fn finalize_tree_with_root<'info>(
);

require!(
ctx.accounts.fee_receiver.key == &FEE_RECEIVER,
ctx.accounts.fee_receiver.key.to_string() == FEE_RECEIVER,
BubblegumError::FeeReceiverMismatch
);
check_stake(
Expand Down Expand Up @@ -126,11 +129,16 @@ pub(crate) fn check_stake<'info>(
BubblegumError::MiningOwnerMismatch
);

let realm_key =
Pubkey::from_str(DAO_PUBKEY).map_err(|_| BubblegumError::PubkeyReconstruction)?;
let realm_governing_mint_key =
Pubkey::from_str(DAO_GOVERNING_MINT).map_err(|_| BubblegumError::PubkeyReconstruction)?;

let generated_registrar = Pubkey::find_program_address(
&[
REALM.to_bytes().as_ref(),
realm_key.to_bytes().as_ref(),
b"registrar".as_ref(),
REALM_GOVERNING_MINT.to_bytes().as_ref(),
realm_governing_mint_key.to_bytes().as_ref(),
],
&mplx_staking_states::ID,
)
Expand Down Expand Up @@ -164,11 +172,11 @@ pub(crate) fn check_stake<'info>(
let registrar: &Registrar = bytemuck::from_bytes(&registrar_bytes[DISCRIMINATOR_LEN..]);

require!(
registrar.realm == REALM,
registrar.realm.to_string() == DAO_PUBKEY,
BubblegumError::StakingRegistrarRealmMismatch
);
require!(
registrar.realm_governing_token_mint == REALM_GOVERNING_MINT,
registrar.realm_governing_token_mint.to_string() == DAO_GOVERNING_MINT,
BubblegumError::StakingRegistrarRealmMismatch
);
let voter_bytes = voter_acc.to_account_info().data;
Expand Down
7 changes: 0 additions & 7 deletions programs/bubblegum/program/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ pub const COLLECTION_CPI_PREFIX: &str = "collection_cpi";

pub const MAX_ACC_PROOFS_SIZE: u32 = 17;

// TODO: set real keys before mainnet deploy
pub const REALM: Pubkey = solana_program::pubkey!("EzsKaQq68FLZwRaiUx7t17LWVVzsE8wRkhBghFrZGGwG");
pub const REALM_GOVERNING_MINT: Pubkey =
solana_program::pubkey!("Dqa4iCUDXvSh5FwhopFJM76xdxQb5vSw39LvggbUWH9o");
pub const FEE_RECEIVER: Pubkey =
solana_program::pubkey!("EzsKaQq61FLZwRaiUx7t17LWVVzsE8wRkhBghFrZGGwG");

pub const VOTER_DISCRIMINATOR: [u8; 8] = [241, 93, 35, 191, 254, 147, 17, 202];

pub const MINIMUM_WEIGHTED_STAKE: u64 = 30_000_000_000_000; // 30 weighted MPLX
Expand Down
49 changes: 32 additions & 17 deletions programs/bubblegum/program/tests/batch-mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use bubblegum::{
error::BubblegumError,
state::{
metaplex_adapter::{MetadataArgs, TokenProgramVersion, TokenStandard},
FEE_RECEIVER, PROTOCOL_FEE_PER_1024_ASSETS, REALM, REALM_GOVERNING_MINT,
VOTER_DISCRIMINATOR,
PROTOCOL_FEE_PER_1024_ASSETS, VOTER_DISCRIMINATOR,
},
};
use mpl_common_constants::constants::{DAO_GOVERNING_MINT, DAO_PUBKEY, FEE_RECEIVER};
use mplx_staking_states::state::{
DepositEntry, Lockup, LockupKind, LockupPeriod, Registrar, Voter, VotingMintConfig,
REGISTRAR_DISCRIMINATOR,
Expand Down Expand Up @@ -155,11 +155,14 @@ async fn initialize_staking_accounts(
let mining_key = Pubkey::new_unique();
let reward_pool_key = Pubkey::new_unique();

let realm_key = Pubkey::from_str(DAO_PUBKEY).unwrap();
let realm_governing_mint_key = Pubkey::from_str(DAO_GOVERNING_MINT).unwrap();

let registrar_key = Pubkey::find_program_address(
&[
REALM.to_bytes().as_ref(),
realm_key.to_bytes().as_ref(),
b"registrar".as_ref(),
REALM_GOVERNING_MINT.to_bytes().as_ref(),
realm_governing_mint_key.to_bytes().as_ref(),
],
&mplx_staking_states::ID,
)
Expand All @@ -182,8 +185,8 @@ async fn initialize_staking_accounts(

let registrar = Registrar {
governance_program_id: governance_program_id.clone(),
realm: REALM,
realm_governing_token_mint: REALM_GOVERNING_MINT,
realm: realm_key,
realm_governing_token_mint: realm_governing_mint_key,
realm_authority: realm_authority.clone(),
voting_mints: [voting_mint_config, voting_mint_config],
reward_pool: reward_pool_key,
Expand Down Expand Up @@ -301,17 +304,19 @@ async fn test_prepare_tree_without_canopy() {
let (registrar_key, voter_key, mining_key) =
initialize_staking_accounts(&mut program_context).await;

let fee_receiver = Pubkey::from_str(FEE_RECEIVER).unwrap();

program_context
.fund_account(tree.creator_pubkey(), 10_000_000_000)
.await
.unwrap();
program_context
.fund_account(FEE_RECEIVER, 10_000_000_000)
.fund_account(fee_receiver, 10_000_000_000)
.await
.unwrap();
let start_fee_receiver_balance = program_context
.client()
.get_account(FEE_RECEIVER)
.get_account(fee_receiver)
.await
.unwrap()
.unwrap()
Expand Down Expand Up @@ -345,7 +350,7 @@ async fn test_prepare_tree_without_canopy() {
registrar_key,
voter_key,
mining_key,
FEE_RECEIVER,
fee_receiver,
);

for proof in rightmost_proof {
Expand All @@ -360,7 +365,7 @@ async fn test_prepare_tree_without_canopy() {

let end_fee_receiver_balance = program_context
.client()
.get_account(FEE_RECEIVER)
.get_account(fee_receiver)
.await
.unwrap()
.unwrap()
Expand Down Expand Up @@ -441,6 +446,8 @@ async fn test_prepare_tree_with_canopy() {
.unwrap();
}

let fee_receiver = Pubkey::from_str(FEE_RECEIVER).unwrap();

let mut tree_tx_builder = tree.finalize_tree_with_root_tx(
&program_context.test_context().payer,
&tree_creator,
Expand All @@ -452,7 +459,7 @@ async fn test_prepare_tree_with_canopy() {
registrar_key,
voter_key,
mining_key,
FEE_RECEIVER,
fee_receiver,
);

for proof in rightmost_proof[..canopy_depth as usize].iter() {
Expand Down Expand Up @@ -520,6 +527,8 @@ async fn test_put_wrong_canopy() {
.unwrap();
}

let fee_receiver = Pubkey::from_str(FEE_RECEIVER).unwrap();

let mut tree_tx_builder = tree.finalize_tree_with_root_tx(
&program_context.test_context().payer,
&tree_creator,
Expand All @@ -531,7 +540,7 @@ async fn test_put_wrong_canopy() {
registrar_key,
voter_key,
mining_key,
FEE_RECEIVER,
fee_receiver,
);

for proof in rightmost_proof {
Expand Down Expand Up @@ -628,12 +637,14 @@ async fn test_put_wrong_fee_receiver() {
let (registrar_key, voter_key, mining_key) =
initialize_staking_accounts(&mut program_context).await;

let fee_receiver = Pubkey::from_str(FEE_RECEIVER).unwrap();

program_context
.fund_account(tree.creator_pubkey(), 10_000_000_000)
.await
.unwrap();
program_context
.fund_account(FEE_RECEIVER, 10_000_000_000)
.fund_account(fee_receiver, 10_000_000_000)
.await
.unwrap();

Expand Down Expand Up @@ -707,6 +718,8 @@ async fn test_prepare_tree_with_collection() {
)
.await;

let fee_receiver = Pubkey::from_str(FEE_RECEIVER).unwrap();

let rightmost_proof = tree.proof_of_leaf((num_of_assets_to_mint - 1) as u32);
let rightmost_leaf = tree.get_node(num_of_assets_to_mint - 1);

Expand All @@ -718,7 +731,7 @@ async fn test_prepare_tree_with_collection() {
.await
.unwrap();
program_context
.fund_account(FEE_RECEIVER, 10_000_000_000)
.fund_account(fee_receiver, 10_000_000_000)
.await
.unwrap();

Expand All @@ -745,7 +758,7 @@ async fn test_prepare_tree_with_collection() {
registrar_key,
voter_key,
mining_key,
FEE_RECEIVER,
fee_receiver,
);

for proof in rightmost_proof {
Expand Down Expand Up @@ -778,6 +791,8 @@ async fn test_prepare_tree_with_collection_wrong_authority() {
)
.await;

let fee_receiver = Pubkey::from_str(FEE_RECEIVER).unwrap();

let rightmost_leaf = tree.get_node(num_of_assets_to_mint - 1);

let (registrar_key, voter_key, mining_key) =
Expand All @@ -788,7 +803,7 @@ async fn test_prepare_tree_with_collection_wrong_authority() {
.await
.unwrap();
program_context
.fund_account(FEE_RECEIVER, 10_000_000_000)
.fund_account(fee_receiver, 10_000_000_000)
.await
.unwrap();

Expand All @@ -815,7 +830,7 @@ async fn test_prepare_tree_with_collection_wrong_authority() {
registrar_key,
voter_key,
mining_key,
FEE_RECEIVER,
fee_receiver,
);

let res = tree_tx_builder.execute().await;
Expand Down

0 comments on commit de0c59c

Please sign in to comment.