From d853a40f90798b307a30824f01978a80580e2762 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Sat, 25 May 2024 10:43:06 -0500 Subject: [PATCH] impl BurnHeldBalance msgServer with test --- app/app.go | 2 +- x/manifest/keeper/msg_server.go | 15 ++++++ x/manifest/keeper/msg_server_test.go | 74 ++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/app/app.go b/app/app.go index fbf1f7f..2d19fcc 100644 --- a/app/app.go +++ b/app/app.go @@ -204,7 +204,7 @@ var maccPerms = map[string][]string{ ibcfeetypes.ModuleName: nil, icatypes.ModuleName: nil, tokenfactorytypes.ModuleName: {authtypes.Minter, authtypes.Burner}, - manifesttypes.ModuleName: {authtypes.Minter}, + manifesttypes.ModuleName: {authtypes.Minter, authtypes.Burner}, } var ( diff --git a/x/manifest/keeper/msg_server.go b/x/manifest/keeper/msg_server.go index 9d6a38e..322e209 100644 --- a/x/manifest/keeper/msg_server.go +++ b/x/manifest/keeper/msg_server.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/liftedinit/manifest-ledger/x/manifest/types" ) @@ -47,3 +48,17 @@ func (ms msgServer) PayoutStakeholders(ctx context.Context, req *types.MsgPayout return nil, ms.k.PayoutStakeholders(ctx, req.Payout) } + +// BurnHeldBalance implements types.MsgServer. +func (ms msgServer) BurnHeldBalance(ctx context.Context, msg *types.MsgBurnHeldBalance) (*types.MsgBurnHeldBalanceResponse, error) { + addr, err := sdk.AccAddressFromBech32(msg.Sender) + if err != nil { + return nil, err + } + + if err := ms.k.bankKeeper.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, msg.BurnCoins); err != nil { + return nil, fmt.Errorf("not enough balance to burn %s: %w", msg.BurnCoins, err) + } + + return &types.MsgBurnHeldBalanceResponse{}, ms.k.bankKeeper.BurnCoins(ctx, types.ModuleName, msg.BurnCoins) +} diff --git a/x/manifest/keeper/msg_server_test.go b/x/manifest/keeper/msg_server_test.go index 18fe7a9..4f4e270 100644 --- a/x/manifest/keeper/msg_server_test.go +++ b/x/manifest/keeper/msg_server_test.go @@ -235,3 +235,77 @@ func TestCalculatePayoutLogic(t *testing.T) { } } } + +func TestBurnCoins(t *testing.T) { + _, _, authority := testdata.KeyTestPubAddr() + + f := initFixture(t) + + k := f.App.ManifestKeeper + k.SetAuthority(authority.String()) + ms := keeper.NewMsgServerImpl(k) + + type tc struct { + name string + initial sdk.Coins + burn sdk.Coins + expected sdk.Coins + success bool + } + + stake := sdk.NewCoin("stake", sdkmath.NewInt(100_000_000)) + mfx := sdk.NewCoin("umfx", sdkmath.NewInt(100_000_000)) + + cases := []tc{ + { + name: "fail; not enough balance to burn", + initial: sdk.NewCoins(), + burn: sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(7))), + expected: sdk.NewCoins(), + }, + { + name: "success; burn 1 token successfully", + initial: sdk.NewCoins(stake, mfx), + burn: sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(7))), + expected: sdk.NewCoins(mfx, stake.SubAmount(sdkmath.NewInt(7))), + success: true, + }, + { + name: "success; burn many tokens successfully", + initial: sdk.NewCoins(stake, mfx), + burn: sdk.NewCoins(sdk.NewCoin("umfx", sdkmath.NewInt(9)), sdk.NewCoin("stake", sdkmath.NewInt(7))), + expected: sdk.NewCoins(mfx.SubAmount(sdkmath.NewInt(9)), stake.SubAmount(sdkmath.NewInt(7))), + success: true, + }, + } + + for _, c := range cases { + c := c + t.Run(c.name, func(t *testing.T) { + _, _, acc := testdata.KeyTestPubAddr() + + // setup initial balances for the new account + if len(c.initial) > 0 { + require.NoError(t, f.App.BankKeeper.MintCoins(f.Ctx, "mint", c.initial)) + require.NoError(t, f.App.BankKeeper.SendCoinsFromModuleToAccount(f.Ctx, "mint", acc, c.initial)) + } + + // validate initial balance + require.Equal(t, c.initial, f.App.BankKeeper.GetAllBalances(f.Ctx, acc)) + + // burn coins + _, err := ms.BurnHeldBalance(f.Ctx, &types.MsgBurnHeldBalance{ + Sender: acc.String(), + BurnCoins: c.burn, + }) + if c.success { + require.NoError(t, err) + } else { + require.Error(t, err) + } + + require.Equal(t, c.expected, f.App.BankKeeper.GetAllBalances(f.Ctx, acc)) + }) + } + +}