diff --git a/apps/contracts/strategies/blend/src/lib.rs b/apps/contracts/strategies/blend/src/lib.rs index 0bff008..6625f1a 100644 --- a/apps/contracts/strategies/blend/src/lib.rs +++ b/apps/contracts/strategies/blend/src/lib.rs @@ -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}; @@ -72,7 +73,7 @@ impl DeFindexStrategyTrait for BlendStrategy { e: Env, amount: i128, from: Address, - ) -> Result<(), StrategyError> { + ) -> Result { check_initialized(&e)?; check_nonnegative_amount(amount)?; extend_instance_ttl(&e); @@ -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> { @@ -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( @@ -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; \ No newline at end of file diff --git a/apps/contracts/strategies/blend/src/reserves.rs b/apps/contracts/strategies/blend/src/reserves.rs index ce484bb..9a2fad1 100644 --- a/apps/contracts/strategies/blend/src/reserves.rs +++ b/apps/contracts/strategies/blend/src/reserves.rs @@ -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, @@ -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 @@ -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( diff --git a/apps/contracts/strategies/core/src/lib.rs b/apps/contracts/strategies/core/src/lib.rs index e067227..6b571d6 100644 --- a/apps/contracts/strategies/core/src/lib.rs +++ b/apps/contracts/strategies/core/src/lib.rs @@ -64,7 +64,7 @@ pub trait DeFindexStrategyTrait { env: Env, amount: i128, from: Address - ) -> Result<(), StrategyError>; + ) -> Result; /// Harvests yields generated by the strategy. /// diff --git a/apps/contracts/strategies/fixed_apr/src/lib.rs b/apps/contracts/strategies/fixed_apr/src/lib.rs index 0a4cc19..e22d9d2 100644 --- a/apps/contracts/strategies/fixed_apr/src/lib.rs +++ b/apps/contracts/strategies/fixed_apr/src/lib.rs @@ -64,7 +64,7 @@ impl DeFindexStrategyTrait for FixAprStrategy { e: Env, amount: i128, from: Address, - ) -> Result<(), StrategyError> { + ) -> Result { check_initialized(&e)?; check_nonnegative_amount(amount)?; extend_instance_ttl(&e); @@ -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> { @@ -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( diff --git a/apps/contracts/strategies/hodl/src/lib.rs b/apps/contracts/strategies/hodl/src/lib.rs index d0a2052..9bb629d 100644 --- a/apps/contracts/strategies/hodl/src/lib.rs +++ b/apps/contracts/strategies/hodl/src/lib.rs @@ -61,7 +61,7 @@ impl DeFindexStrategyTrait for HodlStrategy { e: Env, amount: i128, from: Address, - ) -> Result<(), StrategyError> { + ) -> Result { check_nonnegative_amount(amount)?; extend_instance_ttl(&e); from.require_auth(); @@ -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> { @@ -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( diff --git a/apps/contracts/strategies/soroswap/src/lib.rs b/apps/contracts/strategies/soroswap/src/lib.rs index 9be8726..8425244 100644 --- a/apps/contracts/strategies/soroswap/src/lib.rs +++ b/apps/contracts/strategies/soroswap/src/lib.rs @@ -56,7 +56,7 @@ impl DeFindexStrategyTrait for SoroswapAdapter { e: Env, amount: i128, from: Address, - ) -> Result<(), StrategyError> { + ) -> Result { from.require_auth(); check_initialized(&e)?; check_nonnegative_amount(amount)?; @@ -120,7 +120,7 @@ impl DeFindexStrategyTrait for SoroswapAdapter { &u64::MAX, ); - Ok(()) + Ok(0) } fn harvest(e: Env, _from: Address) -> Result<(), StrategyError> { diff --git a/apps/contracts/strategies/xycloans/src/lib.rs b/apps/contracts/strategies/xycloans/src/lib.rs index feb9c9b..34f0b5e 100644 --- a/apps/contracts/strategies/xycloans/src/lib.rs +++ b/apps/contracts/strategies/xycloans/src/lib.rs @@ -55,7 +55,7 @@ impl DeFindexStrategyTrait for XycloansAdapter { e: Env, amount: i128, from: Address, - ) -> Result<(), StrategyError> { + ) -> Result { check_nonnegative_amount(amount)?; extend_instance_ttl(&e); from.require_auth(); @@ -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> {