Skip to content

Commit

Permalink
Add parser for explicit gamm pool model creation message, works exact…
Browse files Browse the repository at this point in the history
…ly like other CreateBalancerPool messages
  • Loading branch information
pharr117 committed Sep 20, 2023
1 parent 082e970 commit c0e8167
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions osmosis/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var MessageTypeHandler = map[string][]func() txTypes.CosmosMessage{
gamm.MsgExitPool: {func() txTypes.CosmosMessage { return &gamm.WrapperMsgExitPool{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgExitPool2{} }},
gamm.MsgCreatePool: {func() txTypes.CosmosMessage { return &gamm.WrapperMsgCreatePool{} }, func() txTypes.CosmosMessage { return &gamm.WrapperMsgCreatePool2{} }},
gamm.MsgCreateBalancerPool: {func() txTypes.CosmosMessage { return &gamm.WrapperMsgCreateBalancerPool{} }},
gamm.OldMsgCreateBalancerPool: {func() txTypes.CosmosMessage { return &gamm.WrapperOldMsgCreateBalancerPool{} }},
poolmanager.MsgSwapExactAmountIn: {func() txTypes.CosmosMessage { return &poolmanager.WrapperMsgSwapExactAmountIn{} }},
poolmanager.MsgSwapExactAmountOut: {func() txTypes.CosmosMessage { return &poolmanager.WrapperMsgSwapExactAmountOut{} }},
poolmanager.MsgSplitRouteSwapExactAmountIn: {func() txTypes.CosmosMessage { return &poolmanager.WrapperMsgSplitRouteSwapExactAmountIn{} }},
Expand Down
127 changes: 127 additions & 0 deletions osmosis/modules/gamm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
osmosisOldTypes "github.com/DefiantLabs/lens/extra-codecs/osmosis/gamm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
gammBalancerPoolModelsTypes "github.com/osmosis-labs/osmosis/v19/x/gamm/pool-models/balancer"
gammTypes "github.com/osmosis-labs/osmosis/v19/x/gamm/types"
)

Expand All @@ -26,6 +27,7 @@ const (
MsgExitPool = "/osmosis.gamm.v1beta1.MsgExitPool"
MsgCreatePool = "/osmosis.gamm.v1beta1.MsgCreatePool"
MsgCreateBalancerPool = "/osmosis.gamm.v1beta1.MsgCreateBalancerPool"
OldMsgCreateBalancerPool = "/osmosis.gamm.poolmodels.balancer.v1beta1.MsgCreateBalancerPool"
)

const (
Expand Down Expand Up @@ -174,6 +176,14 @@ type WrapperMsgCreatePool2 struct {
WrapperMsgCreatePool
}

type WrapperOldMsgCreateBalancerPool struct {
txModule.Message
OsmosisMsgCreateBalancerPool *gammBalancerPoolModelsTypes.MsgCreateBalancerPool
CoinsSpent []sdk.Coin
GammCoinsReceived sdk.Coin
OtherCoinsReceived []coinReceived // e.g. from claims module (airdrops)
}

func (sf *WrapperMsgSwapExactAmountIn) String() string {
var tokenSwappedOut string
var tokenSwappedIn string
Expand Down Expand Up @@ -284,6 +294,17 @@ func (sf *WrapperMsgCreateBalancerPool) String() string {
sf.OsmosisMsgCreateBalancerPool.Sender, strings.Join(tokensIn, ", "))
}

func (sf *WrapperOldMsgCreateBalancerPool) String() string {
var tokensIn []string
if !(len(sf.OsmosisMsgCreateBalancerPool.PoolAssets) == 0) {
for _, v := range sf.OsmosisMsgCreateBalancerPool.PoolAssets {
tokensIn = append(tokensIn, v.Token.String())
}
}
return fmt.Sprintf("MsgCreateBalancerPool: %s created pool with %s",
sf.OsmosisMsgCreateBalancerPool.Sender, strings.Join(tokensIn, ", "))
}

func (sf *WrapperMsgExitSwapShareAmountIn) String() string {
var tokenSwappedOut string
var tokenSwappedIn string
Expand Down Expand Up @@ -1217,6 +1238,67 @@ func (sf *WrapperMsgCreateBalancerPool) HandleMsg(msgType string, msg sdk.Msg, l
return err
}

func (sf *WrapperOldMsgCreateBalancerPool) HandleMsg(msgType string, msg sdk.Msg, log *txModule.LogMessage) error {
sf.Type = msgType
sf.OsmosisMsgCreateBalancerPool = msg.(*gammBalancerPoolModelsTypes.MsgCreateBalancerPool)

// 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)
}

coinSpentEvents := txModule.GetEventsWithType(bankTypes.EventTypeCoinSpent, log)
if len(coinSpentEvents) == 0 {
return &txModule.MessageLogFormatError{MessageType: msgType, Log: fmt.Sprintf("%+v", log)}
}

coinsSpent := txModule.GetCoinsSpent(sf.OsmosisMsgCreateBalancerPool.Sender, coinSpentEvents)

if len(coinsSpent) < 2 {
return &txModule.MessageLogFormatError{MessageType: msgType, Log: fmt.Sprintf("invalid number of coins spent: %+v", log)}
}

sf.CoinsSpent = []sdk.Coin{}
for _, coin := range coinsSpent {
t, err := sdk.ParseCoinNormalized(coin)
if err != nil {
return &txModule.MessageLogFormatError{MessageType: msgType, Log: fmt.Sprintf("%+v", log)}
}

sf.CoinsSpent = append(sf.CoinsSpent, t)
}

coinReceivedEvents := txModule.GetEventsWithType(bankTypes.EventTypeCoinReceived, log)
if len(coinReceivedEvents) == 0 {
return &txModule.MessageLogFormatError{MessageType: msgType, Log: fmt.Sprintf("%+v", log)}
}

coinsReceived := txModule.GetCoinsReceived(sf.OsmosisMsgCreateBalancerPool.Sender, coinReceivedEvents)

gammCoinsReceived := []string{}

for _, coin := range coinsReceived {
if strings.Contains(coin, "gamm/pool") {
gammCoinsReceived = append(gammCoinsReceived, coin)
} else {
return &txModule.MessageLogFormatError{MessageType: msgType, Log: fmt.Sprintf("unexpected non-gamm/pool coin received: %+v", log)}
}
}

if len(gammCoinsReceived) != 1 {
return &txModule.MessageLogFormatError{MessageType: msgType, Log: fmt.Sprintf("invalid number of coins received: %+v", log)}
}

var err error
sf.GammCoinsReceived, err = sdk.ParseCoinNormalized(gammCoinsReceived[0])
if err != nil {
return &txModule.MessageLogFormatError{MessageType: msgType, Log: fmt.Sprintf("%+v", log)}
}

return nil
}

func (sf *WrapperMsgExitSwapShareAmountIn) HandleMsg(msgType string, msg sdk.Msg, log *txModule.LogMessage) error {
sf.Type = msgType
sf.OsmosisMsgExitSwapShareAmountIn = msg.(*gammTypes.MsgExitSwapShareAmountIn)
Expand Down Expand Up @@ -1761,6 +1843,51 @@ func (sf *WrapperMsgCreateBalancerPool) ParseRelevantData() []parsingTypes.Messa
return relevantData
}

func (sf *WrapperOldMsgCreateBalancerPool) ParseRelevantData() []parsingTypes.MessageRelevantInformation {
// need to make a relevant data block for all Tokens sent to the pool on creation
relevantData := make([]parsingTypes.MessageRelevantInformation, len(sf.CoinsSpent)+len(sf.OtherCoinsReceived))

// figure out how many gams per token
nthGamms, remainderGamms := calcNthGams(sf.GammCoinsReceived.Amount.BigInt(), len(sf.CoinsSpent))
for i, v := range sf.CoinsSpent {
// split received tokens across entry so we receive GAMM tokens for both exchanges
// each swap will get 1 nth of the gams until the last one which will get the remainder
if i != len(sf.CoinsSpent)-1 {
relevantData[i] = parsingTypes.MessageRelevantInformation{
AmountSent: v.Amount.BigInt(),
DenominationSent: v.Denom,
AmountReceived: nthGamms,
DenominationReceived: sf.GammCoinsReceived.Denom,
SenderAddress: sf.OsmosisMsgCreateBalancerPool.Sender,
ReceiverAddress: sf.OsmosisMsgCreateBalancerPool.Sender,
}
} else {
relevantData[i] = parsingTypes.MessageRelevantInformation{
AmountSent: v.Amount.BigInt(),
DenominationSent: v.Denom,
AmountReceived: remainderGamms,
DenominationReceived: sf.GammCoinsReceived.Denom,
SenderAddress: sf.OsmosisMsgCreateBalancerPool.Sender,
ReceiverAddress: sf.OsmosisMsgCreateBalancerPool.Sender,
}
}
}

i := len(sf.CoinsSpent)
for _, c := range sf.OtherCoinsReceived {
relevantData[i] = parsingTypes.MessageRelevantInformation{
AmountSent: c.coinReceived.Amount.BigInt(),
DenominationSent: c.coinReceived.Denom,
AmountReceived: c.coinReceived.Amount.BigInt(),
DenominationReceived: c.coinReceived.Denom,
SenderAddress: c.sender,
ReceiverAddress: sf.OsmosisMsgCreateBalancerPool.Sender,
}
i++
}
return relevantData
}

func (sf *WrapperMsgExitSwapShareAmountIn) ParseRelevantData() []parsingTypes.MessageRelevantInformation {
relevantData := make([]parsingTypes.MessageRelevantInformation, 1)
relevantData[0] = parsingTypes.MessageRelevantInformation{
Expand Down

0 comments on commit c0e8167

Please sign in to comment.