Skip to content
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
2 changes: 1 addition & 1 deletion common/src/market/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub trait MarketExternalInterface {
/// harvested in previous, non-compounding `harvest_yield` calls) is
/// deposited to the supply record, so it will contribute to future yield
/// calculations.
fn harvest_yield(&mut self, compounding: Option<bool>);
fn harvest_yield(&mut self, compounding: Option<bool>) -> BorrowAssetAmount;

/// This value is an *expected average over time*.
/// Supply positions actually earn all of their yield the instant it is
Expand Down
10 changes: 6 additions & 4 deletions common/src/supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,13 @@ impl<'a> LinkedSupplyPositionMut<'a> {

self.market.snapshot();

MarketEvent::SupplyDeposited {
account_id: self.account_id.clone(),
borrow_asset_amount: amount,
if !amount.is_zero() {
MarketEvent::SupplyDeposited {
account_id: self.account_id.clone(),
borrow_asset_amount: amount,
}
.emit();
}
.emit();
}

pub fn record_yield_withdrawal(
Expand Down
6 changes: 3 additions & 3 deletions contract/market/examples/gas_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn harvest_yield_gas(iterations: usize) -> Gas {
c.repay(&borrow_user, 1100).await;
}

let r = c.harvest_yield(&supply_user, true).await;

r.total_gas_burnt
c.harvest_yield_execution(&supply_user, true)
.await
.total_gas_burnt
}
5 changes: 4 additions & 1 deletion contract/market/src/impl_market_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl MarketExternalInterface for Contract {
self.withdrawal_queue.get_status()
}

fn harvest_yield(&mut self, compounding: Option<bool>) {
fn harvest_yield(&mut self, compounding: Option<bool>) -> BorrowAssetAmount {
let predecessor = env::predecessor_account_id();
if let Some(mut supply_position) = self.get_linked_supply_position_mut(predecessor) {
let proof = supply_position.accumulate_yield();
Expand All @@ -216,8 +216,11 @@ impl MarketExternalInterface for Contract {
let total_yield = supply_position.inner().borrow_asset_yield.get_total();
supply_position.record_yield_withdrawal(total_yield);
supply_position.record_deposit(proof, total_yield, env::block_timestamp_ms());
return total_yield;
}
}

BorrowAssetAmount::zero()
}

fn get_last_yield_rate(&self) -> Decimal {
Expand Down
7 changes: 6 additions & 1 deletion contract/market/tests/happy_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ async fn test_happy() {
80,
);
// Move the yield to the principal so that it can be withdrawn
c.harvest_yield(&supply_user, true).await;
let amount_moved_to_principal = c.harvest_yield(&supply_user, true).await;

assert_eq!(
amount_moved_to_principal,
supply_position.borrow_asset_yield.get_total(),
);

let balance_before = c.borrow_asset_balance_of(supply_user.id()).await;
// Withdraw all
Expand Down
21 changes: 20 additions & 1 deletion test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl TestController {
.unwrap()
}

pub async fn harvest_yield(
pub async fn harvest_yield_execution(
&self,
supply_user: &Account,
compounding: bool,
Expand All @@ -369,6 +369,25 @@ impl TestController {
.unwrap()
}

pub async fn harvest_yield(
&self,
supply_user: &Account,
compounding: bool,
) -> BorrowAssetAmount {
eprintln!("{} harvesting yield...", supply_user.id());
supply_user
.call(self.contract.id(), "harvest_yield")
.args_json(json!({
"compounding": compounding,
}))
.max_gas()
.transact()
.await
.unwrap()
.json::<BorrowAssetAmount>()
.unwrap()
}

pub async fn withdraw_static_yield(
&self,
account: &Account,
Expand Down