Skip to content

Commit

Permalink
fix: fix faulty epochs created prior v0.9.0 migration (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
kerber0x authored Jun 22, 2023
1 parent a445636 commit 5bdc4d6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/liquidity_hub/fee_distributor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fee_distributor"
version = "0.9.0"
version = "0.9.1"
authors = ["Kerber0x <[email protected]>"]
edition.workspace = true
description = "Contract to distribute the fees collected by the Fee Collector."
Expand Down
10 changes: 9 additions & 1 deletion contracts/liquidity_hub/fee_distributor/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,18 @@ pub fn migrate(mut deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Respons
});
}

set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

if storage_version < Version::parse("0.9.0")? {
migrations::migrate_to_v090(deps.branch())?;
}

set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
if storage_version == Version::parse("0.9.0")? {
let fees_refund_messages = migrations::migrate_to_v091(deps.branch())?;
return Ok(Response::default()
.add_messages(fees_refund_messages)
.add_attribute("action", "migrate"));
}

Ok(Response::default())
}
47 changes: 45 additions & 2 deletions contracts/liquidity_hub/fee_distributor/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#![cfg(not(tarpaulin_include))]
use crate::state::{CONFIG, EPOCHS};

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_binary, DepsMut, Order, QueryRequest, StdError, StdResult, Timestamp, Uint64, WasmQuery,
to_binary, CosmosMsg, DepsMut, Order, QueryRequest, StdError, StdResult, Timestamp,
Uint64, WasmQuery,
};
use cw_storage_plus::Map;

use white_whale::fee_distributor::Epoch;
use white_whale::pool_network::asset;
use white_whale::pool_network::asset::Asset;
use white_whale::whale_lair::GlobalIndex;
use white_whale::whale_lair::QueryMsg as LairQueryMsg;

use crate::state::{get_claimable_epochs, CONFIG, EPOCHS};

/// Migrates state from the first iteration, v0.8.* to v0.9.0, which includes the global index in
/// the Epoch. This was done to fix bonding issues.
pub fn migrate_to_v090(deps: DepsMut) -> Result<(), StdError> {
Expand Down Expand Up @@ -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<Vec<CosmosMsg>, 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::<Vec<Epoch>>();

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<Asset> = 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)
}

0 comments on commit 5bdc4d6

Please sign in to comment.