Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 update/gas optimization #69

Draft
wants to merge 5 commits into
base: v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions contracts/carrot-app/examples/localnet_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ fn main() -> anyhow::Result<()> {
lower_tick: INITIAL_LOWER_TICK,
upper_tick: INITIAL_UPPER_TICK,
position_id: None,
position_cache: None,
_phantom: std::marker::PhantomData,
}),
},
Expand Down
2 changes: 1 addition & 1 deletion contracts/carrot-app/examples/localnet_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ fn main() -> anyhow::Result<()> {
.bank_send(account.proxy()?.as_str(), coins(10_000, "uosmo")),
)?;

// carrot.deposit(coins(10_000, "uosmo"), None)?;
carrot.deposit(coins(10_000, "uosmo"), None)?;
// carrot.deposit(coins(10_000, "uosmo"), None)?;

// carrot.withdraw(None)?;

Expand Down
2 changes: 2 additions & 0 deletions contracts/carrot-app/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ mod yield_sources {
upper_tick: self.upper_tick,
position_id: self.position_id,
_phantom: PhantomData,
position_cache: self.position_cache,
})
}
}
Expand All @@ -179,6 +180,7 @@ mod yield_sources {
upper_tick: params.upper_tick,
position_id: params.position_id,
_phantom: std::marker::PhantomData,
position_cache: params.position_cache,
})
}
YieldTypeBase::Mars(params) => YieldTypeBase::Mars(params),
Expand Down
6 changes: 3 additions & 3 deletions contracts/carrot-app/src/distribution/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn generate_deposit_strategy(
app: &App,
) -> AppResult<(Vec<(StrategyElement, Decimal)>, Vec<InternalExecuteMsg>)> {
// This is the storage strategy for all assets
let target_strategy = STRATEGY_CONFIG.load(deps.storage)?;
let mut target_strategy = STRATEGY_CONFIG.load(deps.storage)?;

// This is the current distribution of funds inside the strategies
let current_strategy_status = target_strategy.query_current_status(deps, app)?;
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Strategy {
// This method needs to be called on the stored strategy
// We error if deposit value is non-zero here
pub fn current_deposit_strategy(
&self,
&mut self,
deps: Deps,
funds: &mut Coins,
current_strategy_status: Self,
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Strategy {
.0
.iter()
.zip(self.0.clone())
.map(|(target, current)| {
.map(|(target, mut current)| {
// We need to take into account the total value added by the current shares
let value_now = current.share * total_value;
let target_value = target.share * (total_value + deposit_value);
Expand Down
10 changes: 5 additions & 5 deletions contracts/carrot-app/src/distribution/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use crate::{

impl Strategy {
// Returns the total balance
pub fn current_balance(&self, deps: Deps, app: &App) -> AppResult<AssetsBalanceResponse> {
pub fn current_balance(&mut self, deps: Deps, app: &App) -> AppResult<AssetsBalanceResponse> {
let mut funds = Coins::default();
let mut total_value = Uint128::zero();
self.0.iter().try_for_each(|s| {
self.0.iter_mut().try_for_each(|s| {
let deposit_value = s
.yield_source
.ty
Expand All @@ -36,10 +36,10 @@ impl Strategy {
}

/// Returns the current status of the full strategy. It returns shares reflecting the underlying positions
pub fn query_current_status(&self, deps: Deps, app: &App) -> AppResult<Strategy> {
pub fn query_current_status(&mut self, deps: Deps, app: &App) -> AppResult<Strategy> {
let all_strategy_values = self
.0
.iter()
.iter_mut()
.map(|s| s.query_current_value(deps, app))
.collect::<Result<Vec<_>, _>>()?;

Expand Down Expand Up @@ -89,7 +89,7 @@ impl StrategyElement {
/// If there is no deposit or the query for the user deposit value fails
/// the function returns 0 value with the registered asset distribution
pub fn query_current_value(
&self,
&mut self,
deps: Deps,
app: &App,
) -> AppResult<(Uint128, Vec<AssetShare>)> {
Expand Down
2 changes: 1 addition & 1 deletion contracts/carrot-app/src/distribution/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Strategy {
let (rewards, msgs): (Vec<Vec<Coin>>, _) = self
.0
.into_iter()
.map(|s| {
.map(|mut s| {
let (rewards, raw_msgs) = s.yield_source.ty.withdraw_rewards(deps, app)?;

Ok::<_, AppError>((
Expand Down
4 changes: 2 additions & 2 deletions contracts/carrot-app/src/distribution/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Strategy {

impl StrategyElement {
pub fn withdraw(
self,
mut self,
deps: Deps,
withdraw_share: Option<Decimal>,
app: &App,
Expand All @@ -48,7 +48,7 @@ impl StrategyElement {
}

pub fn withdraw_preview(
&self,
&mut self,
deps: Deps,
withdraw_share: Option<Decimal>,
app: &App,
Expand Down
9 changes: 5 additions & 4 deletions contracts/carrot-app/src/handlers/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use cosmwasm_std::{
to_json_binary, Coin, Coins, CosmosMsg, Decimal, Deps, DepsMut, Env, MessageInfo, Uint128,
WasmMsg,
};
use super::internal::execute_internal_action;

use super::internal::{execute_internal_action, save_strategy};

pub fn execute_handler(
deps: DepsMut,
Expand Down Expand Up @@ -84,7 +85,7 @@ fn withdraw(
}

fn update_strategy(
deps: DepsMut,
mut deps: DepsMut,
env: Env,
_info: MessageInfo,
strategy: StrategyUnchecked,
Expand All @@ -109,7 +110,7 @@ fn update_strategy(
let (withdrawn_funds, withdraw_msgs): (Vec<Vec<Coin>>, Vec<Option<ExecutorMsg>>) =
all_stale_sources
.into_iter()
.map(|s| {
.map(|mut s| {
Ok::<_, AppError>((
s.withdraw_preview(deps.as_ref(), None, &app)
.unwrap_or_default(),
Expand All @@ -125,7 +126,7 @@ fn update_strategy(
.try_for_each(|f| f.into_iter().try_for_each(|f| available_funds.add(f)))?;

// 2. We replace the strategy with the new strategy
STRATEGY_CONFIG.save(deps.storage, &strategy)?;
save_strategy(deps.branch(), strategy)?;

// 3. We deposit the funds into the new strategy
let deposit_msgs = _inner_deposit(deps.as_ref(), &env, available_funds.into(), None, &app)?;
Expand Down
8 changes: 4 additions & 4 deletions contracts/carrot-app/src/handlers/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use crate::{
check::Checkable,
contract::{App, AppResult},
msg::AppInstantiateMsg,
state::{CONFIG, STRATEGY_CONFIG},
state::CONFIG,
};
use abstract_app::abstract_sdk::AbstractResponse;
use cosmwasm_std::{DepsMut, Env, MessageInfo};

use super::execute::_inner_deposit;
use super::{execute::_inner_deposit, internal::save_strategy};

pub fn instantiate_handler(
deps: DepsMut,
mut deps: DepsMut,
env: Env,
_info: MessageInfo,
app: App,
Expand All @@ -21,7 +21,7 @@ pub fn instantiate_handler(

CONFIG.save(deps.storage, &config)?;
let strategy = msg.strategy.check(deps.as_ref(), &app)?;
STRATEGY_CONFIG.save(deps.storage, &strategy)?;
save_strategy(deps.branch(), strategy)?;

let mut response = app.response("instantiate_savings_app");

Expand Down
11 changes: 8 additions & 3 deletions contracts/carrot-app/src/handlers/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub fn execute_one_deposit_step(

pub fn execute_finalize_deposit(
deps: DepsMut,
yield_type: YieldType,
mut yield_type: YieldType,
yield_index: usize,
app: App,
) -> AppResult {
Expand All @@ -162,11 +162,16 @@ pub fn execute_finalize_deposit(

let msgs = yield_type.deposit(deps.as_ref(), available_deposit_coins, &app)?;

deps.api.debug("End finalize deposit");
Ok(app.response("finalize-deposit").add_submessages(msgs))
}

pub fn save_strategy(deps: DepsMut, strategy: Strategy) -> AppResult<()> {
pub fn save_strategy(deps: DepsMut, mut strategy: Strategy) -> AppResult<()> {
// We need to correct positions for which the cache is not empty
// This is a security measure
strategy
.0
.iter_mut()
.for_each(|s| s.yield_source.ty.clear_cache());
STRATEGY_CONFIG.save(deps.storage, &strategy)?;
Ok(())
}
10 changes: 5 additions & 5 deletions contracts/carrot-app/src/handlers/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub fn query_strategy(deps: Deps) -> AppResult<StrategyResponse> {
}

pub fn query_strategy_status(deps: Deps, app: &App) -> AppResult<StrategyResponse> {
let strategy = STRATEGY_CONFIG.load(deps.storage)?;
let mut strategy = STRATEGY_CONFIG.load(deps.storage)?;

Ok(StrategyResponse {
strategy: strategy.query_current_status(deps, app)?.into(),
Expand All @@ -122,8 +122,8 @@ pub fn query_balance(deps: Deps, app: &App) -> AppResult<AssetsBalanceResponse>
let mut funds = Coins::default();
let mut total_value = Uint128::zero();

let strategy = STRATEGY_CONFIG.load(deps.storage)?;
strategy.0.iter().try_for_each(|s| {
let mut strategy = STRATEGY_CONFIG.load(deps.storage)?;
strategy.0.iter_mut().try_for_each(|s| {
let deposit_value = s
.yield_source
.ty
Expand All @@ -147,7 +147,7 @@ fn query_rewards(deps: Deps, app: &App) -> AppResult<AvailableRewardsResponse> {
let strategy = STRATEGY_CONFIG.load(deps.storage)?;

let mut rewards = Coins::default();
strategy.0.into_iter().try_for_each(|s| {
strategy.0.into_iter().try_for_each(|mut s| {
let this_rewards = s.yield_source.ty.user_rewards(deps, app)?;
for fund in this_rewards {
rewards.add(fund)?;
Expand All @@ -166,7 +166,7 @@ pub fn query_positions(deps: Deps, app: &App) -> AppResult<PositionsResponse> {
.load(deps.storage)?
.0
.into_iter()
.map(|s| {
.map(|mut s| {
let balance = s.yield_source.ty.user_deposit(deps, app)?;
let liquidity = s.yield_source.ty.user_liquidity(deps, app)?;

Expand Down
21 changes: 14 additions & 7 deletions contracts/carrot-app/src/yield_sources/mars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct MarsDepositParams {
}

impl YieldTypeImplementation for MarsDepositParams {
fn deposit(&self, deps: Deps, funds: Vec<Coin>, app: &App) -> AppResult<Vec<SubMsg>> {
fn deposit(&mut self, deps: Deps, funds: Vec<Coin>, app: &App) -> AppResult<Vec<SubMsg>> {
let ans = app.name_service(deps);
let ans_fund = ans.query(&AssetInfo::native(self.denom.clone()))?;

Expand All @@ -31,7 +31,7 @@ impl YieldTypeImplementation for MarsDepositParams {
}

fn withdraw(
&self,
&mut self,
deps: Deps,
amount: Option<Uint128>,
app: &App,
Expand All @@ -51,12 +51,16 @@ impl YieldTypeImplementation for MarsDepositParams {
.withdraw(AnsAsset::new(ans_fund, amount))?])
}

fn withdraw_rewards(&self, _deps: Deps, _app: &App) -> AppResult<(Vec<Coin>, Vec<CosmosMsg>)> {
fn withdraw_rewards(
&mut self,
_deps: Deps,
_app: &App,
) -> AppResult<(Vec<Coin>, Vec<CosmosMsg>)> {
// Mars doesn't have rewards, it's automatically auto-compounded
Ok((vec![], vec![]))
}

fn user_deposit(&self, deps: Deps, app: &App) -> AppResult<Vec<Coin>> {
fn user_deposit(&mut self, deps: Deps, app: &App) -> AppResult<Vec<Coin>> {
let ans = app.name_service(deps);
let asset = ans.query(&AssetInfo::native(self.denom.clone()))?;
let user = app.account_base(deps)?.proxy;
Expand All @@ -74,17 +78,20 @@ impl YieldTypeImplementation for MarsDepositParams {
Ok(coins(deposit.u128(), self.denom.clone()))
}

fn user_rewards(&self, _deps: Deps, _app: &App) -> AppResult<Vec<Coin>> {
fn user_rewards(&mut self, _deps: Deps, _app: &App) -> AppResult<Vec<Coin>> {
// No rewards, because mars is already auto-compounding

Ok(vec![])
}

fn user_liquidity(&self, deps: Deps, app: &App) -> AppResult<Uint128> {
fn user_liquidity(&mut self, deps: Deps, app: &App) -> AppResult<Uint128> {
Ok(self.user_deposit(deps, app)?[0].amount)
}

fn share_type(&self) -> super::ShareType {
fn share_type(&mut self) -> super::ShareType {
ShareType::Fixed
}

// No cache for mars
fn clear_cache(&mut self) {}
}
Loading
Loading