-
Notifications
You must be signed in to change notification settings - Fork 127
/
decorators.go
97 lines (87 loc) · 3.17 KB
/
decorators.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
package ante
import (
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
coinswaptypes "github.com/irisnet/irismod/modules/coinswap/types"
servicetypes "github.com/irisnet/irismod/modules/service/types"
tokenkeeper "github.com/irisnet/irismod/modules/token/keeper"
tokentypesv1 "github.com/irisnet/irismod/modules/token/types/v1"
tokentypesv1beta1 "github.com/irisnet/irismod/modules/token/types/v1beta1"
)
// ValidateTokenDecorator is responsible for restricting the token participation of the swap prefix
type ValidateTokenDecorator struct {
tk tokenkeeper.Keeper
}
// NewValidateTokenDecorator returns an instance of ValidateTokenDecorator
func NewValidateTokenDecorator(tk tokenkeeper.Keeper) ValidateTokenDecorator {
return ValidateTokenDecorator{
tk: tk,
}
}
// AnteHandle checks the transaction
func (vtd ValidateTokenDecorator) AnteHandle(
ctx sdk.Context,
tx sdk.Tx,
simulate bool,
next sdk.AnteHandler,
) (sdk.Context, error) {
for _, msg := range tx.GetMsgs() {
switch msg := msg.(type) {
case *ibctransfertypes.MsgTransfer:
if containSwapCoin(msg.Token) {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "can't transfer coinswap liquidity tokens through the IBC module")
}
case *tokentypesv1.MsgBurnToken:
if _, err := vtd.tk.GetToken(ctx, msg.Coin.Denom); err != nil {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "burnt failed, only native tokens can be burnt")
}
case *tokentypesv1beta1.MsgBurnToken:
if _, err := vtd.tk.GetToken(ctx, msg.Symbol); err != nil {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "burnt failed, only native tokens can be burnt")
}
case *govv1.MsgSubmitProposal:
if containSwapCoin(msg.InitialDeposit...) {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "can't deposit coinswap liquidity token for proposal")
}
case *govv1.MsgDeposit:
if containSwapCoin(msg.Amount...) {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "can't deposit coinswap liquidity token for proposal")
}
}
}
return next(ctx, tx, simulate)
}
// ValidateServiceDecorator is responsible for checking the permission to execute MsgCallService
type ValidateServiceDecorator struct{}
// NewValidateServiceDecorator returns an instance of ServiceAuthDecorator
func NewValidateServiceDecorator() ValidateServiceDecorator {
return ValidateServiceDecorator{}
}
// AnteHandle checks the transaction
func (vsd ValidateServiceDecorator) AnteHandle(
ctx sdk.Context,
tx sdk.Tx,
simulate bool,
next sdk.AnteHandler,
) (sdk.Context, error) {
for _, msg := range tx.GetMsgs() {
switch msg := msg.(type) {
case *servicetypes.MsgCallService:
if msg.Repeated {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "currently does not support to create repeatable service invocation")
}
}
}
return next(ctx, tx, simulate)
}
func containSwapCoin(coins ...sdk.Coin) bool {
for _, coin := range coins {
if strings.HasPrefix(coin.Denom, coinswaptypes.LptTokenPrefix) {
return true
}
}
return false
}