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

test(perp): TestAddMargin #989

Merged
merged 5 commits into from
Oct 8, 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 @@ -98,6 +98,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#956](https://github.com/NibiruChain/nibiru/pull/956) - test(perp): partial liquidate unit test
* [#981](https://github.com/NibiruChain/nibiru/pull/981) - chore(testutil): clean up x/testutil packages
* [#980](https://github.com/NibiruChain/nibiru/pull/980) - test(perp): add `MsgClosePosition`, `MsgAddMargin`, and `MsgRemoveMargin` simulation tests
* [#989](https://github.com/NibiruChain/nibiru/pull/989) - test(perp): cli test for AddMargin

### Features

Expand Down
94 changes: 78 additions & 16 deletions x/perp/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,22 +374,6 @@ func (s *IntegrationTestSuite) TestPositionEmptyAndClose() {
s.Contains(out.String(), collections.ErrNotFound.Error())
}

func (s *IntegrationTestSuite) TestGetPrices() {
val := s.network.Validators[0]

s.T().Log("check vpool balances")
reserveAssets, err := testutilcli.QueryVpoolReserveAssets(val.ClientCtx, common.Pair_ETH_NUSD)
s.NoError(err)
s.EqualValues(sdk.MustNewDecFromStr("10000000"), reserveAssets.BaseAssetReserve)
s.EqualValues(sdk.MustNewDecFromStr("60000000000"), reserveAssets.QuoteAssetReserve)

s.T().Log("check prices")
priceInfo, err := testutilcli.QueryBaseAssetPrice(val.ClientCtx, common.Pair_ETH_NUSD, "add", "100")
s.T().Logf("priceInfo: %+v", priceInfo)
s.EqualValues(sdk.MustNewDecFromStr("599994.000059999400006000"), priceInfo.PriceInQuoteDenom)
s.NoError(err)
}

func (s *IntegrationTestSuite) TestQueryCumulativePremiumFractions() {
val := s.network.Validators[0]

Expand Down Expand Up @@ -435,6 +419,84 @@ func (s *IntegrationTestSuite) TestRemoveMargin() {
s.Contains(out.String(), perptypes.ErrFailedRemoveMarginCanCauseBadDebt.Error())
}

func (s *IntegrationTestSuite) TestAddMargin() {
val := s.network.Validators[0]
pair := common.Pair_ETH_NUSD

// Open a new position
s.T().Log("opening a position with user 2....")
args := []string{
"--from",
s.users[2].String(),
"buy",
pair.String(),
"10", // Leverage
"10000", // Quote asset amount
"0.0000001",
}

_, err := sdktestutilcli.ExecTestCLICmd(val.ClientCtx, cli.OpenPositionCmd(), append(args, commonArgs...))
if err != nil {
s.T().Logf("user2 open position err: %+v", err)
}
s.Require().NoError(err)

testCases := []struct {
name string
args []string
expectedCode uint32
expectedMargin sdk.Dec
}{
{
name: "PASS: add margin to correct position",
args: []string{
"--from",
s.users[2].String(),
pair.String(),
fmt.Sprintf("%s%s", "10000", pair.Token1),
},
expectedCode: 0,
expectedMargin: sdk.NewDec(20_000),
},
{
name: "FAIL: position not found",
args: []string{
"--from",
s.users[2].String(),
common.Pair_BTC_NUSD.String(),
fmt.Sprintf("%s%s", "10000", pair.Token1),
},
expectedCode: 1,
},
}

for _, tc := range testCases {
s.T().Run(tc.name, func(t *testing.T) {
s.T().Log("adding margin on user 2....")
out, err := sdktestutilcli.ExecTestCLICmd(val.ClientCtx, cli.AddMarginCmd(), append(tc.args, commonArgs...))
if err != nil {
s.T().Logf("user 2 add margin err: %+v", err)
}
s.Require().NoError(err)

var tx sdk.TxResponse
val.ClientCtx.Codec.MustUnmarshalJSON(out.Bytes(), &tx)

if tc.expectedCode != 0 {
s.EqualValues(tc.expectedCode, tx.Code)
} else {
s.EqualValues(tc.expectedCode, 0)

// query trader position
queryResp, err := testutilcli.QueryPosition(val.ClientCtx, pair, s.users[2])
s.NoError(err)

s.EqualValues(tc.expectedMargin, queryResp.Position.Margin)
}
})
}
}

func (s *IntegrationTestSuite) TestLiquidate() {
// Set up the user accounts
val := s.network.Validators[0]
Expand Down
34 changes: 33 additions & 1 deletion x/vpool/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,23 @@ func (s *VpoolCLISuite) SetupSuite() {

app.SetPrefixes(app.AccountAddressPrefix)

encodingConfig := simapp.MakeTestEncodingConfig()
genesisState := simapp.NewTestGenesisStateFromDefault()
vpoolGenesis := vpooltypes.DefaultGenesis()
vpoolGenesis.Vpools = []vpooltypes.VPool{
{
Pair: common.Pair_ETH_NUSD,
BaseAssetReserve: sdk.NewDec(10_000_000),
QuoteAssetReserve: sdk.NewDec(60_000_000_000),
TradeLimitRatio: sdk.MustNewDecFromStr("0.8"),
FluctuationLimitRatio: sdk.MustNewDecFromStr("0.2"),
MaxOracleSpreadRatio: sdk.MustNewDecFromStr("0.2"),
MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"),
MaxLeverage: sdk.MustNewDecFromStr("15"),
},
}
genesisState[vpooltypes.ModuleName] = encodingConfig.Marshaler.MustMarshalJSON(vpoolGenesis)

s.cfg = testutilcli.BuildNetworkConfig(genesisState)

s.network = testutilcli.NewNetwork(s.T(), s.cfg)
Expand All @@ -52,7 +68,7 @@ func (s *VpoolCLISuite) TearDownSuite() {
s.network.Cleanup()
}

func (s VpoolCLISuite) TestGovAddVpool() {
func (s *VpoolCLISuite) TestGovAddVpool() {
s.Require().Len(s.network.Validators, 1)
val := s.network.Validators[0]
clientCtx := val.ClientCtx.WithOutputFormat("json")
Expand Down Expand Up @@ -195,6 +211,22 @@ e.g. $ nibid tx gov vote 1 yes`)
s.Require().True(found, "pool does not exist")
}

func (s *VpoolCLISuite) TestGetPrices() {
val := s.network.Validators[0]

s.T().Log("check vpool balances")
reserveAssets, err := testutilcli.QueryVpoolReserveAssets(val.ClientCtx, common.Pair_ETH_NUSD)
s.NoError(err)
s.EqualValues(sdk.MustNewDecFromStr("10000000"), reserveAssets.BaseAssetReserve)
s.EqualValues(sdk.MustNewDecFromStr("60000000000"), reserveAssets.QuoteAssetReserve)

s.T().Log("check prices")
priceInfo, err := testutilcli.QueryBaseAssetPrice(val.ClientCtx, common.Pair_ETH_NUSD, "add", "100")
s.T().Logf("priceInfo: %+v", priceInfo)
s.EqualValues(sdk.MustNewDecFromStr("599994.000059999400006000"), priceInfo.PriceInQuoteDenom)
s.NoError(err)
}

func TestVpoolCLISuite(t *testing.T) {
suite.Run(t, new(VpoolCLISuite))
}