From cded6182e5864af660d6b8ee90d583c1eca27648 Mon Sep 17 00:00:00 2001 From: rustdev Date: Wed, 22 May 2024 17:24:57 +0100 Subject: [PATCH] remove custom logic in ibc transfer keeper. --- custom/ibc-transfer/keeper/keeper.go | 104 +-------------------------- 1 file changed, 1 insertion(+), 103 deletions(-) diff --git a/custom/ibc-transfer/keeper/keeper.go b/custom/ibc-transfer/keeper/keeper.go index 469c2eab..60f2ae52 100644 --- a/custom/ibc-transfer/keeper/keeper.go +++ b/custom/ibc-transfer/keeper/keeper.go @@ -2,22 +2,17 @@ package keeper import ( "context" - "encoding/json" - "fmt" - "time" "github.com/cosmos/cosmos-sdk/codec" ibctransferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper" storetypes "github.com/cosmos/cosmos-sdk/store/types" - sdk "github.com/cosmos/cosmos-sdk/types" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" "github.com/cosmos/ibc-go/v7/modules/core/exported" custombankkeeper "github.com/notional-labs/composable/v6/custom/bank/keeper" ibctransfermiddleware "github.com/notional-labs/composable/v6/x/ibctransfermiddleware/keeper" - ibctransfermiddlewaretypes "github.com/notional-labs/composable/v6/x/ibctransfermiddleware/types" ) type Keeper struct { @@ -57,102 +52,5 @@ func NewKeeper( // If the transfer amount is less than the minimum fee, it will charge the full transfer amount. // If the transfer amount is greater than the minimum fee, it will charge the minimum fee and the percentage fee. func (k Keeper) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types.MsgTransferResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - params := k.IbcTransfermiddleware.GetParams(ctx) - charge_coin := sdk.NewCoin(msg.Token.Denom, sdk.ZeroInt()) - if params.ChannelFees != nil && len(params.ChannelFees) > 0 { - channelFee := findChannelParams(params.ChannelFees, msg.SourceChannel) - if channelFee != nil { - if channelFee.MinTimeoutTimestamp > 0 { - - goCtx := sdk.UnwrapSDKContext(goCtx) - blockTime := goCtx.BlockTime() - - timeoutTimeInFuture := time.Unix(0, int64(msg.TimeoutTimestamp)) - if timeoutTimeInFuture.Before(blockTime) { - return nil, fmt.Errorf("incorrect timeout timestamp found during ibc transfer. timeout timestamp is in the past") - } - - difference := timeoutTimeInFuture.Sub(blockTime).Nanoseconds() - if difference < channelFee.MinTimeoutTimestamp { - return nil, fmt.Errorf("incorrect timeout timestamp found during ibc transfer. too soon") - } - } - coin := findCoinByDenom(channelFee.AllowedTokens, msg.Token.Denom) - if coin == nil { - return nil, fmt.Errorf("token not allowed to be transferred in this channel") - } - - minFee := coin.MinFee.Amount - priority := GetPriority(msg.Memo) - if priority != nil { - p := findPriority(coin.TxPriorityFee, *priority) - if p != nil && coin.MinFee.Denom == p.PriorityFee.Denom { - minFee = minFee.Add(p.PriorityFee.Amount) - } - } - - charge := minFee - if charge.GT(msg.Token.Amount) { - charge = msg.Token.Amount - } - - newAmount := msg.Token.Amount.Sub(charge) - - if newAmount.IsPositive() { - percentageCharge := newAmount.QuoRaw(coin.Percentage) - newAmount = newAmount.Sub(percentageCharge) - charge = charge.Add(percentageCharge) - } - - msgSender, err := sdk.AccAddressFromBech32(msg.Sender) - if err != nil { - return nil, err - } - - feeAddress, err := sdk.AccAddressFromBech32(channelFee.FeeAddress) - if err != nil { - return nil, err - } - - charge_coin = sdk.NewCoin(msg.Token.Denom, charge) - send_err := k.bank.SendCoins(ctx, msgSender, feeAddress, sdk.NewCoins(charge_coin)) - if send_err != nil { - return nil, send_err - } - - if newAmount.LTE(sdk.ZeroInt()) { - return &types.MsgTransferResponse{}, nil - } - msg.Token.Amount = newAmount - } - } - ret, err := k.Keeper.Transfer(goCtx, msg) - if err == nil && ret != nil && !charge_coin.IsZero() { - k.IbcTransfermiddleware.SetSequenceFee(ctx, ret.Sequence, charge_coin) - } - return ret, err -} - -func GetPriority(jsonString string) *string { - var data map[string]interface{} - if err := json.Unmarshal([]byte(jsonString), &data); err != nil { - return nil - } - - priority, ok := data["priority"].(string) - if !ok { - return nil - } - - return &priority -} - -func findPriority(priorities []*ibctransfermiddlewaretypes.TxPriorityFee, priority string) *ibctransfermiddlewaretypes.TxPriorityFee { - for _, p := range priorities { - if p.Priority == priority { - return p - } - } - return nil + return k.Keeper.Transfer(goCtx, msg) }