Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add check in max leverage add genesis command #848

Merged
merged 3 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions x/vpool/client/cli/genvpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,31 @@ 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
}

if !maxLeverage.GT(sdk.ZeroDec()) {
return nil, fmt.Errorf("invalid max leverage value")
}

vPool := types.NewPool(
vPair,
tradeLimit,
Expand Down
98 changes: 52 additions & 46 deletions x/vpool/client/cli/genvpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: "",
Expand Down Expand Up @@ -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",
Expand All @@ -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.tradeLimit,
tc.flucLimit,
tc.maxOracle,
tc.maintainRatio,
tc.maxLeverage,
fmt.Sprintf("--%s=home", flags.FlagHome)})

if tc.expectError {
require.Error(t, cmd.ExecuteContext(ctx))
} else {
require.NoError(t, cmd.ExecuteContext(ctx))
}
})
}
}