Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/ante"
ibcante "github.com/cosmos/ibc-go/v3/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper"
"github.com/sei-protocol/sei-chain/x/dex"
"github.com/sei-protocol/sei-chain/x/oracle"
oraclekeeper "github.com/sei-protocol/sei-chain/x/oracle/keeper"
)
Expand Down Expand Up @@ -48,13 +49,15 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
sigGasConsumer = ante.DefaultSigVerificationGasConsumer
}

memPoolDecorator := ante.NewMempoolFeeDecorator()
anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
dex.NewGaslessDecorator(),
wasmkeeper.NewLimitSimulationGasDecorator(options.WasmConfig.SimulationGasLimit), // after setup context to enforce limits early
wasmkeeper.NewCountTXDecorator(options.TXCounterStoreKey),
ante.NewRejectExtensionOptionsDecorator(),
oracle.NewSpammingPreventionDecorator(*options.OracleKeeper),
ante.NewMempoolFeeDecorator(),
dex.NewGaslessDecoratorWrapper(&memPoolDecorator),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
Expand Down
77 changes: 77 additions & 0 deletions x/dex/ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package dex

import (
"bytes"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/x/dex/types"
)

// TODO: migrate this into params state
var WHITELISTED_GASLESS_CANCELLATION_ADDRS = []sdk.AccAddress{}

type GaslessDecoratorWrapper struct {
wrapped sdk.AnteDecorator
}

func NewGaslessDecoratorWrapper(wrapped sdk.AnteDecorator) GaslessDecoratorWrapper {
return GaslessDecoratorWrapper{wrapped: wrapped}
}

func (gd GaslessDecoratorWrapper) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if !isTxGasless(tx) {
return gd.wrapped.AnteHandle(ctx, tx, simulate, next)
}

// skip the wrapped if gasless
return next(ctx, tx, simulate)
}

type GaslessDecorator struct{}

func NewGaslessDecorator() GaslessDecorator {
return GaslessDecorator{}
}

func (gd GaslessDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if !isTxGasless(tx) {
return next(ctx, tx, simulate)
}
gaslessMeter := sdk.NewInfiniteGasMeter()

return next(ctx.WithGasMeter(gaslessMeter), tx, simulate)
}

func isTxGasless(tx sdk.Tx) bool {
for _, msg := range tx.GetMsgs() {
switch msg.(type) {
case *types.MsgPlaceOrders:
continue
case *types.MsgCancelOrders:
if allSignersWhitelisted(msg) {
continue
} else {
return false
}
default:
return false
}
}
return true
}

func allSignersWhitelisted(msg sdk.Msg) bool {
for _, signer := range msg.GetSigners() {
isWhitelisted := false
for _, whitelisted := range WHITELISTED_GASLESS_CANCELLATION_ADDRS {
if bytes.Compare(signer, whitelisted) == 0 {
isWhitelisted = true
break
}
}
if !isWhitelisted {
return false
}
}
return true
}