Skip to content

Commit

Permalink
fix: skip the undelegation if the amount is less than relay fee
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Nov 24, 2023
1 parent 40fafe8 commit bf997ba
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion x/stake/endblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/fees"
"github.com/cosmos/cosmos-sdk/x/stake/keeper"
"github.com/cosmos/cosmos-sdk/x/stake/types"
)
Expand Down Expand Up @@ -280,13 +281,34 @@ func handleRefundStake(ctx sdk.Context, k keeper.Keeper) sdk.Events {
var refundEvents sdk.Events
count := 0

bscValidators := k.GetAllValidators(ctx)
bscValidatorsMap := make(map[string]types.Validator, len(bscValidators))
for _, validator := range bscValidators {
bscValidatorsMap[validator.OperatorAddr.String()] = validator
}
boundDenom := k.BondDenom(ctx)

for ; iterator.Valid(); iterator.Next() {
delegation := types.MustUnmarshalDelegation(k.CDC(), iterator.Key(), iterator.Value())
if delegation.CrossStake {
validator := bscValidatorsMap[delegation.ValidatorAddr.String()]
amount := validator.TokensFromShares(delegation.GetShares()).RawInt()

relayFeeCalc := fees.GetCalculator(types.CrossDistributeUndelegatedRelayFee)
if relayFeeCalc == nil {
ctx.Logger().Error("no fee calculator of distributeUndelegated")
return refundEvents
}
relayFee := relayFeeCalc(nil)
if relayFee.Tokens.AmountOf(boundDenom) >= amount {
// skip if the amount is less than relay fee
continue
}

result := handleMsgSideChainUndelegate(ctx.WithCrossStake(true), types.MsgSideChainUndelegate{
DelegatorAddr: delegation.DelegatorAddr,
ValidatorAddr: delegation.ValidatorAddr,
Amount: sdk.NewCoin(k.BondDenom(ctx), delegation.GetShares().RawInt()),
Amount: sdk.NewCoin(boundDenom, amount),
SideChainId: k.ScKeeper.BscSideChainId(ctx),
}, k)
refundEvents = refundEvents.AppendEvents(result.Events)
Expand Down

0 comments on commit bf997ba

Please sign in to comment.