forked from neutron-org/neutron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_query_estimate_multi_hop_swap.go
51 lines (42 loc) · 1.23 KB
/
grpc_query_estimate_multi_hop_swap.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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/MonikaCat/neutron/v2/x/dex/types"
)
// TODO: This doesn't run ValidateBasic() checks.
func (k Keeper) EstimateMultiHopSwap(
goCtx context.Context,
req *types.QueryEstimateMultiHopSwapRequest,
) (*types.QueryEstimateMultiHopSwapResponse, error) {
msg := types.MsgMultiHopSwap{
Creator: req.Creator,
Receiver: req.Receiver,
Routes: req.Routes,
AmountIn: req.AmountIn,
ExitLimitPrice: req.ExitLimitPrice,
PickBestRoute: req.PickBestRoute,
}
if err := msg.ValidateBasic(); err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(goCtx)
cacheCtx, _ := ctx.CacheContext()
cacheGoCtx := sdk.WrapSDKContext(cacheCtx)
callerAddr := sdk.MustAccAddressFromBech32(req.Creator)
receiverAddr := sdk.MustAccAddressFromBech32(req.Receiver)
coinOut, err := k.MultiHopSwapCore(
cacheGoCtx,
req.AmountIn,
req.Routes,
req.ExitLimitPrice,
req.PickBestRoute,
callerAddr,
receiverAddr,
)
if err != nil {
return nil, err
}
// NB: Critically, we do not write the best route's buffered state context since this is only an estimate.
return &types.QueryEstimateMultiHopSwapResponse{CoinOut: coinOut}, nil
}