Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
davidterpay committed Jan 9, 2023
1 parent 9ce432c commit 5d034e9
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 54 deletions.
4 changes: 2 additions & 2 deletions x/protorev/keeper/epoch_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

epochstypes "github.com/osmosis-labs/osmosis/v13/x/epochs/types"
poolmanagertypes "github.com/osmosis-labs/osmosis/v13/x/poolmanager/types"
"github.com/osmosis-labs/osmosis/v13/x/protorev/types"
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"
)

type EpochHooks struct {
Expand Down Expand Up @@ -95,7 +95,7 @@ func (k Keeper) GetHighestLiquidityPools(ctx sdk.Context) (map[string]LiquidityP

// Pool must be a non-stableswap pool
pooltype, err := k.gammKeeper.GetPoolType(ctx, pool.GetId())
if err != nil || pooltype == swaproutertypes.Stableswap {
if err != nil || pooltype == poolmanagertypes.Stableswap {
continue
}

Expand Down
6 changes: 3 additions & 3 deletions x/protorev/keeper/posthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

gammtypes "github.com/osmosis-labs/osmosis/v13/x/gamm/types"
poolmanagertypes "github.com/osmosis-labs/osmosis/v13/x/poolmanager/types"
)

type SwapToBackrun struct {
Expand Down Expand Up @@ -131,14 +131,14 @@ func ExtractSwappedPools(tx sdk.Tx) []SwapToBackrun {

// Extract only swaps types and the swapped pools from the tx
for _, msg := range tx.GetMsgs() {
if swap, ok := msg.(*gammtypes.MsgSwapExactAmountIn); ok {
if swap, ok := msg.(*poolmanagertypes.MsgSwapExactAmountIn); ok {
for _, route := range swap.Routes {
swappedPools = append(swappedPools, SwapToBackrun{
PoolId: route.PoolId,
TokenOutDenom: route.TokenOutDenom,
TokenInDenom: swap.TokenIn.Denom})
}
} else if swap, ok := msg.(*gammtypes.MsgSwapExactAmountOut); ok {
} else if swap, ok := msg.(*poolmanagertypes.MsgSwapExactAmountOut); ok {
for _, route := range swap.Routes {
swappedPools = append(swappedPools, SwapToBackrun{
PoolId: route.PoolId,
Expand Down
30 changes: 15 additions & 15 deletions x/protorev/keeper/posthandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"

gammtypes "github.com/osmosis-labs/osmosis/v13/x/gamm/types"
poolmanagertypes "github.com/osmosis-labs/osmosis/v13/x/poolmanager/types"
"github.com/osmosis-labs/osmosis/v13/x/protorev/keeper"
"github.com/osmosis-labs/osmosis/v13/x/protorev/types"
)
Expand Down Expand Up @@ -59,9 +59,9 @@ func (suite *KeeperTestSuite) TestAnteHandle() {
name: "No Arb",
params: param{
msgs: []sdk.Msg{
&gammtypes.MsgSwapExactAmountIn{
&poolmanagertypes.MsgSwapExactAmountIn{
Sender: addr0.String(),
Routes: []gammtypes.SwapAmountInRoute{
Routes: []poolmanagertypes.SwapAmountInRoute{
{
PoolId: 12,
TokenOutDenom: "akash",
Expand All @@ -86,9 +86,9 @@ func (suite *KeeperTestSuite) TestAnteHandle() {
name: "Mainnet Arb (Block: 5905150) - Highest Liquidity Pool Build",
params: param{
msgs: []sdk.Msg{
&gammtypes.MsgSwapExactAmountIn{
&poolmanagertypes.MsgSwapExactAmountIn{
Sender: addr0.String(),
Routes: []gammtypes.SwapAmountInRoute{
Routes: []poolmanagertypes.SwapAmountInRoute{
{
PoolId: 23,
TokenOutDenom: "ibc/BE1BB42D4BE3C30D50B68D7C41DB4DFCE9678E8EF8C539F6E6A9345048894FCC",
Expand Down Expand Up @@ -118,9 +118,9 @@ func (suite *KeeperTestSuite) TestAnteHandle() {
name: "Mainnet Arb Route - Multi Asset, Same Weights (Block: 6906570) - Hot Route Build - Atom Arb",
params: param{
msgs: []sdk.Msg{
&gammtypes.MsgSwapExactAmountIn{
&poolmanagertypes.MsgSwapExactAmountIn{
Sender: addr0.String(),
Routes: []gammtypes.SwapAmountInRoute{
Routes: []poolmanagertypes.SwapAmountInRoute{
{
PoolId: 33,
TokenOutDenom: "ibc/A0CC0CF735BFB30E730C70019D4218A1244FF383503FF7579C9201AB93CA9293",
Expand Down Expand Up @@ -154,9 +154,9 @@ func (suite *KeeperTestSuite) TestAnteHandle() {
name: "Stableswap Test Arb Route - Hot Route Build",
params: param{
msgs: []sdk.Msg{
&gammtypes.MsgSwapExactAmountIn{
&poolmanagertypes.MsgSwapExactAmountIn{
Sender: addr0.String(),
Routes: []gammtypes.SwapAmountInRoute{
Routes: []poolmanagertypes.SwapAmountInRoute{
{
PoolId: 29,
TokenOutDenom: types.OsmosisDenomination,
Expand Down Expand Up @@ -282,9 +282,9 @@ func (suite *KeeperTestSuite) TestExtractSwappedPools() {
name: "Single Swap",
params: param{
msgs: []sdk.Msg{
&gammtypes.MsgSwapExactAmountIn{
&poolmanagertypes.MsgSwapExactAmountIn{
Sender: addr0.String(),
Routes: []gammtypes.SwapAmountInRoute{
Routes: []poolmanagertypes.SwapAmountInRoute{
{
PoolId: 28,
TokenOutDenom: "ibc/BE1BB42D4BE3C30D50B68D7C41DB4DFCE9678E8EF8C539F6E6A9345048894FCC",
Expand Down Expand Up @@ -314,9 +314,9 @@ func (suite *KeeperTestSuite) TestExtractSwappedPools() {
name: "Two Swaps",
params: param{
msgs: []sdk.Msg{
&gammtypes.MsgSwapExactAmountIn{
&poolmanagertypes.MsgSwapExactAmountIn{
Sender: addr0.String(),
Routes: []gammtypes.SwapAmountInRoute{
Routes: []poolmanagertypes.SwapAmountInRoute{
{
PoolId: 28,
TokenOutDenom: "ibc/BE1BB42D4BE3C30D50B68D7C41DB4DFCE9678E8EF8C539F6E6A9345048894FCC",
Expand All @@ -325,9 +325,9 @@ func (suite *KeeperTestSuite) TestExtractSwappedPools() {
TokenIn: sdk.NewCoin("ibc/D189335C6E4A68B513C10AB227BF1C1D38C746766278BA3EEB4FB14124F1D858", sdk.NewInt(10000)),
TokenOutMinAmount: sdk.NewInt(10000),
},
&gammtypes.MsgSwapExactAmountIn{
&poolmanagertypes.MsgSwapExactAmountIn{
Sender: addr0.String(),
Routes: []gammtypes.SwapAmountInRoute{
Routes: []poolmanagertypes.SwapAmountInRoute{
{
PoolId: 22,
TokenOutDenom: "ibc/BE1BB42D4BE3C30D50B68D7C41DB4DFCE9678E8EF8C539F6E6A9345048894FCC",
Expand Down
2 changes: 1 addition & 1 deletion x/protorev/keeper/rebalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (k Keeper) FindMaxProfitForRoute(ctx sdk.Context, route poolmanagertypes.Sw
}

// ExecuteTrade inputs a route, amount in, and rebalances the pool
func (k Keeper) ExecuteTrade(ctx sdk.Context, route poolmanagertypes.SwapAmountInRoutes, inputCoin sdk.Coin, poolId uint64) error {
func (k Keeper) ExecuteTrade(ctx sdk.Context, route poolmanagertypes.SwapAmountInRoutes, inputCoin sdk.Coin) error {
// Get the module address which will execute the trade
protorevModuleAddress := k.accountKeeper.GetModuleAddress(types.ModuleName)

Expand Down
54 changes: 27 additions & 27 deletions x/protorev/keeper/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

// BuildRoutes builds all of the possible arbitrage routes given the tokenIn, tokenOut and poolId that were used in the swap
func (k Keeper) BuildRoutes(ctx sdk.Context, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64) []swaproutertypes.SwapAmountInRoutes {
routes := make([]swaproutertypes.SwapAmountInRoutes, 0)
func (k Keeper) BuildRoutes(ctx sdk.Context, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64) []poolmanagertypes.SwapAmountInRoutes {
routes := make([]poolmanagertypes.SwapAmountInRoutes, 0)

// Append hot routes if they exist
if tokenPairRoutes, err := k.BuildTokenPairRoutes(ctx, tokenIn, tokenOut, poolId, maxIterableRoutes); err == nil {
Expand All @@ -33,19 +33,19 @@ func (k Keeper) BuildRoutes(ctx sdk.Context, tokenIn, tokenOut string, poolId ui
}

// BuildTokenPairRoutes builds all of the possible arbitrage routes from the hot routes given the tokenIn, tokenOut and poolId that were used in the swap
func (k Keeper) BuildTokenPairRoutes(ctx sdk.Context, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64) ([]swaproutertypes.SwapAmountInRoutes, error) {
func (k Keeper) BuildTokenPairRoutes(ctx sdk.Context, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64) ([]poolmanagertypes.SwapAmountInRoutes, error) {
if *maxIterableRoutes <= 0 {
return []swaproutertypes.SwapAmountInRoutes{}, fmt.Errorf("the number of routes that can be iterated through has been exceeded")
return []poolmanagertypes.SwapAmountInRoutes{}, fmt.Errorf("the number of routes that can be iterated through has been exceeded")
}

// Get all of the routes from the store that match the given tokenIn and tokenOut
tokenPairArbRoutes, err := k.GetTokenPairArbRoutes(ctx, tokenIn, tokenOut)
if err != nil {
return []swaproutertypes.SwapAmountInRoutes{}, err
return []poolmanagertypes.SwapAmountInRoutes{}, err
}

// Iterate through all of the routes and build hot routes
routes := make([]swaproutertypes.SwapAmountInRoutes, 0)
routes := make([]poolmanagertypes.SwapAmountInRoutes, 0)
for index := 0; index < len(tokenPairArbRoutes.ArbRoutes) && *maxIterableRoutes > 0; index++ {
if newRoute, err := k.BuildHotRoute(ctx, tokenPairArbRoutes.ArbRoutes[index], tokenIn, tokenOut, poolId, maxIterableRoutes); err == nil {
routes = append(routes, newRoute)
Expand All @@ -57,8 +57,8 @@ func (k Keeper) BuildTokenPairRoutes(ctx sdk.Context, tokenIn, tokenOut string,

// BuildHotRoute constructs a cyclic arbitrage route given a hot route from the store and information about the swap that should be placed
// in the hot route.
func (k Keeper) BuildHotRoute(ctx sdk.Context, route *types.Route, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64) (swaproutertypes.SwapAmountInRoutes, error) {
newRoute := make(swaproutertypes.SwapAmountInRoutes, 0)
func (k Keeper) BuildHotRoute(ctx sdk.Context, route *types.Route, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64) (poolmanagertypes.SwapAmountInRoutes, error) {
newRoute := make(poolmanagertypes.SwapAmountInRoutes, 0)

for _, trade := range route.Trades {
// 0 is a placeholder for pools swapped on that should be entered into the hot route
Expand All @@ -77,27 +77,27 @@ func (k Keeper) BuildHotRoute(ctx sdk.Context, route *types.Route, tokenIn, toke

// Check that the hot route is valid
if err := k.CheckValidHotRoute(ctx, newRoute); err != nil {
return swaproutertypes.SwapAmountInRoutes{}, err
return poolmanagertypes.SwapAmountInRoutes{}, err
}

// Check that the route can be iterated
if weight, err := k.GetRouteWeight(ctx, newRoute); err == nil && *maxIterableRoutes >= weight {
err := k.IncrementRouteCountForBlock(ctx, weight)
if err != nil {
return swaproutertypes.SwapAmountInRoutes{}, err
return poolmanagertypes.SwapAmountInRoutes{}, err
}

*maxIterableRoutes -= weight
return newRoute, nil
}

return swaproutertypes.SwapAmountInRoutes{}, fmt.Errorf("the number of routes that can be iterated through has been exceeded")
return poolmanagertypes.SwapAmountInRoutes{}, fmt.Errorf("the number of routes that can be iterated through has been exceeded")
}

// CheckValidHotRoute checks if the cyclic arbitrage route that was built using the hot routes method is correct. Much of the stateless
// validation achieves the desired checks, however, we also check that the route is traversing pools that
// are active.
func (k Keeper) CheckValidHotRoute(ctx sdk.Context, route swaproutertypes.SwapAmountInRoutes) error {
func (k Keeper) CheckValidHotRoute(ctx sdk.Context, route poolmanagertypes.SwapAmountInRoutes) error {
if route.Length() != 3 {
return fmt.Errorf("invalid hot route length")
}
Expand All @@ -114,32 +114,32 @@ func (k Keeper) CheckValidHotRoute(ctx sdk.Context, route swaproutertypes.SwapAm
}

// BuildOsmoRoute builds a cyclic arbitrage route that starts and ends with osmo given the tokenIn, tokenOut and poolId that were used in the swap
func (k Keeper) BuildOsmoRoute(ctx sdk.Context, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64) (swaproutertypes.SwapAmountInRoutes, error) {
func (k Keeper) BuildOsmoRoute(ctx sdk.Context, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64) (poolmanagertypes.SwapAmountInRoutes, error) {
return k.BuildRoute(ctx, types.OsmosisDenomination, tokenIn, tokenOut, poolId, maxIterableRoutes, k.GetOsmoPool)
}

// BuildAtomRoute builds a cyclic arbitrage route that starts and ends with atom given the tokenIn, tokenOut and poolId that were used in the swap
func (k Keeper) BuildAtomRoute(ctx sdk.Context, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64) (swaproutertypes.SwapAmountInRoutes, error) {
func (k Keeper) BuildAtomRoute(ctx sdk.Context, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64) (poolmanagertypes.SwapAmountInRoutes, error) {
return k.BuildRoute(ctx, types.AtomDenomination, tokenIn, tokenOut, poolId, maxIterableRoutes, k.GetAtomPool)
}

// BuildRoute constructs a cyclic arbitrage route that is starts/ends with swapDenom (atom or osmo) given the swap (tokenIn, tokenOut, poolId), and
// a function that can get the poolId from the store given a (token, swapDenom) pair.
func (k Keeper) BuildRoute(ctx sdk.Context, swapDenom, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64, getPoolIDFromStore func(sdk.Context, string) (uint64, error)) (swaproutertypes.SwapAmountInRoutes, error) {
func (k Keeper) BuildRoute(ctx sdk.Context, swapDenom, tokenIn, tokenOut string, poolId uint64, maxIterableRoutes *uint64, getPoolIDFromStore func(sdk.Context, string) (uint64, error)) (poolmanagertypes.SwapAmountInRoutes, error) {
if *maxIterableRoutes <= 0 {
return swaproutertypes.SwapAmountInRoutes{}, fmt.Errorf("the number of routes that can be iterated through has been exceeded")
return poolmanagertypes.SwapAmountInRoutes{}, fmt.Errorf("the number of routes that can be iterated through has been exceeded")
}

// Creating the first trade in the arb
entryPoolId, err := getPoolIDFromStore(ctx, tokenOut)
if err != nil {
return swaproutertypes.SwapAmountInRoutes{}, err
return poolmanagertypes.SwapAmountInRoutes{}, err
}

// Check that the pool exists and is active
_, err = k.GetAndCheckPool(ctx, entryPoolId)
if err != nil {
return swaproutertypes.SwapAmountInRoutes{}, err
return poolmanagertypes.SwapAmountInRoutes{}, err
}
// Create the first swap for the MultiHopSwap Route
entryRoute := poolmanagertypes.SwapAmountInRoute{
Expand All @@ -150,7 +150,7 @@ func (k Keeper) BuildRoute(ctx sdk.Context, swapDenom, tokenIn, tokenOut string,
// Creating the second trade in the arb
_, err = k.GetAndCheckPool(ctx, poolId)
if err != nil {
return swaproutertypes.SwapAmountInRoutes{}, err
return poolmanagertypes.SwapAmountInRoutes{}, err
}
middleRoute := poolmanagertypes.SwapAmountInRoute{
PoolId: poolId,
Expand All @@ -160,31 +160,31 @@ func (k Keeper) BuildRoute(ctx sdk.Context, swapDenom, tokenIn, tokenOut string,
// Creating the third trade in the arb
exitPoolId, err := getPoolIDFromStore(ctx, tokenIn)
if err != nil {
return swaproutertypes.SwapAmountInRoutes{}, err
return poolmanagertypes.SwapAmountInRoutes{}, err
}
_, err = k.GetAndCheckPool(ctx, exitPoolId)
if err != nil {
return swaproutertypes.SwapAmountInRoutes{}, err
return poolmanagertypes.SwapAmountInRoutes{}, err
}
exitRoute := poolmanagertypes.SwapAmountInRoute{
PoolId: exitPoolId,
TokenOutDenom: swapDenom,
}

newRoute := swaproutertypes.SwapAmountInRoutes{entryRoute, middleRoute, exitRoute}
newRoute := poolmanagertypes.SwapAmountInRoutes{entryRoute, middleRoute, exitRoute}

// Check that the route can be iterated
if weight, err := k.GetRouteWeight(ctx, newRoute); err == nil && *maxIterableRoutes >= weight {
err := k.IncrementRouteCountForBlock(ctx, weight)
if err != nil {
return swaproutertypes.SwapAmountInRoutes{}, err
return poolmanagertypes.SwapAmountInRoutes{}, err
}

*maxIterableRoutes -= weight
return newRoute, nil
}

return swaproutertypes.SwapAmountInRoutes{}, fmt.Errorf("the number of routes that can be iterated through has been exceeded")
return poolmanagertypes.SwapAmountInRoutes{}, fmt.Errorf("the number of routes that can be iterated through has been exceeded")
}

// GetAndCheckPool retrieves the pool from the x/gamm module given a poolId and ensures that the pool can be traded on
Expand All @@ -201,7 +201,7 @@ func (k Keeper) GetAndCheckPool(ctx sdk.Context, poolId uint64) (gammtypes.CFMMP

// GetRouteWeight retrieves the weight of a route. The weight of a route is determined by the pools that are used in the route.
// Different pools will have different execution times hence the need for a weighted point system.
func (k Keeper) GetRouteWeight(ctx sdk.Context, route swaproutertypes.SwapAmountInRoutes) (uint64, error) {
func (k Keeper) GetRouteWeight(ctx sdk.Context, route poolmanagertypes.SwapAmountInRoutes) (uint64, error) {
// Routes must always be of length 3
if route.Length() != 3 {
return 0, fmt.Errorf("invalid route length")
Expand All @@ -221,9 +221,9 @@ func (k Keeper) GetRouteWeight(ctx sdk.Context, route swaproutertypes.SwapAmount
}

switch poolType {
case swaproutertypes.Balancer:
case poolmanagertypes.Balancer:
return routeWeights.BalancerWeight, nil
case swaproutertypes.Stableswap:
case poolmanagertypes.Stableswap:
return routeWeights.StableWeight, nil
default:
return 0, fmt.Errorf("invalid pool type")
Expand Down
2 changes: 1 addition & 1 deletion x/protorev/keeper/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (k Keeper) UpdateProfitsByRoute(ctx sdk.Context, route []uint64, denom stri
}

// UpdateStatistics updates the module statistics after each trade is executed
func (k Keeper) UpdateStatistics(ctx sdk.Context, route poolmanagertypes.SwapAmountInRoutes, inputCoin sdk.Coin, outputAmt sdk.Int) error {
func (k Keeper) UpdateStatistics(ctx sdk.Context, route poolmanagertypes.SwapAmountInRoutes, denom string, profit sdk.Int) error {
// Increment the number of trades executed by the ProtoRev module
if err := k.IncrementNumberOfTrades(ctx); err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions x/protorev/keeper/statistics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ func (suite *KeeperTestSuite) TestUpdateStatistics() {
// Psuedo execute a trade
err := suite.App.AppKeepers.ProtoRevKeeper.UpdateStatistics(suite.Ctx,
poolmanagertypes.SwapAmountInRoutes{{TokenOutDenom: "", PoolId: 1}, {TokenOutDenom: "", PoolId: 2}, {TokenOutDenom: "", PoolId: 3}},
sdk.NewCoin(types.OsmosisDenomination, sdk.NewInt(800)),
sdk.NewInt(1000),
types.OsmosisDenomination, sdk.NewInt(1000),
)
suite.Require().NoError(err)

Expand All @@ -160,8 +159,7 @@ func (suite *KeeperTestSuite) TestUpdateStatistics() {
// Psuedo execute a second trade
err = suite.App.AppKeepers.ProtoRevKeeper.UpdateStatistics(suite.Ctx,
poolmanagertypes.SwapAmountInRoutes{{TokenOutDenom: "", PoolId: 2}, {TokenOutDenom: "", PoolId: 3}, {TokenOutDenom: "", PoolId: 4}},
sdk.NewCoin(types.OsmosisDenomination, sdk.NewInt(850)),
sdk.NewInt(1100),
types.OsmosisDenomination, sdk.NewInt(1100),
)
suite.Require().NoError(err)

Expand Down
4 changes: 3 additions & 1 deletion x/protorev/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ type GAMMKeeper interface {
GetPoolAndPoke(ctx sdk.Context, poolId uint64) (gammtypes.CFMMPoolI, error)
GetPoolsAndPoke(ctx sdk.Context) (res []gammtypes.CFMMPoolI, err error)
GetPoolDenoms(ctx sdk.Context, poolId uint64) ([]string, error)
GetPoolType(ctx sdk.Context, poolId uint64) (swaproutertypes.PoolType, error)
GetPoolType(ctx sdk.Context, poolId uint64) (poolmanagertypes.PoolType, error)
}

// PoolManagerKeeper defines the PoolManager contract that must be fulfilled when
// creating a x/protorev keeper.
type PoolManagerKeeper interface {
RouteExactAmountIn(
ctx sdk.Context,
Expand Down

0 comments on commit 5d034e9

Please sign in to comment.