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
34 changes: 34 additions & 0 deletions contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,40 @@ impl NesteraContract {
ensure_not_paused(&env)?;
strategy::routing::withdraw_from_strategy(&env, StrategyPositionKey::Group(group_id), to)
}

/// Harvests yield from a yield strategy.
///
/// Calculates profit as `strategy_balance - principal`, calls `strategy_harvest`,
/// allocates the protocol fee to treasury, and credits the remainder to users.
///
/// Returns the total yield harvested (treasury fee + user yield).
pub fn harvest_strategy(
env: Env,
caller: Address,
strategy_address: Address,
) -> Result<i128, SavingsError> {
caller.require_auth();
ensure_not_paused(&env)?;
strategy::routing::harvest_strategy(&env, strategy_address)
}

/// Returns the total principal that Nestera has deposited into a given strategy.
///
/// This is the sum of all routed deposits minus all withdrawals.
pub fn get_strategy_principal(env: Env, strategy_address: Address) -> i128 {
env.storage()
.persistent()
.get(&DataKey::StrategyTotalPrincipal(strategy_address))
.unwrap_or(0)
}

/// Returns the accumulated user yield credited from harvesting a given strategy.
pub fn get_strategy_yield(env: Env, strategy_address: Address) -> i128 {
env.storage()
.persistent()
.get(&DataKey::StrategyYield(strategy_address))
.unwrap_or(0)
}
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/storage_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ pub enum DataKey {
EarlyBreakFeeBps,
/// Fee recipient for protocol/treasury fees
FeeRecipient,
/// Track total principal deposited in a strategy (deposits - withdrawals)
StrategyTotalPrincipal(Address),
/// Track accumulated yield designated for Nestera users from a strategy
StrategyYield(Address),
User(Address),
/// Maps a (user address, plan_id) tuple to a SavingsPlan
SavingsPlan(Address, u64),
Expand Down
Loading