Skip to content

Commit

Permalink
impl BurnHeldBalance msgServer with test
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed May 25, 2024
1 parent c3e3491 commit d853a40
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
15 changes: 15 additions & 0 deletions x/manifest/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/liftedinit/manifest-ledger/x/manifest/types"
)

Expand Down Expand Up @@ -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

Check warning on line 56 in x/manifest/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/manifest/keeper/msg_server.go#L56

Added line #L56 was not covered by tests
}

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)
}
74 changes: 74 additions & 0 deletions x/manifest/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}

}

0 comments on commit d853a40

Please sign in to comment.