Skip to content

Commit

Permalink
wired up price query
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs committed Nov 22, 2023
1 parent d6efd34 commit 5904ae6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 78 deletions.
15 changes: 15 additions & 0 deletions x/interchainquery/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import fmt "fmt"

const (
// ModuleName defines the module name
ModuleName = "interchainquery"
Expand Down Expand Up @@ -31,6 +33,14 @@ const (
STAKING_STORE_QUERY_WITH_PROOF = "store/staking/key"
// The bank store is key'd by the account address
BANK_STORE_QUERY_WITH_PROOF = "store/bank/key"
// The Osmosis twap store - key'd by the pool ID and denom's
TWAP_STORE_QUERY_WITH_PROOF = "store/twap/key"
)

var (
// Osmosis TWAP query info
OsmosisKeySeparator = "|"
OsmosisMostRecentTWAPsPrefix = "recent_twap" + OsmosisKeySeparator
)

var (
Expand All @@ -42,3 +52,8 @@ var (
func KeyPrefix(p string) []byte {
return []byte(p)
}

func FormatOsmosisMostRecentTWAPKey(poolId uint64, denom1, denom2 string) []byte {
poolIdBz := fmt.Sprintf("%0.20d", poolId)
return []byte(fmt.Sprintf("%s%s%s%s%s%s", OsmosisMostRecentTWAPsPrefix, poolIdBz, OsmosisKeySeparator, denom1, OsmosisKeySeparator, denom2))
}
45 changes: 25 additions & 20 deletions x/stakeibc/keeper/icqcallbacks_pool_price.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package keeper

import (
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/proto"

"github.com/Stride-Labs/stride/v16/utils"
icqtypes "github.com/Stride-Labs/stride/v16/x/interchainquery/types"
"github.com/Stride-Labs/stride/v16/x/stakeibc/types"
)

// TradeRewardBalanceCallback is a callback handler for TradeRewardBalance queries.
Expand All @@ -16,30 +21,30 @@ func PoolPriceCallback(k Keeper, ctx sdk.Context, args []byte, query icqtypes.Qu
k.Logger(ctx).Info(utils.LogICQCallbackWithHostZone(query.ChainId, ICQCallbackID_PoolPrice,
"Starting pool spot price callback, QueryId: %vs, QueryType: %s, Connection: %s", query.Id, query.QueryType, query.ConnectionId))

// chainId := query.ChainId // should be the tradeZoneId, used in logging
chainId := query.ChainId // should be the tradeZoneId, used in logging

// TODO: [DYDX] Fix in separate PR
// Unmarshal the query response args, should be a SpotPriceResponse type
// var reponse types.QuerySpotPriceResponse
// err := reponse.Unmarshal(args)
// if err != nil {
// return errorsmod.Wrap(err, "unable to unmarshal the query response")
// }
// Unmarshal the query response args, should be a TwapRecord type
var twapRecord types.OsmosisTwapRecord
err := twapRecord.Unmarshal(args)
if err != nil {
return errorsmod.Wrap(err, "unable to unmarshal the query response")
}

// response.SpotPrice should be a string representation of the denom ratios in pool like "10.203"
price := sdk.ZeroDec()
fmt.Printf("%+v\n", twapRecord)

// Unmarshal the callback data containing the tradeRoute we are on
// var tradeRoute types.TradeRoute
// if err := proto.Unmarshal(query.CallbackData, &tradeRoute); err != nil {
// return errorsmod.Wrapf(err, "unable to unmarshal trade reward balance callback data")
// }
// k.Logger(ctx).Info(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_PoolSpotPrice,
// "Query response - spot price ratio of %s to %s is %s",
// tradeRoute.RewardDenomOnTradeZone, tradeRoute.TargetDenomOnTradeZone, reponse.SpotPrice))

// // Update the spot price stored on the trade route data in the keeper
// tradeRoute.SpotPrice = reponse.SpotPrice
// k.SetTradeRoute(ctx, tradeRoute)
var tradeRoute types.TradeRoute
if err := proto.Unmarshal(query.CallbackData, &tradeRoute); err != nil {
return errorsmod.Wrapf(err, "unable to unmarshal trade reward balance callback data")
}
k.Logger(ctx).Info(utils.LogICQCallbackWithHostZone(chainId, ICQCallbackID_PoolPrice,
"Query response - spot price ratio of %s to %s is %s",
tradeRoute.RewardDenomOnTradeZone, tradeRoute.TargetDenomOnTradeZone, price))

// Update the spot price stored on the trade route data in the keeper
tradeRoute.InputToOutputTwap = price
k.SetTradeRoute(ctx, tradeRoute)

return nil
}
45 changes: 0 additions & 45 deletions x/stakeibc/keeper/icqcallbacks_pool_spot_price.go

This file was deleted.

22 changes: 9 additions & 13 deletions x/stakeibc/keeper/reward_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,12 @@ func (k Keeper) TradeRewardTokens(ctx sdk.Context, amount sdk.Int, route types.T
// If there is a valid spot price, use it to set a floor for the acceptable minimum output tokens
// 5% slippage is allowed so multiply by 0.95 the expected spot price to get the target minimum
// minOut is the minimum number of route.TargetDenomOnTradeZone we must receive or the swap will fail
minOut := sdk.ZeroInt()
if route.InputToOutputTwap == sdk.ZeroDec() {
return fmt.Errorf("TWAP not found for pool %d", route.PoolId)
}
slippageRatio := sdk.NewDecWithPrec(95, 2) // 0.95 to allow 5% loss to slippage
inputToOutputRatio := slippageRatio.Mul(route.InputToOutputTwap)
minOut = sdk.NewDecFromInt(amount).Mul(inputToOutputRatio).TruncateInt()
minOut := sdk.NewDecFromInt(amount).Mul(inputToOutputRatio).TruncateInt()

tradeIcaAccount := route.RewardToTradeHop.ToAccount
tradeTokens := sdk.NewCoin(route.RewardDenomOnTradeZone, amount)
Expand Down Expand Up @@ -352,17 +351,14 @@ func (k Keeper) PoolPriceQuery(ctx sdk.Context, route types.TradeRoute) error {
tradeAccount := route.RewardToTradeHop.ToAccount
k.Logger(ctx).Info(utils.LogWithHostZone(tradeAccount.ChainId, "Submitting ICQ for spot price in this pool"))

// The stride interchainquery module likely can't actually handle this type of query so this likely won't work yet...
// Sort denom's
denom1, denom2 := route.RewardDenomOnTradeZone, route.TargetDenomOnTradeZone
if denom1 > denom2 {
denom1, denom2 = denom2, denom1
}

// Encode the osmosis spot price query request for the specific pool and denoms
// spotPriceRequest := types.QuerySpotPriceRequest{
// PoolId: route.PoolId,
// BaseAssetDenom: route.RewardDenomOnTradeZone,
// QuoteAssetDenom: route.TargetDenomOnTradeZone,
// }
// queryData := k.cdc.MustMarshal(&spotPriceRequest)
// TODO [DYDX]: Use TWAP in separate PR
queryData := []byte{}
// Build query request data which consists of the TWAP store key built from each denom
queryData := icqtypes.FormatOsmosisMostRecentTWAPKey(route.PoolId, denom1, denom2)

// Timeout query at end of epoch
strideEpochTracker, found := k.GetEpochTracker(ctx, epochstypes.STRIDE_EPOCH)
Expand All @@ -383,7 +379,7 @@ func (k Keeper) PoolPriceQuery(ctx sdk.Context, route types.TradeRoute) error {
query := icqtypes.Query{
ChainId: tradeAccount.ChainId,
ConnectionId: tradeAccount.ConnectionId, // query needs to go to the trade zone, not the host zone
QueryType: icqtypes.BANK_STORE_QUERY_WITH_PROOF,
QueryType: icqtypes.TWAP_STORE_QUERY_WITH_PROOF,
RequestData: queryData,
CallbackModule: types.ModuleName,
CallbackId: ICQCallbackID_PoolPrice,
Expand Down

0 comments on commit 5904ae6

Please sign in to comment.