Skip to content

Commit

Permalink
Strategies now returning vault balance
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquinsoza committed Dec 11, 2024
1 parent b441b7e commit 51736ee
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 34 deletions.
41 changes: 24 additions & 17 deletions apps/contracts/strategies/blend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
use blend_pool::perform_reinvest;
use constants::{MIN_DUST, SCALAR_9};
use reserves::StrategyReserves;
use soroban_sdk::{
contract, contractimpl, token::TokenClient, Address, Env, IntoVal, String, Val, Vec};

Expand Down Expand Up @@ -72,7 +73,7 @@ impl DeFindexStrategyTrait for BlendStrategy {
e: Env,
amount: i128,
from: Address,
) -> Result<(), StrategyError> {
) -> Result<i128, StrategyError> {
check_initialized(&e)?;
check_nonnegative_amount(amount)?;
extend_instance_ttl(&e);
Expand All @@ -96,10 +97,12 @@ impl DeFindexStrategyTrait for BlendStrategy {
let b_tokens_minted = blend_pool::supply(&e, &from, &amount, &config);

// Keeping track of the total deposited amount and the total bTokens owned by the strategy depositors
reserves::deposit(&e, reserves, &from, amount, b_tokens_minted);
let vault_shares = reserves::deposit(&e, reserves.clone(), &from, amount, b_tokens_minted);

let underlying_balance = shares_to_underlying(vault_shares, reserves);

event::emit_deposit(&e, String::from_str(&e, STARETEGY_NAME), amount, from);
Ok(())
Ok(underlying_balance)
}

fn harvest(e: Env, from: Address) -> Result<(), StrategyError> {
Expand Down Expand Up @@ -137,11 +140,12 @@ impl DeFindexStrategyTrait for BlendStrategy {

let (tokens_withdrawn, b_tokens_burnt) = blend_pool::withdraw(&e, &from, &amount, &config);

let _burnt_shares = reserves::withdraw(&e, reserves, &from, tokens_withdrawn, b_tokens_burnt);
let vault_shares = reserves::withdraw(&e, reserves.clone(), &from, tokens_withdrawn, b_tokens_burnt);
let underlying_balance = shares_to_underlying(vault_shares, reserves);

event::emit_withdraw(&e, String::from_str(&e, STARETEGY_NAME), amount, from);

Ok(tokens_withdrawn)
Ok(underlying_balance)
}

fn balance(
Expand All @@ -156,22 +160,25 @@ impl DeFindexStrategyTrait for BlendStrategy {

// Get the strategy's total shares and bTokens
let reserves = storage::get_strategy_reserves(&e);
let total_shares = reserves.total_shares;
let total_b_tokens = reserves.total_b_tokens;

if total_shares == 0 || total_b_tokens == 0 {
// No shares or bTokens in the strategy
return Ok(0);
}

// Calculate the bTokens corresponding to the vault's shares
let vault_b_tokens = (vault_shares * total_b_tokens) / total_shares;

// Use the b_rate to convert bTokens to underlying assets
let underlying_balance = (vault_b_tokens * reserves.b_rate) / SCALAR_9;
let underlying_balance = shares_to_underlying(vault_shares, reserves);

Ok(underlying_balance)
}
}

fn shares_to_underlying(shares: i128, reserves: StrategyReserves) -> i128 {
let total_shares = reserves.total_shares;
let total_b_tokens = reserves.total_b_tokens;

if total_shares == 0 || total_b_tokens == 0 {
// No shares or bTokens in the strategy
return 0i128;
}
// Calculate the bTokens corresponding to the vault's shares
let vault_b_tokens = (shares * total_b_tokens) / total_shares;

// Use the b_rate to convert bTokens to underlying assets
(vault_b_tokens * reserves.b_rate) / SCALAR_9
}
mod test;
5 changes: 3 additions & 2 deletions apps/contracts/strategies/blend/src/reserves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use soroban_sdk::{contracttype, panic_with_error, Address, Env};
use crate::{constants::SCALAR_9, storage};

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StrategyReserves {
/// The total deposited amount of the underlying asset
pub total_shares: i128,
Expand Down Expand Up @@ -83,7 +84,7 @@ pub fn deposit(

storage::set_strategy_reserves(&e, reserves);
storage::set_vault_shares(&e, &from, vault_shares);
share_amount
vault_shares
}

/// Withdraw from the reserve vault. This function expects the withdraw to have already been made
Expand Down Expand Up @@ -122,7 +123,7 @@ pub fn withdraw(
storage::set_strategy_reserves(&e, reserves);
storage::set_vault_shares(&e, &from, vault_shares);

share_amount
vault_shares
}

pub fn harvest(
Expand Down
2 changes: 1 addition & 1 deletion apps/contracts/strategies/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub trait DeFindexStrategyTrait {
env: Env,
amount: i128,
from: Address
) -> Result<(), StrategyError>;
) -> Result<i128, StrategyError>;

/// Harvests yields generated by the strategy.
///
Expand Down
10 changes: 5 additions & 5 deletions apps/contracts/strategies/fixed_apr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl DeFindexStrategyTrait for FixAprStrategy {
e: Env,
amount: i128,
from: Address,
) -> Result<(), StrategyError> {
) -> Result<i128, StrategyError> {
check_initialized(&e)?;
check_nonnegative_amount(amount)?;
extend_instance_ttl(&e);
Expand All @@ -79,9 +79,9 @@ impl DeFindexStrategyTrait for FixAprStrategy {
receive_balance(&e, from.clone(), amount);

set_last_harvest_time(&e, e.ledger().timestamp(), from.clone());
event::emit_deposit(&e, String::from_str(&e, STRATEGY_NAME), amount, from);
event::emit_deposit(&e, String::from_str(&e, STRATEGY_NAME), amount, from.clone());

Ok(())
Ok(read_balance(&e, from))
}

fn harvest(e: Env, from: Address) -> Result<(), StrategyError> {
Expand Down Expand Up @@ -118,9 +118,9 @@ impl DeFindexStrategyTrait for FixAprStrategy {
let contract_address = e.current_contract_address();
let underlying_asset = get_underlying_asset(&e);
TokenClient::new(&e, &underlying_asset).transfer(&contract_address, &from, &amount);
event::emit_withdraw(&e, String::from_str(&e, STRATEGY_NAME), amount, from);
event::emit_withdraw(&e, String::from_str(&e, STRATEGY_NAME), amount, from.clone());

Ok(amount)
Ok(read_balance(&e, from))
}

fn balance(
Expand Down
10 changes: 5 additions & 5 deletions apps/contracts/strategies/hodl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl DeFindexStrategyTrait for HodlStrategy {
e: Env,
amount: i128,
from: Address,
) -> Result<(), StrategyError> {
) -> Result<i128, StrategyError> {
check_nonnegative_amount(amount)?;
extend_instance_ttl(&e);
from.require_auth();
Expand All @@ -72,9 +72,9 @@ impl DeFindexStrategyTrait for HodlStrategy {
TokenClient::new(&e, &underlying_asset).transfer(&from, &contract_address, &amount);

receive_balance(&e, from.clone(), amount);
event::emit_deposit(&e, String::from_str(&e, STARETEGY_NAME), amount, from);
event::emit_deposit(&e, String::from_str(&e, STARETEGY_NAME), amount, from.clone());

Ok(())
Ok(read_balance(&e, from))
}

fn harvest(e: Env, from: Address) -> Result<(), StrategyError> {
Expand All @@ -98,9 +98,9 @@ impl DeFindexStrategyTrait for HodlStrategy {
let contract_address = e.current_contract_address();
let underlying_asset = get_underlying_asset(&e);
TokenClient::new(&e, &underlying_asset).transfer(&contract_address, &from, &amount);
event::emit_withdraw(&e, String::from_str(&e, STARETEGY_NAME), amount, from);
event::emit_withdraw(&e, String::from_str(&e, STARETEGY_NAME), amount, from.clone());

Ok(amount)
Ok(read_balance(&e, from))
}

fn balance(
Expand Down
4 changes: 2 additions & 2 deletions apps/contracts/strategies/soroswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl DeFindexStrategyTrait for SoroswapAdapter {
e: Env,
amount: i128,
from: Address,
) -> Result<(), StrategyError> {
) -> Result<i128, StrategyError> {
from.require_auth();
check_initialized(&e)?;
check_nonnegative_amount(amount)?;
Expand Down Expand Up @@ -120,7 +120,7 @@ impl DeFindexStrategyTrait for SoroswapAdapter {
&u64::MAX,
);

Ok(())
Ok(0)
}

fn harvest(e: Env, _from: Address) -> Result<(), StrategyError> {
Expand Down
4 changes: 2 additions & 2 deletions apps/contracts/strategies/xycloans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl DeFindexStrategyTrait for XycloansAdapter {
e: Env,
amount: i128,
from: Address,
) -> Result<(), StrategyError> {
) -> Result<i128, StrategyError> {
check_nonnegative_amount(amount)?;
extend_instance_ttl(&e);
from.require_auth();
Expand All @@ -70,7 +70,7 @@ impl DeFindexStrategyTrait for XycloansAdapter {
let xycloans_pool_client = XycloansPoolClient::new(&e, &xycloans_address);
xycloans_pool_client.deposit(&from, &total_swapped_amount);

Ok(())
Ok(0)
}

fn harvest(e: Env, _from: Address) -> Result<(), StrategyError> {
Expand Down

0 comments on commit 51736ee

Please sign in to comment.