Skip to content

Commit

Permalink
Merge pull request #556 from ChorusOne/anker_add_metrics
Browse files Browse the repository at this point in the history
Add metrics for Anker deposit and withdrawals
  • Loading branch information
enriquefynn authored Apr 11, 2022
2 parents 1af9872 + 69d63f3 commit 0d9a19c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 8 deletions.
4 changes: 2 additions & 2 deletions anker/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ accounts_struct! {
DepositAccountsMeta, DepositAccountsInfo {
pub anker {
is_signer: false,
is_writable: false,
is_writable: true, // We update metrics.
},
// For reading the stSOL/SOL exchange rate.
pub solido {
Expand Down Expand Up @@ -222,7 +222,7 @@ accounts_struct! {
WithdrawAccountsMeta, WithdrawAccountsInfo {
pub anker {
is_signer: false,
is_writable: false,
is_writable: true, // Needed to update metrics.
},
// For reading the stSOL/SOL exchange rate.
pub solido {
Expand Down
54 changes: 53 additions & 1 deletion anker/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use lido::token::StLamports;
use serde::Serialize;

use crate::token::{self, MicroUst};
use crate::token::{self, BLamports, MicroUst};

#[repr(C)]
#[derive(
Expand All @@ -16,13 +16,41 @@ pub struct Metrics {
/// Total amount of UST received through swaps.
#[serde(rename = "swapped_rewards_ust_total_microust")]
pub swapped_rewards_ust_total: MicroUst,

/// Metric for deposits.
pub deposit_metric: DepositWithdrawMetric,

/// Metrics for withdrawals.
pub withdraw_metric: DepositWithdrawMetric,
}

#[repr(C)]
#[derive(
Clone, Debug, Default, BorshDeserialize, BorshSerialize, BorshSchema, Eq, PartialEq, Serialize,
)]
pub struct DepositWithdrawMetric {
/// Total amount of StSOL.
pub st_sol_total: StLamports,

/// Total amount of bSol.
pub b_sol_total: BLamports,

/// Total number of times the metric was called.
pub count: u64,
}

impl Metrics {
pub fn new() -> Self {
let empty_metric = DepositWithdrawMetric {
st_sol_total: StLamports(0),
b_sol_total: BLamports(0),
count: 0,
};
Metrics {
swapped_rewards_st_sol_total: StLamports(0),
swapped_rewards_ust_total: MicroUst(0),
deposit_metric: empty_metric.clone(),
withdraw_metric: empty_metric,
}
}

Expand All @@ -36,4 +64,28 @@ impl Metrics {

Ok(())
}

pub fn observe_deposit(
&mut self,
st_sol_amount: StLamports,
b_sol_amount: BLamports,
) -> token::Result<()> {
self.deposit_metric.st_sol_total = (self.deposit_metric.st_sol_total + st_sol_amount)?;
self.deposit_metric.b_sol_total = (self.deposit_metric.b_sol_total + b_sol_amount)?;
self.deposit_metric.count += 1;

Ok(())
}

pub fn observe_withdraw(
&mut self,
st_sol_amount: StLamports,
b_sol_amount: BLamports,
) -> token::Result<()> {
self.withdraw_metric.st_sol_total = (self.withdraw_metric.st_sol_total + st_sol_amount)?;
self.withdraw_metric.b_sol_total = (self.withdraw_metric.b_sol_total + b_sol_amount)?;
self.withdraw_metric.count += 1;

Ok(())
}
}
10 changes: 6 additions & 4 deletions anker/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ fn process_deposit(
return Err(ProgramError::InvalidArgument);
}

let (solido, anker) = deserialize_anker(program_id, accounts.anker, accounts.solido)?;
let (solido, mut anker) = deserialize_anker(program_id, accounts.anker, accounts.solido)?;
anker.check_st_sol_reserve_address(
program_id,
accounts.anker.key,
Expand Down Expand Up @@ -249,8 +249,9 @@ fn process_deposit(
amount,
b_sol_amount,
);
anker.metrics.observe_deposit(amount, b_sol_amount)?;

Ok(())
anker.save(accounts.anker)
}

/// Sample the current pool price, used later to limit slippage in `sell_rewards`.
Expand Down Expand Up @@ -402,7 +403,7 @@ fn process_withdraw(
) -> ProgramResult {
let accounts = WithdrawAccountsInfo::try_from_slice(accounts_raw)?;

let (solido, anker) = deserialize_anker(program_id, accounts.anker, accounts.solido)?;
let (solido, mut anker) = deserialize_anker(program_id, accounts.anker, accounts.solido)?;
anker.check_is_st_sol_account(&solido, accounts.reserve_account)?;
anker.check_mint(accounts.b_sol_mint)?;

Expand Down Expand Up @@ -484,8 +485,9 @@ fn process_withdraw(
)?;

msg!("Anker: Withdrew {} for {}.", amount, st_sol_amount,);
anker.metrics.observe_withdraw(st_sol_amount, amount)?;

Ok(())
anker.save(accounts.anker)
}

/// Change the Terra rewards destination.
Expand Down
2 changes: 1 addition & 1 deletion anker/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use spl_token_swap::state::SwapV1;
use crate::token::{self, BLamports, MicroUst};

/// Size of the serialized [`Anker`] struct, in bytes.
pub const ANKER_LEN: usize = 322;
pub const ANKER_LEN: usize = 370;
pub const ANKER_VERSION: u8 = 0;

// Next are three constants related to stored stSOL/UST prices. Because Anker is
Expand Down
10 changes: 10 additions & 0 deletions anker/tests/tests/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ async fn test_successful_deposit_during_first_epoch() {
// so the amounts in SOL, stSOL, and bSOL are all equal.
assert_eq!(reserve_balance, TEST_DEPOSIT_AMOUNT);
assert_eq!(recipient_balance, BLamports(TEST_DEPOSIT_AMOUNT.0));
let anker = context.get_anker().await;
assert_eq!(
anker.metrics.deposit_metric.st_sol_total,
TEST_DEPOSIT_AMOUNT
);
assert_eq!(
anker.metrics.deposit_metric.b_sol_total,
BLamports(TEST_DEPOSIT_AMOUNT.0)
);
assert_eq!(anker.metrics.deposit_metric.count, 1);
}

#[tokio::test]
Expand Down
4 changes: 4 additions & 0 deletions anker/tests/tests/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ async fn test_withdraw_single_epoch() {
.get_st_sol_balance(context.st_sol_reserve)
.await;
assert_eq!(reserve_st_sol, StLamports(0));
let anker = context.get_anker().await;
assert_eq!(anker.metrics.withdraw_metric.st_sol_total, st_sol_balance);
assert_eq!(anker.metrics.withdraw_metric.b_sol_total, b_sol_balance);
assert_eq!(anker.metrics.withdraw_metric.count, 1);
}

#[tokio::test]
Expand Down

0 comments on commit 0d9a19c

Please sign in to comment.