Skip to content

Commit 624bbd2

Browse files
committed
Use a struct to return expired claims data
Fix type_complexity warning
1 parent 074e49f commit 624bbd2

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

contracts/tg4-stake/src/claim.rs

+31-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ use tg_utils::Expiration;
1515
const MAX_LIMIT: u32 = 100;
1616
const DEFAULT_LIMIT: u32 = 30;
1717

18+
#[derive(Clone, Debug, PartialEq, Eq)]
19+
pub(crate) struct TokenReleaseInfo {
20+
pub addr: Addr,
21+
pub amount: Uint128,
22+
}
23+
24+
#[derive(Clone, Debug, PartialEq, Eq)]
25+
pub(crate) struct ReleaseData {
26+
pub liquid_releases: Vec<TokenReleaseInfo>,
27+
pub vesting_releases: Vec<TokenReleaseInfo>,
28+
}
29+
1830
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
1931
pub struct Claim {
2032
/// Address owning the claim
@@ -154,14 +166,14 @@ impl<'a> Claims<'a> {
154166

155167
/// This iterates over all mature claims of any addresses, and removes them. Up to `limit`
156168
/// claims would be processed, starting from the oldest. It removes the finished claims and
157-
/// returns vector of pairs: `(addr, amount)`, representing amount of tokens to be released to particular addresses
158-
#[allow(clippy::type_complexity)]
159-
pub fn claim_expired(
169+
/// returns a pair of vectors representing the amounts of liquid and vesting tokens
170+
/// to be released to particular addresses.
171+
pub(crate) fn claim_expired(
160172
&self,
161173
storage: &mut dyn Storage,
162174
block: &BlockInfo,
163175
limit: impl Into<Option<u64>>,
164-
) -> StdResult<(Vec<(Addr, Uint128)>, Vec<(Addr, Uint128)>)> {
176+
) -> StdResult<ReleaseData> {
165177
let claims = self
166178
.claims
167179
.idx
@@ -183,7 +195,10 @@ impl<'a> Claims<'a> {
183195
// is stabilized [https://github.com/rust-lang/rust/issues/80552]
184196
.group_by(|claim| &claim.addr)
185197
.into_iter()
186-
.map(|(addr, group)| (addr.clone(), group.map(|claim| claim.amount).sum()))
198+
.map(|(addr, group)| TokenReleaseInfo {
199+
addr: addr.clone(),
200+
amount: group.map(|claim| claim.amount).sum(),
201+
})
187202
.collect();
188203

189204
let vesting_releases = claims
@@ -192,19 +207,22 @@ impl<'a> Claims<'a> {
192207
// is stabilized [https://github.com/rust-lang/rust/issues/80552]
193208
.group_by(|claim| &claim.addr)
194209
.into_iter()
195-
.map(|(addr, group)| {
196-
(
197-
addr.clone(),
198-
group
199-
.map(|claim| claim.vesting_amount.unwrap_or_default())
200-
.sum(),
201-
)
210+
.map(|(addr, group)| TokenReleaseInfo {
211+
addr: addr.clone(),
212+
amount: group
213+
.map(|claim| claim.vesting_amount.unwrap_or_default())
214+
.sum(),
202215
})
203216
.collect();
204217

205218
self.release_claims(storage, claims)?;
206219

207-
Ok((liquid_releases, vesting_releases))
220+
let release_data = ReleaseData {
221+
liquid_releases,
222+
vesting_releases,
223+
};
224+
225+
Ok(release_data)
208226
}
209227

210228
/// Processes claims filtering those which are to be released. Returns vector of claims to be

contracts/tg4-stake/src/contract.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -535,29 +535,31 @@ fn release_expired_claims<Q: CustomQuery>(
535535
env: Env,
536536
config: Config,
537537
) -> Result<Vec<SubMsg>, ContractError> {
538-
let (liquid_releases, vesting_releases) =
538+
let release_data =
539539
claims().claim_expired(deps.storage, &env.block, config.auto_return_limit)?;
540540

541-
let send_msgs = liquid_releases
541+
let send_msgs = release_data
542+
.liquid_releases
542543
.into_iter()
543-
.filter(|(_, amount)| !amount.is_zero())
544-
.map(|(addr, amount)| {
545-
let amount = coins(amount.into(), config.denom.clone());
544+
.filter(|release_info| !release_info.amount.is_zero())
545+
.map(|release_info| {
546+
let amount = coins(release_info.amount.into(), config.denom.clone());
546547
Ok(SubMsg::new(BankMsg::Send {
547-
to_address: addr.into(),
548+
to_address: release_info.addr.into(),
548549
amount,
549550
}))
550551
})
551552
.collect::<StdResult<Vec<_>>>()?;
552553

553-
let undelegate_msgs = vesting_releases
554+
let undelegate_msgs = release_data
555+
.vesting_releases
554556
.into_iter()
555-
.filter(|(_, amount)| !amount.is_zero())
556-
.map(|(addr, amount)| {
557-
let amount = coin(amount.into(), config.denom.clone());
557+
.filter(|release_info| !release_info.amount.is_zero())
558+
.map(|release_info| {
559+
let amount = coin(release_info.amount.into(), config.denom.clone());
558560
Ok(SubMsg::new(TgradeMsg::Undelegate {
559561
funds: amount,
560-
recipient: addr.to_string(),
562+
recipient: release_info.addr.to_string(),
561563
}))
562564
})
563565
.collect::<StdResult<Vec<_>>>()?;

0 commit comments

Comments
 (0)