Skip to content

Commit db4ee51

Browse files
authored
feat(inflation): add burn method (#1823)
* feat(inflation): add burn method * Update CHANGELOG.md
1 parent dbdf02d commit db4ee51

File tree

11 files changed

+679
-58
lines changed

11 files changed

+679
-58
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6464
- [#1797](https://github.com/NibiruChain/nibiru/pull/1797) - fix(inflation): fix num skipped epoch updates logic
6565
- [#1804](https://github.com/NibiruChain/nibiru/pull/1804) - fix(inflation): update default parameters
6666
- [#1816](https://github.com/NibiruChain/nibiru/pull/1816) - fix(ibc): fix ibc transaction from wasm contract
67+
- [#1823](https://github.com/NibiruChain/nibiru/pull/1823) - feat(inflation): add burn method
6768

6869
#### Dapp modules: perp, spot, etc
6970

@@ -117,7 +118,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
117118
- Bump `robinraju/release-downloader` from 1.8 to 1.9 ([#1783](https://github.com/NibiruChain/nibiru/pull/1783))
118119
- Bump `codecov/codecov-action` from 3 to 4 ([#1784](https://github.com/NibiruChain/nibiru/pull/1784))
119120
- Bump `golangci/golangci-lint-action` from 3 to 4 ([#1791](https://github.com/NibiruChain/nibiru/pull/1791))
120-
121121
- Bump `github.com/prometheus/client_golang` from 1.17.0 to 1.18.0 ([#1750](https://github.com/NibiruChain/nibiru/pull/1750))
122122
- Bump `google.golang.org/protobuf` from 1.31.0 to 1.32.0 ([#1756](https://github.com/NibiruChain/nibiru/pull/1756))
123123
- Bump `google.golang.org/grpc` from 1.59.0 to 1.60.0 ([#1720](https://github.com/NibiruChain/nibiru/pull/1720))

app/keepers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ func ModuleAccPerms() map[string][]string {
794794
return map[string][]string{
795795
authtypes.FeeCollectorName: nil,
796796
distrtypes.ModuleName: nil,
797-
inflationtypes.ModuleName: {authtypes.Minter},
797+
inflationtypes.ModuleName: {authtypes.Minter, authtypes.Burner},
798798
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
799799
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
800800
govtypes.ModuleName: {authtypes.Burner},

proto/nibiru/inflation/v1/tx.proto

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ package nibiru.inflation.v1;
44
import "gogoproto/gogo.proto";
55
import "google/api/annotations.proto";
66
import "nibiru/inflation/v1/inflation.proto";
7+
import "cosmos/base/v1beta1/coin.proto";
78

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

10-
1111
service Msg {
1212
// ToggleInflation defines a method to enable or disable inflation.
1313
rpc ToggleInflation(MsgToggleInflation) returns (MsgToggleInflationResponse) {
1414
option (google.api.http).post = "/nibiru/inflation/v1/toggle";
1515
};
1616

1717
// EditInflationParams defines a method to edit the inflation params.
18-
rpc EditInflationParams(MsgEditInflationParams)
19-
returns (MsgEditInflationParamsResponse) {
18+
rpc EditInflationParams(MsgEditInflationParams)
19+
returns (MsgEditInflationParamsResponse) {
2020
option (google.api.http).post = "/nibiru/inflation/edit-inflation-params";
21-
};
21+
};
22+
23+
rpc Burn(MsgBurn) returns (MsgBurnResponse) {
24+
option (google.api.http).post = "/nibiru/inflation/v1/burn";
25+
};
2226
}
2327

2428
// MsgToggleInflation defines a message to enable or disable inflation.
@@ -33,16 +37,16 @@ message MsgToggleInflation {
3337
message MsgEditInflationParams {
3438
option (gogoproto.equal) = false;
3539
option (gogoproto.goproto_getters) = false;
36-
40+
3741
string sender = 1;
3842
bool inflation_enabled = 2;
39-
repeated string polynomial_factors = 3[
43+
repeated string polynomial_factors = 3 [
4044
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
4145
(gogoproto.nullable) = true
4246
];
4347
InflationDistribution inflation_distribution = 4
4448
[ (gogoproto.nullable) = true ];
45-
49+
4650
string epochs_per_period = 5 [
4751
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
4852
(gogoproto.nullable) = true
@@ -57,8 +61,15 @@ message MsgEditInflationParams {
5761
];
5862
}
5963

60-
message MsgToggleInflationResponse {
61-
}
64+
message MsgToggleInflationResponse {}
6265

63-
message MsgEditInflationParamsResponse {
66+
message MsgEditInflationParamsResponse {}
67+
68+
// MsgBurn: allows burning of any token
69+
message MsgBurn {
70+
string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
71+
cosmos.base.v1beta1.Coin coin = 2
72+
[ (gogoproto.moretags) = "yaml:\"coin\"", (gogoproto.nullable) = false ];
6473
}
74+
75+
message MsgBurnResponse {}

x/inflation/keeper/keeper.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,13 @@ func NewKeeper(
8989
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
9090
return ctx.Logger().With("module", "x/"+types.ModuleName)
9191
}
92+
93+
func (k Keeper) Burn(ctx sdk.Context, coins sdk.Coins, sender sdk.AccAddress) error {
94+
if err := k.bankKeeper.SendCoinsFromAccountToModule(
95+
ctx, sender, types.ModuleName, coins,
96+
); err != nil {
97+
return err
98+
}
99+
100+
return k.bankKeeper.BurnCoins(ctx, types.ModuleName, coins)
101+
}

x/inflation/keeper/keeper_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package keeper_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/NibiruChain/nibiru/x/common/testutil"
11+
"github.com/NibiruChain/nibiru/x/common/testutil/testapp"
12+
"github.com/NibiruChain/nibiru/x/inflation/types"
13+
)
14+
15+
func init() {
16+
testapp.EnsureNibiruPrefix()
17+
}
18+
19+
func TestBurn(t *testing.T) {
20+
testCases := []struct {
21+
name string
22+
sender sdk.AccAddress
23+
burnCoin sdk.Coin
24+
expectedErr error
25+
}{
26+
{
27+
name: "pass",
28+
sender: testutil.AccAddress(),
29+
burnCoin: sdk.NewCoin("nibiru", sdk.NewInt(100)),
30+
expectedErr: nil,
31+
},
32+
}
33+
for _, tc := range testCases {
34+
t.Run(fmt.Sprintf("Case %s", tc.name), func(t *testing.T) {
35+
nibiruApp, ctx := testapp.NewNibiruTestAppAndContext()
36+
require.NoError(t,
37+
nibiruApp.BankKeeper.MintCoins(
38+
ctx, types.ModuleName, sdk.NewCoins(tc.burnCoin)))
39+
require.NoError(t,
40+
nibiruApp.BankKeeper.SendCoinsFromModuleToAccount(
41+
ctx, types.ModuleName, tc.sender, sdk.NewCoins(tc.burnCoin)),
42+
)
43+
44+
// Burn coins
45+
err := nibiruApp.InflationKeeper.Burn(ctx, sdk.NewCoins(tc.burnCoin), tc.sender)
46+
if tc.expectedErr != nil {
47+
require.EqualError(t, err, tc.expectedErr.Error())
48+
} else {
49+
require.NoError(t, err)
50+
}
51+
})
52+
}
53+
}

x/inflation/keeper/msg_server.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,17 @@ func (ms msgServer) ToggleInflation(
4646
resp = &types.MsgToggleInflationResponse{}
4747
return resp, err
4848
}
49+
50+
func (ms msgServer) Burn(
51+
goCtx context.Context, msg *types.MsgBurn,
52+
) (resp *types.MsgBurnResponse, err error) {
53+
ctx := sdk.UnwrapSDKContext(goCtx)
54+
55+
sender, err := sdk.AccAddressFromBech32(msg.Sender)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
err = ms.Keeper.Burn(ctx, sdk.NewCoins(msg.Coin), sender)
61+
return &types.MsgBurnResponse{}, err
62+
}

x/inflation/keeper/msg_server_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,22 @@ func TestMsgEditInflationParams(t *testing.T) {
7070
params = app.InflationKeeper.GetParams(ctx)
7171
require.EqualValues(t, params.EpochsPerPeriod, 42)
7272
}
73+
74+
func TestMsgBurn(t *testing.T) {
75+
app, ctx := testapp.NewNibiruTestAppAndContext()
76+
sender := testutil.AccAddress()
77+
err := app.BankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin("unibi", sdk.NewInt(100))))
78+
require.NoError(t, err)
79+
err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, sdk.NewCoins(sdk.NewCoin("unibi", sdk.NewInt(100))))
80+
require.NoError(t, err)
81+
82+
msgServer := keeper.NewMsgServerImpl(app.InflationKeeper)
83+
84+
msg := types.MsgBurn{
85+
Sender: sender.String(),
86+
Coin: sdk.NewCoin("unibi", sdk.NewInt(100)),
87+
}
88+
89+
_, err = msgServer.Burn(ctx, &msg)
90+
require.NoError(t, err)
91+
}

x/inflation/types/interfaces.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ type BankKeeper interface {
2121
GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
2222
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
2323
SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error
24+
SendCoinsFromAccountToModule(
25+
ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins,
26+
) error
2427
MintCoins(ctx sdk.Context, name string, amt sdk.Coins) error
2528
BurnCoins(ctx sdk.Context, name string, amt sdk.Coins) error
2629
HasSupply(ctx sdk.Context, denom string) bool

x/inflation/types/msgs.go

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,30 @@ import (
44
"fmt"
55

66
sdk "github.com/cosmos/cosmos-sdk/types"
7+
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
78
)
89

910
// ensure Msg interface compliance at compile time
1011
var (
11-
_ sdk.Msg = &MsgEditInflationParams{}
12-
_ sdk.Msg = &MsgToggleInflation{}
12+
_ legacytx.LegacyMsg = &MsgEditInflationParams{}
13+
_ legacytx.LegacyMsg = &MsgToggleInflation{}
14+
_ legacytx.LegacyMsg = &MsgBurn{}
1315
)
1416

1517
// oracle message types
1618
const (
1719
TypeMsgEditInflationParams = "edit_inflation_params"
1820
TypeMsgToggleInflation = "toggle_inflation"
21+
TypeMsgBurn = "msg_burn"
1922
)
2023

21-
// Route implements sdk.Msg
24+
// Route implements legacytx.LegacyMsg
2225
func (msg MsgEditInflationParams) Route() string { return RouterKey }
2326

24-
// Type implements sdk.Msg
27+
// Type implements legacytx.LegacyMsg
2528
func (msg MsgEditInflationParams) Type() string { return TypeMsgEditInflationParams }
2629

27-
// GetSignBytes implements sdk.Msg
30+
// GetSignBytes implements legacytx.LegacyMsg
2831
func (msg MsgEditInflationParams) GetSignBytes() []byte {
2932
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
3033
}
@@ -75,13 +78,13 @@ func (m MsgEditInflationParams) ValidateBasic() error {
7578

7679
// -------------------------------------------------
7780
// MsgToggleInflation
78-
// Route implements sdk.Msg
81+
// Route implements legacytx.LegacyMsg
7982
func (msg MsgToggleInflation) Route() string { return RouterKey }
8083

81-
// Type implements sdk.Msg
84+
// Type implements legacytx.LegacyMsg
8285
func (msg MsgToggleInflation) Type() string { return TypeMsgToggleInflation }
8386

84-
// GetSignBytes implements sdk.Msg
87+
// GetSignBytes implements legacytx.LegacyMsg
8588
func (msg MsgToggleInflation) GetSignBytes() []byte {
8689
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
8790
}
@@ -102,3 +105,42 @@ func (m MsgToggleInflation) ValidateBasic() error {
102105
}
103106
return nil
104107
}
108+
109+
// -------------------------------------------------
110+
// MsgBurn
111+
// Route implements legacytx.LegacyMsg
112+
func (msg MsgBurn) Route() string { return RouterKey }
113+
114+
// Type implements legacytx.LegacyMsg
115+
func (msg MsgBurn) Type() string { return TypeMsgBurn }
116+
117+
// GetSignBytes implements legacytx.LegacyMsg
118+
func (msg MsgBurn) GetSignBytes() []byte {
119+
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
120+
}
121+
122+
// GetSigners implements legacytx.LegacyMsg
123+
func (msg MsgBurn) GetSigners() []sdk.AccAddress {
124+
feeder, err := sdk.AccAddressFromBech32(msg.Sender)
125+
if err != nil {
126+
panic(err)
127+
}
128+
129+
return []sdk.AccAddress{feeder}
130+
}
131+
132+
func (m MsgBurn) ValidateBasic() error {
133+
if _, err := sdk.AccAddressFromBech32(m.Sender); err != nil {
134+
return err
135+
}
136+
137+
if err := m.Coin.Validate(); err != nil {
138+
return err
139+
}
140+
141+
if m.Coin.Amount.IsZero() {
142+
return fmt.Errorf("coin amount should not be zero")
143+
}
144+
145+
return nil
146+
}

0 commit comments

Comments
 (0)