Skip to content

Commit

Permalink
Add undelegations processing to migration handler
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Jan 8, 2023
1 parent a8c4fe7 commit 7c73785
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
25 changes: 24 additions & 1 deletion contracts/tg4-stake/src/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ use itertools::Itertools;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::msg::Undelegation;
use crate::state::CONFIG;
use cosmwasm_std::{
Addr, BlockInfo, CustomQuery, Decimal, Deps, Order, StdResult, Storage, Uint128,
coin, Addr, BlockInfo, CustomQuery, Decimal, Deps, Order, StdResult, Storage, Uint128,
};
use cw_storage_plus::{Bound, Index, IndexList, IndexedMap, MultiIndex, PrefixBound};
use tg_bindings::TgradeMsg;
use tg_bindings::TgradeMsg::Undelegate;
use tg_utils::Expiration;

// settings for pagination
Expand Down Expand Up @@ -315,3 +319,22 @@ impl<'a> Claims<'a> {
.collect()
}
}

// Helper to repair the auto-release claims bug (#198)
pub fn process_pending_undelegations<Q: CustomQuery>(
deps: Deps<Q>,
undelegations: &[Undelegation],
) -> StdResult<Vec<TgradeMsg>> {
let cfg = CONFIG.load(deps.storage)?;
let msgs = undelegations
.iter()
.map(|undelegation| {
let amount = coin(undelegation.amount.into(), cfg.denom.clone());
Undelegate {
funds: amount,
recipient: undelegation.addr.clone(),
}
})
.collect();
Ok(msgs)
}
8 changes: 7 additions & 1 deletion contracts/tg4-stake/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cosmwasm_std::{
use std::cmp::min;
use std::ops::Sub;

use crate::claim::process_pending_undelegations;
use cw2::set_contract_version;
use cw_storage_plus::Bound;
use cw_utils::{ensure_from_older_version, maybe_addr};
Expand Down Expand Up @@ -756,7 +757,12 @@ pub fn migrate(
Ok(cfg)
})?;

Ok(Response::new())
if let Some(undelegations) = msg.undelegations {
let msgs = process_pending_undelegations(deps.as_ref(), &undelegations)?;
Ok(Response::new().add_messages(msgs))
} else {
Ok(Response::new())
}
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions contracts/tg4-stake/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,20 @@ pub struct ClaimsResponse {
pub claims: Vec<Claim>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct Undelegation {
pub addr: String,
pub amount: Uint128,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub struct MigrateMsg {
pub tokens_per_point: Option<Uint128>,
pub min_bond: Option<Uint128>,
pub unbonding_period: Option<u64>,
pub auto_return_limit: Option<u64>,
pub undelegations: Option<Vec<Undelegation>>,
}

#[cfg(test)]
Expand Down

0 comments on commit 7c73785

Please sign in to comment.