-
Notifications
You must be signed in to change notification settings - Fork 39
/
service.go
88 lines (73 loc) · 2.37 KB
/
service.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package tx
import (
"context"
gogogrpc "github.com/gogo/protobuf/grpc"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
customante "github.com/classic-terra/core/v2/custom/auth/ante"
"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var _ ServiceServer = txServer{}
// txServer is the server for the protobuf Tx service.
type txServer struct {
clientCtx client.Context
treasuryKeeper customante.TreasuryKeeper
}
// NewTxServer creates a new Tx service server.
func NewTxServer(clientCtx client.Context, treasuryKeeper customante.TreasuryKeeper) ServiceServer {
return txServer{
clientCtx: clientCtx,
treasuryKeeper: treasuryKeeper,
}
}
// ComputeTax implements the ServiceServer.ComputeTax RPC method.
func (ts txServer) ComputeTax(c context.Context, req *ComputeTaxRequest) (*ComputeTaxResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
if req == nil {
return nil, status.Error(codes.InvalidArgument, "request cannot be nil")
}
var msgs []sdk.Msg
switch {
case len(req.TxBytes) != 0:
tx, err := ts.clientCtx.TxConfig.TxDecoder()(req.TxBytes)
if err != nil {
return nil, err
}
msgs = tx.GetMsgs()
case req.Tx != nil:
msgs = req.Tx.GetMsgs()
default:
return nil, status.Errorf(codes.InvalidArgument, "empty txBytes is not allowed")
}
taxAmount := customante.FilterMsgAndComputeTax(ctx, ts.treasuryKeeper, msgs...)
return &ComputeTaxResponse{
TaxAmount: taxAmount,
}, nil
}
// RegisterTxService registers the tx service on the gRPC router.
func RegisterTxService(
qrt gogogrpc.Server,
clientCtx client.Context,
treasuryKeeper customante.TreasuryKeeper,
) {
RegisterServiceServer(
qrt,
NewTxServer(clientCtx, treasuryKeeper),
)
}
// RegisterGRPCGatewayRoutes mounts the tx service's GRPC-gateway routes on the
// given Mux.
func RegisterGRPCGatewayRoutes(clientConn gogogrpc.ClientConn, mux *runtime.ServeMux) {
_ = RegisterServiceHandlerClient(context.Background(), mux, NewServiceClient(clientConn))
}
var _ codectypes.UnpackInterfacesMessage = ComputeTaxRequest{}
// UnpackInterfaces implements the UnpackInterfacesMessage interface.
func (m ComputeTaxRequest) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if m.Tx != nil {
return m.Tx.UnpackInterfaces(unpacker)
}
return nil
}