-
Notifications
You must be signed in to change notification settings - Fork 40
/
msg_server_swap_exact_amount_in.go
50 lines (42 loc) · 1.44 KB
/
msg_server_swap_exact_amount_in.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package keeper
import (
"context"
// "cosmossdk.io/math"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
)
func (k msgServer) SwapExactAmountIn(goCtx context.Context, msg *types.MsgSwapExactAmountIn) (*types.MsgSwapExactAmountInResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
}
recipient, err := sdk.AccAddressFromBech32(msg.Recipient)
if err != nil {
recipient = sender
}
// Try executing the tx on cached context environment, to filter invalid transactions out
cacheCtx, _ := ctx.CacheContext()
tokenOutAmount, swapFee, discount, err := k.RouteExactAmountIn(cacheCtx, sender, recipient, msg.Routes, msg.TokenIn, math.Int(msg.TokenOutMinAmount), msg.Discount)
if err != nil {
return nil, err
}
lastSwapIndex := k.GetLastSwapRequestIndex(ctx)
k.SetSwapExactAmountInRequests(ctx, msg, lastSwapIndex+1)
k.SetLastSwapRequestIndex(ctx, lastSwapIndex+1)
// 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,
SwapFee: swapFee,
Discount: discount,
Recipient: recipient.String(),
}, nil
}