diff --git a/CHANGELOG.md b/CHANGELOG.md index 966b9c28e2..b4563eedc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/foundation) [\#912](https://github.com/line/lbm-sdk/pull/912) Introduce censorship into x/foundation * (x/foundation) [\#933](https://github.com/line/lbm-sdk/pull/933) Clean up x/foundation apis * (x/collection) [\#938](https://github.com/line/lbm-sdk/pull/938) Add progress log into x/collection import-genesis +* (x/foundation) [\#952](https://github.com/line/lbm-sdk/pull/952) Address generation of the empty coins in x/foundation ### Bug Fixes * (swagger) [\#898](https://github.com/line/lbm-sdk/pull/898) fix a bug not added `lbm.tx.v1beta1.Service/GetBlockWithTxs` in swagger diff --git a/client/grpc/tmservice/service_test.go b/client/grpc/tmservice/service_test.go index 8873f19ade..6e72fc4ed4 100644 --- a/client/grpc/tmservice/service_test.go +++ b/client/grpc/tmservice/service_test.go @@ -152,7 +152,7 @@ func (s IntegrationTestSuite) TestQueryBlockResultsByHeight() { s.Require().Equal(0, len(txResult)) beginBlock := blockResultsRes.GetResBeginBlock() - s.Require().Equal(11, len(beginBlock.Events)) // coinbase event (6) + transfer mintModule to feeCollectorName(5) + s.Require().Equal(7, len(beginBlock.Events)) // coinbase event (6) + transfer mintModule to feeCollectorName(5) - foundation abci (4) endBlock := blockResultsRes.GetResEndBlock() s.Require().Equal(0, len(endBlock.Events)) diff --git a/x/foundation/keeper/internal/abci_test.go b/x/foundation/keeper/internal/abci_test.go index 274c5e0375..0af7903346 100644 --- a/x/foundation/keeper/internal/abci_test.go +++ b/x/foundation/keeper/internal/abci_test.go @@ -7,28 +7,36 @@ import ( ) func (s *KeeperTestSuite) TestBeginBlocker() { - ctx, _ := s.ctx.CacheContext() - - taxRatio := sdk.MustNewDecFromStr("0.123456789") - s.impl.SetParams(ctx, foundation.Params{ - FoundationTax: taxRatio, - }) - - before := s.impl.GetTreasury(ctx) - s.Require().Equal(1, len(before)) - s.Require().Equal(sdk.NewDecFromInt(s.balance), before[0].Amount) - - // collect - internal.BeginBlocker(ctx, s.impl) + for name, tc := range map[string]struct { + taxRatio sdk.Dec + valid bool + }{ + "valid ratio": { + taxRatio: sdk.OneDec(), + valid: true, + }, + "ratio > 1": { + taxRatio: sdk.MustNewDecFromStr("1.00000001"), + }, + } { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() - tax := sdk.NewDecFromInt(s.balance).MulTruncate(taxRatio).TruncateInt() - // ensure the behavior does not change - s.Require().Equal(sdk.NewInt(121932631), tax) + s.impl.SetParams(ctx, foundation.Params{ + FoundationTax: tc.taxRatio, + }) - expectedAfter := s.balance.Add(tax) - after := s.impl.GetTreasury(ctx) - s.Require().Equal(1, len(after)) - s.Require().Equal(sdk.NewDecFromInt(expectedAfter), after[0].Amount) + // collect + testing := func() { + internal.BeginBlocker(ctx, s.impl) + } + if tc.valid { + s.Require().NotPanics(testing) + } else { + s.Require().Panics(testing) + } + }) + } } func (s *KeeperTestSuite) TestEndBlocker() { diff --git a/x/foundation/keeper/internal/treasury.go b/x/foundation/keeper/internal/treasury.go index fb2d1457f2..7398f34992 100644 --- a/x/foundation/keeper/internal/treasury.go +++ b/x/foundation/keeper/internal/treasury.go @@ -9,11 +9,17 @@ import ( func (k Keeper) CollectFoundationTax(ctx sdk.Context) error { feeCollector := k.authKeeper.GetModuleAccount(ctx, k.feeCollectorName).GetAddress() feesCollectedInt := k.bankKeeper.GetAllBalances(ctx, feeCollector) + if feesCollectedInt.Empty() { + return nil + } feesCollected := sdk.NewDecCoinsFromCoins(feesCollectedInt...) // calculate the tax taxRatio := k.GetFoundationTax(ctx) tax, _ := feesCollected.MulDecTruncate(taxRatio).TruncateDecimal() + if tax.Empty() { + return nil + } // collect the tax if err := k.FundTreasury(ctx, feeCollector, tax); err != nil { diff --git a/x/foundation/keeper/internal/treasury_test.go b/x/foundation/keeper/internal/treasury_test.go index 59e729e375..717173d94e 100644 --- a/x/foundation/keeper/internal/treasury_test.go +++ b/x/foundation/keeper/internal/treasury_test.go @@ -6,6 +6,80 @@ import ( "github.com/line/lbm-sdk/x/foundation" ) +func (s *KeeperTestSuite) TestCollectFoundationTax() { + ctx, _ := s.ctx.CacheContext() + + // empty fee collector first + // send the fee to the stranger + // and get it back later if the test case requires + collector := authtypes.NewModuleAddress(authtypes.FeeCollectorName) + fees := s.bankKeeper.GetAllBalances(ctx, collector) + s.bankKeeper.SendCoinsFromModuleToAccount(ctx, authtypes.FeeCollectorName, s.stranger, fees) + + for name, tc := range map[string]struct { + fee sdk.Int + taxRatio sdk.Dec + tax sdk.Int + valid bool + }{ + "common": { + fee: fees[0].Amount, + taxRatio: sdk.MustNewDecFromStr("0.123456789"), + tax: sdk.NewInt(121932631), + valid: true, + }, + "zero fee": { + fee: sdk.ZeroInt(), + taxRatio: sdk.MustNewDecFromStr("0.123456789"), + tax: sdk.ZeroInt(), + valid: true, + }, + "zero ratio": { + fee: fees[0].Amount, + taxRatio: sdk.ZeroDec(), + tax: sdk.ZeroInt(), + valid: true, + }, + "send fails": { + fee: fees[0].Amount, + taxRatio: sdk.MustNewDecFromStr("1.00000001"), + tax: sdk.NewInt(987654330), + }, + } { + s.Run(name, func() { + ctx, _ := ctx.CacheContext() + + // set fee + s.bankKeeper.SendCoinsFromAccountToModule(ctx, s.stranger, authtypes.FeeCollectorName, sdk.NewCoins(sdk.NewCoin(fees[0].Denom, tc.fee))) + + // set tax ratio + s.impl.SetParams(ctx, foundation.Params{ + FoundationTax: tc.taxRatio, + }) + + before := s.impl.GetTreasury(ctx) + s.Require().Equal(1, len(before)) + s.Require().Equal(sdk.NewDecFromInt(s.balance), before[0].Amount) + + tax := sdk.NewDecFromInt(tc.fee).MulTruncate(tc.taxRatio).TruncateInt() + // ensure the behavior does not change + s.Require().Equal(tc.tax, tax) + + err := s.impl.CollectFoundationTax(ctx) + if !tc.valid { + s.Require().Error(err) + return + } + s.Require().NoError(err) + + expectedAfter := s.balance.Add(tax) + after := s.impl.GetTreasury(ctx) + s.Require().Equal(1, len(after)) + s.Require().Equal(sdk.NewDecFromInt(expectedAfter), after[0].Amount) + }) + } +} + func (s *KeeperTestSuite) TestFundTreasury() { testCases := map[string]struct { amount sdk.Int