Skip to content

Commit

Permalink
Add tax2gas module and move all handle fees logic to tax2gas module
Browse files Browse the repository at this point in the history
  • Loading branch information
phamminh0811 committed Jun 24, 2024
1 parent 383a96d commit 419ce91
Show file tree
Hide file tree
Showing 27 changed files with 2,209 additions and 32 deletions.
4 changes: 3 additions & 1 deletion custom/auth/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

dyncommante "github.com/classic-terra/core/v3/x/dyncomm/ante"
dyncommkeeper "github.com/classic-terra/core/v3/x/dyncomm/keeper"
tax2gasante "github.com/classic-terra/core/v3/x/tax2gas/ante"

"github.com/cosmos/cosmos-sdk/codec"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
ibcante "github.com/cosmos/ibc-go/v7/modules/core/ante"
Expand Down Expand Up @@ -88,7 +90,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
// MinInitialDepositDecorator prevents submitting governance proposal low initial deposit
NewMinInitialDepositDecorator(options.GovKeeper, options.TreasuryKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
NewFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TreasuryKeeper),
tax2gasante.NewFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TreasuryKeeper),
dyncommante.NewDyncommDecorator(options.Cdc, options.DyncommKeeper, options.StakingKeeper),

// Do not add any other decorators below this point unless explicitly explain.
Expand Down
30 changes: 15 additions & 15 deletions custom/auth/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ import (

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/classic-terra/core/v3/custom/auth/ante"
core "github.com/classic-terra/core/v3/types"
markettypes "github.com/classic-terra/core/v3/x/market/types"
oracletypes "github.com/classic-terra/core/v3/x/oracle/types"
tax2gasante "github.com/classic-terra/core/v3/x/tax2gas/ante"
)

func (s *AnteTestSuite) TestDeductFeeDecorator_ZeroGas() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

mfd := ante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
mfd := tax2gasante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
antehandler := sdk.ChainAnteDecorators(mfd)

// keys and addresses
Expand Down Expand Up @@ -62,7 +62,7 @@ func (s *AnteTestSuite) TestEnsureMempoolFees() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

mfd := ante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
mfd := tax2gasante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
antehandler := sdk.ChainAnteDecorators(mfd)

// keys and addresses
Expand Down Expand Up @@ -146,7 +146,7 @@ func (s *AnteTestSuite) TestDeductFees() {
err = testutil.FundAccount(s.app.BankKeeper, s.ctx, addr1, coins)
s.Require().NoError(err)

dfd := ante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
dfd := tax2gasante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
antehandler := sdk.ChainAnteDecorators(dfd)

_, err = antehandler(s.ctx, tx, false)
Expand All @@ -167,7 +167,7 @@ func (s *AnteTestSuite) TestEnsureMempoolFeesSend() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

mfd := ante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
mfd := tax2gasante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
antehandler := sdk.ChainAnteDecorators(mfd)

// keys and addresses
Expand Down Expand Up @@ -221,7 +221,7 @@ func (s *AnteTestSuite) TestEnsureMempoolFeesSwapSend() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

mfd := ante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
mfd := tax2gasante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
antehandler := sdk.ChainAnteDecorators(mfd)

// keys and addresses
Expand Down Expand Up @@ -274,7 +274,7 @@ func (s *AnteTestSuite) TestEnsureMempoolFeesMultiSend() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

mfd := ante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
mfd := tax2gasante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
antehandler := sdk.ChainAnteDecorators(mfd)

// keys and addresses
Expand Down Expand Up @@ -341,7 +341,7 @@ func (s *AnteTestSuite) TestEnsureMempoolFeesInstantiateContract() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

mfd := ante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
mfd := tax2gasante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
antehandler := sdk.ChainAnteDecorators(mfd)

// keys and addresses
Expand Down Expand Up @@ -400,7 +400,7 @@ func (s *AnteTestSuite) TestEnsureMempoolFeesExecuteContract() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

mfd := ante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
mfd := tax2gasante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
antehandler := sdk.ChainAnteDecorators(mfd)

// keys and addresses
Expand Down Expand Up @@ -458,7 +458,7 @@ func (s *AnteTestSuite) TestEnsureMempoolFeesAuthzExec() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

mfd := ante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
mfd := tax2gasante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
antehandler := sdk.ChainAnteDecorators(mfd)

// keys and addresses
Expand Down Expand Up @@ -697,7 +697,7 @@ func (s *AnteTestSuite) TestTaxExemption() {
tk.AddBurnTaxExemptionAddress(s.ctx, addrs[0].String())
tk.AddBurnTaxExemptionAddress(s.ctx, addrs[1].String())

mfd := ante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
mfd := tax2gasante.NewFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, s.app.TreasuryKeeper)
antehandler := sdk.ChainAnteDecorators(mfd)

for i := 0; i < 4; i++ {
Expand Down Expand Up @@ -747,7 +747,7 @@ func (s *AnteTestSuite) runBurnSplitTaxTest(burnSplitRate sdk.Dec) {
ak := s.app.AccountKeeper
bk := s.app.BankKeeper
tk := s.app.TreasuryKeeper
mfd := ante.NewFeeDecorator(ak, bk, s.app.FeeGrantKeeper, tk)
mfd := tax2gasante.NewFeeDecorator(ak, bk, s.app.FeeGrantKeeper, tk)
antehandler := sdk.ChainAnteDecorators(mfd)

// Set burn split tax
Expand Down Expand Up @@ -804,7 +804,7 @@ func (s *AnteTestSuite) runBurnSplitTaxTest(burnSplitRate sdk.Dec) {
tk.BurnCoinsFromBurnAccount(s.ctx)

feeCollectorAfter := sdk.NewDecCoinsFromCoins(bk.GetAllBalances(s.ctx, ak.GetModuleAddress(authtypes.FeeCollectorName))...)
taxes := ante.FilterMsgAndComputeTax(s.ctx, tk, msg)
taxes := tax2gasante.FilterMsgAndComputeTax(s.ctx, tk, msg)
burnTax := sdk.NewDecCoinsFromCoins(taxes...)

if burnSplitRate.IsPositive() {
Expand Down Expand Up @@ -839,7 +839,7 @@ func (s *AnteTestSuite) TestEnsureIBCUntaxed() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

mfd := ante.NewFeeDecorator(
mfd := tax2gasante.NewFeeDecorator(
s.app.AccountKeeper,
s.app.BankKeeper,
s.app.FeeGrantKeeper,
Expand Down Expand Up @@ -888,7 +888,7 @@ func (s *AnteTestSuite) TestOracleZeroFee() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

mfd := ante.NewFeeDecorator(
mfd := tax2gasante.NewFeeDecorator(
s.app.AccountKeeper,
s.app.BankKeeper,
s.app.FeeGrantKeeper,
Expand Down
3 changes: 2 additions & 1 deletion custom/auth/tx/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"google.golang.org/grpc/status"

customante "github.com/classic-terra/core/v3/custom/auth/ante"
tax2gasante "github.com/classic-terra/core/v3/x/tax2gas/ante"

"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -52,7 +53,7 @@ func (ts txServer) ComputeTax(c context.Context, req *ComputeTaxRequest) (*Compu
return nil, status.Errorf(codes.InvalidArgument, "empty txBytes is not allowed")
}

taxAmount := customante.FilterMsgAndComputeTax(ctx, ts.treasuryKeeper, msgs...)
taxAmount := tax2gasante.FilterMsgAndComputeTax(ctx, ts.treasuryKeeper, msgs...)
return &ComputeTaxResponse{
TaxAmount: taxAmount,
}, nil
Expand Down
2 changes: 1 addition & 1 deletion custom/wasm/keeper/handler_plugin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package keeper

import (
"github.com/classic-terra/core/v3/custom/auth/ante"
"github.com/classic-terra/core/v3/x/tax2gas/ante"
treasurykeeper "github.com/classic-terra/core/v3/x/treasury/keeper"

wasmvmtypes "github.com/CosmWasm/wasmvm/types"
Expand Down
16 changes: 16 additions & 0 deletions proto/terra/tax2gas/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";
package terra.tax2gas.v1beta1;

import "gogoproto/gogo.proto";
import "google/protobuf/any.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/classic-terra/core/v3/x/tax2gas/types";

message Params {}

// GenesisState defines the tax2gas module's genesis state.
message GenesisState {
// params is the container of tax2gas parameters.
Params params = 1 [ (gogoproto.nullable) = false ];
}
19 changes: 19 additions & 0 deletions proto/terra/tax2gas/v1beta1/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto3";
package terra.tax2gas.v1beta1;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "terra/tax2gas/v1beta1/genesis.proto";
import "terra/tax2gas/v1beta1/tx.proto";

option go_package = "github.com/classic-terra/core/v3/x/tax2gas/types";

service Query {
rpc Params(ParamsRequest) returns (ParamsResponse) {
option (google.api.http).get = "/terra/tax2gas/Params";
}
}

//=============================== Params
message ParamsRequest {}
message ParamsResponse { Params params = 1 [ (gogoproto.nullable) = false ]; }
18 changes: 18 additions & 0 deletions proto/terra/tax2gas/v1beta1/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";
package terra.tax2gas.v1beta1;

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/classic-terra/core/v3/x/tax2gas/types";

service Msg {
rpc Test(MsgTest) returns (MsgTestResponse);
}

// ===================== MsgTest
message MsgTest {
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
}

message MsgTestResponse {}
5 changes: 1 addition & 4 deletions scripts/protocgen.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#!/usr/bin/env sh

set -eo pipefail

# get protoc executions
go get github.com/regen-network/cosmos-proto/protoc-gen-gocosmos 2>/dev/null
set -eux pipefail

echo "Generating gogo proto code"
cd proto
Expand Down
18 changes: 10 additions & 8 deletions custom/auth/ante/fee.go → x/tax2gas/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/classic-terra/core/v3/x/tax2gas/types"
)

// FeeDecorator deducts fees from the first signer of the tx
Expand All @@ -17,12 +19,12 @@ import (
// CONTRACT: Tx must implement FeeTx interface to use DeductFeeDecorator
type FeeDecorator struct {
accountKeeper ante.AccountKeeper
bankKeeper BankKeeper
bankKeeper types.BankKeeper
feegrantKeeper ante.FeegrantKeeper
treasuryKeeper TreasuryKeeper
treasuryKeeper types.TreasuryKeeper
}

func NewFeeDecorator(ak ante.AccountKeeper, bk BankKeeper, fk ante.FeegrantKeeper, tk TreasuryKeeper) FeeDecorator {
func NewFeeDecorator(ak ante.AccountKeeper, bk types.BankKeeper, fk ante.FeegrantKeeper, tk types.TreasuryKeeper) FeeDecorator {
return FeeDecorator{
accountKeeper: ak,
bankKeeper: bk,
Expand Down Expand Up @@ -67,8 +69,8 @@ func (fd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, nex
}

func (fd FeeDecorator) checkDeductFee(ctx sdk.Context, feeTx sdk.FeeTx, taxes sdk.Coins, simulate bool) error {
if addr := fd.accountKeeper.GetModuleAddress(types.FeeCollectorName); addr == nil {
return fmt.Errorf("fee collector module account (%s) has not been set", types.FeeCollectorName)
if addr := fd.accountKeeper.GetModuleAddress(authtypes.FeeCollectorName); addr == nil {
return fmt.Errorf("fee collector module account (%s) has not been set", authtypes.FeeCollectorName)
}

fee := feeTx.GetFee()
Expand Down Expand Up @@ -127,12 +129,12 @@ func (fd FeeDecorator) checkDeductFee(ctx sdk.Context, feeTx sdk.FeeTx, taxes sd
}

// DeductFees deducts fees from the given account.
func DeductFees(bankKeeper types.BankKeeper, ctx sdk.Context, acc types.AccountI, fees sdk.Coins) error {
func DeductFees(bankKeeper authtypes.BankKeeper, ctx sdk.Context, acc authtypes.AccountI, fees sdk.Coins) error {
if !fees.IsValid() {
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "invalid fee amount: %s", fees)
}

err := bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), types.FeeCollectorName, fees)
err := bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), authtypes.FeeCollectorName, fees)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions custom/auth/ante/fee_tax.go → x/tax2gas/ante/fee_tax.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

marketexported "github.com/classic-terra/core/v3/x/market/exported"
oracleexported "github.com/classic-terra/core/v3/x/oracle/exported"
"github.com/classic-terra/core/v3/x/tax2gas/types"
)

var IBCRegexp = regexp.MustCompile("^ibc/[a-fA-F0-9]{64}$")
Expand All @@ -20,7 +21,7 @@ func isIBCDenom(denom string) bool {
}

// FilterMsgAndComputeTax computes the stability tax on messages.
func FilterMsgAndComputeTax(ctx sdk.Context, tk TreasuryKeeper, msgs ...sdk.Msg) sdk.Coins {
func FilterMsgAndComputeTax(ctx sdk.Context, tk types.TreasuryKeeper, msgs ...sdk.Msg) sdk.Coins {
taxes := sdk.Coins{}

for _, msg := range msgs {
Expand Down Expand Up @@ -77,7 +78,7 @@ func FilterMsgAndComputeTax(ctx sdk.Context, tk TreasuryKeeper, msgs ...sdk.Msg)
}

// computes the stability tax according to tax-rate and tax-cap
func computeTax(ctx sdk.Context, tk TreasuryKeeper, principal sdk.Coins) sdk.Coins {
func computeTax(ctx sdk.Context, tk types.TreasuryKeeper, principal sdk.Coins) sdk.Coins {
taxRate := tk.GetTaxRate(ctx)
if taxRate.Equal(sdk.ZeroDec()) {
return sdk.Coins{}
Expand Down
60 changes: 60 additions & 0 deletions x/tax2gas/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cli

import (
"context"
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"

"github.com/classic-terra/core/v3/x/tax2gas/types"
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
tax2gasQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

tax2gasQueryCmd.AddCommand(
GetCmdQueryParams(),
)

return tax2gasQueryCmd
}


Check failure on line 32 in x/tax2gas/client/cli/query.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
// GetCmdQueryParams implements a command to return the current parameters.
func GetCmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "Query the current tax2gas module parameters",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

params := &types.ParamsRequest{}

res, err := queryClient.Params(context.Background(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(&res.Params)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
Loading

0 comments on commit 419ce91

Please sign in to comment.