-
Notifications
You must be signed in to change notification settings - Fork 117
/
legacy.go
112 lines (105 loc) · 3.94 KB
/
legacy.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package keeper
import (
"github.com/Sifchain/sifnode/x/margin/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
)
func NewLegacyQuerier(k types.Keeper, cdc *codec.LegacyAmino) sdk.Querier {
querier := queryServer{k}
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case types.QueryMTP:
return queryMtp(ctx, req, cdc, querier)
case types.QueryMTPsForAddress:
return queryMtpsForAddress(ctx, req, cdc, querier)
case types.QueryParams:
return queryParams(ctx, req, cdc, querier)
default:
return nil, sdkerrors.Wrap(types.ErrUnknownRequest, "unknown request")
}
}
}
func NewLegacyHandler(k types.Keeper) sdk.Handler {
msgServer := NewMsgServerImpl(k)
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
switch msg := msg.(type) {
case *types.MsgOpen:
res, err := msgServer.Open(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgClose:
res, err := msgServer.Close(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgForceClose:
res, err := msgServer.ForceClose(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgUpdateParams:
res, err := msgServer.UpdateParams(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgUpdatePools:
res, err := msgServer.UpdatePools(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgUpdateRowanCollateral:
res, err := msgServer.UpdateRowanCollateral(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgWhitelist:
res, err := msgServer.Whitelist(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgDewhitelist:
res, err := msgServer.Dewhitelist(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgAdminClose:
res, err := msgServer.AdminClose(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgAdminCloseAll:
res, err := msgServer.AdminCloseAll(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized margin message type: %T", msg)
}
}
}
func queryMtp(ctx sdk.Context, req abci.RequestQuery, cdc *codec.LegacyAmino, querier queryServer) ([]byte, error) {
params := types.MTPRequest{}
err := cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
res, err := querier.GetMTP(sdk.WrapSDKContext(ctx), ¶ms)
if err != nil {
return nil, err
}
bz, err := codec.MarshalJSONIndent(cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryMtpsForAddress(ctx sdk.Context, req abci.RequestQuery, cdc *codec.LegacyAmino, querier queryServer) ([]byte, error) {
params := types.PositionsForAddressRequest{}
err := cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
res, err := querier.GetPositionsForAddress(sdk.WrapSDKContext(ctx), ¶ms)
if err != nil {
return nil, err
}
bz, err := codec.MarshalJSONIndent(cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryParams(ctx sdk.Context, req abci.RequestQuery, cdc *codec.LegacyAmino, querier queryServer) ([]byte, error) {
res, err := querier.GetParams(sdk.WrapSDKContext(ctx), &types.ParamsRequest{})
if err != nil {
return nil, err
}
bz, err := codec.MarshalJSONIndent(cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}