Skip to content

Commit

Permalink
accumulate prices in sync with orderbook endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
epanchee committed Feb 11, 2025
1 parent 1ade2d6 commit abbdd77
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions contracts/pair_concentrated_duality/src/orderbook/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use astroport::cosmwasm_ext::{DecimalToInteger, IntegerToDecimal};
use astroport::pair::MIN_TRADE_SIZE;
use astroport::querier::{query_fee_info, query_native_supply, FeeInfo};
use astroport_pcl_common::state::{Config, Precisions};
use astroport_pcl_common::utils::accumulate_prices;
use astroport_pcl_common::utils::{accumulate_prices, calc_last_prices};

use crate::error::ContractError;
use crate::instantiate::LP_TOKEN_PRECISION;
Expand Down Expand Up @@ -95,8 +95,6 @@ pub fn process_cumulative_trade(
}
}

let old_real_price = config.pool_state.price_state.last_price;

// Skip very small trade sizes which could significantly mess up the price due to rounding errors,
// especially if token precisions are 18.
if trade.base_asset.amount >= MIN_TRADE_SIZE && trade.quote_asset.amount >= MIN_TRADE_SIZE {
Expand All @@ -119,13 +117,6 @@ pub fn process_cumulative_trade(
.update_price(&config.pool_params, env, total_share, &ixs, last_price)?;
}

// TODO: if process_cumulative_trade() originates from swap/provide/withdraw context,
// TODO: next TWAP entry won't be added in this block.
// TODO: Is it ok or should we post TWAP entry
// TODO: formed out of cumulative orderbook trade 1 second before current block time?
// Adding TWAP entry
accumulate_prices(env, config, old_real_price);

Ok(Response::default()
.add_messages(messages)
.add_attributes(attrs))
Expand All @@ -150,6 +141,10 @@ pub fn sync_pool_with_orderbook(
fetch_cumulative_trade(&precisions, &ob_state.last_balances, &liquidity.orderbook)?
{
let mut pools = liquidity.total_dec(&precisions)?;

let xs = pools.iter().map(|a| a.amount).collect_vec();
let old_real_price = calc_last_prices(&xs, &config, &env)?;

let mut balances = pools
.iter_mut()
.map(|asset| &mut asset.amount)
Expand All @@ -165,6 +160,8 @@ pub fn sync_pool_with_orderbook(
None,
)?;

accumulate_prices(&env, &mut config, old_real_price);

CONFIG.save(deps.storage, &config)?;

let cancel_msgs = ob_state.cancel_orders(&env.contract.address);
Expand Down

0 comments on commit abbdd77

Please sign in to comment.