Skip to content

Commit

Permalink
read SequenceFee and refund exact amount, then delete SequenceFee value
Browse files Browse the repository at this point in the history
  • Loading branch information
RustNinja committed Apr 25, 2024
1 parent 28723bd commit 1230be8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
5 changes: 5 additions & 0 deletions x/ibctransfermiddleware/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func (k Keeper) SetSequenceFee(ctx sdk.Context, sequence uint64, coin sdk.Coin)
store.Set(types.GetSequenceKey(sequence), types.MustMarshalCoin(k.cdc, &coin))
}

func (k Keeper) DeleteSequenceFee(ctx sdk.Context, sequence uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetSequenceKey(sequence))
}

func (k Keeper) GetCoin(ctx sdk.Context, targetChannelID, denom string) *types.CoinItem {
params := k.GetParams(ctx)
channelFee := findChannelParams(params.ChannelFees, targetChannelID)
Expand Down
38 changes: 11 additions & 27 deletions x/transfermiddleware/ibc_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,39 +127,23 @@ func (im IBCMiddleware) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Pac
return err
}

denom := data.Denom
coin := im.keeper.IbcTransfermiddleware.GetCoin(ctx, packet.SourceChannel, denom)
сhannelFeeAddress := im.keeper.IbcTransfermiddleware.GetChannelFeeAddress(ctx, packet.SourceChannel)
if coin != nil {
amount := data.Amount
transferAmount, ok := sdk.NewIntFromString(amount)
if !ok {
return errors.Wrapf(transfertypes.ErrInvalidAmount, "unable to parse transfer amount: %s", amount)
fee, found := im.keeper.IbcTransfermiddleware.GetSequenceFee(ctx, packet.Sequence)
if found {
сhannelFeeAddress := im.keeper.IbcTransfermiddleware.GetChannelFeeAddress(ctx, packet.SourceChannel)
if сhannelFeeAddress == "" {
return nil
}

// calculate the percentage charge
/*
coin.Percentage is 100 that means that we charge 1/100 of the transfer amount
coin.MinFee.Amount is the minimum fee that we charge
the new amount is the transfer amount minus the percentage charge and the minimum fee
transfer amount is a 99/100 of the original amount
so to get the fee we charge transferAmount.QuoRaw(coin.Percentage - 1) + coin.MinFee.Amount
*/

percentageCharge := sdk.NewInt(0)
if coin.Percentage > 1 {
percentageCharge = transferAmount.QuoRaw(coin.Percentage - 1)
}
percentageCharge = percentageCharge.Add(coin.MinFee.Amount)

fee_address, err := sdk.AccAddressFromBech32(сhannelFeeAddress)
if err != nil {
return errors.Wrapf(err, "failed to decode receiver address: %s", сhannelFeeAddress)
return nil
// return errors.Wrapf(err, "failed to decode receiver address: %s", сhannelFeeAddress)
}

refund_fee := sdk.NewCoin(denom, percentageCharge)
im.keeper.RefundChannelCosmosFee(ctx, fee_address, sdk.AccAddress(data.Sender), []sdk.Coin{refund_fee})

refund_err := im.keeper.RefundChannelCosmosFee(ctx, fee_address, sdk.AccAddress(data.Sender), []sdk.Coin{fee})
if refund_err == nil {
im.keeper.IbcTransfermiddleware.DeleteSequenceFee(ctx, packet.Sequence)
}
}

return nil
Expand Down

0 comments on commit 1230be8

Please sign in to comment.