From 439009a60a421d958004f8e91034dd3b9c26160e Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Tue, 28 May 2024 14:56:32 +0700 Subject: [PATCH] rewrite if-else chain to switch statements --- x/interchainstaking/types/redemptions.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/x/interchainstaking/types/redemptions.go b/x/interchainstaking/types/redemptions.go index e65fccd30..a13fe9cc9 100644 --- a/x/interchainstaking/types/redemptions.go +++ b/x/interchainstaking/types/redemptions.go @@ -57,16 +57,19 @@ func DetermineAllocationsForUndelegation(currentAllocations map[string]math.Int, // use Amount+1 in the line below to avoid 1 remaining where truncation leaves 1 remaining - e.g. 1000 => 333/333/333 + 1. outWeights[overAllocated[idx].ValoperAddress] = sdk.NewDecFromInt(overAllocated[idx].Amount).Quo(sdk.NewDecFromInt(sum)).Mul(sdk.NewDecFromInt(overAllocationSplit)).TruncateInt() overAlloc := availablePerValidator[overAllocated[idx].ValoperAddress] - if overAlloc.IsNil() { + switch { + case overAlloc.IsNil(): availablePerValidator[overAllocated[idx].ValoperAddress] = sdk.ZeroInt() - } else if outWeights[overAllocated[idx].ValoperAddress].GT(overAlloc) { + case outWeights[overAllocated[idx].ValoperAddress].GT(overAlloc): // use up all of overAllocated[idx] and set available to zero. outWeights[overAllocated[idx].ValoperAddress] = availablePerValidator[overAllocated[idx].ValoperAddress] availablePerValidator[overAllocated[idx].ValoperAddress] = sdk.ZeroInt() - } else { + default: // or don't, and reduce available as appropriate. availablePerValidator[overAllocated[idx].ValoperAddress] = availablePerValidator[overAllocated[idx].ValoperAddress].Sub(outWeights[overAllocated[idx].ValoperAddress]) + } + overAllocated[idx].Amount = overAllocated[idx].Amount.Sub(outWeights[overAllocated[idx].ValoperAddress]) outSum = outSum.Add(outWeights[overAllocated[idx].ValoperAddress]) }