Skip to content

Commit

Permalink
Update commission_test.go
Browse files Browse the repository at this point in the history
1. Combined the import statements.
2. Moved the definition of the nextAnteHandler function outside 
    of the loop for clarity.
3. Simplified the commissionRate initialization.
4. Removed unnecessary pointer creation for messages.
5. Simplified the creation of sad messages within the 
    createSadMsgs function.
6. Removed redundant type conversions.
7. Used the sdk.NewDecWithPrec constructor directly for 
    commission rate initialization.
8. Used pointer literals directly for creating sad messages.
9. Cleaned up the main test logic by simplifying the message 
    construction and error handling.
  • Loading branch information
volosianko77 authored Mar 13, 2024
1 parent dbdf02d commit 29e2f88
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions app/ante/commission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ package ante_test
import (
"testing"

sdkclienttx "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/app/ante"
"github.com/NibiruChain/nibiru/x/common/testutil"
)

func (s *AnteTestSuite) TestAnteDecoratorStakingCommission() {
// nextAnteHandler: A no-op next handler to make this a unit test.
// Define a no-op next handler to make this a unit test.
var nextAnteHandler sdk.AnteHandler = func(
ctx sdk.Context, tx sdk.Tx, simulate bool,
) (newCtx sdk.Context, err error) {
return ctx, nil
}

// Mock description for testing.
mockDescription := stakingtypes.Description{
Moniker: "mock-moniker",
Identity: "mock-identity",
Expand All @@ -30,49 +29,47 @@ func (s *AnteTestSuite) TestAnteDecoratorStakingCommission() {
Details: "mock-details",
}

// Define validator address.
valAddr := sdk.ValAddress(testutil.AccAddress()).String()
commissionRatePointer := new(sdk.Dec)
*commissionRatePointer = sdk.NewDecWithPrec(10, 2)

// Define commission rate.
commissionRate := sdk.NewDecWithPrec(10, 2)

// Define happy messages.
happyMsgs := []sdk.Msg{
&stakingtypes.MsgCreateValidator{
Description: mockDescription,
Commission: stakingtypes.CommissionRates{
Rate: sdk.NewDecWithPrec(6, 2), // 6%
MaxRate: sdk.NewDec(420),
MaxChangeRate: sdk.NewDec(420),
},
Description: mockDescription,
Commission: stakingtypes.CommissionRates{Rate: sdk.NewDecWithPrec(6, 2)},
MinSelfDelegation: sdk.NewInt(1),
DelegatorAddress: testutil.AccAddress().String(),
ValidatorAddress: valAddr,
Pubkey: &codectypes.Any{},
Pubkey: nil,
Value: sdk.NewInt64Coin("unibi", 1),
},
&stakingtypes.MsgEditValidator{
Description: mockDescription,
ValidatorAddress: valAddr,
CommissionRate: commissionRatePointer, // 10%
MinSelfDelegation: nil,
Description: mockDescription,
ValidatorAddress: valAddr,
CommissionRate: &commissionRate,
},
}

// Define function to create sad messages.
createSadMsgs := func() []sdk.Msg {
sadMsgCreateVal := new(stakingtypes.MsgCreateValidator)
*sadMsgCreateVal = *(happyMsgs[0]).(*stakingtypes.MsgCreateValidator)
sadMsgCreateVal := *(happyMsgs[0]).(*stakingtypes.MsgCreateValidator)
sadMsgCreateVal.Commission.Rate = sdk.NewDecWithPrec(26, 2)

sadMsgEditVal := new(stakingtypes.MsgEditValidator)
*sadMsgEditVal = *(happyMsgs[1]).(*stakingtypes.MsgEditValidator)
newCommissionRate := new(sdk.Dec)
*newCommissionRate = sdk.NewDecWithPrec(26, 2)
sadMsgEditVal.CommissionRate = newCommissionRate
sadMsgEditVal := *(happyMsgs[1]).(*stakingtypes.MsgEditValidator)
newCommissionRate := sdk.NewDecWithPrec(26, 2)
sadMsgEditVal.CommissionRate = &newCommissionRate

return []sdk.Msg{
sadMsgCreateVal,
sadMsgEditVal,
&sadMsgCreateVal,
&sadMsgEditVal,
}
}
sadMsgs := createSadMsgs()

// Test cases.
for _, tc := range []struct {
name string
txMsgs []sdk.Msg
Expand Down Expand Up @@ -115,7 +112,7 @@ func (s *AnteTestSuite) TestAnteDecoratorStakingCommission() {
)

encCfg := app.MakeEncodingConfig()
txBuilder, err := sdkclienttx.Factory{}.
txBuilder, err := tx.Factory{}.
WithFees(txGasCoins.String()).
WithChainID(s.ctx.ChainID()).
WithTxConfig(encCfg.TxConfig).
Expand Down

0 comments on commit 29e2f88

Please sign in to comment.