Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
TropicalDog17 committed Oct 4, 2024
1 parent f92bc5b commit c2014cc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
53 changes: 39 additions & 14 deletions x/tax2gas/post/fee_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package post_test

import (
"cosmossdk.io/math"
oracle "github.com/classic-terra/core/v3/x/oracle/types"
"github.com/classic-terra/core/v3/x/tax2gas/post"
tax2gastypes "github.com/classic-terra/core/v3/x/tax2gas/types"
"github.com/classic-terra/core/v3/x/tax2gas/utils"
"github.com/classic-terra/core/v3/x/treasury/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -27,30 +29,48 @@ func (s *PostTestSuite) TestDeductFeeDecorator() {
tx, err := s.CreateTestTx(privs, accNums, accSeqs, s.ctx.ChainID())
s.Require().NoError(err)

burnTaxRate := s.app.Tax2gasKeeper.GetBurnTaxRate(s.ctx)
lunaGasPrice := sdk.NewDecCoinFromDec("uluna", sdk.NewDecWithPrec(28325, 3))
testCases := []struct {
name string
simulation bool
checkTx bool
mallate func()
feeAmount sdk.Coins
expFail bool
expErrMsg string
name string
simulation bool
checkTx bool
mallate func()
feeAmount sdk.Coins
expectedOracle sdk.Coins
expectedCp sdk.Coins
expectedBurn sdk.Coins
expFail bool
expErrMsg string
}{
{name: "happy case",
{
name: "happy case",
simulation: false,
checkTx: false,
mallate: func() {
msg := banktypes.NewMsgSend(addr1, addr1, sdk.NewCoins(sdk.NewCoin("uluna", sdk.NewInt(100000000))))
gm := tax2gastypes.NewTax2GasMeter(s.ctx.GasMeter().Limit(), false)
gm.(*tax2gastypes.Tax2GasMeter).ConsumeTax(math.NewInt(17778), "tax")
tax := utils.FilterMsgAndComputeTax(s.ctx, s.app.TreasuryKeeper, burnTaxRate, msg)
taxDecCoin := sdk.NewDecFromInt(tax.AmountOf("uluna"))
gm.(*tax2gastypes.Tax2GasMeter).ConsumeTax(taxDecCoin.Quo(lunaGasPrice.Amount).RoundInt(), "tax")
s.ctx = s.ctx.WithGasMeter(gm)
s.ctx = s.ctx.WithValue(tax2gastypes.AnteConsumedGas, uint64(1000))
s.ctx = s.ctx.WithValue(tax2gastypes.AnteConsumedGas, uint64(7328))
s.Require().NoError(s.txBuilder.SetMsgs(msg))
s.txBuilder.SetGasLimit(100000)
s.ctx = s.ctx.WithValue(tax2gastypes.PaidDenom, "uluna")
},
feeAmount: sdk.NewCoins(sdk.NewInt64Coin("uluna", 100000)),
expFail: false,
// amount: 100000000
// tax(0.5%): 499993
// use default value
// burn: tax * 0.9
// distributtion: tax * 0.1 = 49999
// cp: 2% of distribution: 499994 * 0.1 * 0.02 ~ 1000
// oracle: distribution - oracle = 48999
feeAmount: sdk.NewCoins(sdk.NewInt64Coin("uluna", 711128)),
expFail: false,
expectedBurn: sdk.NewCoins(sdk.NewInt64Coin("uluna", 449994)),
expectedCp: sdk.NewCoins(sdk.NewInt64Coin("uluna", 1000)),
expectedOracle: sdk.NewCoins(sdk.NewInt64Coin("uluna", 48999)),
},
}

Expand All @@ -62,12 +82,17 @@ func (s *PostTestSuite) TestDeductFeeDecorator() {
s.txBuilder.SetFeeAmount(tc.feeAmount)

_, err = posthandler(s.ctx, tx, tc.simulation, true)

currentOracle := s.app.BankKeeper.GetBalance(s.ctx, s.app.AccountKeeper.GetModuleAddress(oracle.ModuleName), "uluna")
currentCp := s.app.DistrKeeper.GetFeePoolCommunityCoins(s.ctx).AmountOf("uluna")
currentBurn := s.app.BankKeeper.GetBalance(s.ctx, s.app.AccountKeeper.GetModuleAddress(types.BurnModuleName), "uluna")
if tc.expFail {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expErrMsg)
} else {
s.Require().NoError(err)
s.Require().Equal(tc.expectedOracle[0].Amount.Int64(), currentOracle.Amount.Int64())
s.Require().Equal(tc.expectedCp[0].Amount.Int64(), currentCp.RoundInt64())
s.Require().Equal(tc.expectedBurn[0].Amount.Int64(), currentBurn.Amount.Int64())
}
})
}
Expand Down
5 changes: 4 additions & 1 deletion x/tax2gas/post/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func (tgd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate

if !simulate {
// Deduct feeCoins with paid amount
if paidAmount.Ceil().RoundInt().Int64() > feeCoins.AmountOf(paidDenom).Int64() {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "insufficient fee: %s", feeCoins)
}
feeCoins = feeCoins.Sub(sdk.NewCoin(paidDenom, paidAmount.Ceil().RoundInt()))
}

Expand Down Expand Up @@ -155,7 +158,7 @@ func (tgd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate

// First, we will deduct the fees covered taxGas and handle BurnTaxSplit
taxes, payableFees, gasRemaining := tax2gasutils.CalculateTaxesAndPayableFee(gasPrices, feeCoins, taxGas, totalGasRemaining)
if !simulate && !ctx.IsCheckTx() && gasRemaining.IsPositive() {
if !simulate && gasRemaining.IsPositive() {
gasRemainingFees, err := tax2gasutils.ComputeFeesOnGasConsumed(tx, gasPrices, gasRemaining)
if err != nil {
return ctx, err
Expand Down

0 comments on commit c2014cc

Please sign in to comment.