Skip to content

Commit

Permalink
feat(CL): swap router messages and wiring (#3070)
Browse files Browse the repository at this point in the history
* feat(CL): swap router messages and wiring

* fix lint
  • Loading branch information
p0mvn authored Oct 20, 2022
1 parent f940013 commit 9178d75
Show file tree
Hide file tree
Showing 9 changed files with 1,931 additions and 26 deletions.
58 changes: 36 additions & 22 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ import (
// IBC Transfer: Defines the "transfer" IBC port
transfer "github.com/cosmos/ibc-go/v3/modules/apps/transfer"

"github.com/osmosis-labs/osmosis/v12/x/swaprouter"

_ "github.com/osmosis-labs/osmosis/v12/client/docs/statik"
owasm "github.com/osmosis-labs/osmosis/v12/wasmbinding"
concentratedliquidity "github.com/osmosis-labs/osmosis/v12/x/concentrated-liquidity"
epochskeeper "github.com/osmosis-labs/osmosis/v12/x/epochs/keeper"
epochstypes "github.com/osmosis-labs/osmosis/v12/x/epochs/types"
gammkeeper "github.com/osmosis-labs/osmosis/v12/x/gamm/keeper"
Expand All @@ -65,6 +68,7 @@ import (
"github.com/osmosis-labs/osmosis/v12/x/superfluid"
superfluidkeeper "github.com/osmosis-labs/osmosis/v12/x/superfluid/keeper"
superfluidtypes "github.com/osmosis-labs/osmosis/v12/x/superfluid/types"
swaproutertypes "github.com/osmosis-labs/osmosis/v12/x/swaprouter/types"
tokenfactorykeeper "github.com/osmosis-labs/osmosis/v12/x/tokenfactory/keeper"
tokenfactorytypes "github.com/osmosis-labs/osmosis/v12/x/tokenfactory/types"
"github.com/osmosis-labs/osmosis/v12/x/twap"
Expand All @@ -89,28 +93,30 @@ type AppKeepers struct {
ScopedWasmKeeper capabilitykeeper.ScopedKeeper

// "Normal" keepers
AccountKeeper *authkeeper.AccountKeeper
BankKeeper *bankkeeper.BaseKeeper
AuthzKeeper *authzkeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
DistrKeeper *distrkeeper.Keeper
SlashingKeeper *slashingkeeper.Keeper
IBCKeeper *ibckeeper.Keeper
ICAHostKeeper *icahostkeeper.Keeper
TransferKeeper *ibctransferkeeper.Keeper
EvidenceKeeper *evidencekeeper.Keeper
GAMMKeeper *gammkeeper.Keeper
TwapKeeper *twap.Keeper
LockupKeeper *lockupkeeper.Keeper
EpochsKeeper *epochskeeper.Keeper
IncentivesKeeper *incentiveskeeper.Keeper
MintKeeper *mintkeeper.Keeper
PoolIncentivesKeeper *poolincentiveskeeper.Keeper
TxFeesKeeper *txfeeskeeper.Keeper
SuperfluidKeeper *superfluidkeeper.Keeper
GovKeeper *govkeeper.Keeper
WasmKeeper *wasm.Keeper
TokenFactoryKeeper *tokenfactorykeeper.Keeper
AccountKeeper *authkeeper.AccountKeeper
BankKeeper *bankkeeper.BaseKeeper
AuthzKeeper *authzkeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
DistrKeeper *distrkeeper.Keeper
SlashingKeeper *slashingkeeper.Keeper
IBCKeeper *ibckeeper.Keeper
ICAHostKeeper *icahostkeeper.Keeper
TransferKeeper *ibctransferkeeper.Keeper
EvidenceKeeper *evidencekeeper.Keeper
GAMMKeeper *gammkeeper.Keeper
TwapKeeper *twap.Keeper
LockupKeeper *lockupkeeper.Keeper
EpochsKeeper *epochskeeper.Keeper
IncentivesKeeper *incentiveskeeper.Keeper
MintKeeper *mintkeeper.Keeper
PoolIncentivesKeeper *poolincentiveskeeper.Keeper
TxFeesKeeper *txfeeskeeper.Keeper
SuperfluidKeeper *superfluidkeeper.Keeper
GovKeeper *govkeeper.Keeper
WasmKeeper *wasm.Keeper
TokenFactoryKeeper *tokenfactorykeeper.Keeper
SwapRouterKeeper *swaprouter.Keeper
ConcentratedLiquidityKeeper *concentratedliquidity.Keeper
// IBC modules
// transfer module
TransferModule transfer.AppModule
Expand Down Expand Up @@ -250,6 +256,13 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.GetSubspace(twaptypes.ModuleName),
appKeepers.GAMMKeeper)

appKeepers.SwapRouterKeeper = swaprouter.NewKeeper(
appKeepers.keys[swaproutertypes.StoreKey],
appKeepers.GetSubspace(swaproutertypes.ModuleName),
appKeepers.GAMMKeeper,
appKeepers.ConcentratedLiquidityKeeper,
)

appKeepers.LockupKeeper = lockupkeeper.NewKeeper(
appKeepers.keys[lockuptypes.StoreKey],
// TODO: Visit why this needs to be deref'd
Expand Down Expand Up @@ -437,6 +450,7 @@ func (appKeepers *AppKeepers) initParamsKeeper(appCodec codec.BinaryCodec, legac
paramsKeeper.Subspace(wasm.ModuleName)
paramsKeeper.Subspace(tokenfactorytypes.ModuleName)
paramsKeeper.Subspace(twaptypes.ModuleName)
paramsKeeper.Subspace(swaproutertypes.ModuleName)

return paramsKeeper
}
Expand Down
72 changes: 72 additions & 0 deletions proto/osmosis/swaprouter/v1beta1/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
syntax = "proto3";
package osmosis.swaprouter.v1beta1;

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

option go_package = "github.com/osmosis-labs/osmosis/v12/x/swaprouter/types";

service Msg {
rpc SwapExactAmountIn(MsgSwapExactAmountIn)
returns (MsgSwapExactAmountInResponse);
rpc SwapExactAmountOut(MsgSwapExactAmountOut)
returns (MsgSwapExactAmountOutResponse);
}

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

message MsgSwapExactAmountIn {
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
repeated SwapAmountInRoute routes = 2 [ (gogoproto.nullable) = false ];
cosmos.base.v1beta1.Coin token_in = 3 [
(gogoproto.moretags) = "yaml:\"token_in\"",
(gogoproto.nullable) = false
];
string token_out_min_amount = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"token_out_min_amount\"",
(gogoproto.nullable) = false
];
}

message MsgSwapExactAmountInResponse {
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\"" ];
}

message MsgSwapExactAmountOut {
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
repeated 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\"",
(gogoproto.nullable) = false
];
cosmos.base.v1beta1.Coin token_out = 4 [
(gogoproto.moretags) = "yaml:\"token_out\"",
(gogoproto.nullable) = false
];
}

message MsgSwapExactAmountOutResponse {
string token_in_amount = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"token_in_amount\"",
(gogoproto.nullable) = false
];
}
13 changes: 9 additions & 4 deletions x/swaprouter/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ import (
)

type Keeper struct {
storeKey sdk.StoreKey
transientKey *sdk.TransientStoreKey
storeKey sdk.StoreKey
// TODO: remove nolint
// nolint: unused
gammKeeper types.SwapI
// TODO: remove nolint
// nolint: unused
concentratedKeeper types.SwapI

paramSpace paramtypes.Subspace
}

func NewKeeper(storeKey sdk.StoreKey, transientKey *sdk.TransientStoreKey, paramSpace paramtypes.Subspace) *Keeper {
func NewKeeper(storeKey sdk.StoreKey, paramSpace paramtypes.Subspace, gammKeeper types.SwapI, concentratedKeeper types.SwapI) *Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}

return &Keeper{storeKey: storeKey, transientKey: transientKey, paramSpace: paramSpace}
return &Keeper{storeKey: storeKey, paramSpace: paramSpace}
}

// GetParams returns the total set of swaprouter parameters.
Expand Down
72 changes: 72 additions & 0 deletions x/swaprouter/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package swaprouter

import (
"context"

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

"github.com/osmosis-labs/osmosis/v12/x/swaprouter/types"
)

type msgServer struct {
keeper *Keeper
}

func NewMsgServerImpl(keeper *Keeper) types.MsgServer {
return &msgServer{
keeper: keeper,
}
}

// TODO: spec and tests, including events
func (server msgServer) SwapExactAmountIn(goCtx context.Context, msg *types.MsgSwapExactAmountIn) (*types.MsgSwapExactAmountInResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: will be propagated to actual swaps
_, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
}

tokenOutAmount, err := server.keeper.RouteExactAmountIn(ctx)
if err != nil {
return nil, err
}

// Swap event is handled elsewhere
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
),
})

return &types.MsgSwapExactAmountInResponse{TokenOutAmount: tokenOutAmount}, nil
}

// TODO: spec and tests, including events
func (server msgServer) SwapExactAmountOut(goCtx context.Context, msg *types.MsgSwapExactAmountOut) (*types.MsgSwapExactAmountOutResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

_, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
}

tokenInAmount, err := server.keeper.RouteExactAmountOut(ctx)
if err != nil {
return nil, err
}

// Swap event is handled elsewhere
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
),
})

return &types.MsgSwapExactAmountOutResponse{TokenInAmount: tokenInAmount}, nil
}
15 changes: 15 additions & 0 deletions x/swaprouter/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package swaprouter

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// TODO: spec and tests
func (k Keeper) RouteExactAmountIn(ctx sdk.Context) (tokenOutAmount sdk.Int, err error) {
return sdk.ZeroInt(), nil
}

// TODO: spec and tests
func (k Keeper) RouteExactAmountOut(ctx sdk.Context) (tokenInAmount sdk.Int, err error) {
return sdk.ZeroInt(), nil
}
5 changes: 5 additions & 0 deletions x/swaprouter/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

const (
AttributeValueCategory = ModuleName
)
7 changes: 7 additions & 0 deletions x/swaprouter/types/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

const (
StoreKey = ModuleName

ModuleName = "swaprouter"
)
5 changes: 5 additions & 0 deletions x/swaprouter/types/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

// TODO: godoc
type SwapI interface {
}
Loading

0 comments on commit 9178d75

Please sign in to comment.