-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate_limit.go
49 lines (44 loc) · 1.38 KB
/
rate_limit.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
package ante
import (
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/furyanprotocol/v4-chain/protocol/x/clob/types"
)
var _ sdktypes.AnteDecorator = (*ClobRateLimitDecorator)(nil)
// ClobRateLimitDecorator is an AnteDecorator which is responsible for rate limiting MsgCancelOrder and MsgPlaceOrder
// requests.
//
// This AnteDecorator is a no-op if:
// - No messages in the transaction are `MsgCancelOrder` or `MsgPlaceOrder`.
//
// This AnteDecorator returns an error if:
// - The rate limit is exceeded for any `MsgCancelOrder` messages.
// - The rate limit is exceeded for any `MsgPlaceOrder` messages.
//
// TODO(CLOB-721): Rate limit short term order cancellations.
type ClobRateLimitDecorator struct {
clobKeeper types.ClobKeeper
}
func NewRateLimitDecorator(clobKeeper types.ClobKeeper) ClobRateLimitDecorator {
return ClobRateLimitDecorator{
clobKeeper,
}
}
func (r ClobRateLimitDecorator) AnteHandle(
ctx sdktypes.Context,
tx sdktypes.Tx, simulate bool,
next sdktypes.AnteHandler,
) (newCtx sdktypes.Context, err error) {
for _, msg := range tx.GetMsgs() {
switch msg := msg.(type) {
case *types.MsgCancelOrder:
if err = r.clobKeeper.RateLimitCancelOrder(ctx, msg); err != nil {
return ctx, err
}
case *types.MsgPlaceOrder:
if err = r.clobKeeper.RateLimitPlaceOrder(ctx, msg); err != nil {
return ctx, err
}
}
}
return next(ctx, tx, simulate)
}