Skip to content

Commit

Permalink
sys-lend: add priority fees metric
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Jun 10, 2024
1 parent 4e974c8 commit 714ddf9
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 42 deletions.
56 changes: 39 additions & 17 deletions src/bin/sys-lend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use {
commitment_config::CommitmentConfig,
instruction::{AccountMeta, Instruction},
message::{self, Message, VersionedMessage},
native_token::sol_to_lamports,
native_token::{lamports_to_sol, sol_to_lamports},
program_pack::Pack,
pubkey,
pubkey::Pubkey,
Expand Down Expand Up @@ -93,7 +93,7 @@ mod dp {
pub fn supply_balance(
pool: &str,
address: &Pubkey,
maybe_token: Token,
maybe_token: MaybeToken,
ui_amount: f64,
) -> metrics::Point {
metrics::Point::new("sys_lend::supply_balance")
Expand All @@ -103,7 +103,7 @@ mod dp {
.field("amount", ui_amount)
}

pub fn supply_apy(pool: &str, maybe_token: Token, apy_bps: u64) -> metrics::Point {
pub fn supply_apy(pool: &str, maybe_token: MaybeToken, apy_bps: u64) -> metrics::Point {
metrics::Point::new("sys_lend::supply_apy")
.tag("pool", pool)
.tag("token", maybe_token.name())
Expand All @@ -113,7 +113,7 @@ mod dp {
pub fn principal_balance_change(
pool: &str,
address: &Pubkey,
maybe_token: Token,
maybe_token: MaybeToken,
ui_amount: f64,
) -> metrics::Point {
metrics::Point::new("sys_lend::principal_balance_change")
Expand All @@ -122,6 +122,17 @@ mod dp {
.tag("token", maybe_token.name())
.field("amount", ui_amount)
}

pub fn priority_fee(
address: &Pubkey,
maybe_token: MaybeToken,
priority_fee: f64,
) -> metrics::Point {
metrics::Point::new("sys_lend::priority_fee")
.tag("address", metrics::dp::pubkey_to_value(address))
.tag("token", maybe_token.name())
.field("priority_fee", priority_fee)
}
}

fn is_token_supported(token: &Token, pools: &[String]) -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -742,7 +753,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if !raw {
notifier.send(&msg).await;
}
metrics::push(dp::supply_apy(&pool, token, apy_as_bps)).await;
metrics::push(dp::supply_apy(&pool, maybe_token, apy_as_bps)).await;
println!("{msg}");
}
}
Expand Down Expand Up @@ -793,7 +804,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
metrics::push(dp::supply_balance(
pool,
&address,
token,
maybe_token,
token.ui_amount(amount),
))
.await;
Expand Down Expand Up @@ -1426,10 +1437,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (recent_blockhash, last_valid_block_height) =
rpc_client.get_latest_blockhash_with_commitment(rpc_client.commitment())?;

let transaction = {
let (transaction, priority_fee) = {
let mut instructions = instructions;

apply_priority_fee(
let priority_fee = apply_priority_fee(
rpc_client,
&mut instructions,
required_compute_units,
Expand All @@ -1442,25 +1453,36 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
&address_lookup_table_accounts,
recent_blockhash,
)?;
VersionedTransaction::try_new(VersionedMessage::V0(message), &vec![signer])?
(
VersionedTransaction::try_new(VersionedMessage::V0(message), &vec![signer])?,
priority_fee,
)
};
let signature = transaction.signatures[0];

if !send_transaction_until_expired(&rpc_clients, &transaction, last_valid_block_height)
{
let transaction_result =
send_transaction_until_expired(&rpc_clients, &transaction, last_valid_block_height);
if transaction_result.is_some() {
metrics::push(dp::priority_fee(
&address,
maybe_token,
lamports_to_sol(priority_fee),
))
.await;
}
if transaction_result.unwrap_or_default() {
let msg = format!("Transaction failed: {signature}");
notifier.send(&msg).await;
return Err(msg.into());
} else {
println!("Transaction confirmed: {signature}");
}
println!("Transaction confirmed: {signature}");

match cmd {
Command::Deposit => {
metrics::push(dp::principal_balance_change(
deposit_pool,
&address,
token,
maybe_token,
token.ui_amount(amount),
))
.await;
Expand All @@ -1469,7 +1491,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
metrics::push(dp::principal_balance_change(
withdraw_pool,
&address,
token,
maybe_token,
-token.ui_amount(amount),
))
.await;
Expand All @@ -1478,14 +1500,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
metrics::push(dp::principal_balance_change(
withdraw_pool,
&address,
token,
maybe_token,
-token.ui_amount(amount),
))
.await;
metrics::push(dp::principal_balance_change(
deposit_pool,
&address,
token,
maybe_token,
token.ui_amount(amount),
))
.await;
Expand Down
23 changes: 13 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ pub fn send_transaction_until_expired(
rpc_clients: &RpcClients,
transaction: &impl SerializableTransaction,
last_valid_block_height: u64,
) -> bool {
) -> Option<bool> {
send_transaction_until_expired_with_slot(rpc_clients, transaction, last_valid_block_height)
.is_some()
.map(|(_context_slot, success)| success)
}

// Same as `send_transaction_until_expired` but on success returns a `Slot` that the transaction
Expand All @@ -93,7 +93,7 @@ fn send_transaction_until_expired_with_slot(
rpc_clients: &RpcClients,
transaction: &impl SerializableTransaction,
last_valid_block_height: u64,
) -> Option<Slot> {
) -> Option<(Slot, bool)> {
let mut last_send_attempt = None;

let config = RpcSendTransactionConfig {
Expand Down Expand Up @@ -130,13 +130,16 @@ fn send_transaction_until_expired_with_slot(
Ok(rpc_response::Response { context, value }) => {
let confirmation_context_slot = context.slot;
if let Some(ref transaction_status) = value[0] {
match transaction_status.err {
None => return Some(confirmation_context_slot),
Some(ref err) => {
println!("Transaction failed: {err}");
return None;
}
}
return Some((
confirmation_context_slot,
match transaction_status.err {
None => true,
Some(ref err) => {
println!("Transaction failed: {err}");
false
}
},
));
} else {
match rpc_clients.default().get_epoch_info() {
Ok(epoch_info) => {
Expand Down
40 changes: 29 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,9 @@ async fn process_exchange_deposit<T: Signers>(
lot_selection_method,
lot_numbers,
)?;
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height)
.unwrap_or_default()
{
return Err("Deposit failed".into());
}
Ok(())
Expand Down Expand Up @@ -1233,7 +1235,9 @@ async fn process_jup_swap<T: Signers>(
lot_selection_method,
)?;

if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height)
.unwrap_or_default()
{
db.cancel_swap(signature)?;
return Err("Swap failed".into());
}
Expand Down Expand Up @@ -1364,7 +1368,7 @@ async fn process_tulip_deposit<T: Signers>(
lot_selection_method,
)?;
if !send_transaction_until_expired(rpc_client, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_client, &transaction, last_valid_block_height).unwrap_or_default() {
db.cancel_swap(signature).expect("cancel_swap");
return Err("Swap failed".into());
}
Expand Down Expand Up @@ -1460,7 +1464,7 @@ async fn process_tulip_withdraw<T: Signers>(
lot_selection_method,
)?;
if !send_transaction_until_expired(rpc_client, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_client, &transaction, last_valid_block_height).unwrap_or_default() {
db.cancel_swap(signature).expect("cancel_swap");
return Err("Swap failed".into());
}
Expand Down Expand Up @@ -3058,7 +3062,9 @@ async fn process_account_merge<T: Signers>(
None,
)?;

if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height)
.unwrap_or_default()
{
db.cancel_transfer(signature)?;
return Err("Merge failed".into());
}
Expand Down Expand Up @@ -3404,7 +3410,9 @@ async fn process_account_sweep<T: Signers>(
)?;

if let Some(transaction) = maybe_transaction {
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height)
.unwrap_or_default()
{
db.cancel_transfer(signature)?;
if let Some((transitory_stake_account, ..)) = via_transitory_stake.as_ref() {
db.remove_transitory_sweep_stake_address(transitory_stake_account.pubkey())?;
Expand Down Expand Up @@ -3544,7 +3552,9 @@ async fn process_account_split<T: Signers>(
lot_numbers,
)?;

if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height)
.unwrap_or_default()
{
db.cancel_transfer(signature)?;
db.remove_account(into_keypair.pubkey(), MaybeToken::SOL())?;
return Err("Split failed".into());
Expand Down Expand Up @@ -3658,7 +3668,9 @@ async fn process_account_redelegate<T: Signers>(
None,
)?;

if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height)
.unwrap_or_default()
{
db.cancel_transfer(signature)?;
db.remove_account(into_keypair.pubkey(), MaybeToken::SOL())?;
return Err("Redelegate failed".into());
Expand Down Expand Up @@ -3997,7 +4009,9 @@ async fn process_account_wrap<T: Signers>(
lot_numbers,
)?;

if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height)
.unwrap_or_default()
{
db.cancel_transfer(signature)?;
return Err("Wrap failed".into());
}
Expand Down Expand Up @@ -4096,7 +4110,9 @@ async fn process_account_unwrap<T: Signers>(
lot_numbers,
)?;

if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height)
.unwrap_or_default()
{
db.cancel_transfer(signature)?;
return Err("Wrap failed".into());
}
Expand Down Expand Up @@ -4295,7 +4311,9 @@ async fn process_account_sync_sweep(
None,
)?;

if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height)
.unwrap_or_default()
{
db.cancel_transfer(signature)?;
return Err("Merge failed".into());
}
Expand Down
4 changes: 2 additions & 2 deletions src/priority_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn apply_priority_fee(
instructions: &mut Vec<Instruction>,
compute_unit_limit: u32,
priority_fee: PriorityFee,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<u64, Box<dyn std::error::Error>> {
let compute_budget = match priority_fee {
PriorityFee::Exact { lamports } => ComputeBudget::new(compute_unit_limit, lamports),
PriorityFee::Auto {
Expand Down Expand Up @@ -187,5 +187,5 @@ pub fn apply_priority_fee(
),
);

Ok(())
Ok(compute_budget.priority_fee_lamports())
}
8 changes: 6 additions & 2 deletions src/stake_spreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,9 @@ pub async fn run<T: Signers>(
None,
)?;

if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height) {
if !send_transaction_until_expired(rpc_clients, &transaction, last_valid_block_height)
.unwrap_or_default()
{
db.cancel_transfer(signature)?;
eprintln!("Merge failed");
} else {
Expand Down Expand Up @@ -598,7 +600,9 @@ pub async fn run<T: Signers>(
rpc_clients,
&transaction,
last_valid_block_height,
) {
)
.unwrap_or_default()
{
eprintln!("Delegation failed");
transaction_failures += 1;
}
Expand Down

0 comments on commit 714ddf9

Please sign in to comment.