Skip to content

Commit

Permalink
edge case handling in tokens module and fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kiragpg committed Jul 1, 2024
1 parent 8dae08c commit cb16365
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 41 deletions.
31 changes: 26 additions & 5 deletions x/feeprocessing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"testing"

"cosmossdk.io/math"
simapp "github.com/KiraCore/sekai/app"
kiratypes "github.com/KiraCore/sekai/types"
govtypes "github.com/KiraCore/sekai/x/gov/types"
Expand Down Expand Up @@ -44,7 +45,10 @@ func TestNewKeeper_Executions(t *testing.T) {
require.True(t, len(executions) == 0)

msg1 := tokenstypes.NewMsgUpsertTokenInfo(
addr, "ukex", sdk.NewDec(1), true,
addr,
"adr20",
"ukex", sdk.NewDec(1), true,
sdk.ZeroInt(), sdk.ZeroInt(),
sdk.ZeroDec(),
sdk.ZeroInt(),
false,
Expand All @@ -53,13 +57,17 @@ func TestNewKeeper_Executions(t *testing.T) {
"Kira",
"",
10,
"", "", "", 0, math.ZeroInt(), "", false, "", "",
)
app.FeeProcessingKeeper.AddExecutionStart(ctx, msg1)
executions = app.FeeProcessingKeeper.GetExecutionsStatus(ctx)
require.True(t, len(executions) == 1)

msg3 := tokenstypes.NewMsgUpsertTokenInfo(
addr, "ukex", sdk.NewDec(1), true,
addr,
"adr20",
"ukex", sdk.NewDec(1), true,
sdk.ZeroInt(), sdk.ZeroInt(),
sdk.ZeroDec(),
sdk.ZeroInt(),
false,
Expand All @@ -68,6 +76,7 @@ func TestNewKeeper_Executions(t *testing.T) {
"Kira",
"",
10,
"", "", "", 0, math.ZeroInt(), "", false, "", "",
)
app.FeeProcessingKeeper.AddExecutionStart(ctx, msg3)
executions = app.FeeProcessingKeeper.GetExecutionsStatus(ctx)
Expand Down Expand Up @@ -178,7 +187,10 @@ func TestNewKeeper_ProcessExecutionFeeReturn(t *testing.T) {
fees := sdk.Coins{sdk.NewInt64Coin("ukex", 1000)}
app.FeeProcessingKeeper.SendCoinsFromAccountToModule(ctx, addr, authtypes.FeeCollectorName, fees)
msg := tokenstypes.NewMsgUpsertTokenInfo(
addr, "ukex", sdk.NewDec(1), true,
addr,
"adr20",
"ukex", sdk.NewDec(1), true,
sdk.ZeroInt(), sdk.ZeroInt(),
sdk.ZeroDec(),
sdk.ZeroInt(),
false,
Expand All @@ -187,6 +199,7 @@ func TestNewKeeper_ProcessExecutionFeeReturn(t *testing.T) {
"Kira",
"",
10,
"", "", "", 0, math.ZeroInt(), "", false, "", "",
)
app.FeeProcessingKeeper.AddExecutionStart(ctx, msg)
app.FeeProcessingKeeper.ProcessExecutionFeeReturn(ctx)
Expand Down Expand Up @@ -214,7 +227,10 @@ func TestNewKeeper_ProcessExecutionFeeReturn(t *testing.T) {
app.FeeProcessingKeeper.SendCoinsFromAccountToModule(ctx, addr2, authtypes.FeeCollectorName, fees)
app.FeeProcessingKeeper.SendCoinsFromAccountToModule(ctx, addr3, authtypes.FeeCollectorName, fees)
msg2 := tokenstypes.NewMsgUpsertTokenInfo(
addr2, "ukex", sdk.NewDec(1), true,
addr2,
"adr20",
"ukex", sdk.NewDec(1), true,
sdk.ZeroInt(), sdk.ZeroInt(),
sdk.ZeroDec(),
sdk.ZeroInt(),
false,
Expand All @@ -223,9 +239,13 @@ func TestNewKeeper_ProcessExecutionFeeReturn(t *testing.T) {
"Kira",
"",
10,
"", "", "", 0, math.ZeroInt(), "", false, "", "",
)
msg3 := tokenstypes.NewMsgUpsertTokenInfo(
addr3, "ukex", sdk.NewDec(1), true,
addr3,
"adr20",
"ukex", sdk.NewDec(1), true,
sdk.ZeroInt(), sdk.ZeroInt(),
sdk.ZeroDec(),
sdk.ZeroInt(),
false,
Expand All @@ -234,6 +254,7 @@ func TestNewKeeper_ProcessExecutionFeeReturn(t *testing.T) {
"Kira",
"",
10,
"", "", "", 0, math.ZeroInt(), "", false, "", "",
)
app.FeeProcessingKeeper.AddExecutionStart(ctx, msg3)
app.FeeProcessingKeeper.AddExecutionStart(ctx, msg2)
Expand Down
5 changes: 5 additions & 0 deletions x/gov/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"cosmossdk.io/math"
simapp "github.com/KiraCore/sekai/app"
"github.com/KiraCore/sekai/x/gov"
"github.com/KiraCore/sekai/x/gov/types"
Expand Down Expand Up @@ -455,8 +456,11 @@ func TestEndBlocker_ActiveProposal(t *testing.T) {
"some desc",
tokenstypes.NewUpsertTokenInfosProposal(
"btc",
"adr20",
sdk.NewDec(1234),
false,
sdk.ZeroInt(),
sdk.ZeroInt(),
sdk.ZeroDec(),
sdk.ZeroInt(),
false,
Expand All @@ -465,6 +469,7 @@ func TestEndBlocker_ActiveProposal(t *testing.T) {
"Bitcoin",
"",
9,
"", "", "", 0, math.ZeroInt(), "", false, "", "",
),
time.Now(),
time.Now().Add(10*time.Second),
Expand Down
6 changes: 3 additions & 3 deletions x/gov/client/cli/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ func (s IntegrationTestSuite) SetNetworkPropertyProposal(property string, value
s.Require().Contains(result.RawLog, "SetNetworkProperty")
}

func (s IntegrationTestSuite) UpsertRate(denom string, rate string, flagFeePayments bool) sdk.TxResponse {
func (s IntegrationTestSuite) UpsertRate(denom string, rate string, flagFeeEnabled bool) sdk.TxResponse {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
cmd := tokenscli.GetTxUpsertTokenInfoCmd()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, []string{
fmt.Sprintf("--%s=%s", tokenscli.FlagDenom, denom),
fmt.Sprintf("--%s=%s", tokenscli.FlagRate, rate),
fmt.Sprintf("--%s=%s", tokenscli.FlagFeePayments, strconv.FormatBool(flagFeePayments)),
fmt.Sprintf("--%s=%s", tokenscli.FlagFeeRate, rate),
fmt.Sprintf("--%s=%s", tokenscli.FlagFeeEnabled, strconv.FormatBool(flagFeeEnabled)),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
Expand Down
2 changes: 1 addition & 1 deletion x/layer2/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ func (k msgServer) MintCreateNftTx(goCtx context.Context, msg *types.MsgMintCrea
Description: msg.Description,
Website: msg.Website,
Social: msg.Social,
Decimals: msg.Decimals,
Decimals: 0,
SupplyCap: msg.Cap,
Supply: msg.Supply,
Holders: msg.Holders,
Expand Down
11 changes: 7 additions & 4 deletions x/tokens/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ func (s *IntegrationTestSuite) TestUpsertTokenInfoAndQuery() {
_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=%s", cli.FlagDenom, "ubtc"),
fmt.Sprintf("--%s=%f", cli.FlagRate, 0.00001),
fmt.Sprintf("--%s=%s", cli.FlagFeePayments, "true"),
fmt.Sprintf("--%s=%s", cli.FlagSupply, "0"),
fmt.Sprintf("--%s=%s", cli.FlagSupplyCap, "0"),
fmt.Sprintf("--%s=%f", cli.FlagFeeRate, 0.00001),
fmt.Sprintf("--%s=%s", cli.FlagFeeEnabled, "true"),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.DefaultDenom, sdk.NewInt(100))).String()),
Expand Down Expand Up @@ -119,10 +121,11 @@ func (s IntegrationTestSuite) TestCreateProposalUpsertTokenInfo() {
cmd := cli.GetTxProposalUpsertTokenInfoCmd()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, []string{
fmt.Sprintf("--%s=%s", cli.FlagDenom, "ubtc"),
fmt.Sprintf("--%s=%f", cli.FlagRate, 0.00001),
fmt.Sprintf("--%s=%f", cli.FlagFeeRate, 0.00001),
fmt.Sprintf("--%s=%s", cli.FlagTitle, "title"),
fmt.Sprintf("--%s=%s", cli.FlagDescription, "some desc"),
fmt.Sprintf("--%s=%s", cli.FlagFeePayments, "true"),
fmt.Sprintf("--%s=%s", cli.FlagFeeEnabled, "true"),
fmt.Sprintf("--%s=%s", cli.FlagDecimals, "6"),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
Expand Down
49 changes: 24 additions & 25 deletions x/tokens/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func setPermissionToAddr(t *testing.T, app *simapp.SekaiApp, ctx sdk.Context, ad
}

func TestNewHandler_MsgUpsertTokenInfo(t *testing.T) {

app := simapp.Setup(false)
ctx := app.NewContext(false, tmproto.Header{})
handler := tokens.NewHandler(app.TokensKeeper, app.CustomGovKeeper)
Expand Down Expand Up @@ -129,30 +128,30 @@ func TestNewHandler_MsgUpsertTokenInfo(t *testing.T) {
},
handlerErr: "rate should be positive",
},
{
name: "bond denom rate change test",
constructor: func(addr sdk.AccAddress) (*tokenstypes.MsgUpsertTokenInfo, error) {
err := setPermissionToAddr(t, app, ctx, addr, types.PermUpsertTokenInfo)
require.NoError(t, err)
return tokenstypes.NewMsgUpsertTokenInfo(
addr,
"adr20",
"ukex", sdk.NewDec(10),
true,
sdk.ZeroInt(), sdk.ZeroInt(),
sdk.ZeroDec(),
sdk.ZeroInt(),
false,
false,
"ETH",
"Ethereum",
"icon",
6,
"", "", "", 0, math.ZeroInt(), "", false, "", "",
), nil
},
handlerErr: "bond denom rate is read-only",
},
// {
// name: "bond denom rate change test",
// constructor: func(addr sdk.AccAddress) (*tokenstypes.MsgUpsertTokenInfo, error) {
// err := setPermissionToAddr(t, app, ctx, addr, types.PermUpsertTokenInfo)
// require.NoError(t, err)
// return tokenstypes.NewMsgUpsertTokenInfo(
// addr,
// "adr20",
// "ukex", sdk.NewDec(10),
// true,
// sdk.ZeroInt(), sdk.ZeroInt(),
// sdk.ZeroDec(),
// sdk.ZeroInt(),
// false,
// false,
// "ETH",
// "Ethereum",
// "icon",
// 6,
// "", "", "", 0, math.ZeroInt(), "", false, "", "",
// ), nil
// },
// handlerErr: "bond denom rate is read-only",
// },
}
for i, tt := range tests {
addr := NewAccountByIndex(i)
Expand Down
4 changes: 4 additions & 0 deletions x/tokens/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (k msgServer) UpsertTokenInfo(goCtx context.Context, msg *types.MsgUpsertTo
tokenInfo.Description = msg.Description
tokenInfo.Website = msg.Website
tokenInfo.Social = msg.Social
if !tokenInfo.SupplyCap.IsZero() &&
(tokenInfo.SupplyCap.LT(msg.SupplyCap) || msg.SupplyCap.IsZero()) {
return nil, types.ErrSupplyCapShouldNotBeIncreased
}
tokenInfo.SupplyCap = msg.SupplyCap
tokenInfo.MintingFee = msg.MintingFee
tokenInfo.Owner = msg.Owner
Expand Down
6 changes: 3 additions & 3 deletions x/tokens/keeper/token_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func (k Keeper) UpsertTokenInfo(ctx sdk.Context, info types.TokenInfo) error {
store := ctx.KVStore(k.storeKey)
// we use denom of TokenInfo as an ID inside KVStore storage
tokenInfoStoreID := append([]byte(PrefixKeyTokenInfo), []byte(info.Denom)...)
if info.Denom == k.DefaultDenom(ctx) && store.Has(tokenInfoStoreID) {
return types.ErrBondDenomIsReadOnly
}
// if info.Denom == k.DefaultDenom(ctx) && store.Has(tokenInfoStoreID) {
// return types.ErrBondDenomIsReadOnly
// }

if !info.SupplyCap.IsNil() && info.SupplyCap.IsPositive() && info.Supply.GT(info.SupplyCap) {
return types.ErrCannotExceedTokenCap
Expand Down
1 change: 1 addition & 0 deletions x/tokens/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ var (
ErrCannotExceedTokenCap = errors.Register(ModuleName, 6, "cannot exceed token cap")
ErrBondDenomIsReadOnly = errors.Register(ModuleName, 7, "bond denom rate is read-only")
ErrTokenNotRegistered = errors.Register(ModuleName, 8, "token not registered")
ErrSupplyCapShouldNotBeIncreased = errors.Register(ModuleName, 9, "supply cap should not be increased")
)
7 changes: 7 additions & 0 deletions x/tokens/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func NewTokenInfo(
nftMetadata string,
nftHash string,
) TokenInfo {
if tokenType == "adr43" {
decimals = 0
}
if tokenType == "adr20" {
nftHash = ""
nftMetadata = ""
}
return TokenInfo{
Denom: denom,
TokenType: tokenType,
Expand Down

0 comments on commit cb16365

Please sign in to comment.