Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Dec 26, 2023
1 parent d22d908 commit 63056c5
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 10 deletions.
173 changes: 163 additions & 10 deletions x/txfees/keeper/txfee_filters/arb_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ type AffiliateSwapMsg struct {
Swap `json:"swap"`
}

type InputCoin struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
}

type Slippage struct {
MinOutputAmount string `json:"min_output_amount"`
}

type ContractSwap struct {
InputCoin InputCoin `json:"input_coin"`
OutputDenom string `json:"output_denom"`
Slippage Slippage `json:"slippage"`
}

type ContractSwapMsg struct {
ContractSwap `json:"swap"`
}

// TokenDenomsOnPath implements types.SwapMsgRoute.
func (m AffiliateSwapMsg) TokenDenomsOnPath() []string {
denoms := make([]string, 0, len(m.Routes)+1)
Expand Down Expand Up @@ -70,6 +89,10 @@ func IsArbTxLoose(tx sdk.Tx) bool {
lpTypesSeen := make(map[gammtypes.LiquidityChangeType]bool, 2)
isArb := false

if isArb := isMultiMsgExecArb(msgs); isArb {
return true
}

for _, m := range msgs {
swapInDenom, isArb = isArbTxLooseAuthz(m, swapInDenom, lpTypesSeen)
if isArb {
Expand All @@ -80,9 +103,44 @@ func IsArbTxLoose(tx sdk.Tx) bool {
return false
}

func isMultiMsgExecArb(msgs []sdk.Msg) bool {
if len(msgs) == 2 {
firstMsg := msgs[0]
secondMsg := msgs[1]

fistMsgExec, isFirstExec := firstMsg.(*wasmtypes.MsgExecuteContract)

secondMsgExec, isSecondExec := secondMsg.(*wasmtypes.MsgExecuteContract)

if isFirstExec && isSecondExec {
firstRawMsg := fistMsgExec.GetMsg()
isFirstSetRouteMsg := isSetRouteMsg(firstRawMsg)
if !isFirstSetRouteMsg {
return false
}

secondRawMsg := secondMsgExec.GetMsg()
isSecondSwapMsg := isSwapContractMsg(secondRawMsg)
if !isSecondSwapMsg {
return false
}

return true
}

return false
}
return false
}

func isArbTxLooseAuthz(msg sdk.Msg, swapInDenom string, lpTypesSeen map[gammtypes.LiquidityChangeType]bool) (string, bool) {
if authzMsg, ok := msg.(*authztypes.MsgExec); ok {
msgs, _ := authzMsg.GetMessages()

if isArb := isMultiMsgExecArb(msgs); isArb {
return swapInDenom, true
}

for _, m := range msgs {
swapInDenom, isAuthz := isArbTxLooseAuthz(m, swapInDenom, lpTypesSeen)
if isAuthz {
Expand All @@ -107,21 +165,37 @@ func isArbTxLooseAuthz(msg sdk.Msg, swapInDenom string, lpTypesSeen map[gammtype
contractMessage := msgExecuteContract.GetMsg()

// Check that the contract message is an affiliate swap message
if ok := isAffiliateSwapMsg(contractMessage); !ok {
isAffliliateSwap := isAffiliateSwapMsg(contractMessage)
isContractSwap := isSwapContractMsg(contractMessage)

if !isAffliliateSwap && !isContractSwap {
return swapInDenom, false
}

var affiliateSwapMsg AffiliateSwapMsg
if err := json.Unmarshal(contractMessage, &affiliateSwapMsg); err != nil {
// If we can't unmarshal it, it's not an affiliate swap message
return swapInDenom, false
if isAffliliateSwap {
var affiliateSwapMsg AffiliateSwapMsg
if err := json.Unmarshal(contractMessage, &affiliateSwapMsg); err != nil {
// If we can't unmarshal it, it's not an affiliate swap message
return swapInDenom, false
}

// Otherwise, we have an affiliate swap message, so we check if it's an arb
affiliateSwapMsg.TokenIn = tokenIn.Denom
swapInDenom, isArb := isArbTxLooseSwapMsg(affiliateSwapMsg, swapInDenom)
if isArb {
return swapInDenom, true
}
}

// Otherwise, we have an affiliate swap message, so we check if it's an arb
affiliateSwapMsg.TokenIn = tokenIn.Denom
swapInDenom, isArb := isArbTxLooseSwapMsg(affiliateSwapMsg, swapInDenom)
if isArb {
return swapInDenom, true
if isContractSwap {
var contractSwapMsg ContractSwapMsg
if err := json.Unmarshal(contractMessage, &contractSwapMsg); err != nil {
// If we can't unmarshal it, it's not a contract swap message
return swapInDenom, false
}

// Otherwise, we have a contract swap message, so we check if it's an arb

}

return swapInDenom, false
Expand Down Expand Up @@ -195,3 +269,82 @@ func isAffiliateSwapMsg(msg []byte) bool {

return true
}

type Coin struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
}

// "msg": {
// "set_route": {
// "input_denom": "ibc/D1542AA8762DB13087D8364F3EA6509FD6F009A34F00426AF9E4F9FA85CBBF1F",
// "output_denom": "ibc/D1542AA8762DB13087D8364F3EA6509FD6F009A34F00426AF9E4F9FA85CBBF1F",
// "pool_route": [
// {
// "pool_id": "712",
// "token_out_denom": "uosmo"
// },
// {
// "pool_id": "1221",
// "token_out_denom": "ibc/498A0751C798A0D9A389AA3691123DADA57DAA4FE165D5C75894505B876BA6E4"
// },
// {
// "pool_id": "1277",
// "token_out_denom": "ibc/D1542AA8762DB13087D8364F3EA6509FD6F009A34F00426AF9E4F9FA85CBBF1F"
// }
// ]
// }

func isSetRouteMsg(msg []byte) bool {
// Check that the contract message is a valid JSON object
jsonObject := make(map[string]interface{})
err := json.Unmarshal(msg, &jsonObject)
if err != nil {
return false
}

// check the main key is "set_route"
setRoute, ok := jsonObject["set_route"].(map[string]interface{})
if !ok {
return false
}

if inputDenom, ok := setRoute["input_denom"].(string); !ok || len(inputDenom) == 0 {
return false
}

if outputDenom, ok := setRoute["output_denom"].(string); !ok || len(outputDenom) == 0 {
return false
}

if poolRoute, ok := setRoute["pool_route"].([]interface{}); !ok || len(poolRoute) == 0 {
return false
}

return true
}

func isSwapContractMsg(msg []byte) bool {
// Check that the contract message is a valid JSON object
jsonObject := make(map[string]interface{})
err := json.Unmarshal(msg, &jsonObject)
if err != nil {
return false
}

// check the main key is "swap"
swap, ok := jsonObject["swap"].(map[string]interface{})
if !ok {
return false
}

if input_coin, ok := swap["input_coin"].(map[string]interface{}); !ok || len(input_coin) == 0 {
return false
}

if outputDenom, ok := swap["output_denom"].(string); !ok || len(outputDenom) == 0 {
return false
}

return true
}
30 changes: 30 additions & 0 deletions x/txfees/keeper/txfee_filters/arb_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ func (suite *KeeperTestSuite) TestIsArbTxLooseAuthz_AffiliateSwapMsg() {
suite.Require().True(isArb)
}

// Tests that the arb filter is enabled on swap msg.
func (suite *KeeperTestSuite) TestIsArbTxLooseAuthz_SwapMsg() {
contractSwapMsg := &txfee_filters.ContractSwapMsg{
ContractSwap: txfee_filters.ContractSwap{
InputCoin: txfee_filters.InputCoin{
Amount: "2775854",
Denom: "ibc/D1542AA8762DB13087D8364F3EA6509FD6F009A34F00426AF9E4F9FA85CBBF1F",
},
OutputDenom: "ibc/D1542AA8762DB13087D8364F3EA6509FD6F009A34F00426AF9E4F9FA85CBBF1F",
Slippage: txfee_filters.Slippage{
MinOutputAmount: "2775854",
},
},
}

msgBz, err := json.Marshal(contractSwapMsg)
suite.Require().NoError(err)

// https://celatone.osmosis.zone/osmosis-1/txs/8D20755D4E009CB72C763963A76886BCCCC5C2EBFC3F57266332710216A0D10D
executeMsg := &wasmtypes.MsgExecuteContract{
Contract: "osmo1etpha3a65tds0hmn3wfjeag6wgxgrkuwg2zh94cf5hapz7mz04dq6c25s5",
Sender: "osmo1dldrxz5p8uezxz3qstpv92de7wgfp7hvr72dcm",
Funds: sdk.NewCoins(sdk.NewCoin("ibc/498A0751C798A0D9A389AA3691123DADA57DAA4FE165D5C75894505B876BA6E4", sdk.NewInt(217084399))),
Msg: msgBz,
}

_, isArb := txfee_filters.IsArbTxLooseAuthz(executeMsg, executeMsg.Funds[0].Denom, map[types.LiquidityChangeType]bool{})
suite.Require().True(isArb)
}

func (suite *KeeperTestSuite) TestIsArbTxLooseAuthz_OtherMsg() {
otherMsg := []byte(`{"update_feed": {}}`)

Expand Down

0 comments on commit 63056c5

Please sign in to comment.