Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add inflation events detailed distribution #1695

Merged
merged 8 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* [#1690](https://github.com/NibiruChain/nibiru/pull/1690) - docs(CHANGELOG.md): Correct the change log, providing clarity on what's released.
* [#1679](https://github.com/NibiruChain/nibiru/pull/1679) - test(perp): add more tests for perp module msg server
* [#1695](https://github.com/NibiruChain/nibiru/pull/1695) - feat(inflation): add events for inflation distribution
* [#1695](https://github.com/NibiruChain/nibiru/pull/1695) - fix(sudo): Make blank sudoers root invalid at genesis time.

### Dependencies

Expand Down
1 change: 1 addition & 0 deletions app/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func SetupNibiruTestingApp() (
// Create genesis state
encCdc := app.MakeEncodingConfig()
genesisState := app.NewDefaultGenesisState(encCdc.Marshaler)
testapp.SetDefaultSudoGenesis(genesisState)

return nibiruApp, genesisState
}
Expand Down
26 changes: 26 additions & 0 deletions proto/nibiru/inflation/v1/event.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
syntax = "proto3";
package nibiru.inflation.v1;

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

option go_package = "github.com/NibiruChain/nibiru/x/inflation/types";

// EventInflationDistribution: Emitted when NIBI tokens are minted on the
// network based on Nibiru's inflation schedule.
message EventInflationDistribution {
cosmos.base.v1beta1.Coin staking_rewards = 1 [
(gogoproto.moretags) = "yaml:\"staking_rewards\"",
(gogoproto.nullable) = false
];

cosmos.base.v1beta1.Coin strategic_reserve = 2 [
(gogoproto.moretags) = "yaml:\"strategic_reserve\"",
(gogoproto.nullable) = false
];

cosmos.base.v1beta1.Coin community_pool = 3 [
(gogoproto.moretags) = "yaml:\"community_pool\"",
(gogoproto.nullable) = false
];
}
4 changes: 2 additions & 2 deletions wasmbinding/test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/NibiruChain/nibiru/x/common/testutil"
testutilcli "github.com/NibiruChain/nibiru/x/common/testutil/cli"
"github.com/NibiruChain/nibiru/x/common/testutil/genesis"
"github.com/NibiruChain/nibiru/x/common/testutil/testapp"
epochstypes "github.com/NibiruChain/nibiru/x/epochs/types"
perpv2types "github.com/NibiruChain/nibiru/x/perp/v2/types"
)
Expand All @@ -41,8 +42,7 @@ type IntegrationTestSuite struct {

func (s *IntegrationTestSuite) SetupSuite() {
testutil.BeforeIntegrationSuite(s.T())

app.SetPrefixes(app.AccountAddressPrefix)
testapp.EnsureNibiruPrefix()

encodingConfig := app.MakeEncodingConfig()
genesisState := genesis.NewTestGenesisState(encodingConfig)
Expand Down
3 changes: 3 additions & 0 deletions x/common/testutil/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

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

/*
Expand All @@ -28,5 +29,7 @@ func NewTestGenesisState(encodingConfig app.EncodingConfig) app.GenesisState {
govGenState.Params.MinDeposit = sdk.NewCoins(sdk.NewInt64Coin(denoms.NIBI, 1_000_000)) // min deposit of 1 NIBI
genState[gov.ModuleName] = codec.MustMarshalJSON(&govGenState)

testapp.SetDefaultSudoGenesis(genState)

return genState
}
26 changes: 26 additions & 0 deletions x/common/testutil/testapp/testapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
sudotypes "github.com/NibiruChain/nibiru/x/sudo/types"
)

func init() {
EnsureNibiruPrefix()
}

// NewNibiruTestAppAndContext creates an 'app.NibiruApp' instance with an
// in-memory 'tmdb.MemDB' and fresh 'sdk.Context'.
func NewNibiruTestAppAndContext() (*app.NibiruApp, sdk.Context) {
Expand All @@ -32,9 +36,17 @@ func NewNibiruTestAppAndContext() (*app.NibiruApp, sdk.Context) {
encoding := app.MakeEncodingConfig()
var appGenesis app.GenesisState = app.NewDefaultGenesisState(encoding.Marshaler)
genModEpochs := epochstypes.DefaultGenesisFromTime(time.Now().UTC())

// Set happy genesis: epochs
appGenesis[epochstypes.ModuleName] = encoding.Marshaler.MustMarshalJSON(
genModEpochs,
)

// Set happy genesis: sudo
sudoGenesis := new(sudotypes.GenesisState)
sudoGenesis.Sudoers = DefaultSudoers()
appGenesis[sudotypes.ModuleName] = encoding.Marshaler.MustMarshalJSON(sudoGenesis)

app := NewNibiruTestApp(appGenesis)
ctx := NewContext(app)

Expand Down Expand Up @@ -68,6 +80,18 @@ func DefaultSudoRoot() sdk.AccAddress {
return sdk.MustAccAddressFromBech32(testutil.ADDR_SUDO_ROOT)
}

// SetDefaultSudoGenesis: Sets the sudo module genesis state to a valid
// default. See "DefaultSudoers".
func SetDefaultSudoGenesis(gen app.GenesisState) {
sudoGen := new(sudotypes.GenesisState)
encoding := app.MakeEncodingConfig()
encoding.Marshaler.MustUnmarshalJSON(gen[sudotypes.ModuleName], sudoGen)
if err := sudoGen.Validate(); err != nil {
sudoGen.Sudoers = DefaultSudoers()
gen[sudotypes.ModuleName] = encoding.Marshaler.MustMarshalJSON(sudoGen)
}
}

// NewNibiruTestAppAndZeroTimeCtx: Runs NewNibiruTestAppAndZeroTimeCtx with the
// block time set to time zero.
func NewNibiruTestAppAndContextAtTime(startTime time.Time) (*app.NibiruApp, sdk.Context) {
Expand All @@ -84,6 +108,8 @@ func NewNibiruTestApp(gen app.GenesisState) *app.NibiruApp {
logger := log.NewNopLogger()

encoding := app.MakeEncodingConfig()
SetDefaultSudoGenesis(gen)

app := app.NewNibiruApp(
logger,
db,
Expand Down
24 changes: 17 additions & 7 deletions x/inflation/keeper/inflation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -76,20 +78,28 @@ func (k Keeper) AllocatePolynomialInflation(
return sdk.Coin{}, sdk.Coin{}, sdk.Coin{}, err
}

// Remaining balance is strategic reserve allocation to the root account of the x/sudo module
// Remaining balance is strategic reserve allocation to the root account
// of the x/sudo module
strategic = k.bankKeeper.GetBalance(ctx, inflationModuleAddr, denoms.NIBI)
strategicAccountAddr, err := k.sudoKeeper.GetRoot(ctx)
strategicAccountAddr, err := k.sudoKeeper.GetRootAddr(ctx)
if err != nil {
k.Logger(ctx).Error("get root account error", "error", err)
return staking, strategic, community, nil
err := fmt.Errorf("inflation error: failed to get sudo root account: %w", err)
k.Logger(ctx).Error(err.Error())
return staking, strategic, community, err
}

if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, strategicAccountAddr, sdk.NewCoins(strategic)); err != nil {
k.Logger(ctx).Error("send coins to root account error", "error", err)
return sdk.Coin{}, sdk.Coin{}, sdk.Coin{}, nil
err := fmt.Errorf("inflation error: failed to send coins to sudo root account: %w", err)
k.Logger(ctx).Error(err.Error())
return sdk.Coin{}, sdk.Coin{}, sdk.Coin{}, err
}

return staking, strategic, community, nil
return staking, strategic, community, ctx.EventManager().EmitTypedEvents(
&types.EventInflationDistribution{
StakingRewards: staking,
StrategicReserve: strategic,
CommunityPool: community,
})
}

// GetAllocationProportion calculates the proportion of coins that is to be
Expand Down
21 changes: 16 additions & 5 deletions x/inflation/keeper/inflation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,20 @@ func TestMintAndAllocateInflation(t *testing.T) {
})

staking, strategic, community, err := nibiruApp.InflationKeeper.MintAndAllocateInflation(ctx, tc.coinsToMint, types.DefaultParams())
require.NoError(t, err)
if tc.rootAccount != "" {
require.NoError(t, err)
} else {
require.Error(t, err)
return
}
assert.Equal(t, tc.expectedStakingAmt, staking)
assert.Equal(t, tc.expectedStrategicAmt, strategic)
assert.Equal(t, tc.expectedCommunityAmt, community)

// Get balances
var balanceStrategicReserve sdk.Coin
if tc.rootAccount != "" {
strategicAccount, err := nibiruApp.SudoKeeper.GetRoot(ctx)
strategicAccount, err := nibiruApp.SudoKeeper.GetRootAddr(ctx)
require.NoError(t, err)
balanceStrategicReserve = nibiruApp.BankKeeper.GetBalance(
ctx,
Expand All @@ -108,9 +113,15 @@ func TestMintAndAllocateInflation(t *testing.T) {
balanceCommunityPool := nibiruApp.DistrKeeper.GetFeePoolCommunityCoins(ctx)

require.NoError(t, err, tc.name)
assert.Equal(t, tc.expectedStakingRewardsBalance, balanceStakingRewards)
assert.Equal(t, tc.expectedStrategicReservesBalance, balanceStrategicReserve)
assert.Equal(t, tc.expectedCommunityPoolBalance, balanceCommunityPool)
assert.Equal(t,
tc.expectedStakingRewardsBalance.String(),
balanceStakingRewards.String())
assert.Equal(t,
tc.expectedStrategicReservesBalance.String(),
balanceStrategicReserve.String())
assert.Equal(t,
tc.expectedCommunityPoolBalance.String(),
balanceCommunityPool.String())
})
}
}
Expand Down
Loading
Loading