Skip to content

Commit

Permalink
never return negative convert rate
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Nov 26, 2024
1 parent 4cd13af commit 2f2d958
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
52 changes: 38 additions & 14 deletions x/photon/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/atomone-hub/atomone/x/photon/keeper"
"github.com/atomone-hub/atomone/x/photon/testutil"
"github.com/atomone-hub/atomone/x/photon/types"
"github.com/stretchr/testify/require"
Expand All @@ -23,17 +22,42 @@ func TestParamsQuery(t *testing.T) {
}

func TestConversionRateQuery(t *testing.T) {
k, m, ctx := testutil.SetupPhotonKeeper(t)
m.StakingKeeper.EXPECT().BondDenom(ctx).Return("uatone")
uatoneSupply := int64(100_000_000_000_000) // 100,000,000atone
m.BankKeeper.EXPECT().GetSupply(ctx, "uatone").Return(sdk.NewInt64Coin("uatone", uatoneSupply))
uphotonSupply := int64(100_000_000_000) // 100,000photon
m.BankKeeper.EXPECT().GetSupply(ctx, types.Denom).Return(sdk.NewInt64Coin("uatone", uphotonSupply))

resp, err := k.ConversionRate(ctx, &types.QueryConversionRateRequest{})

require.NoError(t, err)
expectedConversionRate := sdk.NewDec(keeper.UphotonMaxSupply - uphotonSupply).
QuoInt64(uatoneSupply).String()
require.Equal(t, expectedConversionRate, resp.ConversionRate)
tests := []struct {
name string
uatoneSupply int64
uphotonSupply int64
expectedResponse *types.QueryConversionRateResponse
}{
{
name: "nominal case",
uatoneSupply: 100_000_000_000_000, // 100,000,000atone
uphotonSupply: 100_000_000_000, // 100,000photon
expectedResponse: &types.QueryConversionRateResponse{
ConversionRate: "9.999000000000000000",
},
},
{
name: "max supply of photon exceeded",
uatoneSupply: 100_000_000_000_000, // 100,000,000atone
uphotonSupply: types.MaxSupply + 1,
expectedResponse: &types.QueryConversionRateResponse{
ConversionRate: "0.000000000000000000",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k, m, ctx := testutil.SetupPhotonKeeper(t)
m.StakingKeeper.EXPECT().BondDenom(ctx).Return("uatone")
m.BankKeeper.EXPECT().GetSupply(ctx, "uatone").
Return(sdk.NewInt64Coin("uatone", tt.uatoneSupply))
m.BankKeeper.EXPECT().GetSupply(ctx, types.Denom).
Return(sdk.NewInt64Coin("uatone", tt.uphotonSupply))

resp, err := k.ConversionRate(ctx, &types.QueryConversionRateRequest{})

require.NoError(t, err)
require.Equal(t, tt.expectedResponse, resp)
})
}
}
4 changes: 4 additions & 0 deletions x/photon/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
// photon.
func (k Keeper) conversionRate(_ sdk.Context, bondDenomSupply, uphotonSupply sdk.Dec) sdk.Dec {
remainMintableUphotons := sdk.NewDec(types.MaxSupply).Sub(uphotonSupply)
if remainMintableUphotons.IsNegative() {
// If for any reason the max supply is exceeded, avoid returning a negative number
return sdk.ZeroDec()
}
return remainMintableUphotons.Quo(bondDenomSupply)
}
5 changes: 2 additions & 3 deletions x/photon/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper_test
import (
"testing"

"github.com/atomone-hub/atomone/x/photon/keeper"
"github.com/atomone-hub/atomone/x/photon/testutil"
"github.com/atomone-hub/atomone/x/photon/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -60,7 +59,7 @@ func TestMsgServerMintPhoton(t *testing.T) {
setup: func(ctx sdk.Context, m testutil.Mocks) {
m.StakingKeeper.EXPECT().BondDenom(ctx).Return("uatone")
m.BankKeeper.EXPECT().GetSupply(ctx, "uatone").Return(sdk.NewInt64Coin("uatone", atoneSupply))
m.BankKeeper.EXPECT().GetSupply(ctx, types.Denom).Return(sdk.NewInt64Coin(types.Denom, keeper.UphotonMaxSupply))
m.BankKeeper.EXPECT().GetSupply(ctx, types.Denom).Return(sdk.NewInt64Coin(types.Denom, types.MaxSupply))
},
expectedErr: "no more photon can be minted",
},
Expand All @@ -74,7 +73,7 @@ func TestMsgServerMintPhoton(t *testing.T) {
setup: func(ctx sdk.Context, m testutil.Mocks) {
m.StakingKeeper.EXPECT().BondDenom(ctx).Return("uatone")
m.BankKeeper.EXPECT().GetSupply(ctx, "uatone").Return(sdk.NewInt64Coin("uatone", atoneSupply))
m.BankKeeper.EXPECT().GetSupply(ctx, types.Denom).Return(sdk.NewInt64Coin(types.Denom, keeper.UphotonMaxSupply-1_000_000))
m.BankKeeper.EXPECT().GetSupply(ctx, types.Denom).Return(sdk.NewInt64Coin(types.Denom, types.MaxSupply-1_000_000))
},
expectedErr: "not enough photon can be minted",
},
Expand Down

0 comments on commit 2f2d958

Please sign in to comment.