From e75000cf4139d782ef59eacc4d9ba354dee9f115 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Wed, 24 Aug 2022 14:20:57 +0200 Subject: [PATCH 1/3] add check in max leverage --- x/vpool/client/cli/genvpool.go | 10 +++ x/vpool/client/cli/genvpool_test.go | 98 +++++++++++++++-------------- 2 files changed, 62 insertions(+), 46 deletions(-) diff --git a/x/vpool/client/cli/genvpool.go b/x/vpool/client/cli/genvpool.go index ed62bc277..2b6e66014 100644 --- a/x/vpool/client/cli/genvpool.go +++ b/x/vpool/client/cli/genvpool.go @@ -98,23 +98,33 @@ func parseVpoolParams(args []string) (*types.Pool, error) { if err != nil { return nil, err } + fluctuationLimitRatio, err := sdk.NewDecFromStr(args[4]) if err != nil { return nil, err } + maxOracleSpread, err := sdk.NewDecFromStr(args[5]) if err != nil { return nil, err } + maintenanceMarginRatio, err := sdk.NewDecFromStr(args[6]) if err != nil { return nil, err } + maxLeverage, err := sdk.NewDecFromStr(args[7]) if err != nil { return nil, err } + fmt.Printf("%s", maxLeverage) + + if !maxLeverage.GT(sdk.ZeroDec()) { + return nil, fmt.Errorf("invalid max leverage value") + } + vPool := types.NewPool( vPair, tradeLimit, diff --git a/x/vpool/client/cli/genvpool_test.go b/x/vpool/client/cli/genvpool_test.go index 6f0b0d09c..58b600d17 100644 --- a/x/vpool/client/cli/genvpool_test.go +++ b/x/vpool/client/cli/genvpool_test.go @@ -23,7 +23,7 @@ var testModuleBasicManager = module.NewBasicManager(genutil.AppModuleBasic{}) // Tests "add-genesis-vpool", a command that adds a vpool to genesis.json func TestAddGenesisVpoolCmd(t *testing.T) { - type TestCase struct { + tests := []struct { name string pairName string baseAsset string @@ -34,49 +34,7 @@ func TestAddGenesisVpoolCmd(t *testing.T) { maintainRatio string maxLeverage string expectError bool - } - - var executeTest = func(t *testing.T, testCase TestCase) { - tc := testCase - t.Run(tc.name, func(t *testing.T) { - home := t.TempDir() - logger := log.NewNopLogger() - cfg, err := genutiltest.CreateDefaultTendermintConfig(home) - require.NoError(t, err) - - appCodec := simapp.MakeTestEncodingConfig().Marshaler - err = genutiltest.ExecInitCmd( - testModuleBasicManager, home, appCodec) - require.NoError(t, err) - - serverCtx := server.NewContext(viper.New(), cfg, logger) - clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home) - - ctx := context.Background() - ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) - ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx) - - cmd := cli.AddVPoolGenesisCmd("home") - cmd.SetArgs([]string{ - tc.pairName, - tc.baseAsset, - tc.maxOracle, - tc.maxLeverage, - tc.tradeLimit, - tc.maintainRatio, - tc.flucLimit, - tc.quoteAsset, - fmt.Sprintf("--%s=home", flags.FlagHome)}) - - if tc.expectError { - require.Error(t, cmd.ExecuteContext(ctx)) - } else { - require.NoError(t, cmd.ExecuteContext(ctx)) - } - }) - } - - testCases := []TestCase{ + }{ { name: "pair name empty", pairName: "", @@ -125,6 +83,18 @@ func TestAddGenesisVpoolCmd(t *testing.T) { maxLeverage: "1", expectError: true, }, + { + name: "max leverage cannot be zero", + pairName: "token0:token1", + baseAsset: "100", + quoteAsset: "100", + tradeLimit: "0.1", + flucLimit: "0.1", + maxOracle: "0.1", + maintainRatio: "0.1", + maxLeverage: "0", + expectError: true, + }, { name: "valid vpool pair", pairName: "token0:token1", @@ -139,7 +109,43 @@ func TestAddGenesisVpoolCmd(t *testing.T) { }, } - for _, testCase := range testCases { - executeTest(t, testCase) + for _, tc := range tests { + tc := tc + t.Run(tc.name, func(t *testing.T) { + home := t.TempDir() + logger := log.NewNopLogger() + cfg, err := genutiltest.CreateDefaultTendermintConfig(home) + require.NoError(t, err) + + appCodec := simapp.MakeTestEncodingConfig().Marshaler + err = genutiltest.ExecInitCmd( + testModuleBasicManager, home, appCodec) + require.NoError(t, err) + + serverCtx := server.NewContext(viper.New(), cfg, logger) + clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home) + + ctx := context.Background() + ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) + ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx) + + cmd := cli.AddVPoolGenesisCmd("home") + cmd.SetArgs([]string{ + tc.pairName, + tc.baseAsset, + tc.quoteAsset, + tc.maxOracle, + tc.tradeLimit, + tc.maintainRatio, + tc.flucLimit, + tc.maxLeverage, + fmt.Sprintf("--%s=home", flags.FlagHome)}) + + if tc.expectError { + require.Error(t, cmd.ExecuteContext(ctx)) + } else { + require.NoError(t, cmd.ExecuteContext(ctx)) + } + }) } } From 5a30ca63c0308a3ecefd65541f83d25ac8e3261f Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Wed, 24 Aug 2022 14:22:23 +0200 Subject: [PATCH 2/3] remove print --- x/vpool/client/cli/genvpool.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/x/vpool/client/cli/genvpool.go b/x/vpool/client/cli/genvpool.go index 2b6e66014..8dd56e8ab 100644 --- a/x/vpool/client/cli/genvpool.go +++ b/x/vpool/client/cli/genvpool.go @@ -119,8 +119,6 @@ func parseVpoolParams(args []string) (*types.Pool, error) { return nil, err } - fmt.Printf("%s", maxLeverage) - if !maxLeverage.GT(sdk.ZeroDec()) { return nil, fmt.Errorf("invalid max leverage value") } From bf9b019b477e1bba7578e36ba72dcdfe6ebb6a9e Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Wed, 24 Aug 2022 14:39:03 +0200 Subject: [PATCH 3/3] add changelog entry --- CHANGELOG.md | 1 + x/vpool/client/cli/genvpool_test.go | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0854a65c..43d777a6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#836](https://github.com/NibiruChain/nibiru/pull/836) - refactor(genesis): DRY improvements and functions added to localnet.sh for readability * [#842](https://github.com/NibiruChain/nibiru/pull/842) - use self-hosted runner * [#843](https://github.com/NibiruChain/nibiru/pull/843) - add timeout to github actions integration tests +* [#848](https://github.com/NibiruChain/nibiru/pull/848) - add check max leverage on add vpool in genesis command ### Features diff --git a/x/vpool/client/cli/genvpool_test.go b/x/vpool/client/cli/genvpool_test.go index 58b600d17..dc527407b 100644 --- a/x/vpool/client/cli/genvpool_test.go +++ b/x/vpool/client/cli/genvpool_test.go @@ -134,10 +134,10 @@ func TestAddGenesisVpoolCmd(t *testing.T) { tc.pairName, tc.baseAsset, tc.quoteAsset, - tc.maxOracle, tc.tradeLimit, - tc.maintainRatio, tc.flucLimit, + tc.maxOracle, + tc.maintainRatio, tc.maxLeverage, fmt.Sprintf("--%s=home", flags.FlagHome)})