Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reward converter - remove cache context wrapper and add trade route callback #999

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions proto/stride/stakeibc/callbacks.proto
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@ message CommunityPoolBalanceQueryCallback {
ICAAccountType ica_type = 1;
string denom = 2;
}

message TradeRouteCallback {
string reward_denom = 1;
string host_denom = 2;
}
11 changes: 9 additions & 2 deletions x/stakeibc/keeper/icqcallbacks_pool_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ func PoolPriceCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Qu
}

// Unmarshal the callback data containing the tradeRoute we are on
var tradeRoute types.TradeRoute
if err := proto.Unmarshal(query.CallbackData, &tradeRoute); err != nil {
var tradeRouteCallback types.TradeRouteCallback
if err := proto.Unmarshal(query.CallbackData, &tradeRouteCallback); err != nil {
return errorsmod.Wrapf(err, "unable to unmarshal trade reward balance callback data")
}

// Lookup the trade route from the keys in the callback
tradeRoute, found := k.GetTradeRoute(ctx, tradeRouteCallback.RewardDenom, tradeRouteCallback.HostDenom)
if !found {
return types.ErrTradeRouteNotFound.Wrapf("trade route from %s to %s not found",
tradeRouteCallback.RewardDenom, tradeRouteCallback.HostDenom)
}

// Confirm the denom's from the query response match the denom's in the route
if err := AssertTwapAssetsMatchTradeRoute(twapRecord, tradeRoute); err != nil {
return err
Expand Down
21 changes: 12 additions & 9 deletions x/stakeibc/keeper/icqcallbacks_trade_converted_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ func TradeConvertedBalanceCallback(k Keeper, ctx sdk.Context, args []byte, query
return errorsmod.Wrap(err, "unable to determine balance from query response")
}

// Unmarshal the callback data containing the trade route we are on
var tradeRoute types.TradeRoute
if err := proto.Unmarshal(query.CallbackData, &tradeRoute); err != nil {
// Unmarshal the callback data containing the tradeRoute we are on
var tradeRouteCallback types.TradeRouteCallback
if err := proto.Unmarshal(query.CallbackData, &tradeRouteCallback); err != nil {
return errorsmod.Wrapf(err, "unable to unmarshal trade reward balance callback data")
}

// Lookup the trade route from the keys in the callback
tradeRoute, found := k.GetTradeRoute(ctx, tradeRouteCallback.RewardDenom, tradeRouteCallback.HostDenom)
if !found {
return types.ErrTradeRouteNotFound.Wrapf("trade route from %s to %s not found",
tradeRouteCallback.RewardDenom, tradeRouteCallback.HostDenom)
}

// Confirm the balance is greater than zero, or else exit with no further action
if tradeConvertedBalanceAmount.LTE(sdkmath.ZeroInt()) {
k.Logger(ctx).Info(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_TradeConvertedBalance,
Expand All @@ -45,12 +52,8 @@ func TradeConvertedBalanceCallback(k Keeper, ctx sdk.Context, args []byte, query
}

// Using ICA commands on the trade address, transfer the found converted tokens from the trade zone to the host zone
err = utils.ApplyFuncIfNoError(ctx, func(c sdk.Context) error {
return k.TransferConvertedTokensTradeToHost(ctx, tradeConvertedBalanceAmount, tradeRoute)
})
if err != nil {
k.Logger(ctx).Error(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_TradeConvertedBalance,
"Initiating transfer of converted tokens to back to host zone failed: %s", err.Error()))
if err := k.TransferConvertedTokensTradeToHost(ctx, tradeConvertedBalanceAmount, tradeRoute); err != nil {
return errorsmod.Wrapf(err, "initiating transfer of converted tokens to back to host zone failed")
}

k.Logger(ctx).Info(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_TradeConvertedBalance,
Expand Down
27 changes: 15 additions & 12 deletions x/stakeibc/keeper/icqcallbacks_trade_reward_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,28 @@ func TradeRewardBalanceCallback(k Keeper, ctx sdk.Context, args []byte, query ic
}

// Unmarshal the callback data containing the tradeRoute we are on
var tradeRoute types.TradeRoute
if err := proto.Unmarshal(query.CallbackData, &tradeRoute); err != nil {
var tradeRouteCallback types.TradeRouteCallback
if err := proto.Unmarshal(query.CallbackData, &tradeRouteCallback); err != nil {
return errorsmod.Wrapf(err, "unable to unmarshal trade reward balance callback data")
}

// Lookup the trade route from the keys in the callback
tradeRoute, found := k.GetTradeRoute(ctx, tradeRouteCallback.RewardDenom, tradeRouteCallback.HostDenom)
if !found {
return types.ErrTradeRouteNotFound.Wrapf("trade route from %s to %s not found",
tradeRouteCallback.RewardDenom, tradeRouteCallback.HostDenom)
}
k.Logger(ctx).Info(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_TradeRewardBalance,
"Query response - Withdrawal Reward Balance: %v %s", tradeRewardBalanceAmount, tradeRoute.RewardDenomOnTradeZone))

// Trade all found reward tokens in the trade ICA to the output denom of their trade pool
err = utils.ApplyFuncIfNoError(ctx, func(c sdk.Context) error {
return k.SwapRewardTokens(ctx, tradeRewardBalanceAmount, tradeRoute)
})
if err != nil {
k.Logger(ctx).Error(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_TradeRewardBalance,
"Submitting ICA to swapping reward tokens failed: %s", err.Error()))
} else {
k.Logger(ctx).Info(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_TradeRewardBalance,
"Swapping discovered reward tokens %v %s for %s",
tradeRewardBalanceAmount, tradeRoute.RewardDenomOnTradeZone, tradeRoute.HostDenomOnTradeZone))
if err := k.SwapRewardTokens(ctx, tradeRewardBalanceAmount, tradeRoute); err != nil {
return errorsmod.Wrapf(err, "unable to swap reward tokens")
}

k.Logger(ctx).Info(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_TradeRewardBalance,
"Swapping discovered reward tokens %v %s for %s",
tradeRewardBalanceAmount, tradeRoute.RewardDenomOnTradeZone, tradeRoute.HostDenomOnTradeZone))

return nil
}
32 changes: 18 additions & 14 deletions x/stakeibc/keeper/icqcallbacks_withdrawal_reward_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,30 @@ func WithdrawalRewardBalanceCallback(k Keeper, ctx sdk.Context, args []byte, que
return nil
}

// Unmarshal the callback data which is just the trade route
var tradeRoute types.TradeRoute
if err := proto.Unmarshal(query.CallbackData, &tradeRoute); err != nil {
return errorsmod.Wrapf(err, "unable to unmarshal withdrawal reward balance callback data")
// Unmarshal the callback data containing the tradeRoute we are on
var tradeRouteCallback types.TradeRouteCallback
if err := proto.Unmarshal(query.CallbackData, &tradeRouteCallback); err != nil {
return errorsmod.Wrapf(err, "unable to unmarshal trade reward balance callback data")
}

// Lookup the trade route from the keys in the callback
tradeRoute, found := k.GetTradeRoute(ctx, tradeRouteCallback.RewardDenom, tradeRouteCallback.HostDenom)
if !found {
return types.ErrTradeRouteNotFound.Wrapf("trade route from %s to %s not found",
tradeRouteCallback.RewardDenom, tradeRouteCallback.HostDenom)
}

k.Logger(ctx).Info(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_WithdrawalRewardBalance,
"Query response - Withdrawal Reward Balance: %v %s", withdrawalRewardBalanceAmount, tradeRoute.RewardDenomOnHostZone))

// Using ICA commands on the withdrawal address, transfer the found reward tokens from the host zone to the trade zone
err = utils.ApplyFuncIfNoError(ctx, func(c sdk.Context) error {
return k.TransferRewardTokensHostToTrade(ctx, withdrawalRewardBalanceAmount, tradeRoute)
})
if err != nil {
k.Logger(ctx).Error(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_WithdrawalRewardBalance,
"Initiating transfer of reward tokens to trade ICA failed: %s", err.Error()))
} else {
k.Logger(ctx).Info(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_WithdrawalRewardBalance,
"Sending discovered reward tokens %v %s from hostZone to tradeZone",
withdrawalRewardBalanceAmount, tradeRoute.RewardDenomOnHostZone))
if err := k.TransferRewardTokensHostToTrade(ctx, withdrawalRewardBalanceAmount, tradeRoute); err != nil {
return errorsmod.Wrapf(err, "initiating transfer of reward tokens to trade ICA failed")
}

k.Logger(ctx).Info(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_WithdrawalRewardBalance,
"Sending discovered reward tokens %v %s from hostZone to tradeZone",
withdrawalRewardBalanceAmount, tradeRoute.RewardDenomOnHostZone))

return nil
}
28 changes: 20 additions & 8 deletions x/stakeibc/keeper/reward_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,11 @@ func (k Keeper) WithdrawalRewardBalanceQuery(ctx sdk.Context, route types.TradeR
timeout := time.Unix(0, int64(strideEpochTracker.NextEpochStartTime))
timeoutDuration := timeout.Sub(ctx.BlockTime())

// The only callback data we need is the trade route
callbackData := route
// We need the trade route keys in the callback to look up the tradeRoute struct
callbackData := types.TradeRouteCallback{
RewardDenom: route.RewardDenomOnRewardZone,
HostDenom: route.HostDenomOnHostZone,
}
callbackDataBz, err := proto.Marshal(&callbackData)
if err != nil {
return errorsmod.Wrapf(err, "unable to marshal TradeRoute callback data")
Expand Down Expand Up @@ -286,8 +289,11 @@ func (k Keeper) TradeRewardBalanceQuery(ctx sdk.Context, route types.TradeRoute)
timeout := time.Unix(0, int64(hourEpochTracker.NextEpochStartTime))
timeoutDuration := timeout.Sub(ctx.BlockTime())

// The only callback data we need is the trade route
callbackData := route
// We need the trade route keys in the callback to look up the tradeRoute struct
callbackData := types.TradeRouteCallback{
RewardDenom: route.RewardDenomOnRewardZone,
HostDenom: route.HostDenomOnHostZone,
}
callbackDataBz, err := proto.Marshal(&callbackData)
if err != nil {
return errorsmod.Wrapf(err, "unable to marshal TradeRewardBalanceQuery callback data")
Expand Down Expand Up @@ -334,8 +340,11 @@ func (k Keeper) TradeConvertedBalanceQuery(ctx sdk.Context, route types.TradeRou
timeout := time.Unix(0, int64(strideEpochTracker.NextEpochStartTime))
timeoutDuration := timeout.Sub(ctx.BlockTime())

// The only callback data we need is the trade route
callbackData := route
// We need the trade route keys in the callback to look up the tradeRoute struct
callbackData := types.TradeRouteCallback{
RewardDenom: route.RewardDenomOnRewardZone,
HostDenom: route.HostDenomOnHostZone,
}
callbackDataBz, err := proto.Marshal(&callbackData)
if err != nil {
return errorsmod.Wrapf(err, "unable to marshal trade route as callback data")
Expand Down Expand Up @@ -383,8 +392,11 @@ func (k Keeper) PoolPriceQuery(ctx sdk.Context, route types.TradeRoute) error {
timeout := time.Unix(0, int64(hourEpochTracker.NextEpochStartTime))
timeoutDuration := timeout.Sub(ctx.BlockTime())

// The only callback data we will need is the trade route
callbackData := route
// We need the trade route keys in the callback to look up the tradeRoute struct
callbackData := types.TradeRouteCallback{
RewardDenom: route.RewardDenomOnRewardZone,
HostDenom: route.HostDenomOnHostZone,
}
callbackDataBz, err := proto.Marshal(&callbackData)
if err != nil {
return errorsmod.Wrapf(err, "unable to marshal TradeRewardBalanceQuery callback data")
Expand Down
Loading