Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: add split route to message server in pool manager #5204

Merged
merged 4 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions proto/osmosis/poolmanager/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ service Msg {
returns (MsgSwapExactAmountInResponse);
rpc SwapExactAmountOut(MsgSwapExactAmountOut)
returns (MsgSwapExactAmountOutResponse);
rpc SplitRouteSwapExactAmountIn(MsgSplitRouteSwapExactAmountIn)
returns (MsgSplitRouteSwapExactAmountInResponse);
rpc SplitRouteSwapExactAmountOut(MsgSplitRouteSwapExactAmountOut)
returns (MsgSplitRouteSwapExactAmountOutResponse);
}

// ===================== MsgSwapExactAmountIn
Expand Down
50 changes: 50 additions & 0 deletions x/poolmanager/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,53 @@ func (server msgServer) SwapExactAmountOut(goCtx context.Context, msg *types.Msg

return &types.MsgSwapExactAmountOutResponse{TokenInAmount: tokenInAmount}, nil
}

func (server msgServer) SplitRouteSwapExactAmountIn(goCtx context.Context, msg *types.MsgSplitRouteSwapExactAmountIn) (*types.MsgSplitRouteSwapExactAmountInResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

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

tokenOutAmount, err := server.keeper.SplitRouteExactAmountIn(ctx, sender, msg.Routes, msg.TokenInDenom, msg.TokenOutMinAmount)
if err != nil {
return nil, err
}

// Swap event is handled elsewhere
p0mvn marked this conversation as resolved.
Show resolved Hide resolved
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
),
})

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

func (server msgServer) SplitRouteSwapExactAmountOut(goCtx context.Context, msg *types.MsgSplitRouteSwapExactAmountOut) (*types.MsgSplitRouteSwapExactAmountOutResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

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

tokenInAmount, err := server.keeper.SplitRouteExactAmountOut(ctx, sender, msg.Routes, msg.TokenOutDenom, msg.TokenInMaxAmount)
if err != nil {
return nil, err
}

// Swap event is handled elsewhere
p0mvn marked this conversation as resolved.
Show resolved Hide resolved
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
),
})

return &types.MsgSplitRouteSwapExactAmountOutResponse{TokenInAmount: tokenInAmount}, nil
}
18 changes: 18 additions & 0 deletions x/poolmanager/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ func (k Keeper) SplitRouteExactAmountIn(
return sdk.Int{}, types.PriceImpactProtectionExactInError{Actual: totalOutAmount, MinAmount: tokenOutMinAmount}
}

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeMsgSplitRouteSwapExactAmountIn,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
sdk.NewAttribute(types.AttributeKeyTokensOut, totalOutAmount.String()),
),
})

return totalOutAmount, nil
}

Expand Down Expand Up @@ -436,6 +445,15 @@ func (k Keeper) SplitRouteExactAmountOut(
return sdk.Int{}, types.PriceImpactProtectionExactOutError{Actual: totalInAmount, MaxAmount: tokenInMaxAmount}
}

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeMsgSplitRouteSwapExactAmountOut,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
sdk.NewAttribute(types.AttributeKeyTokensOut, totalInAmount.String()),
),
})

return totalInAmount, nil
}

Expand Down
9 changes: 6 additions & 3 deletions x/poolmanager/types/events.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package types

const (
AttributeValueCategory = ModuleName
TypeEvtPoolCreated = "pool_created"
AttributeKeyPoolId = "pool_id"
AttributeValueCategory = ModuleName
TypeEvtPoolCreated = "pool_created"
TypeEvtSplitRouteSwapExactIn = "split_route_swap_exact_in"
AttributeKeyTokensIn = "tokens_in"
AttributeKeyTokensOut = "tokens_out"
AttributeKeyPoolId = "pool_id"
)
167 changes: 121 additions & 46 deletions x/poolmanager/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.