Skip to content

Commit

Permalink
reafctor(x/swaprouter, x/gamm): unify swap routes in swaprouter
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Dec 29, 2022
1 parent 574443a commit a3afcda
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 522 deletions.
7 changes: 4 additions & 3 deletions app/apptesting/gamm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/osmosis-labs/osmosis/v13/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v13/x/gamm/pool-models/stableswap"
gammtypes "github.com/osmosis-labs/osmosis/v13/x/gamm/types"
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"
)

var DefaultAcctFunds sdk.Coins = sdk.NewCoins(
Expand Down Expand Up @@ -188,7 +189,7 @@ func (s *KeeperTestHelper) ModifySpotPrice(poolID uint64, targetSpotPrice sdk.De
s.FundAcc(s.TestAccs[0], swapIn)
msg := gammtypes.MsgSwapExactAmountIn{
Sender: s.TestAccs[0].String(),
Routes: []gammtypes.SwapAmountInRoute{{PoolId: poolID, TokenOutDenom: baseDenom}},
Routes: []swaproutertypes.SwapAmountInRoute{{PoolId: poolID, TokenOutDenom: baseDenom}},
TokenIn: swapIn[0],
TokenOutMinAmount: sdk.ZeroInt(),
}
Expand All @@ -204,7 +205,7 @@ func (s *KeeperTestHelper) ModifySpotPrice(poolID uint64, targetSpotPrice sdk.De
s.FundAcc(s.TestAccs[0], sdk.NewCoins(tokenIn))
msg := gammtypes.MsgSwapExactAmountOut{
Sender: s.TestAccs[0].String(),
Routes: []gammtypes.SwapAmountOutRoute{{PoolId: poolID, TokenInDenom: baseDenom}},
Routes: []swaproutertypes.SwapAmountOutRoute{{PoolId: poolID, TokenInDenom: baseDenom}},
TokenInMaxAmount: sdk.NewInt(int64Max),
TokenOut: swapOut[0],
}
Expand All @@ -224,7 +225,7 @@ func (s *KeeperTestHelper) RunBasicSwap(poolId uint64) {

msg := gammtypes.MsgSwapExactAmountIn{
Sender: s.TestAccs[0].String(),
Routes: []gammtypes.SwapAmountInRoute{{PoolId: poolId, TokenOutDenom: denoms[1]}},
Routes: []swaproutertypes.SwapAmountInRoute{{PoolId: poolId, TokenOutDenom: denoms[1]}},
TokenIn: swapIn[0],
TokenOutMinAmount: sdk.ZeroInt(),
}
Expand Down
25 changes: 20 additions & 5 deletions proto/osmosis/gamm/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package osmosis.gamm.v1beta1;

import "gogoproto/gogo.proto";
import "osmosis/gamm/v1beta1/tx.proto";
import "osmosis/swaprouter/v1beta1/swap_route.proto";

import "cosmos/base/v1beta1/coin.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
Expand Down Expand Up @@ -85,15 +86,18 @@ service Query {
"/osmosis/gamm/v1beta1/pools/{pool_id}/prices";
}

// Estimate the swap.
// Deprecated: please use EstimateSwapExactAmountIn in x/swaprouter.
rpc EstimateSwapExactAmountIn(QuerySwapExactAmountInRequest)
returns (QuerySwapExactAmountInResponse) {
option deprecated = true;
option (google.api.http).get =
"/osmosis/gamm/v1beta1/{pool_id}/estimate/swap_exact_amount_in";
}

// Deprecated: please use EstimateSwapExactAmountOut in x/swaprouter.
rpc EstimateSwapExactAmountOut(QuerySwapExactAmountOutRequest)
returns (QuerySwapExactAmountOutResponse) {
option deprecated = true;
option (google.api.http).get =
"/osmosis/gamm/v1beta1/{pool_id}/estimate/swap_exact_amount_out";
}
Expand Down Expand Up @@ -121,8 +125,11 @@ message QueryPoolsResponse {
}

//=============================== NumPools
message QueryNumPoolsRequest {}
// Deprecated: use QueryNumPoolsRequest in x/swaprouter.
message QueryNumPoolsRequest { option deprecated = true; }
// Deprecated: use QueryNumPoolsResponse in x/swaprouter.
message QueryNumPoolsResponse {
option deprecated = true;
uint64 num_pools = 1 [ (gogoproto.moretags) = "yaml:\"num_pools\"" ];
}

Expand Down Expand Up @@ -258,18 +265,22 @@ message QuerySpotPriceResponse {
}

//=============================== EstimateSwapExactAmountIn
// Deprecated: use QuerySwapExactAmountInRequest in x/swaprouter.
message QuerySwapExactAmountInRequest {
option deprecated = true;
// TODO: CHANGE THIS TO RESERVED IN A PATCH RELEASE
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
uint64 pool_id = 2 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string token_in = 3 [ (gogoproto.moretags) = "yaml:\"token_in\"" ];
repeated SwapAmountInRoute routes = 4 [
repeated osmosis.swaprouter.v1beta1.SwapAmountInRoute routes = 4 [
(gogoproto.moretags) = "yaml:\"routes\"",
(gogoproto.nullable) = false
];
}

// Deprecated: use QuerySwapExactAmountInResponse in x/swaprouter.
message QuerySwapExactAmountInResponse {
option deprecated = true;
string token_out_amount = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"token_out_amount\"",
Expand All @@ -278,18 +289,22 @@ message QuerySwapExactAmountInResponse {
}

//=============================== EstimateSwapExactAmountOut
// Deprecated: use QuerySwapExactAmountOutRequest in x/swaprouter.
message QuerySwapExactAmountOutRequest {
option deprecated = true;
// TODO: CHANGE THIS TO RESERVED IN A PATCH RELEASE
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
uint64 pool_id = 2 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
repeated SwapAmountOutRoute routes = 3 [
repeated osmosis.swaprouter.v1beta1.SwapAmountOutRoute routes = 3 [
(gogoproto.moretags) = "yaml:\"routes\"",
(gogoproto.nullable) = false
];
string token_out = 4 [ (gogoproto.moretags) = "yaml:\"token_out\"" ];
}

// Deprecated: use QuerySwapExactAmountOutResponse in x/swaprouter.
message QuerySwapExactAmountOutResponse {
option deprecated = true;
string token_in_amount = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"token_in_amount\"",
Expand All @@ -305,4 +320,4 @@ message QueryTotalLiquidityResponse {
(gogoproto.moretags) = "yaml:\"liquidity\"",
(gogoproto.nullable) = false
];
}
}
35 changes: 23 additions & 12 deletions proto/osmosis/gamm/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ package osmosis.gamm.v1beta1;

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "osmosis/swaprouter/v1beta1/swap_route.proto";

option go_package = "github.com/osmosis-labs/osmosis/v13/x/gamm/types";

service Msg {
rpc JoinPool(MsgJoinPool) returns (MsgJoinPoolResponse);
rpc ExitPool(MsgExitPool) returns (MsgExitPoolResponse);
// Deprecated: use SwapExactAmountIn in x/swaprouter.
rpc SwapExactAmountIn(MsgSwapExactAmountIn)
returns (MsgSwapExactAmountInResponse);
returns (MsgSwapExactAmountInResponse) {
option deprecated = true;
}
// Deprecated: use SwapExactAmountOut in x/swaprouter.
rpc SwapExactAmountOut(MsgSwapExactAmountOut)
returns (MsgSwapExactAmountOutResponse);
returns (MsgSwapExactAmountOutResponse) {
option deprecated = true;
}
rpc JoinSwapExternAmountIn(MsgJoinSwapExternAmountIn)
returns (MsgJoinSwapExternAmountInResponse);
rpc JoinSwapShareAmountOut(MsgJoinSwapShareAmountOut)
Expand Down Expand Up @@ -74,16 +81,20 @@ message MsgExitPoolResponse {
];
}

// ===================== MsgSwapExactAmountIn
// Deprecated: use SwapAmountInRoute in x/swaprouter.
// This route will be removed in the future.
message SwapAmountInRoute {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string token_out_denom = 2
[ (gogoproto.moretags) = "yaml:\"token_out_denom\"" ];
}

// Deprecated: use MsgSwapExactAmountIn in x/swaprouter.
message MsgSwapExactAmountIn {
option deprecated = true;
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
repeated SwapAmountInRoute routes = 2 [ (gogoproto.nullable) = false ];
repeated osmosis.swaprouter.v1beta1.SwapAmountInRoute routes = 2
[ (gogoproto.nullable) = false ];
cosmos.base.v1beta1.Coin token_in = 3 [
(gogoproto.moretags) = "yaml:\"token_in\"",
(gogoproto.nullable) = false
Expand All @@ -95,24 +106,22 @@ message MsgSwapExactAmountIn {
];
}

// Deprecated: use MsgSwapExactAmountInResponse in x/swaprouter.
message MsgSwapExactAmountInResponse {
option deprecated = true;
string token_out_amount = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"token_out_amount\"",
(gogoproto.nullable) = false
];
}

// ===================== MsgSwapExactAmountOut
message SwapAmountOutRoute {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string token_in_denom = 2
[ (gogoproto.moretags) = "yaml:\"token_out_denom\"" ];
}

// Deprecated: use MsgSwapExactAmountOut in x/swaprouter.
message MsgSwapExactAmountOut {
option deprecated = true;
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
repeated SwapAmountOutRoute routes = 2 [ (gogoproto.nullable) = false ];
repeated osmosis.swaprouter.v1beta1.SwapAmountOutRoute routes = 2
[ (gogoproto.nullable) = false ];
string token_in_max_amount = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"token_in_max_amount\"",
Expand All @@ -124,7 +133,9 @@ message MsgSwapExactAmountOut {
];
}

// Deprecated: use MsgSwapExactAmountOutResponse in x/swaprouter.
message MsgSwapExactAmountOutResponse {
option deprecated = true;
string token_in_amount = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"token_in_amount\"",
Expand Down
9 changes: 5 additions & 4 deletions x/gamm/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/osmosis-labs/osmosis/v13/x/gamm/client/cli"
"github.com/osmosis-labs/osmosis/v13/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v13/x/gamm/types"
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"

"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/network"
Expand Down Expand Up @@ -215,7 +216,7 @@ func TestNewSwapExactAmountOutCmd(t *testing.T) {
Cmd: "10stake 20 --swap-route-pool-ids=1 --swap-route-denoms=node0token --from=" + testAddresses[0].String(),
ExpectedMsg: &types.MsgSwapExactAmountOut{
Sender: testAddresses[0].String(),
Routes: []types.SwapAmountOutRoute{{PoolId: 1, TokenInDenom: "node0token"}},
Routes: []swaproutertypes.SwapAmountOutRoute{{PoolId: 1, TokenInDenom: "node0token"}},
TokenInMaxAmount: sdk.NewIntFromUint64(20),
TokenOut: sdk.NewInt64Coin("stake", 10),
},
Expand All @@ -231,7 +232,7 @@ func TestNewSwapExactAmountInCmd(t *testing.T) {
Cmd: "10stake 3 --swap-route-pool-ids=1 --swap-route-denoms=node0token --from=" + testAddresses[0].String(),
ExpectedMsg: &types.MsgSwapExactAmountIn{
Sender: testAddresses[0].String(),
Routes: []types.SwapAmountInRoute{{PoolId: 1, TokenOutDenom: "node0token"}},
Routes: []swaproutertypes.SwapAmountInRoute{{PoolId: 1, TokenOutDenom: "node0token"}},
TokenIn: sdk.NewInt64Coin("stake", 10),
TokenOutMinAmount: sdk.NewIntFromUint64(3),
},
Expand Down Expand Up @@ -354,7 +355,7 @@ func TestGetCmdEstimateSwapExactAmountIn(t *testing.T) {
Sender: "osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7",
PoolId: 1,
TokenIn: "10stake",
Routes: []types.SwapAmountInRoute{{PoolId: 2, TokenOutDenom: "node0token"}},
Routes: []swaproutertypes.SwapAmountInRoute{{PoolId: 2, TokenOutDenom: "node0token"}},
},
},
}
Expand All @@ -370,7 +371,7 @@ func TestGetCmdEstimateSwapExactAmountOut(t *testing.T) {
Sender: "osm11vmx8jtggpd9u7qr0t8vxclycz85u925sazglr7",
PoolId: 1,
TokenOut: "10stake",
Routes: []types.SwapAmountOutRoute{{PoolId: 2, TokenInDenom: "node0token"}},
Routes: []swaproutertypes.SwapAmountOutRoute{{PoolId: 2, TokenInDenom: "node0token"}},
},
},
}
Expand Down
13 changes: 7 additions & 6 deletions x/gamm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/osmosis-labs/osmosis/v13/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v13/x/gamm/pool-models/stableswap"
"github.com/osmosis-labs/osmosis/v13/x/gamm/types"
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"

"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -380,7 +381,7 @@ func stringArrayCoinsParser(flagName string, fs *flag.FlagSet) (sdk.Coins, error
return coins, nil
}

func swapAmountInRoutes(fs *flag.FlagSet) ([]types.SwapAmountInRoute, error) {
func swapAmountInRoutes(fs *flag.FlagSet) ([]swaproutertypes.SwapAmountInRoute, error) {
swapRoutePoolIds, err := fs.GetString(FlagSwapRoutePoolIds)
swapRoutePoolIdsArray := strings.Split(swapRoutePoolIds, ",")
if err != nil {
Expand All @@ -397,21 +398,21 @@ func swapAmountInRoutes(fs *flag.FlagSet) ([]types.SwapAmountInRoute, error) {
return nil, errors.New("swap route pool ids and denoms mismatch")
}

routes := []types.SwapAmountInRoute{}
routes := []swaproutertypes.SwapAmountInRoute{}
for index, poolIDStr := range swapRoutePoolIdsArray {
pID, err := strconv.Atoi(poolIDStr)
if err != nil {
return nil, err
}
routes = append(routes, types.SwapAmountInRoute{
routes = append(routes, swaproutertypes.SwapAmountInRoute{
PoolId: uint64(pID),
TokenOutDenom: swapRouteDenomsArray[index],
})
}
return routes, nil
}

func swapAmountOutRoutes(fs *flag.FlagSet) ([]types.SwapAmountOutRoute, error) {
func swapAmountOutRoutes(fs *flag.FlagSet) ([]swaproutertypes.SwapAmountOutRoute, error) {
swapRoutePoolIds, err := fs.GetString(FlagSwapRoutePoolIds)
swapRoutePoolIdsArray := strings.Split(swapRoutePoolIds, ",")
if err != nil {
Expand All @@ -428,13 +429,13 @@ func swapAmountOutRoutes(fs *flag.FlagSet) ([]types.SwapAmountOutRoute, error) {
return nil, errors.New("swap route pool ids and denoms mismatch")
}

routes := []types.SwapAmountOutRoute{}
routes := []swaproutertypes.SwapAmountOutRoute{}
for index, poolIDStr := range swapRoutePoolIdsArray {
pID, err := strconv.Atoi(poolIDStr)
if err != nil {
return nil, err
}
routes = append(routes, types.SwapAmountOutRoute{
routes = append(routes, swaproutertypes.SwapAmountOutRoute{
PoolId: uint64(pID),
TokenInDenom: swapRouteDenomsArray[index],
})
Expand Down
10 changes: 2 additions & 8 deletions x/gamm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,7 @@ func (q Querier) EstimateSwapExactAmountIn(ctx context.Context, req *types.Query

sdkCtx := sdk.UnwrapSDKContext(ctx)

// TODO: remove this redundancy after making routes be shared between x/gamm and x/swaprouter.
swaprouterRoutes := types.ConvertAmountInRoutes(req.Routes)

tokenOutAmount, err := q.Keeper.poolManager.MultihopEstimateOutGivenExactAmountIn(sdkCtx, swaprouterRoutes, tokenIn)
tokenOutAmount, err := q.Keeper.poolManager.MultihopEstimateOutGivenExactAmountIn(sdkCtx, req.Routes, tokenIn)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand All @@ -452,10 +449,7 @@ func (q Querier) EstimateSwapExactAmountOut(ctx context.Context, req *types.Quer

sdkCtx := sdk.UnwrapSDKContext(ctx)

// TODO: remove this redundancy after making routes be shared between x/gamm and x/swaprouter.
swaprouterRoutes := types.ConvertAmountOutRoutes(req.Routes)

tokenInAmount, err := q.Keeper.poolManager.MultihopEstimateInGivenExactAmountOut(sdkCtx, swaprouterRoutes, tokenOut)
tokenInAmount, err := q.Keeper.poolManager.MultihopEstimateInGivenExactAmountOut(sdkCtx, req.Routes, tokenOut)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down
10 changes: 2 additions & 8 deletions x/gamm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,7 @@ func (server msgServer) SwapExactAmountIn(goCtx context.Context, msg *types.MsgS
return nil, err
}

// TODO: remove this redundancy after making routes be shared between x/gamm and x/swaprouter.
swaprouterRoutes := types.ConvertAmountInRoutes(msg.Routes)

tokenOutAmount, err := server.keeper.poolManager.RouteExactAmountIn(ctx, sender, swaprouterRoutes, msg.TokenIn, msg.TokenOutMinAmount)
tokenOutAmount, err := server.keeper.poolManager.RouteExactAmountIn(ctx, sender, msg.Routes, msg.TokenIn, msg.TokenOutMinAmount)
if err != nil {
return nil, err
}
Expand All @@ -191,10 +188,7 @@ func (server msgServer) SwapExactAmountOut(goCtx context.Context, msg *types.Msg
return nil, err
}

// TODO: remove this redundancy after making routes be shared between x/gamm and x/swaprouter.
swaprouterRoutes := types.ConvertAmountOutRoutes(msg.Routes)

tokenInAmount, err := server.keeper.poolManager.RouteExactAmountOut(ctx, sender, swaprouterRoutes, msg.TokenInMaxAmount, msg.TokenOut)
tokenInAmount, err := server.keeper.poolManager.RouteExactAmountOut(ctx, sender, msg.Routes, msg.TokenInMaxAmount, msg.TokenOut)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit a3afcda

Please sign in to comment.