-
Notifications
You must be signed in to change notification settings - Fork 93
/
grpc_query_estimate_place_limit_order.go
70 lines (60 loc) · 1.76 KB
/
grpc_query_estimate_place_limit_order.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package keeper
import (
"context"
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/neutron-org/neutron/v4/x/dex/types"
)
// TODO: This doesn't run ValidateBasic() checks.
func (k Keeper) EstimatePlaceLimitOrder(
goCtx context.Context,
req *types.QueryEstimatePlaceLimitOrderRequest,
) (*types.QueryEstimatePlaceLimitOrderResponse, error) {
msg := types.MsgPlaceLimitOrder{
Creator: req.Creator,
Receiver: req.Receiver,
TokenIn: req.TokenIn,
TokenOut: req.TokenOut,
TickIndexInToOut: req.TickIndexInToOut,
AmountIn: req.AmountIn,
OrderType: req.OrderType,
ExpirationTime: req.ExpirationTime,
MaxAmountOut: req.MaxAmountOut,
}
if err := msg.Validate(); err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(goCtx)
cacheCtx, _ := ctx.CacheContext()
callerAddr := sdk.MustAccAddressFromBech32(req.Creator)
receiverAddr := sdk.MustAccAddressFromBech32(req.Receiver)
blockTime := cacheCtx.BlockTime()
if req.OrderType.IsGoodTil() && !req.ExpirationTime.After(blockTime) {
return nil, sdkerrors.Wrapf(types.ErrExpirationTimeInPast,
"Current BlockTime: %s; Provided ExpirationTime: %s",
blockTime.String(),
req.ExpirationTime.String(),
)
}
_, totalInCoin, swapInCoin, swapOutCoin, err := k.PlaceLimitOrderCore(
cacheCtx,
req.TokenIn,
req.TokenOut,
req.AmountIn,
req.TickIndexInToOut,
req.OrderType,
req.ExpirationTime,
req.MaxAmountOut,
callerAddr,
receiverAddr,
)
if err != nil {
return nil, err
}
// NB: We're only using a cache context so we don't expect any writes to happen.
return &types.QueryEstimatePlaceLimitOrderResponse{
TotalInCoin: totalInCoin,
SwapInCoin: swapInCoin,
SwapOutCoin: swapOutCoin,
}, nil
}