From c960f14f6f58cf14ec999539816ec4b507c85086 Mon Sep 17 00:00:00 2001 From: nick134 <76399455+nick134-bit@users.noreply.github.com> Date: Fri, 30 Aug 2024 13:55:51 +0200 Subject: [PATCH] Update commands.rs --- .../fee_collector/src/commands.rs | 64 ++++++++++++------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/contracts/liquidity_hub/fee_collector/src/commands.rs b/contracts/liquidity_hub/fee_collector/src/commands.rs index 620b74eb..a448ca8d 100644 --- a/contracts/liquidity_hub/fee_collector/src/commands.rs +++ b/contracts/liquidity_hub/fee_collector/src/commands.rs @@ -273,35 +273,53 @@ pub fn aggregate_fees( Ok(operations) => { let execute_swap_operations_msg = to_json_binary(&router::ExecuteMsg::ExecuteSwapOperations { - operations, + operations: operations.clone(), minimum_receive: None, to: None, max_spread: Some(Decimal::percent(50u64)), })?; - - match offer_asset_info.clone() { - AssetInfo::Token { contract_addr } => { - aggregate_fees_messages.push(CosmosMsg::Wasm(WasmMsg::Execute { - contract_addr, - funds: vec![], - msg: to_json_binary(&Cw20ExecuteMsg::Send { - contract: config.pool_router.to_string(), - amount: balance, - msg: execute_swap_operations_msg, - })?, - })); + let operations_simulation: StdResult = + deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { + contract_addr: config.pool_router.to_string(), + msg: to_json_binary(&router::QueryMsg::SimulateSwapOperations { + offer_amount: balance.clone(), + operations: operations.clone(), + })?, + })); + + match operations_simulation { + Ok(_) => { + // maybe add a minimum amount to aggregate? to save gas for user + match offer_asset_info.clone() { + AssetInfo::Token { contract_addr } => { + aggregate_fees_messages.push(CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr, + funds: vec![], + msg: to_json_binary(&Cw20ExecuteMsg::Send { + contract: config.pool_router.to_string(), + amount: balance, + msg: execute_swap_operations_msg, + })?, + })); + } + AssetInfo::NativeToken { denom } => { + aggregate_fees_messages.push(CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr: config.pool_router.to_string(), + funds: vec![Coin { + denom, + amount: balance, + }], + msg: execute_swap_operations_msg, + })); + } + }; } - AssetInfo::NativeToken { denom } => { - aggregate_fees_messages.push(CosmosMsg::Wasm(WasmMsg::Execute { - contract_addr: config.pool_router.to_string(), - funds: vec![Coin { - denom, - amount: balance, - }], - msg: execute_swap_operations_msg, - })); + Err(_) => { + continue; } - }; + + } + } Err(_) => { // if there is no swap route, skip swap and keep the asset in contract