Skip to content

Commit

Permalink
fix(photon): handle when minted uphoton < 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Nov 25, 2024
1 parent 22a813e commit 8e17323
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
8 changes: 4 additions & 4 deletions x/photon/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion x/photon/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"math"
"testing"

"github.com/atomone-hub/atomone/x/photon/testutil"
Expand Down Expand Up @@ -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",
Expand All @@ -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),

Check failure on line 86 in x/photon/keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / Analyze

undefined: appparams

Check failure on line 86 in x/photon/keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: appparams
},
setup: func(ctx sdk.Context, m testutil.Mocks) {
m.StakingKeeper.EXPECT().BondDenom(ctx).Return(appparams.BondDenom)

Check failure on line 89 in x/photon/keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / Analyze

undefined: appparams

Check failure on line 89 in x/photon/keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: appparams
m.BankKeeper.EXPECT().GetSupply(ctx, appparams.BondDenom).

Check failure on line 90 in x/photon/keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / Analyze

undefined: appparams

Check failure on line 90 in x/photon/keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: appparams
Return(sdk.NewInt64Coin(appparams.BondDenom, math.MaxInt))

Check failure on line 91 in x/photon/keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / Analyze

undefined: appparams

Check failure on line 91 in x/photon/keeper/msg_server_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: appparams
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},
Expand Down
9 changes: 4 additions & 5 deletions x/photon/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 8e17323

Please sign in to comment.