From 5bdc4d65099159ed9d92fa5bc2cc982cd00c8918 Mon Sep 17 00:00:00 2001 From: Kerber0x <94062656+kerber0x@users.noreply.github.com> Date: Thu, 22 Jun 2023 15:27:41 +0100 Subject: [PATCH] fix: fix faulty epochs created prior v0.9.0 migration (#198) --- Cargo.lock | 2 +- .../liquidity_hub/fee_distributor/Cargo.toml | 2 +- .../fee_distributor/src/contract.rs | 10 +++- .../fee_distributor/src/migrations.rs | 47 ++++++++++++++++++- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 273c14fd..720841e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -561,7 +561,7 @@ dependencies = [ [[package]] name = "fee_distributor" -version = "0.9.0" +version = "0.9.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", diff --git a/contracts/liquidity_hub/fee_distributor/Cargo.toml b/contracts/liquidity_hub/fee_distributor/Cargo.toml index 91d8c294..7ee9442e 100644 --- a/contracts/liquidity_hub/fee_distributor/Cargo.toml +++ b/contracts/liquidity_hub/fee_distributor/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fee_distributor" -version = "0.9.0" +version = "0.9.1" authors = ["Kerber0x "] edition.workspace = true description = "Contract to distribute the fees collected by the Fee Collector." diff --git a/contracts/liquidity_hub/fee_distributor/src/contract.rs b/contracts/liquidity_hub/fee_distributor/src/contract.rs index 2476423a..bde07b77 100644 --- a/contracts/liquidity_hub/fee_distributor/src/contract.rs +++ b/contracts/liquidity_hub/fee_distributor/src/contract.rs @@ -180,10 +180,18 @@ pub fn migrate(mut deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result Result<(), StdError> { @@ -61,3 +66,41 @@ pub fn migrate_to_v090(deps: DepsMut) -> Result<(), StdError> { Ok(()) } + +/// Fixes the broken state for Epochs created prior to the v0.9.0 migration. +pub fn migrate_to_v091(deps: DepsMut) -> Result, StdError> { + let claimable_epochs = get_claimable_epochs(deps.as_ref())?; + + // 14 June 2023 16:00:00 UTC + let migration_timestamp = Timestamp::from_seconds(1686758400); + let mut faulty_epochs = claimable_epochs + .epochs + .into_iter() + .filter(|epoch| epoch.start_time.seconds() < migration_timestamp.seconds()) + .collect::>(); + + let fee_collector_addr = CONFIG.load(deps.storage)?.fee_collector_addr; + + // collect all available funds on faulty epochs and send them back to the fee collector, to be + // redistributed on the next (new) epoch + + let mut total_fees: Vec = vec![]; + for epoch in faulty_epochs.iter_mut() { + total_fees = asset::aggregate_assets(total_fees, epoch.available.clone())?; + + // set the available fees of this faulty epoch to zero + epoch.available = vec![]; + + // save the faulty epoch in the state + EPOCHS.save(deps.storage, &epoch.id.to_be_bytes(), &epoch)?; + } + + // create messages to send total_fees back to the fee collector + let mut messages = vec![]; + + for fee in total_fees { + messages.push(fee.into_msg(fee_collector_addr.clone())?); + } + + Ok(messages) +}