Skip to content

Commit

Permalink
feat(community): consolidate community funds (#1729)
Browse files Browse the repository at this point in the history
* Add consolidate methods

* Update distr feepool balance with dust, add tests

* Set params for proposal handler to not influence module balances

* Add StakingRewardsPerSecond param for proposal test

* Update changelog

* Update test to check emitted events

* Log dust amounts for x/distribution

* Modify feepool communitypool field instead of entire replacement

* Update tests to include cases with empty balances

* Move EventsContains to app

* Remove extra copied ModuleName

* Add Require() to incentive claims in tests to reduce errors

* Move consolidate tests to testutil

* Only transfer non-ukava coins

* Add DefaultStakingRewardsState to proposal handler test

* Move event emit before consolidate

* add golangci specific timeout

---------

Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
  • Loading branch information
drklee3 and nddeluca authored Oct 20, 2023
1 parent 395b69a commit 8186367
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ jobs:
with:
github_token: ${{ secrets.github_token }}
reporter: github-pr-review
golangci_lint_flags: --timeout 10m
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (community) [#1704] Add param to control when inflation will be disabled
- (community) [#1707] Default staking rewards per second set to `744191`
- (community) [#1706] Add disable inflation upgrade to begin blocker that updates x/mint and x/kavadist params.
- (community) [#1729] Consolidate community funds from `x/distribution` and `x/kavadist` to `x/community`

## [v0.24.0]

Expand Down Expand Up @@ -293,6 +294,7 @@ the [changelog](https://github.com/cosmos/cosmos-sdk/blob/v0.38.4/CHANGELOG.md).
- [#257](https://github.com/Kava-Labs/kava/pulls/257) Include scripts to run
large-scale simulations remotely using aws-batch

[#1729]: https://github.com/Kava-Labs/kava/pull/1729
[#1745]: https://github.com/Kava-Labs/kava/pull/1745
[#1707]: https://github.com/Kava-Labs/kava/pull/1707
[#1706]: https://github.com/Kava-Labs/kava/pull/1706
Expand Down
104 changes: 104 additions & 0 deletions x/community/keeper/consolidate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/community/types"

kavadisttypes "github.com/kava-labs/kava/x/kavadist/types"

distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
)

// StartCommunityFundConsolidation consolidates the community funds from
// x/distribution and x/kavadist into the x/community module account
func (k Keeper) StartCommunityFundConsolidation(ctx sdk.Context) error {
logger := k.Logger(ctx)
logger.Info("community fund consolidation upgrade started")

// Consolidate x/distribution community pool
if err := k.consolidateCommunityDistribution(ctx); err != nil {
return err
}

// Consolidate x/kavadist account
if err := k.consolidateCommunityKavadist(ctx); err != nil {
return err
}

// Log new x/community balance
communityCoins := k.GetModuleAccountBalance(ctx)
logger.Info(fmt.Sprintf("community funds consolidated, x/community balance is now %s", communityCoins))

return nil
}

// consolidateCommunityDistribution transfers all coins from the x/distribution
// community pool to the x/community module account
func (k Keeper) consolidateCommunityDistribution(ctx sdk.Context) error {
logger := k.Logger(ctx)

// Get community coins with leftover leftoverDust
truncatedCoins, leftoverDust := k.distrKeeper.
GetFeePoolCommunityCoins(ctx).
TruncateDecimal()

// Transfer to x/community
err := k.bankKeeper.SendCoinsFromModuleToModule(
ctx,
distrtypes.ModuleName, // sender
types.ModuleName, // recipient
truncatedCoins,
)
if err != nil {
return fmt.Errorf("failed to transfer x/distribution coins to x/community: %w", err)
}

logger.Info(fmt.Sprintf("transferred %s from x/distribution to x/community", truncatedCoins))

// Set x/distribution community pool to remaining dust amounts
feePool := k.distrKeeper.GetFeePool(ctx)
feePool.CommunityPool = leftoverDust
k.distrKeeper.SetFeePool(ctx, feePool)

logger.Info(fmt.Sprintf("remaining x/distribution community pool dust: %s", leftoverDust))

return nil
}

// consolidateCommunityKavadist transfers all coins from the x/kavadist module
// account to the x/community module account
func (k Keeper) consolidateCommunityKavadist(ctx sdk.Context) error {
logger := k.Logger(ctx)

kavadistAcc := k.accountKeeper.GetModuleAccount(ctx, kavadisttypes.KavaDistMacc)
transferCoins := k.bankKeeper.GetAllBalances(ctx, kavadistAcc.GetAddress())

// Remove ukava from transfer coins - ony transfer non-ukava coins
found, kavaCoins := transferCoins.Find("ukava")
if found {
transferCoins = transferCoins.Sub(kavaCoins)
}

// Transfer remaining coins to x/community
err := k.bankKeeper.SendCoinsFromModuleToModule(
ctx,
kavadisttypes.ModuleName, // sender
types.ModuleName, // recipient
transferCoins,
)
if err != nil {
return fmt.Errorf("failed to transfer x/kavadist coins to x/community: %w", err)
}

kavadistRemainingCoins := k.bankKeeper.GetAllBalances(ctx, kavadistAcc.GetAddress())

logger.Info(fmt.Sprintf(
"transferred %s from x/kavadist to x/community, remaining x/kavadist balance: %s",
transferCoins,
kavadistRemainingCoins,
))

return nil
}
14 changes: 9 additions & 5 deletions x/community/keeper/disable_inflation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ func (k Keeper) CheckAndDisableMintAndKavaDistInflation(ctx sdk.Context) {
// run disable inflation logic
k.disableInflation(ctx)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeInflationStop,
),
)

// reset disable inflation time to ensure next call is a no-op
params.UpgradeTimeDisableInflation = time.Time{}
// set staking rewards to provided intial value
params.StakingRewardsPerSecond = params.UpgradeTimeSetStakingRewardsPerSecond
k.SetParams(ctx, params)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeInflationStop,
),
)
if err := k.StartCommunityFundConsolidation(ctx); err != nil {
panic(err)
}
}

// TODO: double check this is correct method for disabling inflation in kavadist without
Expand Down
2 changes: 2 additions & 0 deletions x/community/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Keeper struct {
key storetypes.StoreKey
cdc codec.Codec

accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
cdpKeeper types.CdpKeeper
distrKeeper types.DistributionKeeper
Expand Down Expand Up @@ -63,6 +64,7 @@ func NewKeeper(
key: key,
cdc: cdc,

accountKeeper: ak,
bankKeeper: bk,
cdpKeeper: ck,
distrKeeper: dk,
Expand Down
12 changes: 12 additions & 0 deletions x/community/keeper/proposal_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
abcitypes "github.com/tendermint/tendermint/abci/types"
Expand All @@ -27,9 +28,11 @@ func c(denom string, amount int64) sdk.Coin { return sdk.NewInt64Coin(denom, amo
func ukava(amt int64) sdk.Coins {
return sdk.NewCoins(c("ukava", amt))
}

func usdx(amt int64) sdk.Coins {
return sdk.NewCoins(c("usdx", amt))
}

func otherdenom(amt int64) sdk.Coins {
return sdk.NewCoins(c("other-denom", amt))
}
Expand Down Expand Up @@ -67,10 +70,19 @@ func (suite *proposalTestSuite) SetupTest() {
ChainID: chainID,
})

// Set UpgradeTimeDisableInflation to far future to not influence module
// account balances
params := types.Params{
UpgradeTimeDisableInflation: time.Now().Add(100000 * time.Hour),
StakingRewardsPerSecond: sdkmath.LegacyNewDec(0),
}
communityGs := types.NewGenesisState(params, types.DefaultStakingRewardsState())

tApp.InitializeFromGenesisStatesWithTimeAndChainID(
genTime, chainID,
app.GenesisState{hardtypes.ModuleName: tApp.AppCodec().MustMarshalJSON(&hardGS)},
app.GenesisState{pricefeedtypes.ModuleName: tApp.AppCodec().MustMarshalJSON(&pricefeedGS)},
app.GenesisState{types.ModuleName: tApp.AppCodec().MustMarshalJSON(&communityGs)},
testutil.NewCDPGenState(tApp.AppCodec(), "ukava", "kava", sdk.NewDec(2)),
)

Expand Down
4 changes: 0 additions & 4 deletions x/community/migrations/v2/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"github.com/kava-labs/kava/x/community/types"
)

const (
ModuleName = "mint"
)

// Migrate migrates the x/community module state from the consensus version 1 to
// version 2. Specifically, sets new parameters in the module state.
func Migrate(
Expand Down
Loading

0 comments on commit 8186367

Please sign in to comment.