Skip to content

Commit 30b0f2b

Browse files
authored
Merge pull request #63 from Firstset/59/fix-coin-denom-different
fix the denom panic when commission rewards contain IBC tokens
2 parents d4c4b48 + 6dc7973 commit 30b0f2b

File tree

2 files changed

+89
-18
lines changed

2 files changed

+89
-18
lines changed

td2/alert.go

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -790,32 +790,100 @@ func evaluateStakeChangeAlert(cc *ChainConfig) (bool, bool) {
790790
func evaluateUnclaimedRewardsAlert(cc *ChainConfig) (bool, bool) {
791791
alert, resolved := false, false
792792

793-
selfRewardsLen := len(*cc.valInfo.SelfDelegationRewards)
794-
commissionLen := len(*cc.valInfo.Commission)
793+
selfRewardsLen := 0
794+
commissionLen := 0
795+
if cc.valInfo.SelfDelegationRewards != nil {
796+
selfRewardsLen = len(*cc.valInfo.SelfDelegationRewards)
797+
}
798+
if cc.valInfo.Commission != nil {
799+
commissionLen = len(*cc.valInfo.Commission)
800+
}
795801

796802
if selfRewardsLen > 0 || commissionLen > 0 {
797-
var denom string
798-
var totalRewards github_com_cosmos_cosmos_sdk_types.DecCoin
803+
var targetDenoms []string
804+
addDenom := func(denom string) {
805+
if denom == "" {
806+
return
807+
}
808+
if !slices.Contains(targetDenoms, denom) {
809+
targetDenoms = append(targetDenoms, denom)
810+
}
811+
}
799812

800-
if selfRewardsLen > 0 {
801-
firstReward := (*cc.valInfo.SelfDelegationRewards)[0]
802-
denom = firstReward.Denom
803-
totalRewards = github_com_cosmos_cosmos_sdk_types.DecCoin{
804-
Denom: denom,
805-
Amount: firstReward.Amount,
813+
targetDenom := ""
814+
// we take the chain's native denoms from cc.denomMetadata
815+
if cc.denomMetadata != nil {
816+
addDenom(cc.denomMetadata.Base)
817+
addDenom(cc.denomMetadata.Display)
818+
targetDenom = cc.denomMetadata.Display
819+
if targetDenom == "" {
820+
targetDenom = cc.denomMetadata.Base
806821
}
822+
}
807823

808-
if commissionLen > 0 {
809-
totalRewards = totalRewards.Add((*cc.valInfo.Commission)[0])
824+
// when `cc.denomMetadata` is nil, we try to infer the denom from the rewards
825+
if len(targetDenoms) == 0 {
826+
if selfRewardsLen > 0 {
827+
addDenom((*cc.valInfo.SelfDelegationRewards)[0].Denom)
828+
} else if commissionLen > 0 {
829+
// some chains return commission coins with IBC denoms
830+
// we will filter out these non-native coins for now
831+
for _, coin := range *cc.valInfo.Commission {
832+
if !strings.HasPrefix(coin.Denom, "ibc/") {
833+
addDenom(coin.Denom)
834+
}
835+
}
810836
}
811-
} else {
812-
firstCommission := (*cc.valInfo.Commission)[0]
813-
totalRewards = github_com_cosmos_cosmos_sdk_types.DecCoin{
814-
Denom: firstCommission.Denom,
815-
Amount: firstCommission.Amount,
837+
}
838+
839+
if targetDenom == "" && len(targetDenoms) > 0 {
840+
targetDenom = targetDenoms[0]
841+
}
842+
843+
if len(targetDenoms) == 0 || targetDenom == "" {
844+
return alert, resolved
845+
}
846+
847+
var nativeCoins []github_com_cosmos_cosmos_sdk_types.DecCoin
848+
if selfRewardsLen > 0 {
849+
for _, coin := range *cc.valInfo.SelfDelegationRewards {
850+
if slices.Contains(targetDenoms, coin.Denom) {
851+
nativeCoins = append(nativeCoins, coin)
852+
}
853+
}
854+
}
855+
if commissionLen > 0 {
856+
for _, coin := range *cc.valInfo.Commission {
857+
if slices.Contains(targetDenoms, coin.Denom) {
858+
nativeCoins = append(nativeCoins, coin)
859+
}
860+
}
861+
}
862+
863+
if len(nativeCoins) == 0 {
864+
return alert, resolved
865+
}
866+
867+
if cc.denomMetadata != nil {
868+
convertedCoins, err := utils.ConvertDecCoinToDisplayUnit(nativeCoins, *cc.denomMetadata)
869+
if err == nil {
870+
nativeCoins = *convertedCoins
871+
} else {
872+
l(fmt.Errorf("cannot convert rewards/commission to display unit for %s, err: %w", cc.name, err))
816873
}
817874
}
818875

876+
totalAmount := github_com_cosmos_cosmos_sdk_types.ZeroDec()
877+
for _, coin := range nativeCoins {
878+
totalAmount = totalAmount.Add(coin.Amount)
879+
}
880+
881+
if totalAmount.IsZero() {
882+
return alert, resolved
883+
}
884+
885+
totalRewards := github_com_cosmos_cosmos_sdk_types.NewDecCoinFromDec(targetDenom, totalAmount)
886+
819887
coinPrice, err := td.coinMarketCapClient.GetPrice(td.ctx, cc.Slug)
820888
if err == nil {
821889
totalRewardsConverted := totalRewards.Amount.MustFloat64() * coinPrice.Price

td2/utils/price-conversion.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,10 @@ func ConvertDecCoinToDisplayUnit(coins []github_com_cosmos_cosmos_sdk_types.DecC
273273
}
274274

275275
if !foundCurrentDenom {
276-
return nil, fmt.Errorf("source denomination '%s' not found in denomination units", coin.Denom)
276+
// source denomination not found in denomination units
277+
// the coin is most likely bridged to the chain so we keep it as is
278+
convertedCoins = append(convertedCoins, coin)
279+
continue
277280
}
278281

279282
// Calculate the conversion factor based on exponent difference

0 commit comments

Comments
 (0)