From 8e173236e40866505846027f75b86f7980b183f7 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Mon, 25 Nov 2024 14:29:03 +0100 Subject: [PATCH] fix(photon): handle when minted uphoton < 1 --- x/photon/keeper/msg_server.go | 8 ++++---- x/photon/keeper/msg_server_test.go | 18 +++++++++++++++++- x/photon/types/errors.go | 9 ++++----- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/x/photon/keeper/msg_server.go b/x/photon/keeper/msg_server.go index 7b90a0dd..40a32cd2 100644 --- a/x/photon/keeper/msg_server.go +++ b/x/photon/keeper/msg_server.go @@ -42,14 +42,14 @@ func (k msgServer) MintPhoton(goCtx context.Context, msg *types.MsgMintPhoton) ( uphotonSupply = k.bankKeeper.GetSupply(ctx, types.Denom).Amount.ToLegacyDec() conversionRate = k.conversionRate(ctx, bondDenomSupply, uphotonSupply) bondDenomToBurn = msg.Amount - uphotonToMint = bondDenomToBurn.Amount.ToLegacyDec().Mul(conversionRate) + uphotonToMint = bondDenomToBurn.Amount.ToLegacyDec().Mul(conversionRate).RoundInt() ) // If no photon to mint, do not burn bondDenomToBurn, returns an error if uphotonToMint.IsZero() { - return nil, types.ErrNoMintablePhotons + return nil, types.ErrZeroMintPhotons } // If photonToMint + photonSupply > photonMaxSupply, returns an error - if uphotonSupply.Add(uphotonToMint).GT(sdk.NewDec(types.MaxSupply)) { + if uphotonSupply.Add(uphotonToMint.ToLegacyDec()).GT(sdk.NewDec(types.MaxSupply)) { return nil, types.ErrNotEnoughPhotons } @@ -60,7 +60,7 @@ func (k msgServer) MintPhoton(goCtx context.Context, msg *types.MsgMintPhoton) ( // 4) move PHOTONs from this module address to msg signer address var ( coinsToBurn = sdk.NewCoins(bondDenomToBurn) - coinsToMint = sdk.NewCoins(sdk.NewCoin(types.Denom, uphotonToMint.RoundInt())) + coinsToMint = sdk.NewCoins(sdk.NewCoin(types.Denom, uphotonToMint)) ) // 1) Send atone to photon module for burn to, err := sdk.AccAddressFromBech32(msg.ToAddress) diff --git a/x/photon/keeper/msg_server_test.go b/x/photon/keeper/msg_server_test.go index 483b5fc8..6760a2d3 100644 --- a/x/photon/keeper/msg_server_test.go +++ b/x/photon/keeper/msg_server_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "math" "testing" "github.com/atomone-hub/atomone/x/photon/testutil" @@ -61,7 +62,7 @@ func TestMsgServerMintPhoton(t *testing.T) { m.BankKeeper.EXPECT().GetSupply(ctx, "uatone").Return(sdk.NewInt64Coin("uatone", atoneSupply)) m.BankKeeper.EXPECT().GetSupply(ctx, types.Denom).Return(sdk.NewInt64Coin(types.Denom, types.MaxSupply)) }, - expectedErr: "no more photon can be minted", + expectedErr: "no mintable photon after rounding, try higher burn", }, { name: "fail: photon_supply+minted>max", @@ -77,6 +78,21 @@ func TestMsgServerMintPhoton(t *testing.T) { }, expectedErr: "not enough photon can be minted", }, + { + name: "fail: atone_supply >> photon_supply", + params: types.Params{MintDisabled: false}, + msg: &types.MsgMintPhoton{ + ToAddress: toAddress.String(), + Amount: sdk.NewInt64Coin(appparams.BondDenom, 1), + }, + setup: func(ctx sdk.Context, m testutil.Mocks) { + m.StakingKeeper.EXPECT().BondDenom(ctx).Return(appparams.BondDenom) + m.BankKeeper.EXPECT().GetSupply(ctx, appparams.BondDenom). + Return(sdk.NewInt64Coin(appparams.BondDenom, math.MaxInt)) + m.BankKeeper.EXPECT().GetSupply(ctx, types.Denom).Return(sdk.NewInt64Coin(types.Denom, 0)) + }, + expectedErr: "no mintable photon after rounding, try higher burn", + }, { name: "ok: photon_supply=0", params: types.Params{MintDisabled: false}, diff --git a/x/photon/types/errors.go b/x/photon/types/errors.go index 3f671144..4b409189 100644 --- a/x/photon/types/errors.go +++ b/x/photon/types/errors.go @@ -6,9 +6,8 @@ import ( // x/photon module sentinel errors var ( - ErrMintDisabled = sdkerrors.Register(ModuleName, 1, "photon mint disabled") //nolint:staticcheck - ErrBurnInvalidDenom = sdkerrors.Register(ModuleName, 2, "invalid burned amount denom: expected bond denom") //nolint:staticcheck - ErrNoMintablePhotons = sdkerrors.Register(ModuleName, 3, "no more photon can be minted") //nolint:staticcheck - ErrNotEnoughPhotons = sdkerrors.Register(ModuleName, 4, "not enough photon can be minted") //nolint:staticcheck - + ErrMintDisabled = sdkerrors.Register(ModuleName, 1, "photon mint disabled") //nolint:staticcheck + ErrBurnInvalidDenom = sdkerrors.Register(ModuleName, 2, "invalid burned amount denom: expected bond denom") //nolint:staticcheck + ErrZeroMintPhotons = sdkerrors.Register(ModuleName, 3, "no mintable photon after rounding, try higher burn") //nolint:staticcheck + ErrNotEnoughPhotons = sdkerrors.Register(ModuleName, 4, "not enough photon can be minted") //nolint:staticcheck )