From addee3ac522e2f1c671c80c14b5e478fd63583da Mon Sep 17 00:00:00 2001 From: pharr117 Date: Sun, 4 Feb 2024 17:21:05 -0500 Subject: [PATCH] Add new parser for poolmanager ExactAmountOut, add new parser to fix event format changes in gamm ExactAmountIn --- osmosis/handlers.go | 3 +- osmosis/modules/gamm/swaps.go | 58 +++++++++++++++++ osmosis/modules/poolmanager/types.go | 95 +++++++++++++++++++++++++++- 3 files changed, 152 insertions(+), 4 deletions(-) diff --git a/osmosis/handlers.go b/osmosis/handlers.go index 239611d..8b927cd 100644 --- a/osmosis/handlers.go +++ b/osmosis/handlers.go @@ -12,7 +12,7 @@ import ( // MessageTypeHandler is used to unmarshal JSON to a particular type. var MessageTypeHandler = map[string][]func() txTypes.CosmosMessage{ - gamm.MsgSwapExactAmountIn: {func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountIn{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountIn2{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountIn3{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountIn4{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountIn5{} }}, + gamm.MsgSwapExactAmountIn: {func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountIn{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountIn2{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountIn3{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountIn4{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountIn5{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountIn6{} }}, gamm.MsgSwapExactAmountOut: {func() txTypes.CosmosMessage { return &gamm.WrapperMsgSwapExactAmountOut{} }}, gamm.MsgJoinSwapExternAmountIn: {func() txTypes.CosmosMessage { return &gamm.WrapperMsgJoinSwapExternAmountIn{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgJoinSwapExternAmountIn2{} }}, gamm.MsgJoinSwapShareAmountOut: {func() txTypes.CosmosMessage { return &gamm.WrapperMsgJoinSwapShareAmountOut{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgJoinSwapShareAmountOut2{} }}, @@ -27,6 +27,7 @@ var MessageTypeHandler = map[string][]func() txTypes.CosmosMessage{ poolmanager.MsgSwapExactAmountIn: {func() txTypes.CosmosMessage { return &poolmanager.WrapperMsgSwapExactAmountIn{} }}, poolmanager.MsgSwapExactAmountOut: {func() txTypes.CosmosMessage { return &poolmanager.WrapperMsgSwapExactAmountOut{} }}, poolmanager.MsgSplitRouteSwapExactAmountIn: {func() txTypes.CosmosMessage { return &poolmanager.WrapperMsgSplitRouteSwapExactAmountIn{} }}, + poolmanager.MsgSplitRouteSwapExactAmountOut: {func() txTypes.CosmosMessage { return &poolmanager.WrapperMsgSplitRouteSwapExactAmountOut{} }}, concentratedliquidity.MsgCreatePosition: {func() txTypes.CosmosMessage { return &concentratedliquidity.WrapperMsgCreatePosition{} }}, concentratedliquidity.MsgWithdrawPosition: {func() txTypes.CosmosMessage { return &concentratedliquidity.WrapperMsgWithdrawPosition{} }}, concentratedliquidity.MsgCollectSpreadRewards: {func() txTypes.CosmosMessage { return &concentratedliquidity.WrapperMsgCollectSpreadRewards{} }}, diff --git a/osmosis/modules/gamm/swaps.go b/osmosis/modules/gamm/swaps.go index f237e2b..7566205 100644 --- a/osmosis/modules/gamm/swaps.go +++ b/osmosis/modules/gamm/swaps.go @@ -48,6 +48,10 @@ type WrapperMsgSwapExactAmountIn5 struct { WrapperMsgSwapExactAmountIn } +type WrapperMsgSwapExactAmountIn6 struct { + WrapperMsgSwapExactAmountIn +} + type WrapperMsgSwapExactAmountOut struct { txModule.Message OsmosisMsgSwapExactAmountOut *gammTypes.MsgSwapExactAmountOut @@ -86,6 +90,10 @@ func (sf *WrapperMsgSwapExactAmountIn5) String() string { return sf.WrapperMsgSwapExactAmountIn.String() } +func (sf *WrapperMsgSwapExactAmountIn6) String() string { + return sf.WrapperMsgSwapExactAmountIn.String() +} + func (sf *WrapperMsgSwapExactAmountOut) String() string { var tokenSwappedOut string var tokenSwappedIn string @@ -491,6 +499,52 @@ func (sf *WrapperMsgSwapExactAmountOut) HandleMsg(msgType string, msg sdk.Msg, l return nil } +func (sf *WrapperMsgSwapExactAmountIn6) HandleMsg(msgType string, msg sdk.Msg, log *txModule.LogMessage) error { + sf.Type = msgType + sf.OsmosisMsgSwapExactAmountIn = msg.(*gammTypes.MsgSwapExactAmountIn) + + // Confirm that the action listed in the message log matches the Message type + validLog := txModule.IsMessageActionEquals(sf.GetType(), log) + if !validLog { + return util.ReturnInvalidLog(msgType, log) + } + + // get all transfer events + transferEvents := txModule.GetEventsWithType("transfer", log) + + // loop backwards through transfer events + for i := len(transferEvents) - 1; i >= 0; i-- { + transferEvt := transferEvents[i] + + for _, attr := range transferEvt.Attributes { + if attr.Key == "amount" && attr.Value != "" { + amount, err := sdk.ParseCoinNormalized(attr.Value) + if err != nil { + return err + } + + // if the amount denom matches the last route denom, then it is the amount received + if amount.Denom == sf.OsmosisMsgSwapExactAmountIn.Routes[len(sf.OsmosisMsgSwapExactAmountIn.Routes)-1].TokenOutDenom { + sf.TokenOut = amount + break + } + } + } + + if !sf.TokenOut.IsNil() { + break + } + } + + if sf.TokenOut.IsNil() { + return errors.New("no amount received") + } + + sf.TokenIn = sf.OsmosisMsgSwapExactAmountIn.TokenIn + sf.Address = sf.OsmosisMsgSwapExactAmountIn.Sender + return nil +} + func (sf *WrapperMsgSwapExactAmountIn) ParseRelevantData() []parsingTypes.MessageRelevantInformation { relevantData := make([]parsingTypes.MessageRelevantInformation, 1) relevantData[0] = parsingTypes.MessageRelevantInformation{ @@ -520,3 +574,7 @@ func (sf *WrapperMsgSwapExactAmountOut) ParseRelevantData() []parsingTypes.Messa } return relevantData } + +func (sf *WrapperMsgSwapExactAmountIn6) ParseRelevantData() []parsingTypes.MessageRelevantInformation { + return sf.WrapperMsgSwapExactAmountIn.ParseRelevantData() +} diff --git a/osmosis/modules/poolmanager/types.go b/osmosis/modules/poolmanager/types.go index 45d6cb4..079cde3 100644 --- a/osmosis/modules/poolmanager/types.go +++ b/osmosis/modules/poolmanager/types.go @@ -13,9 +13,10 @@ import ( ) const ( - MsgSwapExactAmountIn = "/osmosis.poolmanager.v1beta1.MsgSwapExactAmountIn" - MsgSwapExactAmountOut = "/osmosis.poolmanager.v1beta1.MsgSwapExactAmountOut" - MsgSplitRouteSwapExactAmountIn = "/osmosis.poolmanager.v1beta1.MsgSplitRouteSwapExactAmountIn" + MsgSwapExactAmountIn = "/osmosis.poolmanager.v1beta1.MsgSwapExactAmountIn" + MsgSwapExactAmountOut = "/osmosis.poolmanager.v1beta1.MsgSwapExactAmountOut" + MsgSplitRouteSwapExactAmountIn = "/osmosis.poolmanager.v1beta1.MsgSplitRouteSwapExactAmountIn" + MsgSplitRouteSwapExactAmountOut = "/osmosis.poolmanager.v1beta1.MsgSplitRouteSwapExactAmountOut" ) type WrapperMsgSwapExactAmountIn struct { @@ -42,6 +43,14 @@ type WrapperMsgSplitRouteSwapExactAmountIn struct { TokenIn sdk.Coin } +type WrapperMsgSplitRouteSwapExactAmountOut struct { + txModule.Message + OsmosisMsgSplitRouteSwapExactAmountOut *poolManagerTypes.MsgSplitRouteSwapExactAmountOut + Address string + TokenOut sdk.Coin + TokenIn sdk.Coin +} + func (sf *WrapperMsgSwapExactAmountIn) String() string { var tokenSwappedOut string var tokenSwappedIn string @@ -82,6 +91,19 @@ func (sf *WrapperMsgSplitRouteSwapExactAmountIn) String() string { sf.Address, tokenSwappedIn, tokenSwappedOut) } +func (sf *WrapperMsgSplitRouteSwapExactAmountOut) String() string { + var tokenSwappedOut string + var tokenSwappedIn string + if !sf.TokenOut.IsNil() { + tokenSwappedOut = sf.TokenOut.String() + } + if !sf.TokenIn.IsNil() { + tokenSwappedIn = sf.TokenIn.String() + } + return fmt.Sprintf("MsgSplitRouteSwapExactAmountOut (pool-manager): %s swapped in %s and received %s", + sf.Address, tokenSwappedIn, tokenSwappedOut) +} + func (sf *WrapperMsgSwapExactAmountIn) HandleMsg(msgType string, msg sdk.Msg, log *txModule.LogMessage) error { sf.Type = msgType sf.OsmosisMsgSwapExactAmountIn = msg.(*poolManagerTypes.MsgSwapExactAmountIn) @@ -267,6 +289,60 @@ func (sf *WrapperMsgSplitRouteSwapExactAmountIn) HandleMsg(msgType string, msg s return nil } +func (sf *WrapperMsgSplitRouteSwapExactAmountOut) HandleMsg(msgType string, msg sdk.Msg, log *txModule.LogMessage) error { + sf.Type = msgType + sf.OsmosisMsgSplitRouteSwapExactAmountOut = msg.(*poolManagerTypes.MsgSplitRouteSwapExactAmountOut) + + sf.Address = sf.OsmosisMsgSplitRouteSwapExactAmountOut.Sender + + denomOut := sf.OsmosisMsgSplitRouteSwapExactAmountOut.TokenOutDenom + + // Contains the addition of all tokens swapped in by the user + splitRouteFinalEvent := txModule.GetEventWithType("split_route_swap_exact_amount_out", log) + + if splitRouteFinalEvent == nil { + return &txModule.MessageLogFormatError{MessageType: msgType, Log: fmt.Sprintf("%+v", log)} + } + + // Mislabled event + tokensOutString, err := txModule.GetValueForAttribute("tokens_out", splitRouteFinalEvent) + if err != nil { + return err + } + + tokenInAmount, ok := sdk.NewIntFromString(tokensOutString) + if !ok { + return &txModule.MessageLogFormatError{MessageType: msgType, Log: fmt.Sprintf("%+v", log)} + } + + tokenInDenom := "" + tokenOutAmount := sdk.NewInt(0) + + for _, routes := range sf.OsmosisMsgSplitRouteSwapExactAmountOut.Routes { + if len(routes.Pools) == 0 { + continue + } + + firstPool := routes.Pools[0] + if tokenInDenom == "" { + tokenInDenom = firstPool.TokenInDenom + } else if tokenInDenom != firstPool.TokenInDenom { + return errors.New("token in denom does not match across routes first pool") + } + + tokenOutAmount = tokenOutAmount.Add(routes.TokenOutAmount) + + } + + finalTokensIn := sdk.NewCoin(tokenInDenom, tokenInAmount) + finalTokensOut := sdk.NewCoin(denomOut, tokenOutAmount) + + sf.TokenIn = finalTokensIn + sf.TokenOut = finalTokensOut + + return nil +} + func (sf *WrapperMsgSwapExactAmountIn) ParseRelevantData() []parsingTypes.MessageRelevantInformation { relevantData := make([]parsingTypes.MessageRelevantInformation, 1) relevantData[0] = parsingTypes.MessageRelevantInformation{ @@ -305,3 +381,16 @@ func (sf *WrapperMsgSplitRouteSwapExactAmountIn) ParseRelevantData() []parsingTy } return relevantData } + +func (sf *WrapperMsgSplitRouteSwapExactAmountOut) ParseRelevantData() []parsingTypes.MessageRelevantInformation { + relevantData := make([]parsingTypes.MessageRelevantInformation, 1) + relevantData[0] = parsingTypes.MessageRelevantInformation{ + AmountSent: sf.TokenIn.Amount.BigInt(), + DenominationSent: sf.TokenIn.Denom, + AmountReceived: sf.TokenOut.Amount.BigInt(), + DenominationReceived: sf.TokenOut.Denom, + SenderAddress: sf.Address, + ReceiverAddress: sf.Address, + } + return relevantData +}