use try mutate for asset state
@@ -535,10 +535,11 @@ pub mod pallet {
Error::<T>::InsufficientBalance
);
- let mut asset_state = Assets::<T>::get(asset).ok_or(Error::<T>::AssetNotFound)?;
+ Assets::<T>::try_mutate(asset, |maybe_asset| -> DispatchResult {
+ let asset_state = maybe_asset.as_mut().ok_or(Error::<T>::AssetNotFound)?;
let state_changes =
- calculate_add_liquidity_state_changes::<T>(&asset_state, amount).ok_or(ArithmeticError::Overflow)?;
+ calculate_add_liquidity_state_changes::<T>(asset_state, amount).ok_or(ArithmeticError::Overflow)?;
// New Asset State
asset_state
@@ -591,10 +592,10 @@ pub mod pallet {
)?;
// Imbalance update
- Self::recalculate_imbalance(&asset_state, state_changes.delta_imbalance)?;
+ Self::recalculate_imbalance(asset_state, state_changes.delta_imbalance)?;
// TVL update
- Self::update_tvl(&mut asset_state)?;
+ Self::update_tvl(asset_state)?;
// Total hub asset liquidity update
Self::update_hub_asset_liquidity(
@@ -611,8 +612,8 @@ pub mod pallet {
amount,
position_id: instance_id,
});
-
Ok(())
+ })
}
/// Remove liquidity of asset `asset` in quantity `amount` from Omnipool
@@ -647,9 +648,10 @@ pub mod pallet {
let asset_id = position.asset_id;
- let mut asset_state = Assets::<T>::get(asset_id).ok_or(Error::<T>::AssetNotFound)?;
+ Assets::<T>::try_mutate(asset_id, |maybe_asset| -> DispatchResult {
+ let asset_state = maybe_asset.as_mut().ok_or(Error::<T>::AssetNotFound)?;
- let state_changes = calculate_remove_liquidity_state_changes::<T>(&asset_state, amount, &position)
+ let state_changes = calculate_remove_liquidity_state_changes::<T>(asset_state, amount, &position)
.ok_or(ArithmeticError::Overflow)?;
// New Asset State
@@ -674,10 +676,10 @@ pub mod pallet {
)?;
// Imbalance update
- Self::recalculate_imbalance(&asset_state, state_changes.delta_imbalance)?;
+ Self::recalculate_imbalance(asset_state, state_changes.delta_imbalance)?;
// TVL update
- Self::update_tvl(&mut asset_state)?;
+ Self::update_tvl(asset_state)?;
// Total Hub asset liquidity
Self::update_hub_asset_liquidity(
@@ -724,6 +726,7 @@ pub mod pallet {
});
Ok(())
+ })
}
/// Execute a swap of `asset_in` for `asset_out`.
@@ -1177,9 +1180,10 @@ impl<T: Config> Pallet<T> {
amount: T::Balance,
limit: T::Balance,
) -> DispatchResult {
- let mut asset_out_state = Assets::<T>::get(asset_out).ok_or(Error::<T>::AssetNotFound)?;
+ Assets::<T>::try_mutate(asset_out, |maybe_asset| -> DispatchResult {
+ let asset_out_state = maybe_asset.as_mut().ok_or(Error::<T>::AssetNotFound)?;
- let state_changes = calculate_sell_hub_state_changes::<T>(&asset_out_state, amount, Self::asset_fee())
+ let state_changes = calculate_sell_hub_state_changes::<T>(asset_out_state, amount, Self::asset_fee())
.ok_or(ArithmeticError::Overflow)?;
ensure!(
@@ -1226,5 +1230,6 @@ impl<T: Config> Pallet<T> {
});
Ok(())
+ })
}
}
