Skip to content

Commit 400dd87

Browse files
Upcoming (#68)
* fix ci solana version (#67) * Update switchboard-program to v0.2.0 (#66) * oracleless repay (#64) * oracleless repay * lint * the easy way * updated test to make sure interest accumulates before repay Co-authored-by: 0xkiplet <[email protected]>
1 parent 920a449 commit 400dd87

File tree

6 files changed

+53
-47
lines changed

6 files changed

+53
-47
lines changed

Cargo.lock

Lines changed: 10 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ci/solana-version.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
if [[ -n $SOLANA_VERSION ]]; then
1515
solana_version="$SOLANA_VERSION"
1616
else
17-
solana_version=v1.7.12
17+
solana_version=v1.8.12
1818
fi
1919

2020
export solana_version="$solana_version"

token-lending/program/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ bytemuck = "1.5.1"
1717
num-derive = "0.3"
1818
num-traits = "0.2"
1919
solana-program = "1.7.12"
20-
spl-token = { path = "../../token/program", features = [ "no-entrypoint" ] }
21-
switchboard-program = "0.1.45"
20+
spl-token = { path = "../../token/program", features = ["no-entrypoint"] }
21+
switchboard-program = "0.2.0"
2222
thiserror = "1.0"
2323
uint = "0.9.0"
2424

token-lending/program/src/processor.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,7 @@ fn process_repay_obligation_liquidity(
14661466
return Err(LendingError::InvalidTokenProgram.into());
14671467
}
14681468

1469+
_refresh_reserve_interest(program_id, repay_reserve_info, clock)?;
14691470
let mut repay_reserve = Reserve::unpack(&repay_reserve_info.data.borrow())?;
14701471
if repay_reserve_info.owner != program_id {
14711472
msg!("Repay reserve provided is not owned by the lending program");
@@ -1497,18 +1498,17 @@ fn process_repay_obligation_liquidity(
14971498
msg!("Obligation lending market does not match the lending market provided");
14981499
return Err(LendingError::InvalidAccountInput.into());
14991500
}
1500-
if obligation.last_update.is_stale(clock.slot)? {
1501-
msg!("Obligation is stale and must be refreshed in the current slot");
1502-
return Err(LendingError::ObligationStale.into());
1503-
}
15041501

15051502
let (liquidity, liquidity_index) =
1506-
obligation.find_liquidity_in_borrows(*repay_reserve_info.key)?;
1503+
obligation.find_liquidity_in_borrows_mut(*repay_reserve_info.key)?;
15071504
if liquidity.borrowed_amount_wads == Decimal::zero() {
15081505
msg!("Liquidity borrowed amount is zero");
15091506
return Err(LendingError::ObligationLiquidityEmpty.into());
15101507
}
15111508

1509+
// refreshing specific borrow instead of checking obligation stale
1510+
liquidity.accrue_interest(repay_reserve.liquidity.cumulative_borrow_rate_wads)?;
1511+
15121512
let CalculateRepayResult {
15131513
settle_amount,
15141514
repay_amount,

token-lending/program/src/state/obligation.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,21 @@ impl Obligation {
176176
Ok((&self.borrows[liquidity_index], liquidity_index))
177177
}
178178

179+
/// Find liquidity by borrow reserve mut
180+
pub fn find_liquidity_in_borrows_mut(
181+
&mut self,
182+
borrow_reserve: Pubkey,
183+
) -> Result<(&mut ObligationLiquidity, usize), ProgramError> {
184+
if self.borrows.is_empty() {
185+
msg!("Obligation has no borrows");
186+
return Err(LendingError::ObligationBorrowsEmpty.into());
187+
}
188+
let liquidity_index = self
189+
._find_liquidity_index_in_borrows(borrow_reserve)
190+
.ok_or(LendingError::InvalidObligationLiquidity)?;
191+
Ok((&mut self.borrows[liquidity_index], liquidity_index))
192+
}
193+
179194
/// Find or add liquidity by borrow reserve
180195
pub fn find_or_add_liquidity_to_borrows(
181196
&mut self,

token-lending/program/tests/repay_obligation_liquidity.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use solana_sdk::{
1010
transaction::Transaction,
1111
};
1212
use spl_token::instruction::approve;
13-
use spl_token_lending::instruction::refresh_obligation;
1413
use spl_token_lending::{
15-
instruction::repay_obligation_liquidity, processor::process_instruction,
14+
instruction::repay_obligation_liquidity,
15+
math::{Decimal, Rate, TryAdd, TryMul, TrySub},
16+
processor::process_instruction,
1617
state::INITIAL_COLLATERAL_RATIO,
1718
};
1819

@@ -31,6 +32,7 @@ async fn test_success() {
3132
const USDC_BORROW_AMOUNT_FRACTIONAL: u64 = 1_000 * FRACTIONAL_TO_USDC;
3233
const SOL_RESERVE_COLLATERAL_LAMPORTS: u64 = 2 * SOL_DEPOSIT_AMOUNT_LAMPORTS;
3334
const USDC_RESERVE_LIQUIDITY_FRACTIONAL: u64 = 2 * USDC_BORROW_AMOUNT_FRACTIONAL;
35+
const USDC_RESERVE_BORROW_RATE: u8 = 110;
3436

3537
let user_accounts_owner = Keypair::new();
3638
let user_transfer_authority = Keypair::new();
@@ -68,6 +70,7 @@ async fn test_success() {
6870
liquidity_amount: USDC_RESERVE_LIQUIDITY_FRACTIONAL,
6971
liquidity_mint_pubkey: usdc_mint.pubkey,
7072
liquidity_mint_decimals: usdc_mint.decimals,
73+
initial_borrow_rate: USDC_RESERVE_BORROW_RATE,
7174
config: reserve_config,
7275
mark_fresh: true,
7376
..AddReserveArgs::default()
@@ -103,11 +106,6 @@ async fn test_success() {
103106
USDC_BORROW_AMOUNT_FRACTIONAL,
104107
)
105108
.unwrap(),
106-
refresh_obligation(
107-
spl_token_lending::id(),
108-
test_obligation.pubkey,
109-
vec![sol_test_reserve.pubkey, usdc_test_reserve.pubkey],
110-
),
111109
repay_obligation_liquidity(
112110
spl_token_lending::id(),
113111
USDC_BORROW_AMOUNT_FRACTIONAL,
@@ -143,5 +141,19 @@ async fn test_success() {
143141
);
144142

145143
let obligation = test_obligation.get_state(&mut banks_client).await;
146-
assert_eq!(obligation.borrows.len(), 0);
144+
assert_eq!(obligation.borrows.len(), 1);
145+
let new_rate = Rate::one()
146+
.try_add(Rate::from_percent(USDC_RESERVE_BORROW_RATE))
147+
.unwrap();
148+
let new_rate_decmial = Decimal::one().try_mul(new_rate).unwrap();
149+
let balance_due = new_rate_decmial
150+
.try_mul(USDC_BORROW_AMOUNT_FRACTIONAL)
151+
.unwrap();
152+
let expected_balance_after_repay = balance_due
153+
.try_sub(Decimal::from(USDC_BORROW_AMOUNT_FRACTIONAL))
154+
.unwrap();
155+
assert_eq!(
156+
obligation.borrows[0].borrowed_amount_wads,
157+
expected_balance_after_repay
158+
);
147159
}

0 commit comments

Comments
 (0)