Skip to content

Commit

Permalink
apply performance
Browse files Browse the repository at this point in the history
  • Loading branch information
sham789 committed Aug 2, 2022
1 parent a30df3a commit 527b757
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 281 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ id.json
**/target
target/

private/
private/

solend-liquidator.tar.gz
17 changes: 17 additions & 0 deletions pack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash


tar -zcvf ./solend-liquidator.tar.gz \
--exclude="./solend-liquidator/target" \
--exclude="./solend-liquidator/private" \
--exclude="./solend-liquidator-benchmarking/target" \
--exclude="./solend-liquidator.tar.gz" \
--exclude="./solana-program-library/target" \
--exclude="./node_modules" \
--exclude="./overriden/anchor/target" \
--exclude="./overriden/anchor/tests" \
--exclude="./overriden/switchboard-program-0.2.1/target" \
--exclude="./overriden/switchboard-program-0.2.1/tests" \
--exclude="./overriden/switchboard-v2-0.1.10/target" \
--exclude="./overriden/switchboard-v2-0.1.10/tests" \
--exclude="./.git" .
1 change: 1 addition & 0 deletions solend-liquidator-benchmarking/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions solend-liquidator/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion solend-liquidator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ log = "0.4.17"
clap = { version = "3.2.14", features = ["derive"] }
parking_lot = "0.12.1"
dashmap = "5.3.4"

crossbeam = "0.8.2"
rayon = "1.5.3"
55 changes: 5 additions & 50 deletions solend-liquidator/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use futures_retry::FutureFactory;
use solend_program::math::{Decimal, Rate};
use solend_program::state::{Obligation, Reserve};

use solana_sdk::clock::Clock;
use solend_program::{
error::LendingError,
math::{TryAdd, TryDiv, TryMul, TrySub},
Expand All @@ -23,7 +22,6 @@ pub fn refresh_obligation(
enhanced_obligation: &Enhanced<Obligation>,
all_reserves: &HashMap<Pubkey, Enhanced<Reserve>>,
tokens_oracle: &HashMap<Pubkey, OracleData>,
clock: &Clock,
) -> Result<(Obligation, Vec<Deposit>, Vec<Borrow>), Box<dyn std::error::Error + Send + Sync>> {
let mut obligation = enhanced_obligation.inner.clone();

Expand All @@ -48,12 +46,9 @@ pub fn refresh_obligation(
let token_oracle = tokens_oracle.get(&collateral.deposit_reserve);

if token_oracle.is_none() {
// println!("collateral.deposit_reserve: {:?}", collateral.deposit_reserve);
// println!("oracles.reserves: {:?}", tokens_oracle.iter().map(|x| x.reserve_address).collect::<Vec<Pubkey>>());
return Err(LendingError::InvalidAccountInput.into());
}
// let token_oracle_idx = token_oracle_idx.unwrap();
// let token_oracle = &tokens_oracle[token_oracle_idx];

let token_oracle = token_oracle.unwrap();

// @TODO: add lookup table https://git.io/JOCYq
Expand Down Expand Up @@ -86,35 +81,25 @@ pub fn refresh_obligation(
market_value,
symbol: token_oracle.symbol.clone(),
};
// println!(" 📥 casted_depo: {:?}", casted_depo);

deposits.push(casted_depo);
}

for (_index, liquidity) in obligation.borrows.iter_mut().enumerate() {
let reserve = all_reserves.get(&liquidity.borrow_reserve);

// let token_oracle = tokens_oracle
// .iter()
// .position(|x| x.reserve_address == liquidity.borrow_reserve);
let token_oracle = tokens_oracle.get(&liquidity.borrow_reserve);

if token_oracle.is_none() {
// println!("BORROW: oracle price not discovered for: {:?}", liquidity);
return Err(LendingError::InvalidAccountInput.into());
}

let token_oracle = token_oracle.unwrap();
// let token_oracle = &tokens_oracle[token_oracle];

if reserve.is_none() {
return Err(LendingError::InvalidAccountInput.into());
}
let reserve = reserve.unwrap();
// let reserve = all_reserves
// .iter()
// .position(|r| r.pubkey == liquidity.borrow_reserve)
// .unwrap();
// let reserve = &all_reserves[reserve];
let borrow_reserve = &reserve.inner;

liquidity.accrue_interest(borrow_reserve.liquidity.cumulative_borrow_rate_wads)?;
Expand Down Expand Up @@ -146,39 +131,9 @@ pub fn refresh_obligation(
obligation.deposited_value = deposited_value;
obligation.borrowed_value = borrowed_value;

// Wednesday, June 22, 2022 12:00:00 PM GMT
let start_timestamp = 1655899200u64;
// Wednesday, June 28, 2022 8:00:00 AM GMT
let end_timestamp = 1656403200u64;
let current_timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();
// let current_timestamp = clock.unix_timestamp as u64;
let current_timestamp_in_range = min(max(start_timestamp, current_timestamp), end_timestamp);
let numerator = end_timestamp
.checked_sub(current_timestamp_in_range)
.ok_or(LendingError::MathOverflow)?;
let denominator = end_timestamp
.checked_sub(start_timestamp)
.ok_or(LendingError::MathOverflow)?;

let start_global_unhealthy_borrow_value = Decimal::from(120000000u64);
let end_global_unhealthy_borrow_value = Decimal::from(50000000u64);

let global_unhealthy_borrow_value = end_global_unhealthy_borrow_value.try_add(
start_global_unhealthy_borrow_value
.try_sub(end_global_unhealthy_borrow_value)?
.try_mul(numerator)?
.try_div(denominator)?,
)?;
let global_allowed_borrow_value =
global_unhealthy_borrow_value.try_sub(Decimal::from(5000000u64))?;

obligation.allowed_borrow_value = min(allowed_borrow_value, global_allowed_borrow_value);
obligation.unhealthy_borrow_value = min(unhealthy_borrow_value, global_unhealthy_borrow_value);

obligation.last_update.update_slot(clock.slot);

obligation.allowed_borrow_value = allowed_borrow_value;
obligation.unhealthy_borrow_value = unhealthy_borrow_value;

Ok((obligation, deposits, borrows))
}
Loading

0 comments on commit 527b757

Please sign in to comment.