Skip to content

Commit

Permalink
checked math ops on get position balance and adjust parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
magiodev committed Jul 31, 2024
1 parent e76303b commit 0f9b6f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ pub fn get_asset0_value(
pub fn get_position_balance(
storage: &dyn Storage,
querier: &QuerierWrapper,
) -> Result<(f64, f64), ContractError> {
) -> Result<(Decimal, Decimal), ContractError> {
let position = get_position(storage, querier)?;
let asset0_amount = Uint128::from_str(&position.clone().asset0.unwrap_or_default().amount)?;
let asset1_amount = Uint128::from_str(&position.clone().asset1.unwrap_or_default().amount)?;

// Handle cases where either asset amount is zero
if asset0_amount.is_zero() && asset1_amount.is_zero() {
return Ok((0.0, 0.0));
return Ok((Decimal::zero(), Decimal::zero()));
}

// Get the total amount of the vault's position in asset0 denom
let asset_0_value = get_asset0_value(storage, querier, asset0_amount, asset1_amount)?;

// Calculate the ratio of the vault's position in asset0 and asset1
let asset_0_ratio = asset0_amount.u128() as f64 / asset_0_value.u128() as f64;
let asset_1_ratio = asset1_amount.u128() as f64 / asset_0_value.u128() as f64;
// Calculate the ratio of the vault's position in asset0 and asset1 using Decimal for safe division
let asset_0_ratio = Decimal::from_ratio(asset0_amount, asset_0_value);
let asset_1_ratio = Decimal::from_ratio(asset1_amount, asset_0_value);

Ok((asset_0_ratio, asset_1_ratio))
}
Expand Down
14 changes: 8 additions & 6 deletions smart-contracts/osmosis/contracts/cl-vault/src/vault/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ pub fn execute_swap_non_vault_funds(
}

// Get the current position balance ratio to compute the amount of external funds we want to swap into either token0 or token1 from the vault's pool
// TODO: This new get_position_balance helper looks redundant with get_depositable_tokens_impl, can we reuse this instead?
let position_balance = get_position_balance(deps.storage, &deps.querier)?;

let to_token0_amount =
Uint128::from((balance_in_contract.u128() as f64 * position_balance.0) as u128); // balance * ratio computed by current position balancing
let to_token1_amount =
Uint128::from((balance_in_contract.u128() as f64 * position_balance.1) as u128); // balance * ratio computed by current position balancing
let to_token0_amount = Uint128::from(
(balance_in_contract.u128() as f64
* position_balance.0.to_string().parse::<f64>().unwrap()) as u128,
);
let to_token1_amount = Uint128::from(
(balance_in_contract.u128() as f64
* position_balance.1.to_string().parse::<f64>().unwrap()) as u128,
);

// TODO: Validate that the swap_operation.pool_id_0 is about token_in_denom and pool_config.token0 assets or throw error
let twap_price_token_0 = get_twap_price(
Expand Down

0 comments on commit 0f9b6f2

Please sign in to comment.