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 8, 2024
1 parent 3f2493b commit 6aef571
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 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.9",
},
},
{
name: "max supply of photon exceeded",
uatoneSupply: 100_000_000_000_000, // 100,000,000atone
uphotonSupply: types.MaxSupply + 1,
expectedResponse: &types.QueryConversionRateResponse{
ConversionRate: "0",
},
},
}
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)
}

0 comments on commit 6aef571

Please sign in to comment.