From 797b299b22091fc47e5d69e35835d3a434af4e3f Mon Sep 17 00:00:00 2001 From: ethan Date: Tue, 28 Nov 2023 06:49:12 -0700 Subject: [PATCH 1/2] min_swap_amount also gates reward token transfers --- proto/stride/stakeibc/trade_route.proto | 1 + x/stakeibc/keeper/reward_converter.go | 14 +++++++++++--- x/stakeibc/types/trade_route.pb.go | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/proto/stride/stakeibc/trade_route.proto b/proto/stride/stakeibc/trade_route.proto index 6e5735a059..9fd9e1cac5 100644 --- a/proto/stride/stakeibc/trade_route.proto +++ b/proto/stride/stakeibc/trade_route.proto @@ -43,6 +43,7 @@ message TradeRoute { string spot_price = 10; // min and max set boundaries of reward denom on trade chain we will swap + // min also decides when reward token transfers are worth it (transfer fees) string min_swap_amount = 11 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false diff --git a/x/stakeibc/keeper/reward_converter.go b/x/stakeibc/keeper/reward_converter.go index f900993494..0d2e76d3d8 100644 --- a/x/stakeibc/keeper/reward_converter.go +++ b/x/stakeibc/keeper/reward_converter.go @@ -5,6 +5,7 @@ import ( "time" errorsmod "cosmossdk.io/errors" + math "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/bech32" bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -41,7 +42,14 @@ import ( // This will be two hops to unwind the ibc denom through the rewardZone using pfm in the transfer memo if possible // // msgs with packet forwarding memos can unwind through the reward zone and chain two transfer hops without callbacks -func (k Keeper) TransferRewardTokensHostToTrade(ctx sdk.Context, amount sdk.Int, route types.TradeRoute) error { +func (k Keeper) TransferRewardTokensHostToTrade(ctx sdk.Context, amount math.Int, route types.TradeRoute) error { + // If the min swap amount was not set it would be ZeroInt, if positive we need to compare to the amount given + // then if the min swap amount is greater than the current amount, do nothing this epoch to avoid small transfers + // Particularly important for the PFM hop if the reward chain has frictional transfer fees (like noble chain) + if route.MinSwapAmount.IsPositive() && route.MinSwapAmount.GT(amount) { + return nil + } + // Timeout for ica tx and the transfer msgs is at end of epoch strideEpochTracker, found := k.GetEpochTracker(ctx, epochstypes.STRIDE_EPOCH) if !found { @@ -90,7 +98,7 @@ func (k Keeper) TransferRewardTokensHostToTrade(ctx sdk.Context, amount sdk.Int, } // ICA tx to kick off transfering the converted tokens back from tradeZone to the hostZone withdrawal ICA -func (k Keeper) TransferConvertedTokensTradeToHost(ctx sdk.Context, amount sdk.Int, route types.TradeRoute) error { +func (k Keeper) TransferConvertedTokensTradeToHost(ctx sdk.Context, amount math.Int, route types.TradeRoute) error { // Timeout for ica tx and the transfer msgs is at end of epoch strideEpochTracker, found := k.GetEpochTracker(ctx, epochstypes.STRIDE_EPOCH) if !found { @@ -133,7 +141,7 @@ func (k Keeper) TransferConvertedTokensTradeToHost(ctx sdk.Context, amount sdk.I // Trade reward tokens in the Trade ICA for the target output token type using ICA remote tx on trade zone // The amount represents the total amount of the reward token in the trade ICA found by the calling ICQ // Depending on min and max swap amounts set in the route, it is possible not the full amount given will swap -func (k Keeper) TradeRewardTokens(ctx sdk.Context, amount sdk.Int, route types.TradeRoute) error { +func (k Keeper) TradeRewardTokens(ctx sdk.Context, amount math.Int, route types.TradeRoute) error { // If the min swap amount was not set it would be ZeroInt, if positive we need to compare to the amount given // then if the min swap amount is greater than the current amount, do nothing this epoch to avoid small swaps if route.MinSwapAmount.IsPositive() && route.MinSwapAmount.GT(amount) { diff --git a/x/stakeibc/types/trade_route.pb.go b/x/stakeibc/types/trade_route.pb.go index 8f6206d810..5f3912fee8 100644 --- a/x/stakeibc/types/trade_route.pb.go +++ b/x/stakeibc/types/trade_route.pb.go @@ -111,6 +111,7 @@ type TradeRoute struct { // Spot price is a decimal ratio of the input to output denom as a string SpotPrice string `protobuf:"bytes,10,opt,name=spot_price,json=spotPrice,proto3" json:"spot_price,omitempty"` // min and max set boundaries of reward denom on trade chain we will swap + // min also decides when reward token transfers are worth it (transfer fees) MinSwapAmount github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,11,opt,name=min_swap_amount,json=minSwapAmount,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"min_swap_amount"` MaxSwapAmount github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,12,opt,name=max_swap_amount,json=maxSwapAmount,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"max_swap_amount"` } From 83714e0eff5352e1452b63ad4fad2c078c99b7d4 Mon Sep 17 00:00:00 2001 From: ethan Date: Tue, 28 Nov 2023 07:04:14 -0700 Subject: [PATCH 2/2] nit rename to sdkmath --- x/stakeibc/keeper/reward_converter.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x/stakeibc/keeper/reward_converter.go b/x/stakeibc/keeper/reward_converter.go index 0d2e76d3d8..ccdf6edcda 100644 --- a/x/stakeibc/keeper/reward_converter.go +++ b/x/stakeibc/keeper/reward_converter.go @@ -5,7 +5,7 @@ import ( "time" errorsmod "cosmossdk.io/errors" - math "cosmossdk.io/math" + sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/bech32" bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -42,7 +42,7 @@ import ( // This will be two hops to unwind the ibc denom through the rewardZone using pfm in the transfer memo if possible // // msgs with packet forwarding memos can unwind through the reward zone and chain two transfer hops without callbacks -func (k Keeper) TransferRewardTokensHostToTrade(ctx sdk.Context, amount math.Int, route types.TradeRoute) error { +func (k Keeper) TransferRewardTokensHostToTrade(ctx sdk.Context, amount sdkmath.Int, route types.TradeRoute) error { // If the min swap amount was not set it would be ZeroInt, if positive we need to compare to the amount given // then if the min swap amount is greater than the current amount, do nothing this epoch to avoid small transfers // Particularly important for the PFM hop if the reward chain has frictional transfer fees (like noble chain) @@ -98,7 +98,7 @@ func (k Keeper) TransferRewardTokensHostToTrade(ctx sdk.Context, amount math.Int } // ICA tx to kick off transfering the converted tokens back from tradeZone to the hostZone withdrawal ICA -func (k Keeper) TransferConvertedTokensTradeToHost(ctx sdk.Context, amount math.Int, route types.TradeRoute) error { +func (k Keeper) TransferConvertedTokensTradeToHost(ctx sdk.Context, amount sdkmath.Int, route types.TradeRoute) error { // Timeout for ica tx and the transfer msgs is at end of epoch strideEpochTracker, found := k.GetEpochTracker(ctx, epochstypes.STRIDE_EPOCH) if !found { @@ -141,7 +141,7 @@ func (k Keeper) TransferConvertedTokensTradeToHost(ctx sdk.Context, amount math. // Trade reward tokens in the Trade ICA for the target output token type using ICA remote tx on trade zone // The amount represents the total amount of the reward token in the trade ICA found by the calling ICQ // Depending on min and max swap amounts set in the route, it is possible not the full amount given will swap -func (k Keeper) TradeRewardTokens(ctx sdk.Context, amount math.Int, route types.TradeRoute) error { +func (k Keeper) TradeRewardTokens(ctx sdk.Context, amount sdkmath.Int, route types.TradeRoute) error { // If the min swap amount was not set it would be ZeroInt, if positive we need to compare to the amount given // then if the min swap amount is greater than the current amount, do nothing this epoch to avoid small swaps if route.MinSwapAmount.IsPositive() && route.MinSwapAmount.GT(amount) {