Skip to content

Commit

Permalink
Denominate VXOR (#1258)
Browse files Browse the repository at this point in the history
  • Loading branch information
vovac12 authored Oct 30, 2024
1 parent 9d4174b commit e15b3b4
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
27 changes: 27 additions & 0 deletions pallets/pool-xyk/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ impl<T: Config> Pallet<T> {
}
*weight = weight.saturating_add(T::DbWeight::get().writes(1));
TotalIssuances::<T>::insert(&pool_acc, new_issuance);
frame_support::log::debug!(
"Pool adjusted {} for {} providers: issuance {} -> {}",
pool_acc,
providers,
current_issuance,
new_issuance
);
Self::deposit_event(crate::Event::<T>::PoolAdjusted {
pool: pool_acc,
old_issuance: current_issuance,
Expand All @@ -328,4 +335,24 @@ impl<T: Config> Pallet<T> {
});
Ok(())
}

pub fn fix_pool_parameters(
dex_id: T::DEXId,
pool_account: &AccountIdOf<T>,
asset_a: &AssetIdOf<T>,
asset_b: &AssetIdOf<T>,
) -> DispatchResult {
let (reserve_a, reserve_b, _) =
Self::get_actual_reserves(pool_account, asset_a, asset_a, asset_b)?;
Self::update_reserves(asset_a, asset_a, asset_b, (&reserve_a, &reserve_b));
frame_support::log::debug!(
"Updated reserves for {:?}({}) => {:?}({})",
asset_a,
reserve_a,
asset_b,
reserve_b
);
Self::adjust_liquidity_in_pool(dex_id, asset_a, asset_b, &mut Default::default())?;
Ok(())
}
}
84 changes: 83 additions & 1 deletion runtime/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,86 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pub type Migrations = ();
use common::Balance;
use frame_support::{dispatch::DispatchResult, traits::OnRuntimeUpgrade};
use sp_runtime::BoundedVec;

pub type Migrations = (DenominateVXor,);

const DENOM_COEFF: Balance = 100_000_000_000_000;

pub struct DenominateVXor;

impl OnRuntimeUpgrade for DenominateVXor {
fn on_runtime_upgrade() -> frame_election_provider_support::Weight {
let result = common::with_transaction(|| {
let mut new_issuance = 0;
tokens::Accounts::<crate::Runtime>::translate::<tokens::AccountData<Balance>, _>(
|account, asset_id, mut data| {
if asset_id == common::VXOR {
let before = data.free;
data.free /= DENOM_COEFF;
data.reserved /= DENOM_COEFF;
data.frozen /= DENOM_COEFF;
new_issuance += data.free;
log::debug!(
"Denominated balance of {}, balance: {} => {}",
account,
before,
data.free
);
}
Some(data)
},
);
tokens::Locks::<crate::Runtime>::translate::<
BoundedVec<tokens::BalanceLock<Balance>, crate::MaxLocksTokens>,
_,
>(|account, asset_id, mut locks| {
if asset_id == common::VXOR {
for lock in locks.iter_mut() {
lock.amount /= DENOM_COEFF;
}
log::debug!("Denominated locks of {}", account);
}
Some(locks)
});
tokens::TotalIssuance::<crate::Runtime>::mutate(common::VXOR, |issuance| {
*issuance = new_issuance;
});

for (dex_id, dex_info) in dex_manager::DEXInfos::<crate::Runtime>::iter() {
if dex_info.base_asset_id == common::VXOR {
for (target_asset_id, (pool_account, _fee_account)) in
pool_xyk::Properties::<crate::Runtime>::iter_prefix(dex_info.base_asset_id)
{
pool_xyk::Pallet::<crate::Runtime>::fix_pool_parameters(
dex_id,
&pool_account,
&dex_info.base_asset_id,
&target_asset_id,
)?;
}
} else if let Some((pool_account, _fee_account)) =
pool_xyk::Properties::<crate::Runtime>::get(
&dex_info.base_asset_id,
&common::VXOR,
)
{
pool_xyk::Pallet::<crate::Runtime>::fix_pool_parameters(
dex_id,
&pool_account,
&dex_info.base_asset_id,
&common::VXOR,
)?;
}
}

DispatchResult::Ok(())
});
if let Err(err) = result {
log::info!("Failed to denominate VXOR, reverting...: {:?}", err);
}
crate::BlockWeights::get().max_block
}
}

0 comments on commit e15b3b4

Please sign in to comment.