Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change solana payers_balance function record metric if payers is conf… #603

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions mobile_packet_verifier/src/burner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,18 @@ where
},
) in payer_totals.into_iter()
{
tracing::info!(%total_dcs, %payer, "Burning DC");
let payer_balance = self
.solana
.payer_balance(&payer)
.await
.map_err(BurnError::SolanaError)?;

if payer_balance < total_dcs {
tracing::warn!(%payer, %payer_balance, %total_dcs, "Payer does not have enough balance to burn dcs");
continue;
}

tracing::info!(%total_dcs, %payer, "Burning DC");
if self
.solana
.burn_data_credits(&payer, total_dcs)
Expand Down Expand Up @@ -123,18 +133,6 @@ where
)
.await?;
}

// Fetch the balance after

metrics::gauge!(
"balance",
self
.solana
.payer_balance(&payer)
.await
.map_err(BurnError::SolanaError)? as f64,
"payer" => payer.to_string()
);
}

Ok(())
Expand Down
35 changes: 33 additions & 2 deletions solana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use solana_sdk::{
signer::Signer,
transaction::Transaction,
};
use std::collections::HashMap;
use std::convert::Infallible;
use std::{collections::HashMap, str::FromStr};
use std::{
sync::Arc,
time::{SystemTime, SystemTimeError},
Expand All @@ -43,7 +43,7 @@ pub enum SolanaRpcError {
#[error("Solana rpc error: {0}")]
RpcClientError(#[from] ClientError),
#[error("Anchor error: {0}")]
AnchorError(#[from] anchor_lang::error::Error),
AnchorError(Box<anchor_lang::error::Error>),
#[error("Solana program error: {0}")]
ProgramError(#[from] solana_sdk::program_error::ProgramError),
#[error("Parse pubkey error: {0}")]
Expand All @@ -54,6 +54,14 @@ pub enum SolanaRpcError {
SystemTimeError(#[from] SystemTimeError),
#[error("Failed to read keypair file")]
FailedToReadKeypairError,
#[error("crypto error: {0}")]
Crypto(#[from] helium_crypto::Error),
}

impl From<anchor_lang::error::Error> for SolanaRpcError {
fn from(err: anchor_lang::error::Error) -> Self {
Self::AnchorError(Box::new(err))
}
}

#[derive(Debug, Deserialize)]
Expand All @@ -63,13 +71,26 @@ pub struct Settings {
burn_keypair: String,
dc_mint: String,
dnt_mint: String,
#[serde(default)]
payers_to_monitor: Vec<String>,
}

impl Settings {
pub fn payers_to_monitor(&self) -> Result<Vec<PublicKeyBinary>, SolanaRpcError> {
self.payers_to_monitor
.iter()
.map(|payer| PublicKeyBinary::from_str(payer))
.collect::<Result<_, _>>()
.map_err(SolanaRpcError::from)
}
}

pub struct SolanaRpc {
provider: RpcClient,
program_cache: BurnProgramCache,
cluster: String,
keypair: [u8; 64],
payers_to_monitor: Vec<PublicKeyBinary>,
}

impl SolanaRpc {
Expand All @@ -90,6 +111,7 @@ impl SolanaRpc {
provider,
program_cache,
keypair: keypair.to_bytes(),
payers_to_monitor: settings.payers_to_monitor()?,
}))
}
}
Expand All @@ -110,6 +132,15 @@ impl SolanaNetwork for SolanaRpc {
return Ok(0);
};
let account_layout = spl_token::state::Account::unpack(account_data.as_slice())?;

if self.payers_to_monitor.contains(payer) {
metrics::gauge!(
"balance",
account_layout.amount as f64,
"payer" => payer.to_string()
);
}

Ok(account_layout.amount)
}

Expand Down
Loading